Ticket #17131: GpxDistance_v6.2.patch

File GpxDistance_v6.2.patch, 14.4 KB (added by taylor.smock, 7 years ago)

Fix some issues I noticed during testing and start work on method for GeoImage layers

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