1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import java.io.PrintWriter;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.Map.Entry;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
9 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
10 | import org.openstreetmap.josm.data.osm.DataSource;
|
---|
11 | import org.openstreetmap.josm.data.osm.Node;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
13 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
14 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
15 | import org.openstreetmap.josm.data.osm.Way;
|
---|
16 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
17 | import org.openstreetmap.josm.tools.DateUtils;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Save the dataset into a stream as osm intern xml format. This is not using any
|
---|
21 | * xml library for storing.
|
---|
22 | * @author imi
|
---|
23 | */
|
---|
24 | public class OsmWriter extends XmlWriter implements Visitor {
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * The counter for newly created objects. Starts at -1 and goes down.
|
---|
28 | */
|
---|
29 | private long newIdCounter = -1;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * All newly created ids and their primitive that uses it. This is a back reference
|
---|
33 | * map to allow references to use the correnct primitives.
|
---|
34 | */
|
---|
35 | public HashMap<OsmPrimitive, Long> usedNewIds = new HashMap<OsmPrimitive, Long>();
|
---|
36 |
|
---|
37 | private boolean osmConform;
|
---|
38 | private boolean withBody = true;
|
---|
39 | private String version;
|
---|
40 | private Changeset changeset;
|
---|
41 |
|
---|
42 | public OsmWriter(PrintWriter out, boolean osmConform, String version) {
|
---|
43 | super(out);
|
---|
44 | this.osmConform = osmConform;
|
---|
45 | this.version = version;
|
---|
46 | }
|
---|
47 |
|
---|
48 | public void setWithBody(boolean wb) {
|
---|
49 | this.withBody = wb;
|
---|
50 | }
|
---|
51 | public void setChangeset(Changeset cs) {
|
---|
52 | this.changeset = cs;
|
---|
53 | }
|
---|
54 | public void setVersion(String v) {
|
---|
55 | this.version = v;
|
---|
56 | }
|
---|
57 |
|
---|
58 | public void header() {
|
---|
59 | out.println("<?xml version='1.0' encoding='UTF-8'?>");
|
---|
60 | out.print("<osm version='");
|
---|
61 | out.print(version);
|
---|
62 | out.println("' generator='JOSM'>");
|
---|
63 | }
|
---|
64 | public void footer() {
|
---|
65 | out.println("</osm>");
|
---|
66 | }
|
---|
67 |
|
---|
68 | public void writeContent(DataSet ds) {
|
---|
69 | for (Node n : ds.nodes)
|
---|
70 | if (shouldWrite(n))
|
---|
71 | visit(n);
|
---|
72 | for (Way w : ds.ways)
|
---|
73 | if (shouldWrite(w))
|
---|
74 | visit(w);
|
---|
75 | for (Relation e : ds.relations)
|
---|
76 | if (shouldWrite(e))
|
---|
77 | visit(e);
|
---|
78 | }
|
---|
79 |
|
---|
80 | private boolean shouldWrite(OsmPrimitive osm) {
|
---|
81 | return osm.id != 0 || !osm.deleted;
|
---|
82 | }
|
---|
83 |
|
---|
84 | public void writeDataSources(DataSet ds) {
|
---|
85 | for (DataSource s : ds.dataSources) {
|
---|
86 | out.println(" <bounds minlat='"
|
---|
87 | + s.bounds.min.lat()+"' minlon='"
|
---|
88 | + s.bounds.min.lon()+"' maxlat='"
|
---|
89 | + s.bounds.max.lat()+"' maxlon='"
|
---|
90 | + s.bounds.max.lon()
|
---|
91 | +"' origin='"+XmlWriter.encode(s.origin)+"' />");
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | public void visit(Node n) {
|
---|
96 | if (n.incomplete) return;
|
---|
97 | addCommon(n, "node");
|
---|
98 | out.print(" lat='"+n.coor.lat()+"' lon='"+n.coor.lon()+"'");
|
---|
99 | if (!withBody) {
|
---|
100 | out.println("/>");
|
---|
101 | } else {
|
---|
102 | addTags(n, "node", true);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | public void visit(Way w) {
|
---|
107 | if (w.incomplete) return;
|
---|
108 | addCommon(w, "way");
|
---|
109 | if (!withBody) {
|
---|
110 | out.println("/>");
|
---|
111 | } else {
|
---|
112 | out.println(">");
|
---|
113 | for (Node n : w.nodes)
|
---|
114 | out.println(" <nd ref='"+getUsedId(n)+"' />");
|
---|
115 | addTags(w, "way", false);
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | public void visit(Relation e) {
|
---|
120 | if (e.incomplete) return;
|
---|
121 | addCommon(e, "relation");
|
---|
122 | if (!withBody) {
|
---|
123 | out.println("/>");
|
---|
124 | } else {
|
---|
125 | out.println(">");
|
---|
126 | for (RelationMember em : e.members) {
|
---|
127 | out.print(" <member type='");
|
---|
128 | out.print(OsmApi.which(em.member));
|
---|
129 | out.println("' ref='"+getUsedId(em.member)+"' role='" +
|
---|
130 | XmlWriter.encode(em.role) + "' />");
|
---|
131 | }
|
---|
132 | addTags(e, "relation", false);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | public void visit(Changeset cs) {
|
---|
137 | addCommon(cs, "changeset");
|
---|
138 | out.println(">\n");
|
---|
139 | addTags(cs, "changeset", false);
|
---|
140 | }
|
---|
141 |
|
---|
142 | public final void footer(PrintWriter out) {
|
---|
143 | out.println("</osm>");
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Return the id for the given osm primitive (may access the usedId map)
|
---|
148 | */
|
---|
149 | private long getUsedId(OsmPrimitive osm) {
|
---|
150 | if (osm.id != 0)
|
---|
151 | return osm.id;
|
---|
152 | if (usedNewIds.containsKey(osm))
|
---|
153 | return usedNewIds.get(osm);
|
---|
154 | usedNewIds.put(osm, newIdCounter);
|
---|
155 | return osmConform ? 0 : newIdCounter--;
|
---|
156 | }
|
---|
157 |
|
---|
158 | private void addTags(OsmPrimitive osm, String tagname, boolean tagOpen) {
|
---|
159 | if (osm.keys != null) {
|
---|
160 | if (tagOpen)
|
---|
161 | out.println(">");
|
---|
162 | for (Entry<String, String> e : osm.keys.entrySet())
|
---|
163 | out.println(" <tag k='"+ XmlWriter.encode(e.getKey()) +
|
---|
164 | "' v='"+XmlWriter.encode(e.getValue())+ "' />");
|
---|
165 | out.println(" </" + tagname + ">");
|
---|
166 | } else if (tagOpen)
|
---|
167 | out.println(" />");
|
---|
168 | else
|
---|
169 | out.println(" </" + tagname + ">");
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Add the common part as the form of the tag as well as the XML attributes
|
---|
174 | * id, action, user, and visible.
|
---|
175 | */
|
---|
176 | private void addCommon(OsmPrimitive osm, String tagname) {
|
---|
177 | long id = getUsedId(osm);
|
---|
178 | out.print(" <"+tagname);
|
---|
179 | if (id != 0) {
|
---|
180 | out.print(" id='"+getUsedId(osm)+"'");
|
---|
181 | }
|
---|
182 | if (!osmConform) {
|
---|
183 | String action = null;
|
---|
184 | if (osm.deleted)
|
---|
185 | action = "delete";
|
---|
186 | else if (osm.modified)
|
---|
187 | action = "modify";
|
---|
188 | if (action != null)
|
---|
189 | out.print(" action='"+action+"'");
|
---|
190 | }
|
---|
191 | if (!osm.isTimestampEmpty()) {
|
---|
192 | out.print(" timestamp='"+DateUtils.fromDate(osm.getTimestamp())+"'");
|
---|
193 | }
|
---|
194 | // user and visible added with 0.4 API
|
---|
195 | if (osm.user != null) {
|
---|
196 | out.print(" user='"+XmlWriter.encode(osm.user.name)+"'");
|
---|
197 | }
|
---|
198 | out.print(" visible='"+osm.visible+"'");
|
---|
199 | if (osm.version != -1)
|
---|
200 | out.print(" version='"+osm.version+"'");
|
---|
201 | if (this.changeset != null && this.changeset.id != 0)
|
---|
202 | out.print(" changeset='"+this.changeset.id+"'" );
|
---|
203 | }
|
---|
204 | }
|
---|