Changeset 6756 in josm
- Timestamp:
- 2014-01-27T00:13:24+01:00 (11 years ago)
- Location:
- trunk
- Files:
-
- 60 added
- 1 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/CONTRIBUTION
r6531 r6756 54 54 is from Johan Montagnat and licensed with GPL. 55 55 56 The JSON code (https:// github.com/douglascrockford/JSON-java)57 is from Douglas Crockford and licensed as follows:58 "The Software shall be used for Good, not Evil." 56 The JSON code (https://java.net/projects/jsonp/) 57 is from Oracle (RI for JSR 353: Java API for JSON Processing) 58 and licensed with GPL / classpath exception. 59 59 60 60 The opening hour validation uses code from opening_hour.js … … 67 67 Some are originally LGPL but redistributed here under GPL. 68 68 69 The world image is from onearth.pl.70 71 69 The keyboard icon comes fom: 72 70 - source: http://www.iconfinder.net/index.php?q=key&page=icondetails&iconid=8553&size=128&q=key&s12=on&s16=on&s22=on&s32=on&s48=on&s64=on&s128=on -
trunk/build.xml
r6686 r6756 210 210 <compilerarg value="-Xlint:unchecked"/> 211 211 </javac> 212 <copy todir="build" failonerror="no" includeemptydirs="no"> 213 <fileset dir="resources"/> 214 </copy> 212 215 </target> 213 216 <target name="init"> -
trunk/src/org/openstreetmap/josm/io/GeoJSONWriter.java
r6485 r6756 2 2 package org.openstreetmap.josm.io; 3 3 4 import java.io.StringWriter; 5 import java.util.HashMap; 4 6 import java.util.Iterator; 5 7 import java.util.Map; 6 8 import java.util.Map.Entry; 7 9 8 import org.json.JSONStringer; 10 import javax.json.Json; 11 import javax.json.JsonArrayBuilder; 12 import javax.json.JsonObjectBuilder; 13 import javax.json.JsonWriter; 14 import javax.json.stream.JsonGenerator; 15 9 16 import org.openstreetmap.josm.data.Bounds; 10 17 import org.openstreetmap.josm.data.coor.LatLon; 11 import org.openstreetmap.josm.data.osm.Changeset; 18 import org.openstreetmap.josm.data.osm.DataSet; 19 import org.openstreetmap.josm.data.osm.INode; 20 import org.openstreetmap.josm.data.osm.IRelation; 21 import org.openstreetmap.josm.data.osm.IWay; 12 22 import org.openstreetmap.josm.data.osm.Node; 13 23 import org.openstreetmap.josm.data.osm.OsmPrimitive; 14 import org.openstreetmap.josm.data.osm.Relation;15 24 import org.openstreetmap.josm.data.osm.Way; 16 import org.openstreetmap.josm.data.osm.visitor. Visitor;25 import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor; 17 26 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 18 27 19 public class GeoJSONWriter implements Visitor { 28 /** 29 * Writes OSM data as a GeoJSON string, using JSR 353: Java API for JSON Processing (JSON-P). 30 */ 31 public class GeoJSONWriter { 20 32 21 33 private OsmDataLayer layer; 22 private JSONStringer out;23 34 private static final boolean skipEmptyNodes = true; 24 35 36 /** 37 * Constructs a new {@code GeoJSONWriter}. 38 * @param layer The OSM data layer to save 39 */ 25 40 public GeoJSONWriter(OsmDataLayer layer) { 26 41 this.layer = layer; 27 42 } 28 43 44 /** 45 * Writes OSM data as a GeoJSON string (prettified). 46 * @return The GeoJSON data 47 */ 29 48 public String write() { 30 out = new JSONStringer(); 31 out.object().key("type").value("FeatureCollection"); 32 out.key("generator").value("JOSM"); 33 appendLayerBounds(); 34 out.key("features").array(); 35 for (Node n : layer.data.getNodes()) { 36 appendPrimitive(n); 37 } 38 for (Way w : layer.data.getWays()) { 39 appendPrimitive(w); 40 } 41 out.endArray().endObject(); 42 return out.toString(); 49 return write(true); 43 50 } 44 51 45 @Override 46 public void visit(Node n) { 47 out.key("type").value("Point").key("coordinates"); 48 appendCoord(n.getCoor()); 52 /** 53 * Writes OSM data as a GeoJSON string (prettified or not). 54 * @param pretty {@code true} to have pretty output, {@code false} otherwise 55 * @return The GeoJSON data 56 * @since 6756 57 */ 58 public String write(boolean pretty) { 59 StringWriter stringWriter = new StringWriter(); 60 Map<String, Object> config = new HashMap<String, Object>(1); 61 config.put(JsonGenerator.PRETTY_PRINTING, pretty); 62 JsonWriter writer = Json.createWriterFactory(config).createWriter(stringWriter); 63 JsonObjectBuilder object = Json.createObjectBuilder() 64 .add("type", "FeatureCollection") 65 .add("generator", "JOSM"); 66 appendLayerBounds(layer.data, object); 67 appendLayerFeatures(layer.data, object); 68 writer.writeObject(object.build()); 69 String result = stringWriter.toString(); 70 writer.close(); 71 return result; 49 72 } 50 73 51 @Override 52 public void visit(Way w) { 53 out.key("type").value("LineString").key("coordinates").array(); 54 for (Node n : w.getNodes()) { 55 appendCoord(n.getCoor()); 56 } 57 out.endArray(); 58 } 59 60 @Override 61 public void visit(Relation e) { 62 } 63 64 @Override 65 public void visit(Changeset cs) { 66 } 67 68 protected void appendPrimitive(OsmPrimitive p) { 74 protected static void appendPrimitive(OsmPrimitive p, JsonArrayBuilder array) { 69 75 if (p.isIncomplete()) { 70 76 return; … … 72 78 return; 73 79 } 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 81 // Properties 82 final JsonObjectBuilder propObj = Json.createObjectBuilder(); 83 for (Entry<String, String> t : p.getKeys().entrySet()) { 84 propObj.add(t.getKey(), t.getValue()); 79 85 } 80 out.endObject(); 81 // append primitive specific 82 out.key("geometry").object(); 83 p.accept(this); 84 out.endObject(); 85 out.endObject(); 86 87 // Geometry 88 final JsonObjectBuilder geomObj = Json.createObjectBuilder(); 89 p.accept(new PrimitiveVisitor() { 90 @Override 91 public void visit(INode n) { 92 geomObj.add("type", "Point"); 93 LatLon ll = n.getCoor(); 94 if (ll != null) { 95 geomObj.add("coordinates", getCoorArray(n.getCoor())); 96 } 97 } 98 99 @Override 100 public void visit(IWay w) { 101 geomObj.add("type", "LineString"); 102 if (w instanceof Way) { 103 JsonArrayBuilder array = Json.createArrayBuilder(); 104 for (Node n : ((Way)w).getNodes()) { 105 LatLon ll = n.getCoor(); 106 if (ll != null) { 107 array.add(getCoorArray(ll)); 108 } 109 } 110 geomObj.add("coordinates", array); 111 } 112 } 113 114 @Override 115 public void visit(IRelation r) { 116 } 117 118 private JsonArrayBuilder getCoorArray(LatLon c) { 119 return Json.createArrayBuilder().add(c.lon()).add(c.lat()); 120 } 121 }); 122 123 // Build primitive JSON object 124 array.add(Json.createObjectBuilder() 125 .add("type", "Feature") 126 .add("properties", propObj) 127 .add("geometry", geomObj)); 86 128 } 87 129 88 protected void appendCoord(LatLon c) { 89 if (c != null) { 90 out.array().value(c.lon()).value(c.lat()).endArray(); 130 protected static void appendLayerBounds(DataSet ds, JsonObjectBuilder object) { 131 if (ds != null) { 132 Iterator<Bounds> it = ds.getDataSourceBounds().iterator(); 133 if (it.hasNext()) { 134 Bounds b = new Bounds(it.next()); 135 while (it.hasNext()) { 136 b.extend(it.next()); 137 } 138 appendBounds(b, object); 139 } 91 140 } 92 141 } 93 142 94 protected void appendLayerBounds() { 95 Iterator<Bounds> it = layer.data.getDataSourceBounds().iterator(); 96 if (it.hasNext()) { 97 Bounds b = new Bounds(it.next()); 98 while (it.hasNext()) { 99 b.extend(it.next()); 100 } 101 appendBounds(b); 143 protected static void appendBounds(Bounds b, JsonObjectBuilder object) { 144 if (b != null) { 145 object.add("bbox", Json.createArrayBuilder() 146 .add(b.getMinLon()).add(b.getMinLat()) 147 .add(b.getMaxLon()).add(b.getMaxLat())); 102 148 } 103 149 } 104 150 105 protected void appendBounds(Bounds b) { 106 if (b != null) { 107 out.key("bbox").array() 108 .value(b.getMinLon()).value(b.getMinLat()) 109 .value(b.getMaxLon()).value(b.getMaxLat()).endArray(); 151 protected static void appendLayerFeatures(DataSet ds, JsonObjectBuilder object) { 152 JsonArrayBuilder array = Json.createArrayBuilder(); 153 if (ds != null) { 154 for (Node n : ds.getNodes()) { 155 appendPrimitive(n, array); 156 } 157 for (Way w : ds.getWays()) { 158 appendPrimitive(w, array); 159 } 110 160 } 161 object.add("features", array); 111 162 } 112 163 }
Note:
See TracChangeset
for help on using the changeset viewer.