Changeset 12049 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2017-05-03T16:58:33+02:00 (8 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/data/osm
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/DataSet.java
r12048 r12049 55 55 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 56 56 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager; 57 import org.openstreetmap.josm.tools.JosmRuntimeException;58 57 import org.openstreetmap.josm.tools.ListenerList; 59 58 import org.openstreetmap.josm.tools.SubclassFilteredCollection; … … 104 103 * @author imi 105 104 */ 106 public final class DataSet implements Data, ProjectionChangeListener {105 public final class DataSet extends QuadBucketPrimitiveStore implements Data, ProjectionChangeListener { 107 106 108 107 /** … … 216 215 try { 217 216 Map<OsmPrimitive, OsmPrimitive> primMap = new HashMap<>(); 218 for (Node n : copyFrom. nodes) {217 for (Node n : copyFrom.getNodes()) { 219 218 Node newNode = new Node(n); 220 219 primMap.put(n, newNode); 221 220 addPrimitive(newNode); 222 221 } 223 for (Way w : copyFrom. ways) {222 for (Way w : copyFrom.getWays()) { 224 223 Way newWay = new Way(w); 225 224 primMap.put(w, newWay); … … 233 232 // Because relations can have other relations as members we first clone all relations 234 233 // and then get the cloned members 235 for (Relation r : copyFrom.relations) { 234 Collection<Relation> relations = copyFrom.getRelations(); 235 for (Relation r : relations) { 236 236 Relation newRelation = new Relation(r, r.isNew()); 237 237 newRelation.setMembers(null); … … 239 239 addPrimitive(newRelation); 240 240 } 241 for (Relation r : copyFrom.relations) {241 for (Relation r : relations) { 242 242 Relation newRelation = (Relation) primMap.get(r); 243 243 List<RelationMember> newMembers = new ArrayList<>(); … … 418 418 419 419 /** 420 * All nodes goes here, even when included in other data (ways etc). This enables the instant421 * conversion of the whole DataSet by iterating over this data structure.422 */423 private final QuadBuckets<Node> nodes = new QuadBuckets<>();424 425 /**426 420 * Gets a filtered collection of primitives matching the given predicate. 427 421 * @param <T> The primitive type. … … 443 437 } 444 438 445 /** 446 * Searches for nodes in the given bounding box. 447 * @param bbox the bounding box 448 * @return List of nodes in the given bbox. Can be empty but not null 449 */ 439 @Override 450 440 public List<Node> searchNodes(BBox bbox) { 451 441 lock.readLock().lock(); 452 442 try { 453 return nodes.search(bbox);443 return super.searchNodes(bbox); 454 444 } finally { 455 445 lock.readLock().unlock(); … … 458 448 459 449 /** 460 * Determines if the given node can be retrieved in the data set through its bounding box. Useful for dataset consistency test.461 * For efficiency reasons this method does not lock the dataset, you have to lock it manually.462 *463 * @param n The node to search464 * @return {@code true} if {@code n} ban be retrieved in this data set, {@code false} otherwise465 * @since 7501466 */467 public boolean containsNode(Node n) {468 return nodes.contains(n);469 }470 471 /**472 * All ways (Streets etc.) in the DataSet.473 *474 * The way nodes are stored only in the way list.475 */476 private final QuadBuckets<Way> ways = new QuadBuckets<>();477 478 /**479 450 * Replies an unmodifiable collection of ways in this dataset 480 451 * … … 485 456 } 486 457 487 /** 488 * Searches for ways in the given bounding box. 489 * @param bbox the bounding box 490 * @return List of ways in the given bbox. Can be empty but not null 491 */ 458 @Override 492 459 public List<Way> searchWays(BBox bbox) { 493 460 lock.readLock().lock(); 494 461 try { 495 return ways.search(bbox);462 return super.searchWays(bbox); 496 463 } finally { 497 464 lock.readLock().unlock(); 498 465 } 499 }500 501 /**502 * Determines if the given way can be retrieved in the data set through its bounding box. Useful for dataset consistency test.503 * For efficiency reasons this method does not lock the dataset, you have to lock it manually.504 *505 * @param w The way to search506 * @return {@code true} if {@code w} ban be retrieved in this data set, {@code false} otherwise507 * @since 7501508 */509 public boolean containsWay(Way w) {510 return ways.contains(w);511 }512 513 /**514 * All relations/relationships515 */516 private final Collection<Relation> relations = new ArrayList<>();517 518 /**519 * Replies an unmodifiable collection of relations in this dataset520 *521 * @return an unmodifiable collection of relations in this dataset522 */523 public Collection<Relation> getRelations() {524 return getPrimitives(Relation.class::isInstance);525 466 } 526 467 … … 530 471 * @return List of relations in the given bbox. Can be empty but not null 531 472 */ 473 @Override 532 474 public List<Relation> searchRelations(BBox bbox) { 533 475 lock.readLock().lock(); 534 476 try { 535 // QuadBuckets might be useful here (don't forget to do reindexing after some of rm is changed) 536 return relations.stream() 537 .filter(r -> r.getBBox().intersects(bbox)) 538 .collect(Collectors.toList()); 477 return super.searchRelations(bbox); 539 478 } finally { 540 479 lock.readLock().unlock(); … … 543 482 544 483 /** 545 * Determines if the given relation can be retrieved in the data set through its bounding box. Useful for dataset consistency test. 546 * For efficiency reasons this method does not lock the dataset, you have to lock it manually. 547 * 548 * @param r The relation to search 549 * @return {@code true} if {@code r} ban be retrieved in this data set, {@code false} otherwise 550 * @since 7501 551 */ 552 public boolean containsRelation(Relation r) { 553 return relations.contains(r); 484 * Replies an unmodifiable collection of relations in this dataset 485 * 486 * @return an unmodifiable collection of relations in this dataset 487 */ 488 public Collection<Relation> getRelations() { 489 return getPrimitives(Relation.class::isInstance); 554 490 } 555 491 … … 605 541 * @param primitive the primitive. 606 542 */ 543 @Override 607 544 public void addPrimitive(OsmPrimitive primitive) { 608 545 Objects.requireNonNull(primitive, "primitive"); … … 616 553 primitive.setDataset(this); 617 554 primitive.updatePosition(); // Set cached bbox for way and relation (required for reindexWay and reindexRelation to work properly) 618 boolean success = false; 619 if (primitive instanceof Node) { 620 success = nodes.add((Node) primitive); 621 } else if (primitive instanceof Way) { 622 success = ways.add((Way) primitive); 623 } else if (primitive instanceof Relation) { 624 success = relations.add((Relation) primitive); 625 } 626 if (!success) 627 throw new JosmRuntimeException("failed to add primitive: "+primitive); 555 super.addPrimitive(primitive); 628 556 firePrimitivesAdded(Collections.singletonList(primitive), false); 629 557 } finally { … … 647 575 if (primitive == null) 648 576 return; 649 boolean success = false; 650 if (primitive instanceof Node) { 651 success = nodes.remove(primitive); 652 } else if (primitive instanceof Way) { 653 success = ways.remove(primitive); 654 } else if (primitive instanceof Relation) { 655 success = relations.remove(primitive); 656 } 657 if (!success) 658 throw new JosmRuntimeException("failed to remove primitive: "+primitive); 577 super.removePrimitive(primitive); 659 578 clearSelection(primitiveId); 660 579 allPrimitives.remove(primitive); … … 1124 1043 beginUpdate(); 1125 1044 try { 1126 for (Relation relation : relations) {1045 for (Relation relation : getRelations()) { 1127 1046 List<RelationMember> members = relation.getMembers(); 1128 1047 … … 1181 1100 } 1182 1101 return false; 1183 }1184 1185 private void reindexNode(Node node, LatLon newCoor, EastNorth eastNorth) {1186 if (!nodes.remove(node))1187 throw new JosmRuntimeException("Reindexing node failed to remove");1188 node.setCoorInternal(newCoor, eastNorth);1189 if (!nodes.add(node))1190 throw new JosmRuntimeException("Reindexing node failed to add");1191 for (OsmPrimitive primitive: node.getReferrers()) {1192 if (primitive instanceof Way) {1193 reindexWay((Way) primitive);1194 } else {1195 reindexRelation((Relation) primitive);1196 }1197 }1198 }1199 1200 private void reindexWay(Way way) {1201 BBox before = way.getBBox();1202 if (!ways.remove(way))1203 throw new JosmRuntimeException("Reindexing way failed to remove");1204 way.updatePosition();1205 if (!ways.add(way))1206 throw new JosmRuntimeException("Reindexing way failed to add");1207 if (!way.getBBox().equals(before)) {1208 for (OsmPrimitive primitive: way.getReferrers()) {1209 reindexRelation((Relation) primitive);1210 }1211 }1212 }1213 1214 private static void reindexRelation(Relation relation) {1215 BBox before = relation.getBBox();1216 relation.updatePosition();1217 if (!before.equals(relation.getBBox())) {1218 for (OsmPrimitive primitive: relation.getReferrers()) {1219 reindexRelation((Relation) primitive);1220 }1221 }1222 1102 } 1223 1103 … … 1372 1252 try { 1373 1253 cleanupDeleted(Stream.concat( 1374 nodes.stream(), Stream.concat(ways.stream(), relations.stream())));1254 getNodes().stream(), Stream.concat(getNodes().stream(), getNodes().stream()))); 1375 1255 } finally { 1376 1256 endUpdate(); … … 1379 1259 1380 1260 private void cleanupDeleted(Stream<? extends OsmPrimitive> it) { 1381 clearSelection(it1382 . filter(primitive -> primitive.isDeleted() && (!primitive.isVisible() || primitive.isNew()))1383 . peek(allPrimitives::remove)1384 .peek(primitive -> primitive.setDataset(null))); 1261 it.filter(primitive -> primitive.isDeleted() && (!primitive.isVisible() || primitive.isNew())) 1262 .collect(Collectors.toList()) 1263 .forEach(primitive -> this.removePrimitive(primitive.getPrimitiveId())); 1264 1385 1265 } 1386 1266 … … 1388 1268 * Removes all primitives from the dataset and resets the currently selected primitives 1389 1269 * to the empty collection. Also notifies selection change listeners if necessary. 1390 * 1391 */1270 */ 1271 @Override 1392 1272 public void clear() { 1393 1273 beginUpdate(); … … 1397 1277 primitive.setDataset(null); 1398 1278 } 1399 nodes.clear(); 1400 ways.clear(); 1401 relations.clear(); 1279 super.clear(); 1402 1280 allPrimitives.clear(); 1403 1281 } finally {
Note:
See TracChangeset
for help on using the changeset viewer.