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 |
|
---|
7 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
8 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
9 | import org.openstreetmap.josm.data.osm.Node;
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
11 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
12 | import org.openstreetmap.josm.data.osm.Way;
|
---|
13 | import org.openstreetmap.josm.tools.Geometry;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * A class to find the distance between an {@link OsmPrimitive} and a GPX point.
|
---|
17 | *
|
---|
18 | * @author Taylor Smock
|
---|
19 | * @since 14802
|
---|
20 | */
|
---|
21 | public final class GpxDistance {
|
---|
22 | private GpxDistance() {
|
---|
23 | // This class should not be instantiated
|
---|
24 | }
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Find the distance between a point and a dataset of surveyed points
|
---|
28 | * @param p OsmPrimitive from which to get the lowest distance to a GPX point
|
---|
29 | * @param gpxData Data from which to get the GPX points
|
---|
30 | * @return The shortest distance
|
---|
31 | */
|
---|
32 | public static double getLowestDistance(OsmPrimitive p, GpxData gpxData) {
|
---|
33 | return gpxData.getTrackPoints()
|
---|
34 | .mapToDouble(tp -> Geometry.getDistance(p, new Node(tp.getCoor())))
|
---|
35 | .filter(x -> x >= 0)
|
---|
36 | .min().orElse(Double.MAX_VALUE);
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Get the distance between an object and a waypoint
|
---|
41 | * @param p OsmPrimitive to get the distance to the WayPoint
|
---|
42 | * @param waypoint WayPoint to get the distance from
|
---|
43 | * @return The shortest distance between p and waypoint
|
---|
44 | * @deprecated Use {@code Geometry.getDistance(p, new Node(waypoint.getCoor()))}
|
---|
45 | * instead
|
---|
46 | */
|
---|
47 | @Deprecated
|
---|
48 | public static double getDistance(OsmPrimitive p, WayPoint waypoint) {
|
---|
49 | return Geometry.getDistance(p, new Node(waypoint.getCoor()));
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Get the shortest distance between a relation and a waypoint
|
---|
54 | * @param relation Relation to get the distance from
|
---|
55 | * @param waypoint WayPoint to get the distance to
|
---|
56 | * @return The distance between the relation and the waypoint
|
---|
57 | * @deprecated Use {@code Geometry.getDistance(relation, new Node(waypoint.getCoor()))}
|
---|
58 | * instead
|
---|
59 | */
|
---|
60 | @Deprecated
|
---|
61 | public static double getDistanceRelation(Relation relation, WayPoint waypoint) {
|
---|
62 | double shortestDistance = Double.MAX_VALUE;
|
---|
63 | List<Node> nodes = new ArrayList<>(relation.getMemberPrimitives(Node.class));
|
---|
64 | List<Way> ways = new ArrayList<>(relation.getMemberPrimitives(Way.class));
|
---|
65 | List<Relation> relations = new ArrayList<>(relation.getMemberPrimitives(Relation.class));
|
---|
66 | if (nodes.isEmpty() && ways.isEmpty() && relations.isEmpty()) return Double.MAX_VALUE;
|
---|
67 | for (Relation nrelation : relations) {
|
---|
68 | double distance = getDistanceRelation(nrelation, waypoint);
|
---|
69 | if (distance < shortestDistance) shortestDistance = distance;
|
---|
70 | }
|
---|
71 | for (Way way : ways) {
|
---|
72 | double distance = getDistanceWay(way, waypoint);
|
---|
73 | if (distance < shortestDistance) shortestDistance = distance;
|
---|
74 | }
|
---|
75 | for (Node node : nodes) {
|
---|
76 | double distance = getDistanceNode(node, waypoint);
|
---|
77 | if (distance < shortestDistance) shortestDistance = distance;
|
---|
78 | }
|
---|
79 | return shortestDistance;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Get the shortest distance between a way and a waypoint
|
---|
84 | * @param way Way to get the distance from
|
---|
85 | * @param waypoint WayPoint to get the distance to
|
---|
86 | * @return The distance between the way and the waypoint
|
---|
87 | * @deprecated Use {@code Geometry.getDistanceWayNode(way, new Node(waypoint.getCoor()))} instead
|
---|
88 | */
|
---|
89 | @Deprecated
|
---|
90 | public static double getDistanceWay(Way way, WayPoint waypoint) {
|
---|
91 | if (way == null || waypoint == null) return Double.MAX_VALUE;
|
---|
92 | return Geometry.getDistanceWayNode(way, new Node(waypoint.getCoor()));
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Get the distance between a node and a waypoint
|
---|
97 | * @param node Node to get the distance from
|
---|
98 | * @param waypoint WayPoint to get the distance to
|
---|
99 | * @return The distance between the two points
|
---|
100 | * @deprecated Use {@code Geometry.getDistance(node, new Node(waypoint.getCoor()))}
|
---|
101 | * instead
|
---|
102 | */
|
---|
103 | @Deprecated
|
---|
104 | public static double getDistanceNode(Node node, WayPoint waypoint) {
|
---|
105 | if (node == null || waypoint == null) return Double.MAX_VALUE;
|
---|
106 | return Geometry.getDistance(node, new Node(waypoint.getCoor()));
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Get the distance between coordinates (provided by EastNorth) and a waypoint
|
---|
111 | * @param en The EastNorth to get the distance to
|
---|
112 | * @param waypoint WayPoint to get the distance to
|
---|
113 | * @return The distance between the two points
|
---|
114 | * @deprecated Use {@code Geometry.getDistance(new Node(en), new Node(waypoint.getCoor()))} instead
|
---|
115 | */
|
---|
116 | @Deprecated
|
---|
117 | public static double getDistanceEastNorth(EastNorth en, WayPoint waypoint) {
|
---|
118 | if (en == null || waypoint == null) return Double.MAX_VALUE;
|
---|
119 | return Geometry.getDistance(new Node(en), new Node(waypoint.getCoor()));
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Get the distance between coordinates (latitude longitude) and a waypoint
|
---|
124 | * @param latlon LatLon to get the distance from
|
---|
125 | * @param waypoint WayPoint to get the distance to
|
---|
126 | * @return The distance between the two points
|
---|
127 | * @deprecated Use {@code Geometry.getDistance(new Node(latlon), new Node(waypoint.getCoor()))} instead
|
---|
128 | */
|
---|
129 | @Deprecated
|
---|
130 | public static double getDistanceLatLon(LatLon latlon, WayPoint waypoint) {
|
---|
131 | if (latlon == null || waypoint == null || waypoint.getCoor() == null) return Double.MAX_VALUE;
|
---|
132 | return Geometry.getDistance(new Node(latlon), new Node(waypoint.getCoor()));
|
---|
133 | }
|
---|
134 | }
|
---|