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

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