001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.gpx;
003
004import org.openstreetmap.josm.data.osm.Node;
005import org.openstreetmap.josm.data.osm.OsmPrimitive;
006import org.openstreetmap.josm.tools.Geometry;
007
008/**
009 * A class to find the distance between an {@link OsmPrimitive} and a GPX point.
010 *
011 * @author Taylor Smock
012 * @since 14802
013 */
014public final class GpxDistance {
015    private GpxDistance() {
016        // This class should not be instantiated
017    }
018
019    /**
020     * Find the distance between a point and a dataset of surveyed points
021     * @param p OsmPrimitive from which to get the lowest distance to a GPX point
022     * @param gpxData Data from which to get the GPX points
023     * @return The shortest distance
024     */
025    public static double getLowestDistance(OsmPrimitive p, GpxData gpxData) {
026        return gpxData.getTrackPoints()
027                .mapToDouble(tp -> Geometry.getDistance(p, new Node(tp.getCoor())))
028                .filter(x -> x >= 0)
029                .min().orElse(Double.MAX_VALUE);
030    }
031}