Changeset 6484 in josm
- Timestamp:
- 2013-12-17T15:56:05+01:00 (11 years ago)
- Location:
- trunk/src/org
- Files:
-
- 8 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/GeoJSONWriter.java
r6246 r6484 4 4 import java.util.Map; 5 5 import java.util.Map.Entry; 6 7 import org.json.JSONStringer; 6 8 import org.openstreetmap.josm.data.coor.LatLon; 7 9 import org.openstreetmap.josm.data.osm.Changeset; … … 16 18 17 19 private OsmDataLayer layer; 18 private StringBuilder out;20 private JSONStringer out; 19 21 private static final boolean skipEmptyNodes = true; 20 private boolean insertComma = false;21 22 22 23 public GeoJSONWriter(OsmDataLayer layer) { … … 25 26 26 27 public String write() { 27 out = new StringBuilder(1 << 12);28 out. append("{\"type\": \"FeatureCollection\",\n");29 out. append("\"features\": [\n");28 out = new JSONStringer(); 29 out.object().key("type").value("FeatureCollection"); 30 out.key("features").array(); 30 31 for (Node n : layer.data.getNodes()) { 31 32 appendPrimitive(n); … … 34 35 appendPrimitive(w); 35 36 } 36 out. append("\n]\n}");37 out.endArray().endObject(); 37 38 return out.toString(); 38 39 } … … 40 41 @Override 41 42 public void visit(Node n) { 42 out. append("\"type\": \"Point\", \"coordinates\":");43 out.key("type").value("Point").key("coordinates"); 43 44 appendCoord(n.getCoor()); 44 45 } … … 46 47 @Override 47 48 public void visit(Way w) { 48 out.append("\"type\": \"LineString\", \"coordinates\": ["); 49 boolean insertCommaCoords = false; 49 out.key("type").value("LineString").key("coordinates").array(); 50 50 for (Node n : w.getNodes()) { 51 if (insertCommaCoords) {52 out.append(", ");53 }54 insertCommaCoords = true;55 51 appendCoord(n.getCoor()); 56 52 } 57 out. append("]");53 out.endArray(); 58 54 } 59 55 … … 76 72 return; 77 73 } 78 if (insertComma) { 79 out.append(",\n"); 74 out.object().key("type").value("Feature"); 75 Map<String, String> tags = p.getKeys(); 76 out.key("properties").object(); 77 for (Entry<String, String> t : tags.entrySet()) { 78 out.key(t.getKey()).value(t.getValue()); 80 79 } 81 insertComma = true; 82 out.append("{\"type\": \"Feature\",\n"); 83 Map<String, String> tags = p.getKeys(); 84 if (!tags.isEmpty()) { 85 out.append("\t\"properties\": {\n"); 86 boolean insertCommaTags = false; 87 for (Entry<String, String> t : tags.entrySet()) { 88 if (insertCommaTags) { 89 out.append(",\n"); 90 } 91 insertCommaTags = true; 92 out.append("\t\t\"").append(escape(t.getKey())).append("\": "); 93 out.append("\"").append(escape(t.getValue())).append("\""); 94 } 95 out.append("\n\t},\n"); 96 } else { 97 out.append("\t\"properties\": {},\n"); 98 } 99 { // append primitive specific 100 out.append("\t\"geometry\": {"); 101 p.accept(this); 102 out.append("}"); 103 } 104 out.append("}"); 80 out.endObject(); 81 // append primitive specific 82 out.key("geometry").object(); 83 p.accept(this); 84 out.endObject(); 85 out.endObject(); 105 86 } 106 87 107 88 protected void appendCoord(LatLon c) { 108 89 if (c != null) { 109 out.a ppend("[").append(c.lon()).append(", ").append(c.lat()).append("]");90 out.array().value(c.lon()).value(c.lat()).endArray(); 110 91 } 111 92 }
Note:
See TracChangeset
for help on using the changeset viewer.