Opened 6 years ago
Closed 6 years ago
#16854 closed enhancement (fixed)
Stability of created primitive IDs
Reported by: | Don-vip | Owned by: | Don-vip |
---|---|---|---|
Priority: | normal | Milestone: | 18.12 |
Component: | Core | Version: | |
Keywords: | Cc: |
Description
This patch should add stability to negative ids of created primitives, when reloading an .osm file.
It needs further tests before merging.
This is needed to edit our internal boundaries file (see #14833, #15036, #15870) and saving command stack state to session files (#12726).
-
src/org/openstreetmap/josm/io/AbstractReader.java
13 13 import java.util.List; 14 14 import java.util.Map; 15 15 import java.util.Map.Entry; 16 import java.util.OptionalLong; 16 17 import java.util.function.Consumer; 17 18 18 19 import org.openstreetmap.josm.data.Bounds; … … 322 323 } catch (IOException e) { 323 324 throw new IllegalDataException(e); 324 325 } finally { 326 OptionalLong minId = externalIdMap.values().stream().mapToLong(AbstractPrimitive::getUniqueId).min(); 327 if (minId.isPresent() && minId.getAsLong() < AbstractPrimitive.currentUniqueId()) { 328 AbstractPrimitive.advanceUniqueId(minId.getAsLong()); 329 } 325 330 progressMonitor.finishTask(); 326 331 progressMonitor.removeCancelListener(cancelListener); 327 332 } … … 603 608 return !Double.isNaN(lat) && !Double.isNaN(lon); 604 609 } 605 610 611 @SuppressWarnings("unchecked") 612 private <T extends OsmPrimitive> T buildPrimitive(PrimitiveData pd) { 613 OsmPrimitive p; 614 if (pd.getUniqueId() < AbstractPrimitive.currentUniqueId()) { 615 p = pd.getType().newInstance(pd.getUniqueId(), true); 616 } else { 617 p = pd.getType().newVersionedInstance(pd.getId(), pd.getVersion()); 618 } 619 p.setVisible(pd.isVisible()); 620 p.load(pd); 621 externalIdMap.put(pd.getPrimitiveId(), p); 622 return (T) p; 623 } 624 606 625 private Node addNode(NodeData nd, NodeReader nodeReader) throws IllegalDataException { 607 Node n = new Node(nd.getId(), nd.getVersion()); 608 n.setVisible(nd.isVisible()); 609 n.load(nd); 626 Node n = buildPrimitive(nd); 610 627 nodeReader.accept(n); 611 externalIdMap.put(nd.getPrimitiveId(), n);612 628 return n; 613 629 } 614 630 615 631 protected final Node parseNode(double lat, double lon, CommonReader commonReader, NodeReader nodeReader) 616 632 throws IllegalDataException { 617 NodeData nd = new NodeData( );633 NodeData nd = new NodeData(0); 618 634 LatLon ll = null; 619 635 if (areLatLonDefined(lat, lon)) { 620 636 try { … … 653 669 } 654 670 655 671 protected final Way parseWay(CommonReader commonReader, WayReader wayReader) throws IllegalDataException { 656 WayData wd = new WayData( );672 WayData wd = new WayData(0); 657 673 commonReader.accept(wd); 658 Way w = new Way(wd.getId(), wd.getVersion()); 659 w.setVisible(wd.isVisible()); 660 w.load(wd); 661 externalIdMap.put(wd.getPrimitiveId(), w); 674 Way w = buildPrimitive(wd); 662 675 663 676 Collection<Long> nodeIds = new ArrayList<>(); 664 677 wayReader.accept(w, nodeIds); … … 671 684 } 672 685 673 686 protected final Relation parseRelation(CommonReader commonReader, RelationReader relationReader) throws IllegalDataException { 674 RelationData rd = new RelationData( );687 RelationData rd = new RelationData(0); 675 688 commonReader.accept(rd); 676 Relation r = new Relation(rd.getId(), rd.getVersion()); 677 r.setVisible(rd.isVisible()); 678 r.load(rd); 679 externalIdMap.put(rd.getPrimitiveId(), r); 689 Relation r = buildPrimitive(rd); 680 690 681 691 Collection<RelationMemberData> members = new ArrayList<>(); 682 692 relationReader.accept(r, members);
Attachments (0)
Change History (9)
comment:1 by , 6 years ago
comment:2 by , 6 years ago
Honestly I don't remember right now it's an update of a patch I made years ago maybe I forgot one part. I'll check it after Karlsruhe.
comment:3 by , 6 years ago
Milestone: | 18.10 → 18.11 |
---|
comment:4 by , 6 years ago
Milestone: | 18.11 → 18.12 |
---|
comment:6 by , 6 years ago
I've accidentally committed the changes. Let's see tomorrow if it causes problems I didn't see in my earlier tests.
comment:8 by , 6 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:9 by , 6 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
I've done a few tests and JOSM behaves OK. Plus, no-one complained about weird issues, so the code seems OK.
I have trouble understanding the process to keep ID stable from the code above. Can you please explain the algorithms with words?
What happens when joining two loaded files and in similar more complex cases?