Ticket #17131: GpxDistance_v6.patch

File GpxDistance_v6.patch, 13.8 KB (added by taylor.smock, 5 years ago)

Add tests and fix some bugs that were exposed

  • src/org/openstreetmap/josm/data/gpx/GpxDistance.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.data.gpx;
     3
     4import java.util.ArrayList;
     5import java.util.List;
     6import java.util.stream.Collectors;
     7
     8import org.openstreetmap.josm.data.coor.EastNorth;
     9import org.openstreetmap.josm.data.coor.LatLon;
     10import org.openstreetmap.josm.data.osm.Node;
     11import org.openstreetmap.josm.data.osm.OsmPrimitive;
     12import org.openstreetmap.josm.data.osm.Relation;
     13import org.openstreetmap.josm.data.osm.Way;
     14import org.openstreetmap.josm.gui.MainApplication;
     15import org.openstreetmap.josm.gui.layer.GpxLayer;
     16import org.openstreetmap.josm.gui.layer.Layer;
     17import org.openstreetmap.josm.gui.layer.LayerManager;
     18import org.openstreetmap.josm.tools.Geometry;
     19
     20/**
     21 * A class to find the distance between an OsmPrimitive and a GPX point
     22 *
     23 * @author Taylor Smock
     24 *
     25 */
     26public final class GpxDistance {
     27    private GpxDistance() {
     28        // This class should not be instantiated
     29    }
     30
     31    /**
     32     * @param p The OsmPrimitive that we are checking the maximum distance for
     33     * @param maximumDistance The maximum distance from a GPX point to the OsmPrimitive
     34     * @return true if the distance to the closest GPX point is lower than maximumDistance
     35     * @since xxx
     36     */
     37    public static boolean isCloseTo(OsmPrimitive p, double maximumDistance) {
     38        double distance = getLowestDistance(p);
     39        return (distance < maximumDistance);
     40    }
     41
     42    /**
     43     * Get the lowest distance from the layers to p
     44     * @param p OsmPrimitive from which to get the lowest distance to a GPX point
     45     * @return the lowest distance from any GpxLayer to p.
     46     * @since xxx
     47     */
     48    public static double getLowestDistance(OsmPrimitive p) {
     49        LayerManager layerManager = MainApplication.getLayerManager();
     50        List<Layer> layers = layerManager.getLayers();
     51        double distance = Double.MAX_VALUE;
     52        for (Layer layer : layers) {
     53            double tdistance = getLowestDistance(p, layer);
     54            if (tdistance < distance) distance = tdistance;
     55        }
     56        return distance;
     57    }
     58
     59    /**
     60     * Find the distance between a point and a layer of surveyed points
     61     * @param p OsmPrimitive from which to get the lowest distance to a GPX point
     62     * @param layer Layer from which to get the GPX points (currently, only GpxLayer is supported)
     63     * @return The shortest distance
     64     * @since xxx
     65     */
     66    public static double getLowestDistance(OsmPrimitive p, Layer layer) {
     67        if (layer instanceof GpxLayer) {
     68            GpxLayer gpxLayer = (GpxLayer) layer;
     69            GpxData gpxData = gpxLayer.data;
     70            List<WayPoint> trackPoints = gpxData.getTrackPoints().collect(Collectors.toList());
     71            double lowestDistance = Double.MAX_VALUE;
     72            for (WayPoint trackPoint : trackPoints) {
     73                double distance = getDistance(p, trackPoint);
     74                if (distance >= 0.0 && distance < lowestDistance) lowestDistance = distance;
     75            }
     76            return lowestDistance;
     77        }
     78        return Double.MAX_VALUE;
     79    }
     80
     81    /**
     82     * Get the distance between an object and a waypoint
     83     * @param p OsmPrimitive to get the distance to the WayPoint
     84     * @param waypoint WayPoint to get the distance from
     85     * @return The shortest distance between p and waypoint
     86     * @since xxx
     87     */
     88    public static double getDistance(OsmPrimitive p, WayPoint waypoint) {
     89        if (p instanceof Node) {
     90            return getDistanceNode((Node) p, waypoint);
     91        } else if (p instanceof Way) {
     92            return getDistanceWay((Way) p, waypoint);
     93        } else if (p instanceof Relation) {
     94            return getDistanceRelation((Relation) p, waypoint);
     95        }
     96        return Double.MAX_VALUE;
     97    }
     98
     99    /**
     100     * Get the shortest distance between a relation and a waypoint
     101     * @param relation Relation to get the distance from
     102     * @param waypoint WayPoint to get the distance to
     103     * @return The distance between the relation and the waypoint
     104     * @since xxx
     105     */
     106    public static double getDistanceRelation(Relation relation, WayPoint waypoint) {
     107        double shortestDistance = Double.MAX_VALUE;
     108        List<Node> nodes = new ArrayList<>(relation.getMemberPrimitives(Node.class));
     109        List<Way> ways = new ArrayList<>(relation.getMemberPrimitives(Way.class));
     110        List<Relation> relations = new ArrayList<>(relation.getMemberPrimitives(Relation.class));
     111        if (nodes.isEmpty() && ways.isEmpty() && relations.isEmpty()) return Double.MAX_VALUE;
     112        for (Relation nrelation : relations) {
     113            double distance = getDistanceRelation(nrelation, waypoint);
     114            if (distance < shortestDistance) shortestDistance = distance;
     115        }
     116        for (Way way : ways) {
     117            double distance = getDistanceWay(way, waypoint);
     118            if (distance < shortestDistance) shortestDistance = distance;
     119        }
     120        for (Node node : nodes) {
     121            double distance = getDistanceNode(node, waypoint);
     122            if (distance < shortestDistance) shortestDistance = distance;
     123        }
     124        return shortestDistance;
     125    }
     126
     127    /**
     128     * Get the shortest distance between a way and a waypoint
     129     * @param way Way to get the distance from
     130     * @param waypoint WayPoint to get the distance to
     131     * @return The distance between the way and the waypoint
     132     * @since xxx
     133     */
     134    public static double getDistanceWay(Way way, WayPoint waypoint) {
     135        double shortestDistance = Double.MAX_VALUE;
     136        if (way == null || waypoint == null) return shortestDistance;
     137        LatLon llwaypoint = waypoint.getCoor();
     138        EastNorth enwaypoint = new EastNorth(llwaypoint.getX(), llwaypoint.getY());
     139        for (int i = 0; i < way.getNodesCount() - 1; i++) {
     140            double distance = Double.MAX_VALUE;
     141            LatLon llfirst = way.getNode(i).getCoor();
     142            LatLon llsecond = way.getNode(i + 1).getCoor();
     143            EastNorth first = new EastNorth(llfirst.getX(), llfirst.getY());
     144            EastNorth second = new EastNorth(llsecond.getX(), llsecond.getY());
     145            if (first.isValid() && second.isValid()) {
     146                EastNorth closestPoint = Geometry.closestPointToSegment(first, second, enwaypoint);
     147                distance = llwaypoint.greatCircleDistance(new LatLon(closestPoint.getX(), closestPoint.getY()));
     148            } else if (first.isValid() && !second.isValid()) {
     149                distance = getDistanceEastNorth(first, waypoint);
     150            } else if (!first.isValid() && second.isValid()) {
     151                distance = getDistanceEastNorth(second, waypoint);
     152            } else if (!first.isValid() && !second.isValid()) {
     153                distance = Double.MAX_VALUE;
     154            }
     155            if (distance < shortestDistance) shortestDistance = distance;
     156
     157        }
     158        return shortestDistance;
     159    }
     160
     161    /**
     162     * Get the distance between a node and a waypoint
     163     * @param node Node to get the distance from
     164     * @param waypoint WayPoint to get the distance to
     165     * @return The distance between the two points
     166     * @since xxx
     167     */
     168    public static double getDistanceNode(Node node, WayPoint waypoint) {
     169        if (node == null) return Double.MAX_VALUE;
     170        return getDistanceLatLon(node.getCoor(), waypoint);
     171    }
     172
     173    /**
     174     * Get the distance between coordinates (provided by EastNorth) and a waypoint
     175     * @param en The EastNorth to get the distance to
     176     * @param waypoint WayPoint to get the distance to
     177     * @return The distance between the two points
     178     * @since xxx
     179     */
     180    public static double getDistanceEastNorth(EastNorth en, WayPoint waypoint) {
     181        if (en == null || !en.isValid()) return Double.MAX_VALUE;
     182        return getDistanceLatLon(new LatLon(en.getY(), en.getX()), waypoint);
     183    }
     184
     185    /**
     186     * Get the distance between coordinates (latitude longitude) and a waypoint
     187     * @param latlon LatLon to get the distance from
     188     * @param waypoint WayPoint to get the distance to
     189     * @return The distance between the two points
     190     * @since xxx
     191     */
     192    public static double getDistanceLatLon(LatLon latlon, WayPoint waypoint) {
     193        if (latlon == null || waypoint == null || waypoint.getCoor() == null) return Double.MAX_VALUE;
     194        return waypoint.getCoor().greatCircleDistance(latlon);
     195    }
     196}
  • test/unit/org/openstreetmap/josm/data/gpx/GpxDistanceTest.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.data.gpx;
     3
     4import static org.junit.Assert.assertEquals;
     5
     6import org.junit.Rule;
     7import org.junit.Test;
     8import org.openstreetmap.josm.data.coor.EastNorth;
     9import org.openstreetmap.josm.data.coor.LatLon;
     10import org.openstreetmap.josm.data.osm.Node;
     11import org.openstreetmap.josm.data.osm.Way;
     12import org.openstreetmap.josm.testutils.JOSMTestRules;
     13
     14import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     15
     16/**
     17 * Unit tests for class {@link GpxDistance}.
     18 */
     19public class GpxDistanceTest {
     20
     21    /**
     22     * Setup test.
     23     */
     24    @Rule
     25    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
     26    public JOSMTestRules test = new JOSMTestRules().projection();
     27
     28    @Test
     29    /**
     30     * Unit test of method {@link GpxDistance#getDistanceWay}.
     31     */
     32    public void testGetDistanceWay() {
     33        Node node1 = new Node();
     34        Node node2 = new Node();
     35        Way way = new Way();
     36        node1.setCoor(new LatLon(0, 0));
     37        node2.setCoor(new LatLon(0, 1));
     38        way.addNode(node1);
     39        way.addNode(node2);
     40
     41        WayPoint waypoint = new WayPoint(new LatLon(1, 0));
     42
     43        double distance = GpxDistance.getDistanceWay(null, waypoint);
     44        assertEquals(Double.MAX_VALUE, distance, 0.1);
     45
     46        distance = GpxDistance.getDistanceWay(way, null);
     47        assertEquals(Double.MAX_VALUE, distance, 0.1);
     48
     49        distance = GpxDistance.getDistanceWay(null, null);
     50        assertEquals(Double.MAX_VALUE, distance, 0.1);
     51
     52        distance = GpxDistance.getDistanceWay(way, waypoint);
     53        /* 111319.49077 uses the WGS84/NAD38/GRS80 model for
     54         * the distance between (0, 0) and (0, 1) */
     55        assertEquals(111319.49077, distance, 0.1);
     56    }
     57
     58    /**
     59     * Unit test of method {@link GpxDistance#getDistanceNode}.
     60     */
     61    @Test
     62    public void testGetDistanceNode() {
     63        double distance = GpxDistance.getDistanceNode(null, null);
     64        assertEquals(Double.MAX_VALUE, distance, 0.1);
     65
     66        Node node = new Node();
     67        node.setCoor(new LatLon(0, 0));
     68        distance = GpxDistance.getDistanceNode(node, null);
     69        assertEquals(Double.MAX_VALUE, distance, 0.1);
     70
     71        WayPoint waypoint = new WayPoint(new LatLon(0, 0));
     72        distance = GpxDistance.getDistanceNode(node, waypoint);
     73        assertEquals(0.0, distance, 0.0001); // should be zero delta
     74
     75        distance = GpxDistance.getDistanceNode(null, waypoint);
     76        assertEquals(Double.MAX_VALUE, distance, 0.1);
     77
     78        node.setCoor(new LatLon(1, 0));
     79        distance = GpxDistance.getDistanceNode(node, waypoint);
     80        /* 111319.49077 uses the WGS84/NAD38/GRS80 model for
     81         * the distance between (0, 0) and (0, 1) */
     82        assertEquals(111319.49077, distance, 0.1);
     83    }
     84
     85    /**
     86     * Unit test of method {@link GpxDistance#getDistanceEastNorth}.
     87     */
     88    @Test
     89    public void testGetDistanceEastNorth() {
     90        double distance = GpxDistance.getDistanceEastNorth(null, null);
     91        assertEquals(Double.MAX_VALUE, distance, 0.1);
     92
     93        EastNorth en = new EastNorth(0, 0);
     94        distance = GpxDistance.getDistanceEastNorth(en, null);
     95        assertEquals(Double.MAX_VALUE, distance, 0.1);
     96
     97        WayPoint waypoint = new WayPoint(new LatLon(0, 0));
     98        distance = GpxDistance.getDistanceEastNorth(en, waypoint);
     99        assertEquals(0.0, distance, 0.0001); // should be zero delta
     100
     101        distance = GpxDistance.getDistanceEastNorth(null, waypoint);
     102        assertEquals(Double.MAX_VALUE, distance, 0.1);
     103
     104        en = new EastNorth(0, 1);
     105        distance = GpxDistance.getDistanceEastNorth(en, waypoint);
     106        /* 111319.49077 uses the WGS84/NAD38/GRS80 model for
     107         * the distance between (0, 0) and (0, 1) */
     108        assertEquals(111319.49077, distance, 0.1);
     109    }
     110
     111
     112    /**
     113     * Unit test of method {@link GpxDistance#getDistanceLatLon}.
     114     */
     115    @Test
     116    public void testGetDistanceLatLon() {
     117        double distance = GpxDistance.getDistanceLatLon(null, null);
     118        assertEquals(Double.MAX_VALUE, distance, 0.1);
     119
     120        LatLon ll = new LatLon(0, 0);
     121        distance = GpxDistance.getDistanceLatLon(ll, null);
     122        assertEquals(Double.MAX_VALUE, distance, 0.1);
     123
     124        WayPoint waypoint = new WayPoint(ll);
     125        distance = GpxDistance.getDistanceLatLon(ll, waypoint);
     126        assertEquals(0.0, distance, 0.0001); // should be zero delta
     127
     128        distance = GpxDistance.getDistanceLatLon(null, waypoint);
     129        assertEquals(Double.MAX_VALUE, distance, 0.1);
     130
     131        ll = new LatLon(0, 1);
     132        distance = GpxDistance.getDistanceLatLon(ll, waypoint);
     133        /* 111319.49077 uses the WGS84/NAD38/GRS80 model for
     134         * the distance between (0, 0) and (0, 1) */
     135        assertEquals(111319.49077, distance, 0.1);
     136    }
     137}