Changeset 1114 in josm for trunk/src/org
- Timestamp:
- 2008-12-12T00:10:10+01:00 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/GpxWriter.java
r872 r1114 2 2 package org.openstreetmap.josm.io; 3 3 4 import java.io.BufferedWriter; 5 import java.io.OutputStream; 6 import java.io.OutputStreamWriter; 4 7 import java.io.PrintWriter; 5 import java.io. OutputStream;8 import java.io.UnsupportedEncodingException; 6 9 import java.util.Collection; 7 10 import java.util.Map; 8 11 9 12 import org.openstreetmap.josm.data.Bounds; 10 11 13 import org.openstreetmap.josm.data.gpx.GpxData; 14 import org.openstreetmap.josm.data.gpx.GpxLink; 15 import org.openstreetmap.josm.data.gpx.GpxRoute; 12 16 import org.openstreetmap.josm.data.gpx.GpxTrack; 13 import org.openstreetmap.josm.data.gpx.GpxRoute;14 import org.openstreetmap.josm.data.gpx.GpxLink;15 17 import org.openstreetmap.josm.data.gpx.WayPoint; 16 18 … … 20 22 public class GpxWriter extends XmlWriter { 21 23 22 23 24 25 26 public GpxWriter(OutputStream out){27 super(new PrintWriter(out));28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 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.print("\n"); 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.print("\n"); 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 } 204 206 }
Note:
See TracChangeset
for help on using the changeset viewer.