| | 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.ImageData; |
| | 9 | import org.openstreetmap.josm.data.coor.EastNorth; |
| | 10 | import org.openstreetmap.josm.data.coor.LatLon; |
| | 11 | import org.openstreetmap.josm.data.osm.Node; |
| | 12 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| | 13 | import org.openstreetmap.josm.data.osm.Relation; |
| | 14 | import org.openstreetmap.josm.data.osm.Way; |
| | 15 | import org.openstreetmap.josm.gui.MainApplication; |
| | 16 | import org.openstreetmap.josm.gui.layer.GpxLayer; |
| | 17 | import org.openstreetmap.josm.gui.layer.Layer; |
| | 18 | import org.openstreetmap.josm.gui.layer.LayerManager; |
| | 19 | import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer; |
| | 20 | import org.openstreetmap.josm.gui.layer.geoimage.ImageEntry; |
| | 21 | import 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 | */ |
| | 29 | public 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 | } |