1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import java.io.BufferedWriter;
|
---|
5 | import java.io.OutputStream;
|
---|
6 | import java.io.OutputStreamWriter;
|
---|
7 | import java.io.PrintWriter;
|
---|
8 | import java.io.UnsupportedEncodingException;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Map;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.Bounds;
|
---|
13 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
14 | import org.openstreetmap.josm.data.gpx.GpxLink;
|
---|
15 | import org.openstreetmap.josm.data.gpx.GpxRoute;
|
---|
16 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
---|
17 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Writes GPX files from GPX data or OSM data.
|
---|
21 | */
|
---|
22 | public class GpxWriter extends XmlWriter {
|
---|
23 |
|
---|
24 | public GpxWriter(PrintWriter out) {
|
---|
25 | super(out);
|
---|
26 | }
|
---|
27 |
|
---|
28 | public GpxWriter(OutputStream out) throws UnsupportedEncodingException {
|
---|
29 | super(new PrintWriter(new BufferedWriter(new OutputStreamWriter(out, "UTF-8"))));
|
---|
30 | }
|
---|
31 |
|
---|
32 | public GpxWriter() {
|
---|
33 | super(null);
|
---|
34 | //sorry for this one here, this will be cleaned up once the new scheme works
|
---|
35 | }
|
---|
36 |
|
---|
37 | private GpxData data;
|
---|
38 | private String indent = "";
|
---|
39 |
|
---|
40 | private final static int WAY_POINT = 0;
|
---|
41 | private final static int ROUTE_POINT = 1;
|
---|
42 | private final static int TRACK_POINT = 2;
|
---|
43 |
|
---|
44 | public void write(GpxData data) {
|
---|
45 | this.data = data;
|
---|
46 | out.println("<?xml version='1.0' encoding='UTF-8'?>");
|
---|
47 | out.println("<gpx version=\"1.1\" creator=\"JOSM GPX export\" xmlns=\"http://www.topografix.com/GPX/1/1\">");
|
---|
48 | indent = " ";
|
---|
49 | writeMetaData();
|
---|
50 | writeWayPoints();
|
---|
51 | writeRoutes();
|
---|
52 | writeTracks();
|
---|
53 | out.print("</gpx>");
|
---|
54 | out.flush();
|
---|
55 | }
|
---|
56 |
|
---|
57 | private void writeAttr(Map<String, Object> attr) {
|
---|
58 | boolean hasAuthor = false;
|
---|
59 | for (Map.Entry<String, Object> ent : attr.entrySet()) {
|
---|
60 | String k = ent.getKey();
|
---|
61 | if (k.indexOf("author") == 0) {
|
---|
62 | hasAuthor = true;
|
---|
63 | } else if (k.equals("link")) {
|
---|
64 | for (GpxLink link : (Collection<GpxLink>) ent.getValue()) {
|
---|
65 | gpxLink(link);
|
---|
66 | }
|
---|
67 | } else {
|
---|
68 | simpleTag(k, (String) ent.getValue());
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | if (hasAuthor) {
|
---|
73 | open("author");
|
---|
74 | simpleTag("name", (String) attr.get("authorname"));
|
---|
75 | simpleTag("email", (String) attr.get("authoremail"));
|
---|
76 | gpxLink((GpxLink) attr.get("authorlink"));
|
---|
77 | closeln("author");
|
---|
78 | }
|
---|
79 |
|
---|
80 | // TODO: copyright
|
---|
81 | }
|
---|
82 |
|
---|
83 | private void writeMetaData() {
|
---|
84 | openln("metadata");
|
---|
85 | writeAttr(data.attr);
|
---|
86 |
|
---|
87 | data.recalculateBounds();
|
---|
88 | Bounds bounds = data.bounds;
|
---|
89 | String b = "minlat=\"" + bounds.min.lat() + "\" minlon=\"" + bounds.min.lon() +
|
---|
90 | "\" maxlat=\"" + bounds.max.lat() + "\" maxlon=\"" + bounds.max.lon() + "\"" ;
|
---|
91 | inline("bounds", b);
|
---|
92 |
|
---|
93 | closeln("metadata");
|
---|
94 | }
|
---|
95 |
|
---|
96 | private void writeWayPoints() {
|
---|
97 | for (WayPoint pnt : data.waypoints) {
|
---|
98 | wayPoint(pnt, WAY_POINT);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | private void writeRoutes() {
|
---|
103 | for (GpxRoute rte : data.routes) {
|
---|
104 | openln("rte");
|
---|
105 | writeAttr(rte.attr);
|
---|
106 | for (WayPoint pnt : rte.routePoints) {
|
---|
107 | wayPoint(pnt, ROUTE_POINT);
|
---|
108 | }
|
---|
109 | closeln("rte");
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | private void writeTracks() {
|
---|
114 | for (GpxTrack trk : data.tracks) {
|
---|
115 | open("trk");
|
---|
116 | writeAttr(trk.attr);
|
---|
117 | for (Collection<WayPoint> seg : trk.trackSegs) {
|
---|
118 | openln("trkseg");
|
---|
119 | for (WayPoint pnt : seg) {
|
---|
120 | wayPoint(pnt, TRACK_POINT);
|
---|
121 | }
|
---|
122 | closeln("trkseg");
|
---|
123 | }
|
---|
124 | closeln("trk");
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | private void openln(String tag) {
|
---|
129 | open(tag);
|
---|
130 | out.println();
|
---|
131 | }
|
---|
132 |
|
---|
133 | private void open(String tag) {
|
---|
134 | out.print(indent + "<" + tag + ">");
|
---|
135 | indent += " ";
|
---|
136 | }
|
---|
137 |
|
---|
138 | private void openAtt(String tag, String attributes) {
|
---|
139 | out.println(indent + "<" + tag + " " + attributes + ">");
|
---|
140 | indent += " ";
|
---|
141 | }
|
---|
142 |
|
---|
143 | private void inline(String tag, String attributes) {
|
---|
144 | out.println(indent + "<" + tag + " " + attributes + " />");
|
---|
145 | }
|
---|
146 |
|
---|
147 | private void close(String tag) {
|
---|
148 | indent = indent.substring(2);
|
---|
149 | out.print(indent + "</" + tag + ">");
|
---|
150 | }
|
---|
151 |
|
---|
152 | private void closeln(String tag) {
|
---|
153 | close(tag);
|
---|
154 | out.println();
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * if content not null, open tag, write encoded content, and close tag
|
---|
159 | * else do nothing.
|
---|
160 | */
|
---|
161 | private void simpleTag(String tag, String content) {
|
---|
162 | if (content != null && content.length() > 0) {
|
---|
163 | open(tag);
|
---|
164 | out.print(encode(content));
|
---|
165 | out.println("</" + tag + ">");
|
---|
166 | indent = indent.substring(2);
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * output link
|
---|
172 | */
|
---|
173 | private void gpxLink(GpxLink link) {
|
---|
174 | if (link != null) {
|
---|
175 | openAtt("link", "href=\"" + link.uri + "\"");
|
---|
176 | simpleTag("text", link.text);
|
---|
177 | simpleTag("type", link.type);
|
---|
178 | closeln("link");
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * output a point
|
---|
184 | */
|
---|
185 | private void wayPoint(WayPoint pnt, int mode) {
|
---|
186 | String type;
|
---|
187 | switch(mode) {
|
---|
188 | case WAY_POINT:
|
---|
189 | type = "wpt";
|
---|
190 | break;
|
---|
191 | case ROUTE_POINT:
|
---|
192 | type = "rtept";
|
---|
193 | break;
|
---|
194 | case TRACK_POINT:
|
---|
195 | type = "trkpt";
|
---|
196 | break;
|
---|
197 | default:
|
---|
198 | throw new RuntimeException("Bug detected. Please report this!");
|
---|
199 | }
|
---|
200 | if (pnt != null) {
|
---|
201 | openAtt(type, "lat=\"" + pnt.latlon.lat() + "\" lon=\"" + pnt.latlon.lon() + "\"");
|
---|
202 | writeAttr(pnt.attr);
|
---|
203 | closeln(type);
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|