- Timestamp:
- 2006-02-09T23:46:27+01:00 (19 years ago)
- Location:
- src/org/openstreetmap/josm
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/actions/SaveAction.java
r40 r49 54 54 FileWriter fileWriter; 55 55 if (fn.endsWith(".gpx")) 56 new GpxWriter(fileWriter = new FileWriter(file)).output(); 56 new GpxWriter(fileWriter = new FileWriter(file), Main.main.ds).output(); 57 57 else if (fn.endsWith(".xml") || fn.endsWith(".osm")) 58 58 new OsmWriter(fileWriter = new FileWriter(file), Main.main.ds).output(); -
src/org/openstreetmap/josm/io/GpxReader.java
r47 r49 2 2 3 3 import static org.openstreetmap.josm.io.GpxWriter.GPX; 4 import static org.openstreetmap.josm.io.GpxWriter.JOSM; 4 5 import static org.openstreetmap.josm.io.GpxWriter.OSM; 5 import static org.openstreetmap.josm.io.GpxWriter.JOSM;6 6 7 7 import java.io.IOException; … … 33 33 */ 34 34 public Reader source; 35 /** 36 * Mapper to find new created objects that occoure more than once. 37 */ 38 private HashMap<Long, OsmPrimitive> newCreatedPrimitives = new HashMap<Long, OsmPrimitive>(); 39 /** 40 * Either set to true or false, depending whether the JOSM namespace declaration 41 * was found. 42 */ 43 private boolean mergeNodes = false; 35 44 36 45 /** … … 49 58 final SAXBuilder builder = new SAXBuilder(); 50 59 Element root = builder.build(source).getRootElement(); 60 mergeNodes = !root.getAdditionalNamespaces().contains(JOSM); 51 61 return parseDataSet(root); 52 62 } catch (NullPointerException npe) { … … 78 88 parseKeyValueTag(data, child); 79 89 } 90 data = (Node)getNewIfSeenBefore(data); 80 91 return data; 81 92 } … … 89 100 DataSet data = new DataSet(); 90 101 // read waypoints not contained in tracks or areas 91 for (Object o : e.getChildren("wpt", GPX)) 92 addNode(data, parseWaypoint((Element)o)); 102 for (Object o : e.getChildren("wpt", GPX)) { 103 Node node = parseWaypoint((Element)o); 104 addNode(data, node); 105 } 93 106 94 107 // read tracks (and line segments) 95 108 for (Object trackElement : e.getChildren("trk", GPX)) 96 109 parseTrack((Element)trackElement, data); 97 110 111 // reset new created ids to zero 112 for (OsmPrimitive osm : data.allPrimitives()) 113 if (osm.id < 0) 114 osm.id = 0; 115 98 116 return data; 99 117 } … … 116 134 Node start = null; 117 135 for (Object w : child.getChildren("trkpt", GPX)) { 118 Node node = parseWaypoint((Element)w); 119 node = addNode(ds, node); 136 Node node = addNode(ds, parseWaypoint((Element)w)); 120 137 if (start == null) 121 138 start = node; … … 123 140 LineSegment lineSegment = new LineSegment(start, node); 124 141 parseKeyValueExtensions(lineSegment, child.getChild("extensions", GPX)); 142 lineSegment = (LineSegment)getNewIfSeenBefore(lineSegment); 125 143 track.segments.add(lineSegment); 126 144 start = node; … … 136 154 parseKeyValueTag(track, child); 137 155 } 156 track = (Track)getNewIfSeenBefore(track); 138 157 ds.lineSegments.addAll(track.segments); 139 158 if (!realLineSegment) 140 159 ds.tracks.add(track); 141 160 } 142 143 161 144 162 /** 145 163 * Adds the node to allNodes if it is not already listed. Does respect the 146 * preferencesetting "mergeNodes". Return the node in the list that correspond164 * setting "mergeNodes". Return the node in the list that correspond 147 165 * to the node in the list (either the new added or the old found). 148 *149 * If reading raw gps data, mergeNodes are always on (To save memory. You150 * can't edit raw gps nodes anyway.)151 166 * 152 167 * @param data The DataSet to add the node to. … … 155 170 */ 156 171 private Node addNode(DataSet data, Node node) { 157 for (Node n : data.nodes) 158 if (node.coor.equalsLatLon(n.coor)) 159 return n; 172 if (mergeNodes) 173 for (Node n : data.nodes) 174 if (node.coor.equalsLatLon(n.coor)) 175 return n; 160 176 data.nodes.add(node); 161 177 return node; 178 } 179 180 181 /** 182 * @return Either the parameter or an index from the newCreatedPrimitives map 183 * with the id seen before. 184 */ 185 private OsmPrimitive getNewIfSeenBefore(OsmPrimitive osm) { 186 if (newCreatedPrimitives.containsKey(osm.id)) 187 return newCreatedPrimitives.get(osm.id); 188 return osm; 162 189 } 163 190 … … 189 216 if (idElement != null) 190 217 osm.id = Long.parseLong(idElement.getText()); 218 if (osm.id < 0 && !newCreatedPrimitives.containsKey(osm.id)) 219 newCreatedPrimitives.put(osm.id, osm); 191 220 osm.modified = e.getChild("modified", JOSM) != null; 192 221 osm.setDeleted(e.getChild("deleted", JOSM) != null); -
src/org/openstreetmap/josm/io/GpxWriter.java
r44 r49 3 3 import java.io.IOException; 4 4 import java.io.Writer; 5 import java.util.Collection; 5 6 import java.util.HashMap; 6 7 import java.util.LinkedList; … … 14 15 import org.jdom.output.Format; 15 16 import org.jdom.output.XMLOutputter; 16 import org.openstreetmap.josm. Main;17 import org.openstreetmap.josm.data.osm.DataSet; 17 18 import org.openstreetmap.josm.data.osm.Key; 18 19 import org.openstreetmap.josm.data.osm.LineSegment; … … 50 51 */ 51 52 private Writer out; 53 /** 54 * The dataset beeing processed. 55 */ 56 private DataSet ds; 57 /** 58 * Map all new primitives to the element which hold them. After inserting everything, 59 * the writer sets ids to every element that was in the list and had more than one 60 * element using it. 61 */ 62 private HashMap<OsmPrimitive, Collection<Element>> usedNewPrimitives = new HashMap<OsmPrimitive, Collection<Element>>(); 63 /** 64 * The counter for new created objects used more than once. 65 * Starting at -1 and goes down. 66 */ 67 private long newIdCounter = -1; 52 68 53 69 /** … … 57 73 * @param out The Writer to store the result data in. 58 74 */ 59 public GpxWriter(Writer out) { 75 public GpxWriter(Writer out, DataSet ds) { 60 76 this.out = out; 77 this.ds = ds; 61 78 } 62 79 … … 86 103 e.setAttribute("creator", "JOSM"); 87 104 // for getting all unreferenced waypoints in the wpt-list 88 LinkedList<Node> unrefNodes = new LinkedList<Node>( Main.main.ds.nodes);105 LinkedList<Node> unrefNodes = new LinkedList<Node>(ds.nodes); 89 106 // for getting all unreferenced line segments 90 LinkedList<LineSegment> unrefLs = new LinkedList<LineSegment>( Main.main.ds.lineSegments);107 LinkedList<LineSegment> unrefLs = new LinkedList<LineSegment>(ds.lineSegments); 91 108 92 109 // tracks 93 for (Track t : Main.main.ds.tracks) { 110 for (Track t : ds.tracks) { 111 if (t.isDeleted() && t.id == 0) 112 continue; 94 113 Element tElem = new Element("trk", GPX); 95 114 HashMap<Key, String> keys = null; … … 119 138 // encode pending line segments as tracks 120 139 for (LineSegment ls : unrefLs) { 140 if (ls.isDeleted() && ls.id == 0) 141 continue; 121 142 Element t = new Element("trk", GPX); 122 143 t.getChildren().add(parseLineSegment(ls)); … … 130 151 131 152 // waypoints (missing nodes) 132 for (Node n : unrefNodes) 153 for (Node n : unrefNodes) { 154 if (n.isDeleted() && n.id == 0) 155 continue; 133 156 e.getChildren().add(parseWaypoint(n, "wpt")); 157 } 158 159 // link all ids used more than once 160 for (Entry<OsmPrimitive, Collection<Element>> entry : usedNewPrimitives.entrySet()) { 161 if (entry.getValue().size() > 1) { 162 long id = newIdCounter--; 163 for (Element element : entry.getValue()) { 164 Element ext = element.getChild("extensions", GPX); 165 if (ext == null) 166 element.getChildren().add(ext = new Element("extensions", GPX)); 167 ext.getChildren().add(new Element("uid", JOSM).setText(""+id)); 168 } 169 } 170 } 134 171 135 172 return e; … … 242 279 @SuppressWarnings("unchecked") 243 280 private void addPropertyExtensions(Element e, Map<Key, String> keys, OsmPrimitive osm) { 244 if ((keys == null || keys.isEmpty()) && osm.id == 0 && !osm.modified && !osm.isDeleted() && !osm.modifiedProperties) 245 return; 246 Element extensions = e.getChild("extensions", GPX); 247 if (extensions == null) 248 e.getChildren().add(extensions = new Element("extensions", GPX)); 281 LinkedList<Element> extensions = new LinkedList<Element>(); 249 282 if (keys != null && !keys.isEmpty()) { 250 283 for (Entry<Key, String> prop : keys.entrySet()) { … … 252 285 propElement.setAttribute("key", prop.getKey().name); 253 286 propElement.setAttribute("value", prop.getValue()); 254 extensions. getChildren().add(propElement);287 extensions.add(propElement); 255 288 } 256 289 } 290 257 291 if (osm.id != 0) { 258 292 Element propElement = new Element("uid", JOSM); 259 293 propElement.setText(""+osm.id); 260 extensions.getChildren().add(propElement); 294 extensions.add(propElement); 295 } else { 296 Collection<Element> l = usedNewPrimitives.get(osm); 297 if (l == null) 298 l = new LinkedList<Element>(); 299 l.add(e); 300 usedNewPrimitives.put(osm, l); 261 301 } 262 302 if (osm.modified) { 263 303 Element modElement = new Element("modified", JOSM); 264 extensions. getChildren().add(modElement);304 extensions.add(modElement); 265 305 } 266 306 if (osm.isDeleted()) { 267 307 Element modElement = new Element("deleted", JOSM); 268 extensions. getChildren().add(modElement);308 extensions.add(modElement); 269 309 } 270 310 if (osm.modifiedProperties) { 271 311 Element modElement = new Element("modifiedProperties", JOSM); 272 extensions.getChildren().add(modElement); 273 } 312 extensions.add(modElement); 313 } 314 315 if (extensions.isEmpty()) 316 return; 317 318 Element ext = e.getChild("extensions", GPX); 319 if (ext == null) 320 e.getChildren().add(ext = new Element("extensions", GPX)); 321 ext.getChildren().addAll(extensions); 274 322 } 275 323 } -
src/org/openstreetmap/josm/io/OsmWriter.java
r44 r49 4 4 import java.io.Writer; 5 5 import java.util.Collection; 6 import java.util.HashMap; 6 7 import java.util.LinkedList; 7 8 import java.util.List; … … 39 40 */ 40 41 private long newIdCounter = -1; 42 /** 43 * All newly created ids and their primitive that uses it. This is a back reference 44 * map to allow references to use the correnct primitives. 45 */ 46 private HashMap<OsmPrimitive, Long> usedNewIds = new HashMap<OsmPrimitive, Long>(); 41 47 42 48 /** … … 72 78 Collection<Element> allDeleted = deleted.getChildren(); 73 79 for (OsmPrimitive osm : ds.allPrimitives()) { 74 if (osm.isDeleted()) { 80 if (osm.isDeleted() && osm.id != 0) { 75 81 osm.visit(this); 76 82 allDeleted.add(element); … … 103 109 private void addProperties(Element e, OsmPrimitive osm) { 104 110 long id = osm.id; 105 if (id == 0) 111 if (id == 0) { 106 112 id = newIdCounter--; 113 usedNewIds.put(osm, id); 114 } 107 115 e.setAttribute("uid", ""+id); 108 116 if (osm.keys != null) … … 127 135 element = new Element("segment"); 128 136 addProperties(element, ls); 129 element.setAttribute("from", ""+ls.start.id); 130 element.setAttribute("to", ""+ls.end.id); 137 element.setAttribute("from", ""+getUsedId(ls.start)); 138 element.setAttribute("to", ""+getUsedId(ls.end)); 139 } 140 141 /** 142 * Return the id for the given osm primitive (may access the usedId map) 143 */ 144 private Long getUsedId(OsmPrimitive osm) { 145 return osm.id == 0 ? usedNewIds.get(osm) : osm.id; 131 146 } 132 147 … … 139 154 addProperties(element, t); 140 155 for (LineSegment ls : t.segments) 141 element.getChildren().add(new Element("segment").setAttribute("uid", ""+ ls.id));156 element.getChildren().add(new Element("segment").setAttribute("uid", ""+getUsedId(ls))); 142 157 } 143 158 … … 147 162 } 148 163 } 149
Note:
See TracChangeset
for help on using the changeset viewer.