Ticket #17131: GpxDistance_v7.patch

File GpxDistance_v7.patch, 16.5 KB (added by taylor.smock, 7 years ago)

Creates a new gpxdata object from image gps points where it is not already associated with a gpx track

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

     
    2424import java.util.zip.CRC32;
    2525
    2626import org.openstreetmap.josm.data.coor.LatLon;
     27import org.openstreetmap.josm.data.gpx.GpxDistance;
    2728import org.openstreetmap.josm.data.osm.IPrimitive;
    2829import org.openstreetmap.josm.data.osm.Node;
     30import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2931import org.openstreetmap.josm.data.osm.Way;
    3032import org.openstreetmap.josm.data.osm.search.SearchCompiler;
    3133import org.openstreetmap.josm.data.osm.search.SearchCompiler.Match;
     
    519521        }
    520522
    521523        /**
     524         * Returns the lowest distance between the OSM object and a GPX point
     525         * <p>
     526         * @param env the environment
     527         * @return the distance between the object and the closest gpx point or {@code Double.MAX_VALUE}
     528         * @since xxx
     529         */
     530        public static double gpx_distance(final Environment env) { // NO_UCD (unused code)
     531            if (env.osm instanceof OsmPrimitive) {
     532                return GpxDistance.getLowestDistance((OsmPrimitive) env.osm);
     533            } else {
     534                return Double.MAX_VALUE;
     535            }
     536        }
     537
     538        /**
    522539         * Determines whether the object has a tag with the given key.
    523540         * @param env the environment
    524541         * @param key the OSM key