source: josm/trunk/src/org/openstreetmap/josm/io/GpxWriter.java@ 18219

Last change on this file since 18219 was 18219, checked in by Don-vip, 3 years ago

see #21257 - fix unit tests

  • Property svn:eol-style set to native
File size: 15.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.BufferedWriter;
7import java.io.OutputStream;
8import java.io.OutputStreamWriter;
9import java.io.PrintWriter;
10import java.nio.charset.StandardCharsets;
11import java.time.Instant;
12import java.util.ArrayList;
13import java.util.Collection;
14import java.util.Date;
15import java.util.List;
16import java.util.Map;
17import java.util.Objects;
18import java.util.stream.Collectors;
19
20import javax.xml.XMLConstants;
21
22import org.openstreetmap.josm.data.Bounds;
23import org.openstreetmap.josm.data.coor.LatLon;
24import org.openstreetmap.josm.data.gpx.GpxConstants;
25import org.openstreetmap.josm.data.gpx.GpxData;
26import org.openstreetmap.josm.data.gpx.GpxData.XMLNamespace;
27import org.openstreetmap.josm.data.gpx.GpxExtension;
28import org.openstreetmap.josm.data.gpx.GpxExtensionCollection;
29import org.openstreetmap.josm.data.gpx.GpxLink;
30import org.openstreetmap.josm.data.gpx.GpxRoute;
31import org.openstreetmap.josm.data.gpx.GpxTrack;
32import org.openstreetmap.josm.data.gpx.IGpxTrack;
33import org.openstreetmap.josm.data.gpx.IGpxTrackSegment;
34import org.openstreetmap.josm.data.gpx.IWithAttributes;
35import org.openstreetmap.josm.data.gpx.WayPoint;
36import org.openstreetmap.josm.tools.JosmRuntimeException;
37import org.openstreetmap.josm.tools.Logging;
38import org.openstreetmap.josm.tools.Utils;
39
40/**
41 * Writes GPX files from GPX data or OSM data.
42 */
43public class GpxWriter extends XmlWriter implements GpxConstants {
44
45 /**
46 * Constructs a new {@code GpxWriter}.
47 * @param out The output writer
48 */
49 public GpxWriter(PrintWriter out) {
50 super(out);
51 }
52
53 /**
54 * Constructs a new {@code GpxWriter}.
55 * @param out The output stream
56 */
57 public GpxWriter(OutputStream out) {
58 super(new PrintWriter(new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8))));
59 }
60
61 private GpxData data;
62 private String indent = "";
63 private Instant metaTime;
64 private List<String> validprefixes;
65
66 private static final int WAY_POINT = 0;
67 private static final int ROUTE_POINT = 1;
68 private static final int TRACK_POINT = 2;
69
70 /**
71 * Returns the forced metadata time information, if any.
72 * @return the forced metadata time information, or {@code null}
73 * @since 18219
74 */
75 public Instant getMetaTime() {
76 return metaTime;
77 }
78
79 /**
80 * Sets the forced metadata time information.
81 * @param metaTime the forced metadata time information, or {@code null} to use the current time
82 * @since 18219
83 */
84 public void setMetaTime(Instant metaTime) {
85 this.metaTime = metaTime;
86 }
87
88 /**
89 * Writes the given GPX data.
90 * @param data The data to write
91 */
92 public void write(GpxData data) {
93 write(data, ColorFormat.GPXD, true);
94 }
95
96 /**
97 * Writes the given GPX data.
98 *
99 * @param data The data to write
100 * @param colorFormat determines if colors are saved and which extension is to be used
101 * @param savePrefs whether layer specific preferences are saved
102 */
103 public void write(GpxData data, ColorFormat colorFormat, boolean savePrefs) {
104 this.data = data;
105
106 //Prepare extensions for writing
107 data.beginUpdate();
108 data.getTracks().stream()
109 .filter(GpxTrack.class::isInstance).map(GpxTrack.class::cast)
110 .forEach(trk -> trk.convertColor(colorFormat));
111 data.getExtensions().removeAllWithPrefix("josm");
112 if (data.fromServer) {
113 data.getExtensions().add("josm", "from-server", "true");
114 }
115 if (savePrefs && !data.getLayerPrefs().isEmpty()) {
116 GpxExtensionCollection layerExts = data.getExtensions().add("josm", "layerPreferences").getExtensions();
117 data.getLayerPrefs().entrySet()
118 .stream()
119 .sorted(Map.Entry.comparingByKey())
120 .forEach(entry -> {
121 GpxExtension e = layerExts.add("josm", "entry");
122 e.put("key", entry.getKey());
123 e.put("value", entry.getValue());
124 });
125 }
126 data.put(META_TIME, (metaTime != null ? metaTime : Instant.now()).toString());
127 data.endUpdate();
128
129 Collection<IWithAttributes> all = new ArrayList<>();
130
131 all.add(data);
132 all.addAll(data.getWaypoints());
133 all.addAll(data.getRoutes());
134 all.addAll(data.getTracks());
135 all.addAll(data.getTrackSegmentsStream().collect(Collectors.toList()));
136
137 List<XMLNamespace> namespaces = all
138 .stream()
139 .flatMap(w -> w.getExtensions().getPrefixesStream())
140 .distinct()
141 .map(p -> data.getNamespaces()
142 .stream()
143 .filter(s -> s.getPrefix().equals(p))
144 .findAny()
145 .orElse(GpxExtension.findNamespace(p)))
146 .filter(Objects::nonNull)
147 .collect(Collectors.toList());
148
149 validprefixes = namespaces.stream().map(n -> n.getPrefix()).collect(Collectors.toList());
150
151 out.println("<?xml version='1.0' encoding='UTF-8'?>");
152 out.println("<gpx version=\"1.1\" creator=\"JOSM GPX export\" xmlns=\"http://www.topografix.com/GPX/1/1\"");
153
154 StringBuilder schemaLocations = new StringBuilder("http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd");
155
156 for (XMLNamespace n : namespaces) {
157 if (n.getURI() != null && !Utils.isEmpty(n.getPrefix())) {
158 out.println(String.format(" xmlns:%s=\"%s\"", n.getPrefix(), n.getURI()));
159 if (n.getLocation() != null) {
160 schemaLocations.append(' ').append(n.getURI()).append(' ').append(n.getLocation());
161 }
162 }
163 }
164
165 out.println(" xmlns:xsi=\""+XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI+"\"");
166 out.println(String.format(" xsi:schemaLocation=\"%s\">", schemaLocations));
167 indent = " ";
168 writeMetaData();
169 writeWayPoints();
170 writeRoutes();
171 writeTracks();
172 out.print("</gpx>");
173 out.flush();
174 }
175
176 private void writeAttr(IWithAttributes obj, List<String> keys) {
177 for (String key : keys) {
178 if (META_LINKS.equals(key)) {
179 Collection<GpxLink> lValue = obj.<GpxLink>getCollection(key);
180 if (lValue != null) {
181 for (GpxLink link : lValue) {
182 gpxLink(link);
183 }
184 }
185 } else {
186 String value = obj.getString(key);
187 if (value != null) {
188 simpleTag(key, value);
189 } else {
190 Object val = obj.get(key);
191 if (val instanceof Date) {
192 throw new IllegalStateException();
193 } else if (val instanceof Instant) {
194 simpleTag(key, String.valueOf(val));
195 } else if (val instanceof Number) {
196 simpleTag(key, val.toString());
197 } else if (val != null) {
198 Logging.warn("GPX attribute '"+key+"' not managed: " + val);
199 }
200 }
201 }
202 }
203 }
204
205 private void writeMetaData() {
206 Map<String, Object> attr = data.attr;
207 openln("metadata");
208
209 // write the description
210 if (attr.containsKey(META_DESC)) {
211 simpleTag("desc", data.getString(META_DESC));
212 }
213
214 // write the author details
215 if (attr.containsKey(META_AUTHOR_NAME)
216 || attr.containsKey(META_AUTHOR_EMAIL)) {
217 openln("author");
218 // write the name
219 simpleTag("name", data.getString(META_AUTHOR_NAME));
220 // write the email address
221 if (attr.containsKey(META_AUTHOR_EMAIL)) {
222 String[] tmp = data.getString(META_AUTHOR_EMAIL).split("@", -1);
223 if (tmp.length == 2) {
224 inline("email", "id=\"" + encode(tmp[0]) + "\" domain=\"" + encode(tmp[1]) +'\"');
225 }
226 }
227 // write the author link
228 gpxLink((GpxLink) data.get(META_AUTHOR_LINK));
229 closeln("author");
230 }
231
232 // write the copyright details
233 if (attr.containsKey(META_COPYRIGHT_LICENSE)
234 || attr.containsKey(META_COPYRIGHT_YEAR)) {
235 openln("copyright", "author=\""+ encode(data.get(META_COPYRIGHT_AUTHOR).toString()) +'\"');
236 if (attr.containsKey(META_COPYRIGHT_YEAR)) {
237 simpleTag("year", (String) data.get(META_COPYRIGHT_YEAR));
238 }
239 if (attr.containsKey(META_COPYRIGHT_LICENSE)) {
240 simpleTag("license", encode((String) data.get(META_COPYRIGHT_LICENSE)));
241 }
242 closeln("copyright");
243 }
244
245 // write links
246 if (attr.containsKey(META_LINKS)) {
247 for (GpxLink link : data.<GpxLink>getCollection(META_LINKS)) {
248 gpxLink(link);
249 }
250 }
251
252 // write keywords
253 if (attr.containsKey(META_KEYWORDS)) {
254 simpleTag("keywords", data.getString(META_KEYWORDS));
255 }
256
257 // write the time
258 if (attr.containsKey(META_TIME)) {
259 simpleTag("time", data.getString(META_TIME));
260 }
261
262 Bounds bounds = data.recalculateBounds();
263 if (bounds != null) {
264 String b = "minlat=\"" + bounds.getMinLat() + "\" minlon=\"" + bounds.getMinLon() +
265 "\" maxlat=\"" + bounds.getMaxLat() + "\" maxlon=\"" + bounds.getMaxLon() + '\"';
266 inline("bounds", b);
267 }
268
269 gpxExtensions(data.getExtensions());
270 closeln("metadata");
271 }
272
273 private void writeWayPoints() {
274 for (WayPoint pnt : data.getWaypoints()) {
275 wayPoint(pnt, WAY_POINT);
276 }
277 }
278
279 private void writeRoutes() {
280 for (GpxRoute rte : data.getRoutes()) {
281 openln("rte");
282 writeAttr(rte, RTE_TRK_KEYS);
283 gpxExtensions(rte.getExtensions());
284 for (WayPoint pnt : rte.routePoints) {
285 wayPoint(pnt, ROUTE_POINT);
286 }
287 closeln("rte");
288 }
289 }
290
291 private void writeTracks() {
292 for (IGpxTrack trk : data.getOrderedTracks()) {
293 openln("trk");
294 writeAttr(trk, RTE_TRK_KEYS);
295 gpxExtensions(trk.getExtensions());
296 for (IGpxTrackSegment seg : trk.getSegments()) {
297 openln("trkseg");
298 gpxExtensions(seg.getExtensions());
299 for (WayPoint pnt : seg.getWayPoints()) {
300 wayPoint(pnt, TRACK_POINT);
301 }
302 closeln("trkseg");
303 }
304 closeln("trk");
305 }
306 }
307
308 private void openln(String tag) {
309 open(tag);
310 out.println();
311 }
312
313 private void openln(String tag, String attributes) {
314 open(tag, attributes);
315 out.println();
316 }
317
318 private void open(String tag) {
319 out.print(indent + '<' + tag + '>');
320 indent += " ";
321 }
322
323 private void open(String tag, String attributes) {
324 out.print(indent + '<' + tag + (attributes.isEmpty() ? "" : ' ') + attributes + '>');
325 indent += " ";
326 }
327
328 private void inline(String tag, String attributes) {
329 out.println(indent + '<' + tag + (attributes.isEmpty() ? "" : ' ') + attributes + "/>");
330 }
331
332 private void close(String tag) {
333 indent = indent.substring(2);
334 out.print(indent + "</" + tag + '>');
335 }
336
337 private void closeln(String tag) {
338 close(tag);
339 out.println();
340 }
341
342 /**
343 * if content not null, open tag, write encoded content, and close tag
344 * else do nothing.
345 * @param tag GPX tag
346 * @param content content
347 */
348 private void simpleTag(String tag, String content) {
349 if (!Utils.isEmpty(content)) {
350 open(tag);
351 out.print(encode(content));
352 out.println("</" + tag + '>');
353 indent = indent.substring(2);
354 }
355 }
356
357 private void simpleTag(String tag, String content, String attributes) {
358 if (!Utils.isEmpty(content)) {
359 open(tag, attributes);
360 out.print(encode(content));
361 out.println("</" + tag + '>');
362 indent = indent.substring(2);
363 }
364 }
365
366 /**
367 * output link
368 * @param link link
369 */
370 private void gpxLink(GpxLink link) {
371 if (link != null) {
372 openln("link", "href=\"" + encode(link.uri) + '\"');
373 simpleTag("text", link.text);
374 simpleTag("type", link.type);
375 closeln("link");
376 }
377 }
378
379 /**
380 * output a point
381 * @param pnt waypoint
382 * @param mode {@code WAY_POINT} for {@code wpt}, {@code ROUTE_POINT} for {@code rtept}, {@code TRACK_POINT} for {@code trkpt}
383 */
384 private void wayPoint(WayPoint pnt, int mode) {
385 String type;
386 switch(mode) {
387 case WAY_POINT:
388 type = "wpt";
389 break;
390 case ROUTE_POINT:
391 type = "rtept";
392 break;
393 case TRACK_POINT:
394 type = "trkpt";
395 break;
396 default:
397 throw new JosmRuntimeException(tr("Unknown mode {0}.", mode));
398 }
399 if (pnt != null) {
400 LatLon c = pnt.getCoor();
401 String coordAttr = "lat=\"" + c.lat() + "\" lon=\"" + c.lon() + '\"';
402 if (pnt.attr.isEmpty() && pnt.getExtensions().isEmpty()) {
403 inline(type, coordAttr);
404 } else {
405 openln(type, coordAttr);
406 writeAttr(pnt, WPT_KEYS);
407 gpxExtensions(pnt.getExtensions());
408 closeln(type);
409 }
410 }
411 }
412
413 private void gpxExtensions(GpxExtensionCollection allExtensions) {
414 if (allExtensions.isVisible()) {
415 openln("extensions");
416 writeExtension(allExtensions);
417 closeln("extensions");
418 }
419 }
420
421 private void writeExtension(List<GpxExtension> extensions) {
422 for (GpxExtension e : extensions) {
423 if (validprefixes.contains(e.getPrefix()) && e.isVisible()) {
424 // this might lead to loss of an unknown extension *after* the file was saved as .osm,
425 // but otherwise the file is invalid and can't even be parsed by SAX anymore
426 String k = (e.getPrefix().isEmpty() ? "" : e.getPrefix() + ":") + e.getKey();
427 String attr = e.getAttributes().entrySet().stream()
428 .map(a -> encode(a.getKey()) + "=\"" + encode(a.getValue().toString()) + "\"")
429 .sorted()
430 .collect(Collectors.joining(" "));
431 if (e.getValue() == null && e.getExtensions().isEmpty()) {
432 inline(k, attr);
433 } else if (e.getExtensions().isEmpty()) {
434 simpleTag(k, e.getValue(), attr);
435 } else {
436 openln(k, attr);
437 if (e.getValue() != null) {
438 out.print(encode(e.getValue()));
439 }
440 writeExtension(e.getExtensions());
441 closeln(k);
442 }
443 }
444 }
445 }
446}
Note: See TracBrowser for help on using the repository browser.