| 1 | // License: GPL. For details, see LICENSE file. |
| 2 | package org.openstreetmap.josm.data.gpx; |
| 3 | |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.List; |
| 6 | import java.util.stream.Collectors; |
| 7 | |
| 8 | import org.openstreetmap.josm.data.coor.EastNorth; |
| 9 | import org.openstreetmap.josm.data.coor.LatLon; |
| 10 | import org.openstreetmap.josm.data.osm.Node; |
| 11 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| 12 | import org.openstreetmap.josm.data.osm.Relation; |
| 13 | import org.openstreetmap.josm.data.osm.Way; |
| 14 | import org.openstreetmap.josm.gui.MainApplication; |
| 15 | import org.openstreetmap.josm.gui.layer.GpxLayer; |
| 16 | import org.openstreetmap.josm.gui.layer.Layer; |
| 17 | import org.openstreetmap.josm.gui.layer.LayerManager; |
| 18 | import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer; |
| 19 | import 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 | */ |
| 27 | public 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 | GpxData gpxData = null; |
| 70 | if (layer instanceof GpxLayer) { |
| 71 | GpxLayer gpxLayer = (GpxLayer) layer; |
| 72 | gpxData = gpxLayer.data; |
| 73 | } else if (layer instanceof GeoImageLayer) { |
| 74 | GeoImageLayer tmp = (GeoImageLayer) layer; |
| 75 | gpxData = tmp.getFauxGpxLayer().data; |
| 76 | } |
| 77 | if (gpxData == null) return Double.MAX_VALUE; |
| 78 | List<WayPoint> trackPoints = gpxData.getTrackPoints().collect(Collectors.toList()); |
| 79 | double lowestDistance = Double.MAX_VALUE; |
| 80 | for (WayPoint trackPoint : trackPoints) { |
| 81 | double distance = getDistance(p, trackPoint); |
| 82 | if (distance >= 0.0 && distance < lowestDistance) lowestDistance = distance; |
| 83 | } |
| 84 | return lowestDistance; |
| 85 | } |
| 86 | return Double.MAX_VALUE; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get the distance between an object and a waypoint |
| 91 | * @param p OsmPrimitive to get the distance to the WayPoint |
| 92 | * @param waypoint WayPoint to get the distance from |
| 93 | * @return The shortest distance between p and waypoint |
| 94 | * @since xxx |
| 95 | */ |
| 96 | public static double getDistance(OsmPrimitive p, WayPoint waypoint) { |
| 97 | if (p instanceof Node) { |
| 98 | return getDistanceNode((Node) p, waypoint); |
| 99 | } else if (p instanceof Way) { |
| 100 | return getDistanceWay((Way) p, waypoint); |
| 101 | } else if (p instanceof Relation) { |
| 102 | return getDistanceRelation((Relation) p, waypoint); |
| 103 | } |
| 104 | return Double.MAX_VALUE; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Get the shortest distance between a relation and a waypoint |
| 109 | * @param relation Relation to get the distance from |
| 110 | * @param waypoint WayPoint to get the distance to |
| 111 | * @return The distance between the relation and the waypoint |
| 112 | * @since xxx |
| 113 | */ |
| 114 | public static double getDistanceRelation(Relation relation, WayPoint waypoint) { |
| 115 | double shortestDistance = Double.MAX_VALUE; |
| 116 | List<Node> nodes = new ArrayList<>(relation.getMemberPrimitives(Node.class)); |
| 117 | List<Way> ways = new ArrayList<>(relation.getMemberPrimitives(Way.class)); |
| 118 | List<Relation> relations = new ArrayList<>(relation.getMemberPrimitives(Relation.class)); |
| 119 | if (nodes.isEmpty() && ways.isEmpty() && relations.isEmpty()) return Double.MAX_VALUE; |
| 120 | for (Relation nrelation : relations) { |
| 121 | double distance = getDistanceRelation(nrelation, waypoint); |
| 122 | if (distance < shortestDistance) shortestDistance = distance; |
| 123 | } |
| 124 | for (Way way : ways) { |
| 125 | double distance = getDistanceWay(way, waypoint); |
| 126 | if (distance < shortestDistance) shortestDistance = distance; |
| 127 | } |
| 128 | for (Node node : nodes) { |
| 129 | double distance = getDistanceNode(node, waypoint); |
| 130 | if (distance < shortestDistance) shortestDistance = distance; |
| 131 | } |
| 132 | return shortestDistance; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Get the shortest distance between a way and a waypoint |
| 137 | * @param way Way to get the distance from |
| 138 | * @param waypoint WayPoint to get the distance to |
| 139 | * @return The distance between the way and the waypoint |
| 140 | * @since xxx |
| 141 | */ |
| 142 | public static double getDistanceWay(Way way, WayPoint waypoint) { |
| 143 | double shortestDistance = Double.MAX_VALUE; |
| 144 | if (way == null || waypoint == null) return shortestDistance; |
| 145 | LatLon llwaypoint = waypoint.getCoor(); |
| 146 | EastNorth enwaypoint = new EastNorth(llwaypoint.getY(), llwaypoint.getX()); |
| 147 | for (int i = 0; i < way.getNodesCount() - 1; i++) { |
| 148 | double distance = Double.MAX_VALUE; |
| 149 | LatLon llfirst = way.getNode(i).getCoor(); |
| 150 | LatLon llsecond = way.getNode(i + 1).getCoor(); |
| 151 | EastNorth first = new EastNorth(llfirst.getY(), llfirst.getX()); |
| 152 | EastNorth second = new EastNorth(llsecond.getY(), llsecond.getX()); |
| 153 | if (first.isValid() && second.isValid()) { |
| 154 | EastNorth closestPoint = Geometry.closestPointToSegment(first, second, enwaypoint); |
| 155 | distance = llwaypoint.greatCircleDistance(new LatLon(closestPoint.getX(), closestPoint.getY())); |
| 156 | } else if (first.isValid() && !second.isValid()) { |
| 157 | distance = getDistanceEastNorth(first, waypoint); |
| 158 | } else if (!first.isValid() && second.isValid()) { |
| 159 | distance = getDistanceEastNorth(second, waypoint); |
| 160 | } else if (!first.isValid() && !second.isValid()) { |
| 161 | distance = Double.MAX_VALUE; |
| 162 | } |
| 163 | if (distance < shortestDistance) shortestDistance = distance; |
| 164 | |
| 165 | } |
| 166 | return shortestDistance; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Get the distance between a node and a waypoint |
| 171 | * @param node Node to get the distance from |
| 172 | * @param waypoint WayPoint to get the distance to |
| 173 | * @return The distance between the two points |
| 174 | * @since xxx |
| 175 | */ |
| 176 | public static double getDistanceNode(Node node, WayPoint waypoint) { |
| 177 | if (node == null) return Double.MAX_VALUE; |
| 178 | return getDistanceLatLon(node.getCoor(), waypoint); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Get the distance between coordinates (provided by EastNorth) and a waypoint |
| 183 | * @param en The EastNorth to get the distance to |
| 184 | * @param waypoint WayPoint to get the distance to |
| 185 | * @return The distance between the two points |
| 186 | * @since xxx |
| 187 | */ |
| 188 | public static double getDistanceEastNorth(EastNorth en, WayPoint waypoint) { |
| 189 | if (en == null || !en.isValid()) return Double.MAX_VALUE; |
| 190 | return getDistanceLatLon(new LatLon(en.getY(), en.getX()), waypoint); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Get the distance between coordinates (latitude longitude) and a waypoint |
| 195 | * @param latlon LatLon to get the distance from |
| 196 | * @param waypoint WayPoint to get the distance to |
| 197 | * @return The distance between the two points |
| 198 | * @since xxx |
| 199 | */ |
| 200 | public static double getDistanceLatLon(LatLon latlon, WayPoint waypoint) { |
| 201 | if (latlon == null || waypoint == null || waypoint.getCoor() == null) return Double.MAX_VALUE; |
| 202 | return waypoint.getCoor().greatCircleDistance(latlon); |
| 203 | } |
| 204 | } |