1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import java.util.Map;
|
---|
5 | import java.util.Map.Entry;
|
---|
6 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
7 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
8 | import org.openstreetmap.josm.data.osm.Node;
|
---|
9 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
10 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
11 | import org.openstreetmap.josm.data.osm.Way;
|
---|
12 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
13 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
14 |
|
---|
15 | public class GeoJSONWriter implements Visitor {
|
---|
16 |
|
---|
17 | private OsmDataLayer layer;
|
---|
18 | private StringBuilder out;
|
---|
19 | private static final boolean skipEmptyNodes = true;
|
---|
20 | private boolean insertComma = false;
|
---|
21 |
|
---|
22 | public GeoJSONWriter(OsmDataLayer layer) {
|
---|
23 | this.layer = layer;
|
---|
24 | }
|
---|
25 |
|
---|
26 | public String write() {
|
---|
27 | out = new StringBuilder(1 << 12);
|
---|
28 | out.append("{\"type\": \"FeatureCollection\",\n");
|
---|
29 | out.append("\"features\": [\n");
|
---|
30 | for (Node n : layer.data.getNodes()) {
|
---|
31 | appendPrimitive(n);
|
---|
32 | }
|
---|
33 | for (Way w : layer.data.getWays()) {
|
---|
34 | appendPrimitive(w);
|
---|
35 | }
|
---|
36 | out.append("\n]\n}");
|
---|
37 | return out.toString();
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public void visit(Node n) {
|
---|
42 | out.append("\"type\": \"Point\", \"coordinates\": ");
|
---|
43 | appendCoord(n.getCoor());
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | public void visit(Way w) {
|
---|
48 | out.append("\"type\": \"LineString\", \"coordinates\": [");
|
---|
49 | boolean insertCommaCoords = false;
|
---|
50 | for (Node n : w.getNodes()) {
|
---|
51 | if (insertCommaCoords) {
|
---|
52 | out.append(", ");
|
---|
53 | }
|
---|
54 | insertCommaCoords = true;
|
---|
55 | appendCoord(n.getCoor());
|
---|
56 | }
|
---|
57 | out.append("]");
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public void visit(Relation e) {
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public void visit(Changeset cs) {
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected String escape(String s) {
|
---|
69 | return s.replace("\"", "\\\"").replace("\\", "\\\\").replace("\n", "\\n");
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected void appendPrimitive(OsmPrimitive p) {
|
---|
73 | if (p.isIncomplete()) {
|
---|
74 | return;
|
---|
75 | } else if (skipEmptyNodes && p instanceof Node && p.getKeys().isEmpty()) {
|
---|
76 | return;
|
---|
77 | }
|
---|
78 | if (insertComma) {
|
---|
79 | out.append(",\n");
|
---|
80 | }
|
---|
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("}");
|
---|
105 | }
|
---|
106 |
|
---|
107 | protected void appendCoord(LatLon c) {
|
---|
108 | if (c != null) {
|
---|
109 | out.append("[").append(c.lon()).append(", ").append(c.lat()).append("]");
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|