| | 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.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 | */ |
| | 26 | public 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 | } |