Changeset 329 in josm for branch/0.5/src/org/openstreetmap/josm/io
- Timestamp:
- 2007-09-24T01:36:24+02:00 (18 years ago)
- Location:
- branch/0.5/src/org/openstreetmap/josm/io
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branch/0.5/src/org/openstreetmap/josm/io/BoundingBoxDownloader.java
r298 r329 103 103 Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data...")); 104 104 final DataSet data = OsmReader.parseDataSet(in, null, Main.pleaseWaitDlg); 105 String origin = Main.pref.get("osm-server.url")+"/"+Main.pref.get("osm-server.version", "0. 4");105 String origin = Main.pref.get("osm-server.url")+"/"+Main.pref.get("osm-server.version", "0.5"); 106 106 Bounds bounds = new Bounds(new LatLon(lat1, lon1), new LatLon(lat2, lon2)); 107 107 DataSource src = new DataSource(bounds, origin); -
branch/0.5/src/org/openstreetmap/josm/io/GpxWriter.java
r298 r329 11 11 import org.openstreetmap.josm.data.osm.Node; 12 12 import org.openstreetmap.josm.data.osm.OsmPrimitive; 13 import org.openstreetmap.josm.data.osm.Segment;14 13 import org.openstreetmap.josm.data.osm.Way; 15 14 import org.openstreetmap.josm.gui.layer.RawGpsLayer.GpsPoint; … … 32 31 33 32 /** 34 * Export the dataset to gpx. Only the physical segment structure is 35 * exported. To do this, the list of ways is processed. If a way span a 36 * sequence of segments, this is added as one trkseg. 37 * Then, all remaining segments are added in one extra trk. Finally, 38 * all remaining nodes are added as wpt. 33 * Export the dataset to gpx. The ways are converted to trksegs, each in 34 * a seperate trk. Finally, all remaining nodes are added as wpt. 39 35 */ 40 36 public static final class All implements XmlWriter.OsmWriterInterface { … … 106 102 continue; 107 103 out.println(" <trk>"); 108 Segment oldLs = null;109 for (Segment ls : w.segments) {110 if (ls.incomplete)111 continue;112 // end old segemnt, if no longer match a chain113 if (oldLs != null && !oldLs.to.coor.equals(ls.from.coor)) {114 out.println(" </trkseg>");115 writer.outputNode(oldLs.to, false);116 all.remove(oldLs.to);117 oldLs = null;118 }119 // start new segment if necessary120 if (oldLs == null)121 104 out.println(" <trkseg>"); 122 writer.outputNode(ls.from, false); 123 all.remove(ls.from); 124 oldLs = ls; 125 all.remove(ls); 126 } 127 // write last node if there 128 if (oldLs != null) { 129 writer.outputNode(oldLs.to, false); 130 all.remove(oldLs.to); 105 for (Node n : w.nodes) { 106 writer.outputNode(n, false); 107 all.remove(n); 108 } 131 109 out.println(" </trkseg>"); 132 }133 110 out.println(" </trk>"); 134 111 all.remove(w); 135 }136 137 // add remaining segments138 Collection<Segment> segments = new LinkedList<Segment>();139 for (OsmPrimitive osm : all)140 if (osm instanceof Segment && !((Segment)osm).incomplete)141 segments.add((Segment)osm);142 if (!segments.isEmpty()) {143 out.println(" <trk>");144 for (Segment ls : segments) {145 out.println(" <trkseg>");146 writer.outputNode(ls.from, false);147 all.remove(ls.from);148 writer.outputNode(ls.to, false);149 all.remove(ls.to);150 out.println(" </trkseg>");151 all.remove(ls);152 }153 out.println(" </trk>");154 112 } 155 113 -
branch/0.5/src/org/openstreetmap/josm/io/OsmIdReader.java
r319 r329 31 31 32 32 @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 33 if (qName.equals("node") || qName.equals(" segment") || qName.equals("way")) {33 if (qName.equals("node") || qName.equals("way")) { 34 34 try { 35 35 entries.put(Long.valueOf(atts.getValue("id")), qName); -
branch/0.5/src/org/openstreetmap/josm/io/OsmReader.java
r319 r329 24 24 import org.openstreetmap.josm.data.osm.DataSet; 25 25 import org.openstreetmap.josm.data.osm.DataSource; 26 import org.openstreetmap.josm.data.osm.Relation; 27 import org.openstreetmap.josm.data.osm.RelationMember; 26 28 import org.openstreetmap.josm.data.osm.Node; 27 29 import org.openstreetmap.josm.data.osm.OsmPrimitive; 28 import org.openstreetmap.josm.data.osm.Segment;29 30 import org.openstreetmap.josm.data.osm.User; 30 31 import org.openstreetmap.josm.data.osm.Way; … … 44 45 * all nodes are read and stored. Other information than nodes are stored in a raw list 45 46 * 46 * The second phase reads from the raw list all segments and create Segment objects. 47 * 48 * The third phase read all ways out of the remaining objects in the raw list. 47 * The second phase read all ways out of the remaining objects in the raw list. 49 48 * 50 49 * @author Imi … … 91 90 92 91 /** 93 * Data structure for the remaining segment objects 94 * Maps the raw attributes to key/value pairs. 95 */ 96 private Map<OsmPrimitiveData, long[]> segs = new HashMap<OsmPrimitiveData, long[]>(); 92 * Used as a temporary storage for relation members, before they 93 * are resolved into pointers to real objects. 94 */ 95 private static class RelationMemberData { 96 public String type; 97 public long id; 98 public RelationMember relationMember; 99 } 97 100 98 101 /** … … 100 103 */ 101 104 private Map<OsmPrimitiveData, Collection<Long>> ways = new HashMap<OsmPrimitiveData, Collection<Long>>(); 105 106 /** 107 * Data structure for relation objects 108 */ 109 private Map<OsmPrimitiveData, Collection<RelationMemberData>> relations = new HashMap<OsmPrimitiveData, Collection<RelationMemberData>>(); 102 110 103 111 /** … … 132 140 ds.dataSources.add(src); 133 141 } 142 143 // ---- PARSING NODES AND WAYS ---- 144 134 145 } else if (qName.equals("node")) { 135 146 current = new Node(new LatLon(getDouble(atts, "lat"), getDouble(atts, "lon"))); 136 147 readCommon(atts, current); 137 148 nodes.put(current.id, (Node)current); 138 } else if (qName.equals("segment")) {139 current = new OsmPrimitiveData();140 readCommon(atts, current);141 segs.put((OsmPrimitiveData)current, new long[]{getLong(atts, "from"), getLong(atts, "to")});142 149 } else if (qName.equals("way")) { 143 150 current = new OsmPrimitiveData(); 144 151 readCommon(atts, current); 145 152 ways.put((OsmPrimitiveData)current, new LinkedList<Long>()); 146 } else if (qName.equals(" seg")) {153 } else if (qName.equals("nd")) { 147 154 Collection<Long> list = ways.get(current); 148 155 if (list == null) 149 throw new SAXException(tr("Found < seg> tag on non-way."));150 long id = getLong(atts, " id");156 throw new SAXException(tr("Found <nd> element in non-way.")); 157 long id = getLong(atts, "ref"); 151 158 if (id == 0) 152 throw new SAXException(tr(" Incomplete segment with id=0"));159 throw new SAXException(tr("<nd> has zero ref")); 153 160 list.add(id); 154 } else if (qName.equals("tag")) 161 162 // ---- PARSING RELATIONS ---- 163 164 } else if (qName.equals("relation")) { 165 current = new OsmPrimitiveData(); 166 readCommon(atts, current); 167 relations.put((OsmPrimitiveData)current, new LinkedList<RelationMemberData>()); 168 } else if (qName.equals("member")) { 169 Collection<RelationMemberData> list = relations.get(current); 170 if (list == null) 171 throw new SAXException(tr("Found <member> tag on non-relation.")); 172 RelationMemberData emd = new RelationMemberData(); 173 emd.relationMember = new RelationMember(); 174 emd.id = getLong(atts, "ref"); 175 emd.type=atts.getValue("type"); 176 emd.relationMember.role = atts.getValue("role"); 177 178 if (emd.id == 0) 179 throw new SAXException(tr("Incomplete <member> specification with ref=0")); 180 181 list.add(emd); 182 183 // ---- PARSING TAGS (applicable to all objects) ---- 184 185 } else if (qName.equals("tag")) { 155 186 current.put(atts.getValue("k"), atts.getValue("v")); 187 } 156 188 } catch (NumberFormatException x) { 157 189 x.printStackTrace(); // SAXException does not chain correctly … … 173 205 public OsmReader() { 174 206 // first add the main server version 175 allowedVersions.add(Main.pref.get("osm-server.version", "0. 4"));207 allowedVersions.add(Main.pref.get("osm-server.version", "0.5")); 176 208 // now also add all compatible versions 177 209 String[] additionalVersions = 178 Main.pref.get("osm-server.additional-versions", "0.3").split("/,/"); 210 Main.pref.get("osm-server.additional-versions", "").split("/,/"); 211 if (additionalVersions.length == 1 && additionalVersions[0].length() == 0) 212 additionalVersions = new String[] {}; 179 213 allowedVersions.addAll(Arrays.asList(additionalVersions)); 180 214 } … … 224 258 throw new SAXException(tr("Missing required attribute \"{0}\".",value)); 225 259 return Long.parseLong(s); 226 }227 228 private void createSegments() {229 for (Entry<OsmPrimitiveData, long[]> e : segs.entrySet()) {230 Node from = findNode(e.getValue()[0]);231 Node to = findNode(e.getValue()[1]);232 if (from == null || to == null)233 continue; //TODO: implement support for incomplete nodes.234 Segment s = new Segment(from, to);235 e.getKey().copyTo(s);236 segments.put(s.id, s);237 adder.visit(s);238 }239 260 } 240 261 … … 253 274 } 254 275 255 private Segment findSegment(long id) {256 Segment s = segments.get(id);257 if (s != null)258 return s;259 for (Segment seg : references.segments)260 if (seg.id == id)261 return seg;262 // TODO: This has to be changed to support multiple layers.263 for (Segment seg : Main.ds.segments)264 if (seg.id == id)265 return new Segment(seg);266 return null;267 }268 269 276 private void createWays() { 270 277 for (Entry<OsmPrimitiveData, Collection<Long>> e : ways.entrySet()) { 271 278 Way w = new Way(); 279 boolean failed = false; 272 280 for (long id : e.getValue()) { 273 Segment s= findSegment(id);274 if ( s== null) {275 s = new Segment(id); // incomplete line segment276 adder.visit(s);281 Node n = findNode(id); 282 if (n == null) { 283 failed = true; 284 break; 277 285 } 278 w. segments.add(s);286 w.nodes.add(n); 279 287 } 288 if (failed) continue; 280 289 e.getKey().copyTo(w); 281 290 adder.visit(w); … … 284 293 285 294 /** 286 * All read segments after phase 2. 287 */ 288 private Map<Long, Segment> segments = new HashMap<Long, Segment>(); 295 * Return the Way object with the given id, or null if it doesn't 296 * exist yet. This method only looks at ways stored in the data set. 297 * 298 * @param id 299 * @return way object or null 300 */ 301 private Way findWay(long id) { 302 for (Way wy : ds.ways) 303 if (wy.id == id) 304 return wy; 305 for (Way wy : Main.ds.ways) 306 if (wy.id == id) 307 return wy; 308 return null; 309 } 310 311 /** 312 * Return the Relation object with the given id, or null if it doesn't 313 * exist yet. This method only looks at relations stored in the data set. 314 * 315 * @param id 316 * @return relation object or null 317 */ 318 private Relation findRelation(long id) { 319 for (Relation e : ds.relations) 320 if (e.id == id) 321 return e; 322 for (Relation e : Main.ds.relations) 323 if (e.id == id) 324 return e; 325 return null; 326 } 327 328 /** 329 * Create relations. This is slightly different than n/s/w because 330 * unlike other objects, relations may reference other relations; it 331 * is not guaranteed that a referenced relation will have been created 332 * before it is referenced. So we have to create all relations first, 333 * and populate them later. 334 */ 335 private void createRelations() { 336 337 // pass 1 - create all relations 338 for (Entry<OsmPrimitiveData, Collection<RelationMemberData>> e : relations.entrySet()) { 339 Relation en = new Relation(); 340 e.getKey().copyTo(en); 341 adder.visit(en); 342 } 343 344 // pass 2 - sort out members 345 for (Entry<OsmPrimitiveData, Collection<RelationMemberData>> e : relations.entrySet()) { 346 Relation en = findRelation(e.getKey().id); 347 if (en == null) throw new Error("Failed to create relation " + e.getKey().id); 348 349 for (RelationMemberData emd : e.getValue()) { 350 RelationMember em = emd.relationMember; 351 if (emd.type.equals("node")) { 352 em.member = findNode(emd.id); 353 if (em.member == null) { 354 em.member = new Node(emd.id); 355 adder.visit((Node)em.member); 356 } 357 } else if (emd.type.equals("way")) { 358 em.member = findWay(emd.id); 359 if (em.member == null) { 360 em.member = new Way(emd.id); 361 adder.visit((Way)em.member); 362 } 363 } else if (emd.type.equals("relation")) { 364 em.member = findRelation(emd.id); 365 if (em.member == null) { 366 em.member = new Relation(emd.id); 367 adder.visit((Relation)em.member); 368 } 369 } else { 370 // this is an error. 371 } 372 en.members.add(em); 373 } 374 } 375 } 289 376 290 377 /** … … 298 385 osm.references = ref == null ? new DataSet() : ref; 299 386 300 // phase 1: Parse nodes and read in raw segments andways387 // phase 1: Parse nodes and read in raw ways 301 388 InputSource inputSource = new InputSource(new InputStreamReader(source, "UTF-8")); 302 389 try { … … 314 401 315 402 try { 316 osm.createSegments();317 403 osm.createWays(); 404 osm.createRelations(); 318 405 } catch (NumberFormatException e) { 319 406 e.printStackTrace(); -
branch/0.5/src/org/openstreetmap/josm/io/OsmServerReader.java
r298 r329 30 30 */ 31 31 protected InputStream getInputStream(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws IOException { 32 String version = Main.pref.get("osm-server.version", "0. 4");32 String version = Main.pref.get("osm-server.version", "0.5"); 33 33 urlStr = Main.pref.get("osm-server.url")+"/"+version+"/" + urlStr; 34 34 System.out.println("download: "+urlStr); -
branch/0.5/src/org/openstreetmap/josm/io/OsmServerWriter.java
r298 r329 17 17 18 18 import org.openstreetmap.josm.Main; 19 import org.openstreetmap.josm.data.osm.Relation; 19 20 import org.openstreetmap.josm.data.osm.Node; 20 21 import org.openstreetmap.josm.data.osm.OsmPrimitive; 21 import org.openstreetmap.josm.data.osm.Segment;22 22 import org.openstreetmap.josm.data.osm.Way; 23 23 import org.openstreetmap.josm.data.osm.visitor.NameVisitor; … … 96 96 97 97 /** 98 * Upload a segment (without the nodes). 99 */ 100 public void visit(Segment ls) { 101 if (ls.id == 0 && !ls.deleted && ls.get("created_by") == null) { 102 ls.put("created_by", "JOSM"); 103 sendRequest("PUT", "segment", ls, true); 104 } else if (ls.deleted) { 105 sendRequest("DELETE", "segment", ls, false); 106 } else { 107 sendRequest("PUT", "segment", ls, true); 108 } 109 processed.add(ls); 110 } 111 112 /** 113 * Upload a whole way with the complete segment id list. 98 * Upload a whole way with the complete node id list. 114 99 */ 115 100 public void visit(Way w) { … … 125 110 } 126 111 112 /** 113 * Upload an relation with all members. 114 */ 115 public void visit(Relation e) { 116 if (e.id == 0 && !e.deleted && e.get("created_by") == null) { 117 e.put("created_by", "JOSM"); 118 sendRequest("PUT", "relation", e, true); 119 } else if (e.deleted) { 120 sendRequest("DELETE", "relation", e, false); 121 } else { 122 sendRequest("PUT", "relation", e, true); 123 } 124 processed.add(e); 125 } 127 126 /** 128 127 * Read a long from the input stream and return it. … … 154 153 OsmPrimitive osm, boolean addBody) { 155 154 try { 156 String version = Main.pref.get("osm-server.version", "0. 4");155 String version = Main.pref.get("osm-server.version", "0.5"); 157 156 URL url = new URL( 158 157 Main.pref.get("osm-server.url") + 159 158 "/" + version + 160 159 "/" + urlSuffix + 161 "/" + ( (version.equals("0.4") &&osm.id==0)? "create":osm.id));160 "/" + (osm.id==0 ? "create" : osm.id)); 162 161 System.out.println("upload to: "+url); 163 162 activeConnection = (HttpURLConnection)url.openConnection(); -
branch/0.5/src/org/openstreetmap/josm/io/OsmWriter.java
r298 r329 9 9 import org.openstreetmap.josm.data.osm.DataSet; 10 10 import org.openstreetmap.josm.data.osm.DataSource; 11 import org.openstreetmap.josm.data.osm.Relation; 12 import org.openstreetmap.josm.data.osm.RelationMember; 11 13 import org.openstreetmap.josm.data.osm.Node; 12 14 import org.openstreetmap.josm.data.osm.OsmPrimitive; 13 import org.openstreetmap.josm.data.osm.Segment;14 15 import org.openstreetmap.josm.data.osm.Way; 15 16 import org.openstreetmap.josm.data.osm.visitor.Visitor; … … 37 38 public void header(PrintWriter out) { 38 39 out.print("<osm version='"); 39 out.print(Main.pref.get("osm-server.version", "0. 4"));40 out.print(Main.pref.get("osm-server.version", "0.5")); 40 41 out.println("' generator='JOSM'>"); 41 42 } … … 44 45 } 45 46 } 47 48 // simple helper to write the object's class to the out stream 49 private Visitor typeWriteVisitor = new Visitor() { 50 public void visit(Node n) { out.print("node"); } 51 public void visit(Way w) { out.print("way"); } 52 public void visit(Relation e) { out.print("relation"); } 53 }; 46 54 47 55 /** … … 69 77 if (shouldWrite(n)) 70 78 writer.visit(n); 71 for (Segment ls : ds.segments)72 if (shouldWrite(ls))73 writer.visit(ls);74 79 for (Way w : ds.ways) 75 80 if (shouldWrite(w)) 76 81 writer.visit(w); 82 for (Relation e : ds.relations) 83 if (shouldWrite(e)) 84 writer.visit(e); 77 85 } 78 86 … … 123 131 } 124 132 125 public void visit(Segment ls) {126 if (ls.incomplete)127 return; // Do not write an incomplete segment128 addCommon(ls, "segment");129 out.print(" from='"+getUsedId(ls.from)+"' to='"+getUsedId(ls.to)+"'");130 addTags(ls, "segment", true);131 }132 133 133 public void visit(Way w) { 134 134 addCommon(w, "way"); 135 135 out.println(">"); 136 for ( Segment ls : w.segments)137 out.println(" < seg id='"+getUsedId(ls)+"' />");136 for (Node n : w.nodes) 137 out.println(" <nd ref='"+getUsedId(n)+"' />"); 138 138 addTags(w, "way", false); 139 139 } 140 141 public void visit(Relation e) { 142 addCommon(e, "relation"); 143 out.println(">"); 144 for (RelationMember em : e.members) { 145 out.print(" <member type='"); 146 em.member.visit(typeWriteVisitor); 147 out.println("' ref='"+getUsedId(em.member)+"' role='" + 148 XmlWriter.encode(em.role) + "' />"); 149 } 150 addTags(e, "relation", false); 151 } 152 140 153 141 154 /** -
branch/0.5/src/org/openstreetmap/josm/io/XmlWriter.java
r298 r329 67 67 } 68 68 69 70 71 69 /** 72 70 * The output writer to save the values to.
Note:
See TracChangeset
for help on using the changeset viewer.