Changeset 1114 in josm
- Timestamp:
- 2008-12-12T00:10:10+01:00 (17 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 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 works33 }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 }54 55 private void writeAttr(Map<String, Object> attr) {56 boolean hasAuthor = false;57 for (Map.Entry<String, Object> ent : attr.entrySet()) {58 String k = ent.getKey();59 if (k.indexOf("author") == 0) {60 hasAuthor = true;61 } else if (k.equals("link")) {62 for (GpxLink link : (Collection<GpxLink>) ent.getValue()) {63 gpxLink(link);64 }65 } else {66 simpleTag(k, (String) ent.getValue());67 }68 }69 70 if (hasAuthor) {71 open("author");72 simpleTag("name", (String) attr.get("authorname"));73 simpleTag("email", (String) attr.get("authoremail"));74 gpxLink((GpxLink) attr.get("authorlink"));75 closeln("author");76 }77 78 // TODO: copyright79 }80 81 private void writeMetaData() {82 openln("metadata");83 writeAttr(data.attr);84 85 data.recalculateBounds();86 Bounds bounds = data.bounds;87 String b = "minlat=\"" + bounds.min.lat() + "\" minlon=\"" + bounds.min.lon() +88 "\" maxlat=\"" + bounds.max.lat() + "\" maxlon=\"" + bounds.max.lon() + "\"" ;89 inline("bounds", b);90 91 closeln("metadata");92 }93 94 private void writeWayPoints() {95 for (WayPoint pnt : data.waypoints) {96 wayPoint(pnt, WAY_POINT);97 }98 }99 100 private void writeRoutes() {101 for (GpxRoute rte : data.routes) {102 openln("rte");103 writeAttr(rte.attr);104 for (WayPoint pnt : rte.routePoints) {105 wayPoint(pnt, ROUTE_POINT);106 }107 closeln("rte");108 }109 }110 111 private void writeTracks() {112 for (GpxTrack trk : data.tracks) {113 open("trk");114 writeAttr(trk.attr);115 for (Collection<WayPoint> seg : trk.trackSegs) {116 openln("trkseg");117 for (WayPoint pnt : seg) {118 wayPoint(pnt, TRACK_POINT);119 }120 closeln("trkseg");121 }122 closeln("trk");123 }124 }125 126 private void openln(String tag) {127 open(tag);128 out.print("\n");129 }130 131 private void open(String tag) {132 out.print(indent + "<" + tag + ">");133 indent += " ";134 }135 136 private void openAtt(String tag, String attributes) {137 out.println(indent + "<" + tag + " " + attributes + ">");138 indent += " ";139 }140 141 private void inline(String tag, String attributes) {142 out.println(indent + "<" + tag + " " + attributes + " />");143 }144 145 private void close(String tag) {146 indent = indent.substring(2);147 out.print(indent + "</" + tag + ">");148 }149 150 private void closeln(String tag) {151 close(tag);152 out.print("\n");153 }154 155 /**156 * if content not null, open tag, write encoded content, and close tag157 * else do nothing.158 */159 private void simpleTag(String tag, String content) {160 if (content != null && content.length() > 0) {161 open(tag);162 out.print(encode(content));163 out.println("</" + tag + ">");164 indent = indent.substring(2);165 }166 }167 168 /**169 * output link170 */171 private void gpxLink(GpxLink link) {172 if (link != null) {173 openAtt("link", "href=\"" + link.uri + "\"");174 simpleTag("text", link.text);175 simpleTag("type", link.type);176 closeln("link");177 }178 }179 180 /**181 * output a point182 */183 private void wayPoint(WayPoint pnt, int mode) {184 String type;185 switch(mode) {186 case WAY_POINT:187 type = "wpt";188 break;189 case ROUTE_POINT:190 type = "rtept";191 break;192 case TRACK_POINT:193 type = "trkpt";194 break;195 default:196 throw new RuntimeException("Bug detected. Please report this!");197 }198 if (pnt != null) {199 openAtt(type, "lat=\"" + pnt.latlon.lat() + "\" lon=\"" + pnt.latlon.lon() + "\"");200 writeAttr(pnt.attr);201 closeln(type);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.
