[8378] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[7937] | 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.util.ArrayList;
|
---|
| 7 | import java.util.Collection;
|
---|
| 8 | import java.util.HashMap;
|
---|
| 9 | import java.util.List;
|
---|
| 10 | import java.util.Map;
|
---|
[8332] | 11 | import java.util.Map.Entry;
|
---|
[7937] | 12 |
|
---|
| 13 | import org.openstreetmap.josm.Main;
|
---|
| 14 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
| 15 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 16 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 18 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
| 19 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
| 20 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
| 21 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
| 22 | import org.openstreetmap.josm.data.osm.RelationMemberData;
|
---|
| 23 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
| 24 | import org.openstreetmap.josm.data.osm.Way;
|
---|
| 25 |
|
---|
| 26 | /**
|
---|
| 27 | * Abstract Reader, allowing other implementations than OsmReader (PbfReader in PBF plugin for example)
|
---|
| 28 | * @author Vincent
|
---|
| 29 | *
|
---|
| 30 | */
|
---|
| 31 | public abstract class AbstractReader {
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * The dataset to add parsed objects to.
|
---|
| 35 | */
|
---|
| 36 | protected DataSet ds = new DataSet();
|
---|
| 37 |
|
---|
| 38 | protected Changeset uploadChangeset;
|
---|
| 39 |
|
---|
| 40 | /** the map from external ids to read OsmPrimitives. External ids are
|
---|
| 41 | * longs too, but in contrast to internal ids negative values are used
|
---|
| 42 | * to identify primitives unknown to the OSM server
|
---|
| 43 | */
|
---|
| 44 | protected final Map<PrimitiveId, OsmPrimitive> externalIdMap = new HashMap<>();
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * Data structure for the remaining way objects
|
---|
| 48 | */
|
---|
| 49 | protected final Map<Long, Collection<Long>> ways = new HashMap<>();
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * Data structure for relation objects
|
---|
| 53 | */
|
---|
| 54 | protected final Map<Long, Collection<RelationMemberData>> relations = new HashMap<>();
|
---|
[8291] | 55 |
|
---|
[7937] | 56 | /**
|
---|
| 57 | * Replies the parsed data set
|
---|
| 58 | *
|
---|
| 59 | * @return the parsed data set
|
---|
| 60 | */
|
---|
| 61 | public DataSet getDataSet() {
|
---|
| 62 | return ds;
|
---|
| 63 | }
|
---|
[8291] | 64 |
|
---|
[7937] | 65 | /**
|
---|
| 66 | * Processes the parsed nodes after parsing. Just adds them to
|
---|
| 67 | * the dataset
|
---|
| 68 | *
|
---|
| 69 | */
|
---|
| 70 | protected void processNodesAfterParsing() {
|
---|
| 71 | for (OsmPrimitive primitive: externalIdMap.values()) {
|
---|
| 72 | if (primitive instanceof Node) {
|
---|
| 73 | this.ds.addPrimitive(primitive);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * Processes the ways after parsing. Rebuilds the list of nodes of each way and
|
---|
| 80 | * adds the way to the dataset
|
---|
| 81 | *
|
---|
[8291] | 82 | * @throws IllegalDataException if a data integrity problem is detected
|
---|
[7937] | 83 | */
|
---|
| 84 | protected void processWaysAfterParsing() throws IllegalDataException{
|
---|
[8332] | 85 | for (Entry<Long, Collection<Long>> entry : ways.entrySet()) {
|
---|
| 86 | Long externalWayId = entry.getKey();
|
---|
[7937] | 87 | Way w = (Way)externalIdMap.get(new SimplePrimitiveId(externalWayId, OsmPrimitiveType.WAY));
|
---|
| 88 | List<Node> wayNodes = new ArrayList<>();
|
---|
[8332] | 89 | for (long id : entry.getValue()) {
|
---|
[7937] | 90 | Node n = (Node)externalIdMap.get(new SimplePrimitiveId(id, OsmPrimitiveType.NODE));
|
---|
| 91 | if (n == null) {
|
---|
| 92 | if (id <= 0)
|
---|
[8419] | 93 | throw new IllegalDataException(
|
---|
[7937] | 94 | tr("Way with external ID ''{0}'' includes missing node with external ID ''{1}''.",
|
---|
| 95 | externalWayId,
|
---|
| 96 | id));
|
---|
| 97 | // create an incomplete node if necessary
|
---|
| 98 | //
|
---|
| 99 | n = (Node)ds.getPrimitiveById(id,OsmPrimitiveType.NODE);
|
---|
| 100 | if (n == null) {
|
---|
| 101 | n = new Node(id);
|
---|
| 102 | ds.addPrimitive(n);
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | if (n.isDeleted()) {
|
---|
| 106 | Main.info(tr("Deleted node {0} is part of way {1}", id, w.getId()));
|
---|
| 107 | } else {
|
---|
| 108 | wayNodes.add(n);
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | w.setNodes(wayNodes);
|
---|
| 112 | if (w.hasIncompleteNodes()) {
|
---|
| 113 | Main.info(tr("Way {0} with {1} nodes has incomplete nodes because at least one node was missing in the loaded data.",
|
---|
| 114 | externalWayId, w.getNodesCount()));
|
---|
| 115 | }
|
---|
| 116 | ds.addPrimitive(w);
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /**
|
---|
| 121 | * Completes the parsed relations with its members.
|
---|
| 122 | *
|
---|
[8291] | 123 | * @throws IllegalDataException if a data integrity problem is detected, i.e. if a
|
---|
[7937] | 124 | * relation member refers to a local primitive which wasn't available in the data
|
---|
| 125 | */
|
---|
| 126 | protected void processRelationsAfterParsing() throws IllegalDataException {
|
---|
| 127 |
|
---|
| 128 | // First add all relations to make sure that when relation reference other relation, the referenced will be already in dataset
|
---|
| 129 | for (Long externalRelationId : relations.keySet()) {
|
---|
| 130 | Relation relation = (Relation) externalIdMap.get(
|
---|
| 131 | new SimplePrimitiveId(externalRelationId, OsmPrimitiveType.RELATION)
|
---|
| 132 | );
|
---|
| 133 | ds.addPrimitive(relation);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[8332] | 136 | for (Entry<Long, Collection<RelationMemberData>> entry : relations.entrySet()) {
|
---|
| 137 | Long externalRelationId = entry.getKey();
|
---|
[7937] | 138 | Relation relation = (Relation) externalIdMap.get(
|
---|
| 139 | new SimplePrimitiveId(externalRelationId, OsmPrimitiveType.RELATION)
|
---|
| 140 | );
|
---|
| 141 | List<RelationMember> relationMembers = new ArrayList<>();
|
---|
[8332] | 142 | for (RelationMemberData rm : entry.getValue()) {
|
---|
[7937] | 143 | OsmPrimitive primitive = null;
|
---|
| 144 |
|
---|
| 145 | // lookup the member from the map of already created primitives
|
---|
| 146 | primitive = externalIdMap.get(new SimplePrimitiveId(rm.getMemberId(), rm.getMemberType()));
|
---|
| 147 |
|
---|
| 148 | if (primitive == null) {
|
---|
| 149 | if (rm.getMemberId() <= 0)
|
---|
| 150 | // relation member refers to a primitive with a negative id which was not
|
---|
| 151 | // found in the data. This is always a data integrity problem and we abort
|
---|
| 152 | // with an exception
|
---|
| 153 | //
|
---|
| 154 | throw new IllegalDataException(
|
---|
| 155 | tr("Relation with external id ''{0}'' refers to a missing primitive with external id ''{1}''.",
|
---|
| 156 | externalRelationId,
|
---|
| 157 | rm.getMemberId()));
|
---|
| 158 |
|
---|
| 159 | // member refers to OSM primitive which was not present in the parsed data
|
---|
| 160 | // -> create a new incomplete primitive and add it to the dataset
|
---|
| 161 | //
|
---|
| 162 | primitive = ds.getPrimitiveById(rm.getMemberId(), rm.getMemberType());
|
---|
| 163 | if (primitive == null) {
|
---|
| 164 | switch (rm.getMemberType()) {
|
---|
| 165 | case NODE:
|
---|
| 166 | primitive = new Node(rm.getMemberId()); break;
|
---|
| 167 | case WAY:
|
---|
| 168 | primitive = new Way(rm.getMemberId()); break;
|
---|
| 169 | case RELATION:
|
---|
| 170 | primitive = new Relation(rm.getMemberId()); break;
|
---|
| 171 | default: throw new AssertionError(); // can't happen
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | ds.addPrimitive(primitive);
|
---|
| 175 | externalIdMap.put(new SimplePrimitiveId(rm.getMemberId(), rm.getMemberType()), primitive);
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | if (primitive.isDeleted()) {
|
---|
| 179 | Main.info(tr("Deleted member {0} is used by relation {1}", primitive.getId(), relation.getId()));
|
---|
| 180 | } else {
|
---|
| 181 | relationMembers.add(new RelationMember(rm.getRole(), primitive));
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | relation.setMembers(relationMembers);
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | protected void processChangesetAfterParsing() {
|
---|
| 189 | if (uploadChangeset != null) {
|
---|
| 190 | for (Map.Entry<String, String> e : uploadChangeset.getKeys().entrySet()) {
|
---|
| 191 | ds.addChangeSetTag(e.getKey(), e.getValue());
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
[8291] | 195 |
|
---|
[7937] | 196 | protected final void prepareDataSet() throws IllegalDataException {
|
---|
| 197 | try {
|
---|
| 198 | ds.beginUpdate();
|
---|
| 199 | processNodesAfterParsing();
|
---|
| 200 | processWaysAfterParsing();
|
---|
| 201 | processRelationsAfterParsing();
|
---|
| 202 | processChangesetAfterParsing();
|
---|
| 203 | } finally {
|
---|
| 204 | ds.endUpdate();
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | }
|
---|