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

Last change on this file since 2474 was 2327, checked in by Gubaer, 15 years ago

Cleanup in download logic (less global, more encapsulation)

  • Property svn:eol-style set to native
File size: 7.8 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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.io.UnsupportedEncodingException;
11import java.util.Collection;
12import java.util.Map;
13
14import org.openstreetmap.josm.data.Bounds;
15import org.openstreetmap.josm.data.coor.LatLon;
16import org.openstreetmap.josm.data.gpx.GpxData;
17import org.openstreetmap.josm.data.gpx.GpxLink;
18import org.openstreetmap.josm.data.gpx.GpxRoute;
19import org.openstreetmap.josm.data.gpx.GpxTrack;
20import org.openstreetmap.josm.data.gpx.WayPoint;
21
22/**
23 * Writes GPX files from GPX data or OSM data.
24 */
25public class GpxWriter extends XmlWriter {
26
27 public GpxWriter(PrintWriter out) {
28 super(out);
29 }
30
31 public GpxWriter(OutputStream out) throws UnsupportedEncodingException {
32 super(new PrintWriter(new BufferedWriter(new OutputStreamWriter(out, "UTF-8"))));
33 }
34
35 public GpxWriter() {
36 super(null);
37 //sorry for this one here, this will be cleaned up once the new scheme works
38 }
39
40 private GpxData data;
41 private String indent = "";
42
43 private final static int WAY_POINT = 0;
44 private final static int ROUTE_POINT = 1;
45 private final static int TRACK_POINT = 2;
46
47 public void write(GpxData data) {
48 this.data = data;
49 out.println("<?xml version='1.0' encoding='UTF-8'?>");
50 out.println("<gpx version=\"1.1\" creator=\"JOSM GPX export\" xmlns=\"http://www.topografix.com/GPX/1/1\"\n" +
51 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" +
52 " xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">");
53 indent = " ";
54 writeMetaData();
55 writeWayPoints();
56 writeRoutes();
57 writeTracks();
58 out.print("</gpx>");
59 out.flush();
60 }
61
62 private void writeAttr(Map<String, Object> attr) {
63 // FIXME this loop is evil, because it does not assure the
64 // correct element order specified by the xml schema.
65 // for now it works, but future extension could get very complex and unmaintainable
66 for (Map.Entry<String, Object> ent : attr.entrySet()) {
67 String k = ent.getKey();
68 if (k.equals(GpxData.META_LINKS)) {
69 for (Object link : (Collection) ent.getValue()) {
70 gpxLink((GpxLink) link);
71 }
72 } else {
73 simpleTag(k, ent.getValue().toString());
74 }
75 }
76 }
77
78 private void writeMetaData() {
79 Map<String, Object> attr = data.attr;
80 openln("metadata");
81
82 // write the description
83 if (attr.containsKey(GpxData.META_DESC)) simpleTag("desc", (String)attr.get(GpxData.META_DESC));
84
85 // write the author details
86 if (attr.containsKey(GpxData.META_AUTHOR_NAME)
87 || attr.containsKey(GpxData.META_AUTHOR_EMAIL)) {
88 openln("author");
89 // write the name
90 simpleTag("name", (String) attr.get(GpxData.META_AUTHOR_NAME));
91 // write the email address
92 if(attr.containsKey(GpxData.META_AUTHOR_EMAIL)) {
93 String[] tmp = ((String)attr.get(GpxData.META_AUTHOR_EMAIL)).split("@");
94 if(tmp.length == 2) {
95 inline("email", "id=\"" + tmp[0] + "\" domain=\""+tmp[1]+"\"");
96 }
97 }
98 // write the author link
99 gpxLink((GpxLink) attr.get(GpxData.META_AUTHOR_LINK));
100 closeln("author");
101 }
102
103 // write the copyright details
104 if(attr.containsKey(GpxData.META_COPYRIGHT_LICENSE)
105 || attr.containsKey(GpxData.META_COPYRIGHT_YEAR)) {
106 openAtt("copyright", "author=\""+ attr.get(GpxData.META_COPYRIGHT_AUTHOR) +"\"");
107 if(attr.containsKey(GpxData.META_COPYRIGHT_YEAR)) {
108 simpleTag("year", (String) attr.get(GpxData.META_COPYRIGHT_YEAR));
109 }
110 if(attr.containsKey(GpxData.META_COPYRIGHT_LICENSE)) {
111 simpleTag("license", encode((String) attr.get(GpxData.META_COPYRIGHT_LICENSE)));
112 }
113 closeln("copyright");
114 }
115
116 // write links
117 if(attr.containsKey(GpxData.META_LINKS)) {
118 for (Object link : (Collection) attr.get(GpxData.META_LINKS)) {
119 gpxLink((GpxLink) link);
120 }
121 }
122
123 // write keywords
124 if (attr.containsKey(GpxData.META_KEYWORDS)) simpleTag("keywords", (String)attr.get(GpxData.META_KEYWORDS));
125
126 Bounds bounds = data.recalculateBounds();
127 if(bounds != null)
128 {
129 String b = "minlat=\"" + bounds.getMin().lat() + "\" minlon=\"" + bounds.getMin().lon() +
130 "\" maxlat=\"" + bounds.getMax().lat() + "\" maxlon=\"" + bounds.getMax().lon() + "\"" ;
131 inline("bounds", b);
132 }
133
134 closeln("metadata");
135 }
136
137 private void writeWayPoints() {
138 for (WayPoint pnt : data.waypoints) {
139 wayPoint(pnt, WAY_POINT);
140 }
141 }
142
143 private void writeRoutes() {
144 for (GpxRoute rte : data.routes) {
145 openln("rte");
146 writeAttr(rte.attr);
147 for (WayPoint pnt : rte.routePoints) {
148 wayPoint(pnt, ROUTE_POINT);
149 }
150 closeln("rte");
151 }
152 }
153
154 private void writeTracks() {
155 for (GpxTrack trk : data.tracks) {
156 open("trk");
157 writeAttr(trk.attr);
158 for (Collection<WayPoint> seg : trk.trackSegs) {
159 openln("trkseg");
160 for (WayPoint pnt : seg) {
161 wayPoint(pnt, TRACK_POINT);
162 }
163 closeln("trkseg");
164 }
165 closeln("trk");
166 }
167 }
168
169 private void openln(String tag) {
170 open(tag);
171 out.println();
172 }
173
174 private void open(String tag) {
175 out.print(indent + "<" + tag + ">");
176 indent += " ";
177 }
178
179 private void openAtt(String tag, String attributes) {
180 out.println(indent + "<" + tag + " " + attributes + ">");
181 indent += " ";
182 }
183
184 private void inline(String tag, String attributes) {
185 out.println(indent + "<" + tag + " " + attributes + " />");
186 }
187
188 private void close(String tag) {
189 indent = indent.substring(2);
190 out.print(indent + "</" + tag + ">");
191 }
192
193 private void closeln(String tag) {
194 close(tag);
195 out.println();
196 }
197
198 /**
199 * if content not null, open tag, write encoded content, and close tag
200 * else do nothing.
201 */
202 private void simpleTag(String tag, String content) {
203 if (content != null && content.length() > 0) {
204 open(tag);
205 out.print(encode(content));
206 out.println("</" + tag + ">");
207 indent = indent.substring(2);
208 }
209 }
210
211 /**
212 * output link
213 */
214 private void gpxLink(GpxLink link) {
215 if (link != null) {
216 openAtt("link", "href=\"" + link.uri + "\"");
217 simpleTag("text", link.text);
218 simpleTag("type", link.type);
219 closeln("link");
220 }
221 }
222
223 /**
224 * output a point
225 */
226 private void wayPoint(WayPoint pnt, int mode) {
227 String type;
228 switch(mode) {
229 case WAY_POINT:
230 type = "wpt";
231 break;
232 case ROUTE_POINT:
233 type = "rtept";
234 break;
235 case TRACK_POINT:
236 type = "trkpt";
237 break;
238 default:
239 throw new RuntimeException(tr("Unknown mode {0}.", mode));
240 }
241 if (pnt != null) {
242 LatLon c = pnt.getCoor();
243 openAtt(type, "lat=\"" + c.lat() + "\" lon=\"" + c.lon() + "\"");
244 writeAttr(pnt.attr);
245 closeln(type);
246 }
247 }
248}
Note: See TracBrowser for help on using the repository browser.