| 1109 | |
| 1110 | /** |
| 1111 | * Get the closest primitive to {@code osm} from the collection of |
| 1112 | * OsmPrimitive {@code primitives} |
| 1113 | * |
| 1114 | * The {@code primitives} should be fully downloaded to ensure accuracy. |
| 1115 | * |
| 1116 | * @param <T> The return type of the primitive |
| 1117 | * @param osm The primitive to get the distances from |
| 1118 | * @param primitives The collection of primitives to get the distance to |
| 1119 | * @return The closest {@link OsmPrimitive}. This is not determinative. |
| 1120 | * To get all primitives that share the same distance, use |
| 1121 | * {@link Geometry#getClosestPrimitives}. |
| 1122 | * @since xxx |
| 1123 | */ |
| 1124 | public static <T extends OsmPrimitive> T getClosestPrimitive(OsmPrimitive osm, Collection<T> primitives) { |
| 1125 | Collection<T> collection = getClosestPrimitives(osm, primitives); |
| 1126 | return collection.iterator().next(); |
| 1127 | } |
| 1128 | |
| 1129 | /** |
| 1130 | * Get the closest primitives to {@code osm} from the collection of |
| 1131 | * OsmPrimitive {@code primitives} |
| 1132 | * |
| 1133 | * The {@code primitives} should be fully downloaded to ensure accuracy. |
| 1134 | * |
| 1135 | * @param <T> The return type of the primitive |
| 1136 | * @param osm The primitive to get the distances from |
| 1137 | * @param primitives The collection of primitives to get the distance to |
| 1138 | * @return The closest {@link OsmPrimitive}s. May be empty. |
| 1139 | * @since xxx |
| 1140 | */ |
| 1141 | public static <T extends OsmPrimitive> Collection<T> getClosestPrimitives(OsmPrimitive osm, Collection<T> primitives) { |
| 1142 | double lowestDistance = Double.MAX_VALUE; |
| 1143 | TreeSet<T> closest = new TreeSet<>(); |
| 1144 | for (T primitive : primitives) { |
| 1145 | double distance = getDistance(osm, primitive); |
| 1146 | if (Double.isNaN(distance)) continue; |
| 1147 | if (distance < lowestDistance) { |
| 1148 | closest.clear(); |
| 1149 | lowestDistance = distance; |
| 1150 | closest.add(primitive); |
| 1151 | } else if (distance == lowestDistance) { |
| 1152 | closest.add(primitive); |
| 1153 | } |
| 1154 | } |
| 1155 | return closest; |
| 1156 | } |
| 1157 | |
| 1158 | /** |
| 1159 | * Get the furthest primitive to {@code osm} from the collection of |
| 1160 | * OsmPrimitive {@code primitives} |
| 1161 | * |
| 1162 | * The {@code primitives} should be fully downloaded to ensure accuracy. |
| 1163 | * |
| 1164 | * It does NOT give the furthest primitive based off of the furthest |
| 1165 | * part of that primitive |
| 1166 | * @param <T> The return type of the primitive |
| 1167 | * @param osm The primitive to get the distances from |
| 1168 | * @param primitives The collection of primitives to get the distance to |
| 1169 | * @return The furthest {@link OsmPrimitive}. This is not determinative. |
| 1170 | * To get all primitives that share the same distance, use |
| 1171 | * {@link Geometry#getFurthestPrimitives} |
| 1172 | * @since xxx |
| 1173 | */ |
| 1174 | public static <T extends OsmPrimitive> T getFurthestPrimitive(OsmPrimitive osm, Collection<T> primitives) { |
| 1175 | return getFurthestPrimitives(osm, primitives).iterator().next(); |
| 1176 | } |
| 1177 | |
| 1178 | /** |
| 1179 | * Get the furthest primitives to {@code osm} from the collection of |
| 1180 | * OsmPrimitive {@code primitives} |
| 1181 | * |
| 1182 | * The {@code primitives} should be fully downloaded to ensure accuracy. |
| 1183 | * |
| 1184 | * It does NOT give the furthest primitive based off of the furthest |
| 1185 | * part of that primitive |
| 1186 | * @param <T> The return type of the primitive |
| 1187 | * @param osm The primitive to get the distances from |
| 1188 | * @param primitives The collection of primitives to get the distance to |
| 1189 | * @return The furthest {@link OsmPrimitive}s. It may return an empty collection. |
| 1190 | * @since xxx |
| 1191 | */ |
| 1192 | public static <T extends OsmPrimitive> Collection<T> getFurthestPrimitives(OsmPrimitive osm, Collection<T> primitives) { |
| 1193 | double furthestDistance = Double.NEGATIVE_INFINITY; |
| 1194 | TreeSet<T> furthest = new TreeSet<>(); |
| 1195 | for (T primitive : primitives) { |
| 1196 | double distance = getDistance(osm, primitive); |
| 1197 | if (Double.isNaN(distance)) continue; |
| 1198 | if (distance > furthestDistance) { |
| 1199 | furthest.clear(); |
| 1200 | furthestDistance = distance; |
| 1201 | furthest.add(primitive); |
| 1202 | } else if (distance == furthestDistance) { |
| 1203 | furthest.add(primitive); |
| 1204 | } |
| 1205 | } |
| 1206 | return furthest; |
| 1207 | } |
| 1208 | |
| 1209 | /** |
| 1210 | * Get the distance between different {@link OsmPrimitive}s |
| 1211 | * @param one The primitive to get the distance from |
| 1212 | * @param two The primitive to get the distance to |
| 1213 | * @return The distance between the primitives in meters |
| 1214 | * (or the unit of the current projection, see {@link Projection}). |
| 1215 | * May return {@link Double#NaN} if one of the primitives is incomplete. |
| 1216 | * @since xxx |
| 1217 | */ |
| 1218 | public static double getDistance(OsmPrimitive one, OsmPrimitive two) { |
| 1219 | double rValue = Double.MAX_VALUE; |
| 1220 | if (one == null || two == null || one.isIncomplete() |
| 1221 | || two.isIncomplete()) return Double.NaN; |
| 1222 | if (one instanceof Node && two instanceof Node) { |
| 1223 | rValue = ((Node) one).getCoor().greatCircleDistance(((Node) two).getCoor()); |
| 1224 | } else if (one instanceof Node && two instanceof Way) { |
| 1225 | rValue = getDistanceWayNode((Way) two, (Node) one); |
| 1226 | } else if (one instanceof Way && two instanceof Node) { |
| 1227 | rValue = getDistanceWayNode((Way) one, (Node) two); |
| 1228 | } else if (one instanceof Way && two instanceof Way) { |
| 1229 | rValue = getDistanceWayWay((Way) one, (Way) two); |
| 1230 | } else if (one instanceof Relation && !(two instanceof Relation)) { |
| 1231 | for (OsmPrimitive osmPrimitive: ((Relation) one).getMemberPrimitives()) { |
| 1232 | double currentDistance = getDistance(osmPrimitive, two); |
| 1233 | if (currentDistance < rValue) rValue = currentDistance; |
| 1234 | } |
| 1235 | } else if (!(one instanceof Relation) && two instanceof Relation) { |
| 1236 | for (OsmPrimitive osmPrimitive : ((Relation) two).getMemberPrimitives()) { |
| 1237 | double currentDistance = getDistance(osmPrimitive, one); |
| 1238 | if (currentDistance < rValue) rValue = currentDistance; |
| 1239 | } |
| 1240 | } else if (one instanceof Relation && two instanceof Relation) { |
| 1241 | for (OsmPrimitive osmPrimitive1 : ((Relation) one).getMemberPrimitives()) { |
| 1242 | for (OsmPrimitive osmPrimitive2 : ((Relation) two).getMemberPrimitives()) { |
| 1243 | double currentDistance = getDistance(osmPrimitive1, osmPrimitive2); |
| 1244 | if (currentDistance < rValue) rValue = currentDistance; |
| 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | return rValue; |
| 1249 | } |
| 1250 | |
| 1251 | /** |
| 1252 | * Get the distance between a way and a node |
| 1253 | * @param way The way to get the distance from |
| 1254 | * @param node The node to get the distance to |
| 1255 | * @return The distance between the {@code way} and the {@code node} in |
| 1256 | * meters (or the unit of the current projection, see {@link Projection}). |
| 1257 | * May return {@link Double#NaN} if the primitives are incomplete. |
| 1258 | * @since xxx |
| 1259 | */ |
| 1260 | public static double getDistanceWayNode(Way way, Node node) { |
| 1261 | if (way == null || node == null || way.isIncomplete() |
| 1262 | || node.isIncomplete()) return Double.NaN; |
| 1263 | double rValue = Double.MAX_VALUE; |
| 1264 | for (Pair<Node, Node> nodes : way.getNodePairs(false)) { |
| 1265 | EastNorth point = Geometry.closestPointToSegment( |
| 1266 | nodes.a.getEastNorth(), nodes.b.getEastNorth(), |
| 1267 | node.getEastNorth()); |
| 1268 | double distance = point.distance(node.getEastNorth()); |
| 1269 | if (distance < rValue) rValue = distance; |
| 1270 | } |
| 1271 | return rValue; |
| 1272 | } |
| 1273 | |
| 1274 | /** |
| 1275 | * Get the closest {@link WaySegment} from a way to a primitive. |
| 1276 | * @param way The {@link Way} to get the distance from and the {@link WaySegment} |
| 1277 | * @param primitive The {@link OsmPrimitive} to get the distance to |
| 1278 | * @return The {@link WaySegment} that is closest to {@code primitive} from {@code way}. |
| 1279 | * If there are multiple {@link WaySegment}s with the same distance, the last |
| 1280 | * {@link WaySegment} with the same distance will be returned. |
| 1281 | * May return {@code null} if the way has fewer than two nodes or one |
| 1282 | * of the primitives is incomplete. |
| 1283 | * @since xxx |
| 1284 | */ |
| 1285 | public static WaySegment getClosestWaySegment(Way way, OsmPrimitive primitive) { |
| 1286 | if (way == null || primitive == null || way.isIncomplete() |
| 1287 | || primitive.isIncomplete()) return null; |
| 1288 | double lowestDistance = Double.MAX_VALUE; |
| 1289 | Pair<Node, Node> closestNodes = null; |
| 1290 | for (Pair<Node, Node> nodes : way.getNodePairs(false)) { |
| 1291 | Way tWay = new Way(); |
| 1292 | tWay.addNode(nodes.a); |
| 1293 | tWay.addNode(nodes.b); |
| 1294 | double distance = getDistance(tWay, primitive); |
| 1295 | if (distance < lowestDistance) { |
| 1296 | lowestDistance = distance; |
| 1297 | closestNodes = nodes; |
| 1298 | } |
| 1299 | } |
| 1300 | if (closestNodes == null) return null; |
| 1301 | return WaySegment.forNodePair(way, closestNodes.a, closestNodes.b); |
| 1302 | } |
| 1303 | |
| 1304 | /** |
| 1305 | * Get the distance between different ways |
| 1306 | * @param one The way to get the distance from |
| 1307 | * @param two The {@link Way} to get the distance to |
| 1308 | * @return The shortest distance between the ways in meters |
| 1309 | * (or the unit of the current projection, see {@link Projection}). |
| 1310 | * May return {@link Double#NaN}. |
| 1311 | * @since xxx |
| 1312 | */ |
| 1313 | public static double getDistanceWayWay(Way one, Way two) { |
| 1314 | if (one == null || two == null || one.isIncomplete() |
| 1315 | || two.isIncomplete()) return Double.NaN; |
| 1316 | |
| 1317 | double rValue = Double.MAX_VALUE; |
| 1318 | for (Pair<Node, Node> oneNodes : one.getNodePairs(false)) { |
| 1319 | for (Pair<Node, Node> twoNodes : two.getNodePairs(false)) { |
| 1320 | double distance = getDistanceSegmentSegment(oneNodes.a, oneNodes.b, twoNodes.a, twoNodes.b); |
| 1321 | if (distance < rValue) rValue = distance; |
| 1322 | } |
| 1323 | } |
| 1324 | return rValue; |
| 1325 | } |
| 1326 | |
| 1327 | /** |
| 1328 | * Get the distance between different {@link WaySegment}s |
| 1329 | * @param one A {@link WaySegment} to get the distance from |
| 1330 | * @param two A {@link WaySegment} to get the distance to |
| 1331 | * @return The distance between the two {@link WaySegment}s in meters |
| 1332 | * (or the unit of the current projection, see {@link Projection}). |
| 1333 | * May return {@link Double#NaN}. |
| 1334 | * @since xxx |
| 1335 | */ |
| 1336 | public static double getDistanceSegmentSegment(WaySegment one, WaySegment two) { |
| 1337 | return getDistanceSegmentSegment(one.getFirstNode(), one.getSecondNode(), two.getFirstNode(), two.getSecondNode()); |
| 1338 | } |
| 1339 | |
| 1340 | /** |
| 1341 | * Get the distance between different {@link WaySegment}s |
| 1342 | * @param way1Node1 The first node of the first WaySegment |
| 1343 | * @param way1Node2 The second node of the second WaySegment |
| 1344 | * @param way2Node1 The first node of the second WaySegment |
| 1345 | * @param way2Node2 The second node of the second WaySegment |
| 1346 | * @return The distance between the two {@link WaySegment}s in meters |
| 1347 | * (or the unit of the current projection, see {@link Projection}). |
| 1348 | * May return {@link Double#NaN}. |
| 1349 | * @since xxx |
| 1350 | */ |
| 1351 | public static double getDistanceSegmentSegment(Node way1Node1, Node way1Node2, Node way2Node1, Node way2Node2) { |
| 1352 | EastNorth enWay1Node1 = way1Node1.getEastNorth(); |
| 1353 | EastNorth enWay1Node2 = way1Node2.getEastNorth(); |
| 1354 | EastNorth enWay2Node1 = way2Node1.getEastNorth(); |
| 1355 | EastNorth enWay2Node2 = way2Node2.getEastNorth(); |
| 1356 | if (enWay1Node1 == null || enWay1Node2 == null || enWay2Node1 == null || enWay2Node2 == null) |
| 1357 | return Double.NaN; |
| 1358 | if (getSegmentSegmentIntersection(enWay1Node1, enWay1Node2, enWay2Node1, enWay2Node2) != null) |
| 1359 | return 0; |
| 1360 | |
| 1361 | EastNorth vectorOne = enWay1Node2.subtract(enWay1Node1); |
| 1362 | EastNorth vectorTwo = enWay2Node2.subtract(enWay2Node1); |
| 1363 | EastNorth vectorThree = enWay1Node1.subtract(enWay2Node1); |
| 1364 | double smallNumber = 0.00000000001; |
| 1365 | double a = dot(vectorOne, vectorOne); |
| 1366 | double b = dot(vectorOne, vectorTwo); |
| 1367 | double c = dot(vectorTwo, vectorTwo); |
| 1368 | double d = dot(vectorOne, vectorThree); |
| 1369 | double e = dot(vectorTwo, vectorThree); |
| 1370 | |
| 1371 | double dotCombination = a * c - b * b; |
| 1372 | double sc; |
| 1373 | double sN; |
| 1374 | double sD = d; |
| 1375 | double tc; |
| 1376 | double tN; |
| 1377 | double tD = dotCombination; |
| 1378 | if (dotCombination < smallNumber) { |
| 1379 | sN = 0.0; |
| 1380 | sD = 1.0; |
| 1381 | tN = e; |
| 1382 | tD = c; |
| 1383 | } else { |
| 1384 | sN = (b * e - c * d); |
| 1385 | tN = (a * e - b * d); |
| 1386 | if (sN < 0.0) { |
| 1387 | sN = 0.0; |
| 1388 | tN = e; |
| 1389 | tD = c; |
| 1390 | } else if (sN > sD) { |
| 1391 | sN = sD; |
| 1392 | tN = e + b; |
| 1393 | tD = c; |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | if (tN < 0.0) { |
| 1398 | tN = 0.0; |
| 1399 | if (-d < 0.0) sN = 0.0; |
| 1400 | else if (-d > a) sN = sD; |
| 1401 | else { |
| 1402 | sN = -d; |
| 1403 | sD = a; |
| 1404 | } |
| 1405 | } else if (tN > tD) { |
| 1406 | tN = tD; |
| 1407 | if ((-d + b) < 0.0) sN = 0; |
| 1408 | else if ((-d + b) > a) sN = sD; |
| 1409 | else { |
| 1410 | sN = (-d + b); |
| 1411 | sD = a; |
| 1412 | } |
| 1413 | } |
| 1414 | sc = Math.abs(sN) < smallNumber ? 0.0 : sN / sD; |
| 1415 | tc = Math.abs(tN) < smallNumber ? 0.0 : tN / tD; |
| 1416 | EastNorth p1 = enWay1Node1.interpolate(enWay1Node2, sc); |
| 1417 | EastNorth p2 = enWay2Node1.interpolate(enWay2Node2, tc); |
| 1418 | return p1.distance(p2); |
| 1419 | } |
| 1420 | |
| 1421 | /** |
| 1422 | * Get the dot product of two different EastNorth points |
| 1423 | * @param one The originating EastNorth |
| 1424 | * @param two The final EastNorth |
| 1425 | * @return the dot product of the EastNorths |
| 1426 | * @since xxx |
| 1427 | */ |
| 1428 | public static double dot(EastNorth one, EastNorth two) { |
| 1429 | return two.getX() * one.getX() + one.getY() * two.getY(); |
| 1430 | } |
| 1431 | |
| 1432 | /** |
| 1433 | * Get the distance between different {@link WaySegment}s |
| 1434 | * @param way1Node1 The first node of the first WaySegment |
| 1435 | * @param way1Node2 The second node of the second WaySegment |
| 1436 | * @param way2Node1 The first node of the second WaySegment |
| 1437 | * @param way2Node2 The second node of the second WaySegment |
| 1438 | * @return The distance between the two {@link WaySegment}s in meters |
| 1439 | * (or the unit of the current projection, see {@link Projection}). |
| 1440 | * May return {@link Double#NaN}. |
| 1441 | * @since xxx |
| 1442 | */ |
| 1443 | public static double getDistanceSegmentSegmentSlow(Node way1Node1, Node way1Node2, Node way2Node1, Node way2Node2) { |
| 1444 | EastNorth enWay1Node1 = way1Node1.getEastNorth(); |
| 1445 | EastNorth enWay1Node2 = way1Node2.getEastNorth(); |
| 1446 | EastNorth enWay2Node1 = way2Node1.getEastNorth(); |
| 1447 | EastNorth enWay2Node2 = way2Node2.getEastNorth(); |
| 1448 | if (enWay1Node1 == null || enWay1Node2 == null || enWay2Node1 == null || enWay2Node2 == null) |
| 1449 | return Double.NaN; |
| 1450 | if (Geometry.getSegmentSegmentIntersection(enWay1Node1, enWay1Node2, enWay2Node1, enWay2Node2) != null) |
| 1451 | return 0; |
| 1452 | |
| 1453 | double dist = Geometry.getNodeSegmentDist(enWay1Node1, enWay1Node2, enWay2Node1); |
| 1454 | dist = Math.min(dist, getNodeSegmentDist(enWay1Node1, enWay1Node2, enWay2Node2)); |
| 1455 | dist = Math.min(dist, getNodeSegmentDist(enWay2Node1, enWay2Node2, enWay1Node1)); |
| 1456 | dist = Math.min(dist, getNodeSegmentDist(enWay2Node1, enWay2Node2, enWay1Node2)); |
| 1457 | return dist; |
| 1458 | } |
| 1459 | |
| 1460 | private static double getNodeSegmentDist(EastNorth s1, EastNorth s2, EastNorth p) { |
| 1461 | EastNorth c1 = Geometry.closestPointTo(s1, s2, p, true); |
| 1462 | return c1.distance(p); |
| 1463 | } |
| 1464 | |