Ticket #17616: 17616_v2.patch
File 17616_v2.patch, 22.7 KB (added by , 6 years ago) |
---|
-
src/org/openstreetmap/josm/data/gpx/GpxDistance.java
31 31 */ 32 32 public static double getLowestDistance(OsmPrimitive p, GpxData gpxData) { 33 33 return gpxData.getTrackPoints() 34 .mapToDouble(tp -> getDistance(p, tp))34 .mapToDouble(tp -> Geometry.getDistance(p, new Node(tp.getCoor()))) 35 35 .filter(x -> x >= 0) 36 36 .min().orElse(Double.MAX_VALUE); 37 37 } … … 41 41 * @param p OsmPrimitive to get the distance to the WayPoint 42 42 * @param waypoint WayPoint to get the distance from 43 43 * @return The shortest distance between p and waypoint 44 * @deprecated Use {@code Geometry.getDistance(p, new Node(waypoint.getCoor()))} 45 * instead 44 46 */ 47 @Deprecated 45 48 public static double getDistance(OsmPrimitive p, WayPoint waypoint) { 46 if (p instanceof Node) { 47 return getDistanceNode((Node) p, waypoint); 48 } else if (p instanceof Way) { 49 return getDistanceWay((Way) p, waypoint); 50 } else if (p instanceof Relation) { 51 return getDistanceRelation((Relation) p, waypoint); 52 } 53 return Double.MAX_VALUE; 49 return Geometry.getDistance(p, new Node(waypoint.getCoor())); 54 50 } 55 51 56 52 /** … … 58 54 * @param relation Relation to get the distance from 59 55 * @param waypoint WayPoint to get the distance to 60 56 * @return The distance between the relation and the waypoint 57 * @deprecated Use {@code Geometry.getDistance(relation, new Node(waypoint.getCoor()))} 58 * instead 61 59 */ 60 @Deprecated 62 61 public static double getDistanceRelation(Relation relation, WayPoint waypoint) { 63 62 double shortestDistance = Double.MAX_VALUE; 64 63 List<Node> nodes = new ArrayList<>(relation.getMemberPrimitives(Node.class)); … … 85 84 * @param way Way to get the distance from 86 85 * @param waypoint WayPoint to get the distance to 87 86 * @return The distance between the way and the waypoint 87 * @deprecated Use {@code Geometry.getDistanceWayNode(way, new Node(waypoint.getCoor()))} instead 88 88 */ 89 @Deprecated 89 90 public static double getDistanceWay(Way way, WayPoint waypoint) { 90 double shortestDistance = Double.MAX_VALUE; 91 if (way == null || waypoint == null) return shortestDistance; 92 LatLon llwaypoint = waypoint.getCoor(); 93 EastNorth enwaypoint = new EastNorth(llwaypoint.getY(), llwaypoint.getX()); 94 for (int i = 0; i < way.getNodesCount() - 1; i++) { 95 double distance = Double.MAX_VALUE; 96 LatLon llfirst = way.getNode(i).getCoor(); 97 LatLon llsecond = way.getNode(i + 1).getCoor(); 98 EastNorth first = new EastNorth(llfirst.getY(), llfirst.getX()); 99 EastNorth second = new EastNorth(llsecond.getY(), llsecond.getX()); 100 if (first.isValid() && second.isValid()) { 101 EastNorth closestPoint = Geometry.closestPointToSegment(first, second, enwaypoint); 102 distance = llwaypoint.greatCircleDistance(new LatLon(closestPoint.getX(), closestPoint.getY())); 103 } else if (first.isValid() && !second.isValid()) { 104 distance = getDistanceEastNorth(first, waypoint); 105 } else if (!first.isValid() && second.isValid()) { 106 distance = getDistanceEastNorth(second, waypoint); 107 } else if (!first.isValid() && !second.isValid()) { 108 distance = Double.MAX_VALUE; 109 } 110 if (distance < shortestDistance) shortestDistance = distance; 111 112 } 113 return shortestDistance; 91 if (way == null || waypoint == null) return Double.MAX_VALUE; 92 return Geometry.getDistanceWayNode(way, new Node(waypoint.getCoor())); 114 93 } 115 94 116 95 /** … … 118 97 * @param node Node to get the distance from 119 98 * @param waypoint WayPoint to get the distance to 120 99 * @return The distance between the two points 100 * @deprecated Use {@code Geometry.getDistance(node, new Node(waypoint.getCoor()))} 101 * instead 121 102 */ 103 @Deprecated 122 104 public static double getDistanceNode(Node node, WayPoint waypoint) { 123 if (node == null ) return Double.MAX_VALUE;124 return getDistanceLatLon(node.getCoor(), waypoint);105 if (node == null || waypoint == null) return Double.MAX_VALUE; 106 return Geometry.getDistance(node, new Node(waypoint.getCoor())); 125 107 } 126 108 127 109 /** … … 129 111 * @param en The EastNorth to get the distance to 130 112 * @param waypoint WayPoint to get the distance to 131 113 * @return The distance between the two points 114 * @deprecated Use {@code Geometry.getDistance(new Node(en), new Node(waypoint.getCoor()))} instead 132 115 */ 116 @Deprecated 133 117 public static double getDistanceEastNorth(EastNorth en, WayPoint waypoint) { 134 if (en == null || !en.isValid()) return Double.MAX_VALUE;135 return getDistanceLatLon(new LatLon(en.getY(), en.getX()), waypoint);118 if (en == null || waypoint == null) return Double.MAX_VALUE; 119 return Geometry.getDistance(new Node(en), new Node(waypoint.getCoor())); 136 120 } 137 121 138 122 /** … … 140 124 * @param latlon LatLon to get the distance from 141 125 * @param waypoint WayPoint to get the distance to 142 126 * @return The distance between the two points 127 * @deprecated Use {@code Geometry.getDistance(new Node(latlon), new Node(waypoint.getCoor()))} instead 143 128 */ 129 @Deprecated 144 130 public static double getDistanceLatLon(LatLon latlon, WayPoint waypoint) { 145 131 if (latlon == null || waypoint == null || waypoint.getCoor() == null) return Double.MAX_VALUE; 146 return waypoint.getCoor().greatCircleDistance(latlon);132 return Geometry.getDistance(new Node(latlon), new Node(waypoint.getCoor())); 147 133 } 148 134 } -
src/org/openstreetmap/josm/tools/Geometry.java
8 8 import java.math.BigDecimal; 9 9 import java.math.MathContext; 10 10 import java.util.ArrayList; 11 import java.util.Collection; 11 12 import java.util.Collections; 12 13 import java.util.Comparator; 13 14 import java.util.EnumSet; … … 30 31 import org.openstreetmap.josm.data.osm.MultipolygonBuilder.JoinedPolygon; 31 32 import org.openstreetmap.josm.data.osm.Node; 32 33 import org.openstreetmap.josm.data.osm.NodePositionComparator; 34 import org.openstreetmap.josm.data.osm.OsmPrimitive; 33 35 import org.openstreetmap.josm.data.osm.Relation; 34 36 import org.openstreetmap.josm.data.osm.Way; 37 import org.openstreetmap.josm.data.osm.WaySegment; 35 38 import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon; 36 39 import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache; 37 40 import org.openstreetmap.josm.data.projection.Projection; … … 1070 1073 } 1071 1074 return new AreaAndPerimeter(Math.abs(area) / 2, perimeter); 1072 1075 } 1076 1077 /** 1078 * Get the closest primitive to {@code osm} from the collection of OsmPrimitive {@code primitives} 1079 * @param osm The primitive to get the distances from 1080 * @param primitives The collection of primitives to get the distance to 1081 * @return The closest {@code OsmPrimitive} 1082 * @since xxx 1083 */ 1084 public static OsmPrimitive getClosestPrimitive(OsmPrimitive osm, Collection<OsmPrimitive> primitives) { 1085 double lowestDistance = Double.MAX_VALUE; 1086 OsmPrimitive closest = null; 1087 for (OsmPrimitive primitive : primitives) { 1088 double distance = getDistance(osm, primitive); 1089 if (distance < lowestDistance) { 1090 lowestDistance = distance; 1091 closest = primitive; 1092 } 1093 } 1094 return closest; 1095 } 1096 1097 /** 1098 * Get the furthest primitive to {@code osm} from the collection of OsmPrimitive {@code primitives} 1099 * @param osm The primitive to get the distances from 1100 * @param primitives The collection of primitives to get the distance to 1101 * @return The furthest {@code OsmPrimitive} 1102 * @since xxx 1103 */ 1104 public static OsmPrimitive getFurthestPrimitive(OsmPrimitive osm, Collection<OsmPrimitive> primitives) { 1105 double furthestDistance = Double.MIN_VALUE; 1106 OsmPrimitive furthest = null; 1107 for (OsmPrimitive primitive : primitives) { 1108 double distance = getDistance(osm, primitive); 1109 if (distance > furthestDistance) { 1110 furthestDistance = distance; 1111 furthest = primitive; 1112 } 1113 } 1114 return furthest; 1115 } 1116 1117 /** 1118 * Get the distance between different {@code OsmPrimitive}s 1119 * @param one The primitive to get the distance from 1120 * @param two The primitive to get the distance to 1121 * @return The distance between the primitives 1122 * @since xxx 1123 */ 1124 public static double getDistance(OsmPrimitive one, OsmPrimitive two) { 1125 double rValue = Double.MAX_VALUE; 1126 if (one == null || two == null) return rValue; 1127 if (one instanceof Node && two instanceof Node) { 1128 rValue = ((Node) one).getCoor().greatCircleDistance(((Node) two).getCoor()); 1129 } else if (one instanceof Node && two instanceof Way) { 1130 rValue = getDistanceWayNode((Way) two, (Node) one); 1131 } else if (one instanceof Way && two instanceof Node) { 1132 rValue = getDistanceWayNode((Way) one, (Node) two); 1133 } else if (one instanceof Way && two instanceof Way) { 1134 rValue = getDistanceWayWay((Way) one, (Way) two); 1135 } else if (one instanceof Relation && !(two instanceof Relation)) { 1136 for (OsmPrimitive osmPrimitive: ((Relation) one).getMemberPrimitives()) { 1137 double currentDistance = getDistance(osmPrimitive, two); 1138 if (currentDistance < rValue) rValue = currentDistance; 1139 } 1140 } else if (!(one instanceof Relation) && two instanceof Relation) { 1141 for (OsmPrimitive osmPrimitive : ((Relation) two).getMemberPrimitives()) { 1142 double currentDistance = getDistance(osmPrimitive, one); 1143 if (currentDistance < rValue) rValue = currentDistance; 1144 } 1145 } else if (one instanceof Relation && two instanceof Relation) { 1146 for (OsmPrimitive osmPrimitive1 : ((Relation) one).getMemberPrimitives()) { 1147 for (OsmPrimitive osmPrimitive2 : ((Relation) two).getMemberPrimitives()) { 1148 double currentDistance = getDistance(osmPrimitive1, osmPrimitive2); 1149 if (currentDistance < rValue) rValue = currentDistance; 1150 } 1151 } 1152 } 1153 return rValue; 1154 } 1155 1156 /** 1157 * Get the distance between a way and a node 1158 * @param way The way to get the distance from 1159 * @param node The node to get the distance to 1160 * @return The distance between the {@code way} and the {@code node} 1161 * @since xxx 1162 */ 1163 public static double getDistanceWayNode(Way way, Node node) { 1164 double rValue = Double.MAX_VALUE; 1165 if (way.getNodesCount() < 2) return rValue; 1166 List<WaySegment> segments = getWaySegments(way); 1167 for (WaySegment segment : segments) { 1168 EastNorth point = Geometry.closestPointToSegment(segment.getFirstNode().getEastNorth(), segment.getSecondNode().getEastNorth(), node.getEastNorth()); 1169 double distance = point.distance(node.getEastNorth()); 1170 if (distance < rValue) rValue = distance; 1171 } 1172 return rValue; 1173 } 1174 1175 /** 1176 * Get the closest {@code WaySegment} from a way to a primitive 1177 * @param way The {@code Way} to get the distance from and the {@code WaySegment} 1178 * @param primitive The {@code Primitive} to get the distance to 1179 * @return The {@code WaySegment} that is closest to {@code primitive} from {@code way} 1180 * @since xxx 1181 */ 1182 public static WaySegment getClosestWaySegment(Way way, OsmPrimitive primitive) { 1183 List<WaySegment> segments = getWaySegments(way); 1184 double lowestDistance = Double.MAX_VALUE; 1185 WaySegment closest = null; 1186 for (WaySegment segment : segments) { 1187 double distance = getDistance(segment.toWay(), primitive); 1188 if (distance < lowestDistance) { 1189 lowestDistance = distance; 1190 closest = segment; 1191 } 1192 } 1193 return closest; 1194 } 1195 1196 /** 1197 * Get the distance between different ways 1198 * @param one The way to get the distance from 1199 * @param two The {@code Way} to get the distance to 1200 * @return The shortest distance between the ways 1201 * @since xxx 1202 */ 1203 public static double getDistanceWayWay(Way one, Way two) { 1204 double rValue = Double.MAX_VALUE; 1205 List<WaySegment> oneSegments = getWaySegments(one); 1206 List<WaySegment> twoSegments = getWaySegments(two); 1207 for (WaySegment oneSegment : oneSegments) { 1208 for (WaySegment twoSegment : twoSegments) { 1209 EastNorth en1 = oneSegment.getFirstNode().getEastNorth(); 1210 EastNorth en2 = oneSegment.getSecondNode().getEastNorth(); 1211 EastNorth en3 = twoSegment.getFirstNode().getEastNorth(); 1212 EastNorth en4 = twoSegment.getSecondNode().getEastNorth(); 1213 if (en1 == null || en2 == null || en3 == null || en4 == null) continue; 1214 EastNorth intersection = Geometry.getSegmentSegmentIntersection( 1215 en1, en2, en3, en4); 1216 if (intersection != null) return 0.0; 1217 double distance = getDistanceSegmentSegment(oneSegment, twoSegment); 1218 if (distance < rValue) rValue = distance; 1219 } 1220 } 1221 return rValue; 1222 } 1223 1224 /** 1225 * Get the distance between different {@code WaySegment}s 1226 * @param one A {@code WaySegment} to get the distance from 1227 * @param two A {@code WaySegment} to get the distance to 1228 * @return The distance between the two {@code WaySegment}s 1229 * @since xxx 1230 */ 1231 public static double getDistanceSegmentSegment(WaySegment one, WaySegment two) { 1232 EastNorth vectorOne = one.getSecondNode().getEastNorth().subtract(one.getFirstNode().getEastNorth()); 1233 EastNorth vectorTwo = two.getSecondNode().getEastNorth().subtract(two.getFirstNode().getEastNorth()); 1234 EastNorth vectorThree = one.getFirstNode().getEastNorth().subtract(two.getFirstNode().getEastNorth()); 1235 double smallNumber = 0.00000000001; 1236 double a = dot(vectorOne, vectorOne); 1237 double b = dot(vectorOne, vectorTwo); 1238 double c = dot(vectorTwo, vectorTwo); 1239 double d = dot(vectorOne, vectorThree); 1240 double e = dot(vectorTwo, vectorThree); 1241 1242 double dotCombination = a * c - b * b; 1243 double sc; 1244 double sN; 1245 double sD = d; 1246 double tc; 1247 double tN; 1248 double tD = dotCombination; 1249 if (dotCombination < smallNumber) { 1250 sN = 0.0; 1251 sD = 1.0; 1252 tN = e; 1253 tD = c; 1254 } else { 1255 sN = (b * e - c * d); 1256 tN = (a * e - b * d); 1257 if (sN < 0.0) { 1258 sN = 0.0; 1259 tN = e; 1260 tD = c; 1261 } else if (sN > sD) { 1262 sN = sD; 1263 tN = e + b; 1264 tD = c; 1265 } 1266 } 1267 1268 if (tN < 0.0) { 1269 tN = 0.0; 1270 if (-d < 0.0) sN = 0.0; 1271 else if (-d > a) sN = sD; 1272 else { 1273 sN = -d; 1274 sD = a; 1275 } 1276 } else if (tN > tD) { 1277 tN = tD; 1278 if ((-d + b) < 0.0) sN = 0; 1279 else if ((-d + b) > a) sN = sD; 1280 else { 1281 sN = (-d + b); 1282 sD = a; 1283 } 1284 } 1285 sc = Math.abs(sN) < smallNumber ? 0.0 : sN / sD; 1286 tc = Math.abs(tN) < smallNumber ? 0.0 : tN / tD; 1287 EastNorth p1 = one.getFirstNode().getEastNorth().interpolate(one.getSecondNode().getEastNorth(), sc); 1288 EastNorth p2 = two.getFirstNode().getEastNorth().interpolate(two.getSecondNode().getEastNorth(), tc); 1289 return p1.distance(p2); 1290 } 1291 1292 /** 1293 * Get the dot product of two different EastNorth points 1294 * @param one The originating EastNorth 1295 * @param two The final EastNorth 1296 * @return the dot product of the EastNorths 1297 * @since xxx 1298 */ 1299 public static double dot(EastNorth one, EastNorth two) { 1300 return two.getX() * one.getX() + one.getY() * two.getY(); 1301 } 1302 1303 /** 1304 * Get the way segments of a way 1305 * @param way The way to get the way segments of 1306 * @return A lest of {@code WaySegment}s 1307 * @since xxx 1308 */ 1309 public static List<WaySegment> getWaySegments(Way way) { 1310 List<WaySegment> segments = new ArrayList<>(); 1311 int i = 0; 1312 do { 1313 segments.add(new WaySegment(way, i)); 1314 i++; 1315 } while (i < way.getNodesCount() - 2); 1316 return segments; 1317 } 1073 1318 } -
test/unit/org/openstreetmap/josm/tools/GeometryTest.java
4 4 import static org.junit.Assert.assertEquals; 5 5 6 6 import java.io.FileInputStream; 7 import java.util.ArrayList; 7 8 import java.util.Arrays; 8 9 import java.util.List; 9 10 … … 15 16 import org.openstreetmap.josm.data.coor.LatLon; 16 17 import org.openstreetmap.josm.data.osm.DataSet; 17 18 import org.openstreetmap.josm.data.osm.Node; 19 import org.openstreetmap.josm.data.osm.OsmPrimitive; 18 20 import org.openstreetmap.josm.data.osm.Relation; 21 import org.openstreetmap.josm.data.osm.RelationMember; 19 22 import org.openstreetmap.josm.data.osm.Way; 20 23 import org.openstreetmap.josm.data.osm.search.SearchCompiler; 21 24 import org.openstreetmap.josm.io.OsmReader; … … 158 161 assertEquals(new EastNorth(125, 300), Geometry.getCentroidEN(Arrays.asList(en1, en2))); 159 162 assertEquals(new EastNorth(150, 266d + 2d/3d), Geometry.getCentroidEN(Arrays.asList(en1, en2, en3))); 160 163 } 164 165 /** 166 * Test of {@link Geometry#getDistance} method. 167 */ 168 @Test 169 public void testGetDistance() { 170 Node node1 = new Node(new LatLon(0, 0)); 171 Node node2 = new Node(new LatLon(0, 1)); 172 Node node3 = new Node(new LatLon(1, 0)); 173 Node node4 = new Node(new LatLon(1, 1)); 174 Way way1 = TestUtils.newWay("", node1, node2); 175 Way way2 = TestUtils.newWay("", node3, node4); 176 Relation testRelation1 = new Relation(); 177 Relation testRelation2 = new Relation(); 178 testRelation1.addMember(new RelationMember("", way1)); 179 testRelation1.addMember(new RelationMember("", way2)); 180 testRelation2.addMember(new RelationMember("", node1)); 181 testRelation2.addMember(new RelationMember("", node2)); 182 testRelation2.addMember(new RelationMember("", node3)); 183 testRelation2.addMember(new RelationMember("", node4)); 184 185 double distance = Geometry.getDistance(null, node3); 186 assertEquals(Double.MAX_VALUE, distance, 0.1); 187 188 distance = Geometry.getDistance(way1, null); 189 assertEquals(Double.MAX_VALUE, distance, 0.1); 190 191 distance = Geometry.getDistance(null, null); 192 assertEquals(Double.MAX_VALUE, distance, 0.1); 193 194 distance = Geometry.getDistance(node1, node2); 195 assertEquals(111319.49079327357, distance, 0.1); 196 197 distance = Geometry.getDistance(way1, node3); 198 assertEquals(111325.1428663855, distance, 0.1); 199 200 distance = Geometry.getDistance(node3, way1); 201 assertEquals(111325.1428663855, distance, 0.1); 202 203 distance = Geometry.getDistance(way1, way2); 204 assertEquals(111325.1428663855, distance, 0.1); 205 206 distance = Geometry.getDistance(testRelation1, new Node(new LatLon(0, 0.5))); 207 assertEquals(0.0, distance, 0.1); 208 209 distance = Geometry.getDistance(new Node(new LatLon(0, 0.5)), testRelation1); 210 assertEquals(0.0, distance, 0.1); 211 212 distance = Geometry.getDistance(testRelation1, testRelation2); 213 assertEquals(0.0, distance, 0.1); 214 } 215 216 /** 217 * Test of {@link Geometry#getClosestPrimitive} method 218 */ 219 @Test 220 public void testGetClosestPrimitive() { 221 Node node1 = new Node(new LatLon(0, 0)); 222 Node node2 = new Node(new LatLon(0, 1)); 223 Node node3 = new Node(new LatLon(1, 0)); 224 Node node4 = new Node(new LatLon(1, 1)); 225 Way way1 = TestUtils.newWay("", node1, node2); 226 Way way2 = TestUtils.newWay("", node3, node4); 227 228 List<OsmPrimitive> primitives = new ArrayList<>(); 229 primitives.add(way1); 230 primitives.add(way2); 231 OsmPrimitive closest = Geometry.getClosestPrimitive(node1, primitives); 232 assertEquals(way1, closest); 233 } 234 235 /** 236 * Test of {@link Geometry#getFurthestPrimitive} method 237 */ 238 @Test 239 public void testGetFurthestPrimitive() { 240 Node node1 = new Node(new LatLon(0, 0)); 241 Node node2 = new Node(new LatLon(0, 1)); 242 Node node3 = new Node(new LatLon(1, 0)); 243 Node node4 = new Node(new LatLon(1, 1)); 244 Way way1 = TestUtils.newWay("", node1, node2); 245 Way way2 = TestUtils.newWay("", node3, node4); 246 Way way3 = TestUtils.newWay("", node2, node4); 247 Way way4 = TestUtils.newWay("", node1, node3); 248 249 List<OsmPrimitive> primitives = new ArrayList<>(); 250 primitives.add(way1); 251 primitives.add(way2); 252 primitives.add(way3); 253 primitives.add(way4); 254 OsmPrimitive furthest = Geometry.getFurthestPrimitive(new Node(new LatLon(0, 0.5)), primitives); 255 assertEquals(way2, furthest); 256 furthest = Geometry.getFurthestPrimitive(new Node(new LatLon(.25, 0.5)), primitives); 257 assertEquals(way2, furthest); 258 } 259 260 /** 261 * Test of {@link Geometry#getClosestWaySegment} method 262 */ 263 @Test 264 public void testGetClosestWaySegment() { 265 Node node1 = new Node(new LatLon(0, 0)); 266 Node node2 = new Node(new LatLon(0, 1)); 267 Node node3 = new Node(new LatLon(1, 0)); 268 Node node4 = new Node(new LatLon(1, 1)); 269 Way way1 = TestUtils.newWay("", node1, node2, node3, node4); 270 271 Way closestSegment = Geometry.getClosestWaySegment(way1, new Node(new LatLon(0, 0.5))).toWay(); 272 Assert.assertTrue(closestSegment.containsNode(node1)); 273 Assert.assertTrue(closestSegment.containsNode(node2)); 274 } 161 275 }