| 1 |
package uk.me.parabola.util; |
| 2 |
|
| 3 |
import java.awt.Shape; |
| 4 |
import java.io.File; |
| 5 |
import java.io.FileWriter; |
| 6 |
import java.io.PrintWriter; |
| 7 |
import java.util.Arrays; |
| 8 |
import java.util.Collection; |
| 9 |
import java.util.List; |
| 10 |
|
| 11 |
import uk.me.parabola.imgfmt.Utils; |
| 12 |
import uk.me.parabola.imgfmt.app.Area; |
| 13 |
import uk.me.parabola.imgfmt.app.Coord; |
| 14 |
import uk.me.parabola.log.Logger; |
| 15 |
import uk.me.parabola.mkgmap.reader.osm.Way; |
| 16 |
|
| 17 |
public class GpxCreator { |
| 18 |
private static final Logger log = Logger.getLogger(GpxCreator.class); |
| 19 |
|
| 20 |
public static String getGpxBaseName() { |
| 21 |
String tilePath = (log.threadTag() == null ? "unknown" : log.threadTag()); |
| 22 |
|
| 23 |
int tilenameStart = tilePath.lastIndexOf('/'); |
| 24 |
|
| 25 |
tilenameStart = (tilenameStart < 0 ? 0 : tilenameStart+1); |
| 26 |
|
| 27 |
int tilenameEnd = tilePath.lastIndexOf(".osm"); |
| 28 |
if (tilenameEnd < tilenameStart) { |
| 29 |
|
| 30 |
|
| 31 |
tilenameEnd = tilePath.length(); |
| 32 |
} |
| 33 |
|
| 34 |
return tilePath.substring(tilenameStart,tilenameEnd) + "/"; |
| 35 |
} |
| 36 |
|
| 37 |
private static void addTrkPoint(PrintWriter pw, int latitude, int longitude) { |
| 38 |
addGpxPoint(pw, "trkpt", latitude, longitude); |
| 39 |
} |
| 40 |
|
| 41 |
private static void addWptPoint(PrintWriter pw, int latitude, int longitude) { |
| 42 |
addGpxPoint(pw, "wpt", latitude, longitude); |
| 43 |
} |
| 44 |
private static void addTrkPoint(PrintWriter pw, Coord co) { |
| 45 |
addGpxPoint(pw, "trkpt", co); |
| 46 |
} |
| 47 |
|
| 48 |
private static void addWptPoint(PrintWriter pw, Coord co) { |
| 49 |
addGpxPoint(pw, "wpt", co); |
| 50 |
} |
| 51 |
|
| 52 |
private static void addGpxPoint(PrintWriter pw, String type, int latitude, |
| 53 |
int longitude) { |
| 54 |
pw.print("<"); |
| 55 |
pw.print(type); |
| 56 |
pw.print(" lat=\""); |
| 57 |
pw.print(Utils.toDegrees(latitude)); |
| 58 |
pw.print("\" lon=\""); |
| 59 |
pw.print(Utils.toDegrees(longitude)); |
| 60 |
pw.print("\"/>"); |
| 61 |
} |
| 62 |
|
| 63 |
private static void addGpxPoint(PrintWriter pw, String type, Coord co) { |
| 64 |
pw.print("<"); |
| 65 |
pw.print(type); |
| 66 |
pw.print(" lat=\""); |
| 67 |
pw.print(co.getLatDegrees()); |
| 68 |
pw.print("\" lon=\""); |
| 69 |
pw.print(co.getLonDegrees()); |
| 70 |
pw.print("\"/>"); |
| 71 |
} |
| 72 |
|
| 73 |
public static void createAreaGpx(String name, Area bbox) { |
| 74 |
GpxCreator.createGpx(name, bbox.toCoords()); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static void createShapeGpx(String baseDir, Shape shape) { |
| 83 |
|
| 84 |
java.awt.geom.Area area = shape instanceof java.awt.geom.Area ? (java.awt.geom.Area) shape |
| 85 |
: new java.awt.geom.Area(shape); |
| 86 |
List<List<Coord>> shapes = Java2DConverter.areaToShapes(area); |
| 87 |
for (int i = 0; i < shapes.size(); i++) { |
| 88 |
List<Coord> points = shapes.get(i); |
| 89 |
String extName = baseDir + Integer.toString(i) + "_" + (Way.clockwise(points) ? "o" : "i"); |
| 90 |
GpxCreator.createGpx(extName, points); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public static void createGpx(String baseDir, Collection<? extends Way> ways) { |
| 101 |
for (Way w : ways) { |
| 102 |
GpxCreator.createGpx(baseDir+w.getId(), w.getPoints()); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
public static void createGpx(String name, List<Coord> points) { |
| 107 |
for (int i = 0; i < 2; i++){ |
| 108 |
String fname = name + (i==0 ? "_mu":"_hp"); |
| 109 |
try { |
| 110 |
File f = new File(fname); |
| 111 |
if (f.getParentFile() != null) { |
| 112 |
f.getParentFile().mkdirs(); |
| 113 |
} |
| 114 |
PrintWriter pw = new PrintWriter(new FileWriter(fname + ".gpx")); |
| 115 |
pw.print("<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" creator=\"mkgmap\" "); |
| 116 |
pw.print("version=\"1.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "); |
| 117 |
pw.print("xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\"> "); |
| 118 |
|
| 119 |
pw.print("<trk><name>"); |
| 120 |
pw.print(fname); |
| 121 |
pw.print("</name><trkseg>"); |
| 122 |
|
| 123 |
for (Coord c : points) { |
| 124 |
if (i == 0) |
| 125 |
addTrkPoint(pw, c.getLatitude(), c.getLongitude()); |
| 126 |
else |
| 127 |
addTrkPoint(pw, c); |
| 128 |
} |
| 129 |
pw.print("</trkseg></trk>"); |
| 130 |
pw.print("</gpx>"); |
| 131 |
pw.close(); |
| 132 |
} catch (Exception exp) { |
| 133 |
|
| 134 |
log.warn("Could not create gpx file", fname); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
public static void createGpx(String name, List<Coord> polygonpoints, Coord... singlePoints) { |
| 140 |
createGpx(name, polygonpoints, Arrays.asList(singlePoints)); |
| 141 |
} |
| 142 |
|
| 143 |
public static void createGpx(String name, List<Coord> polygonpoints, List<Coord> singlePoints) { |
| 144 |
for (int i = 0; i < 2; i++){ |
| 145 |
String fname = name + (i==0 ? "_mu":"_hp"); |
| 146 |
try { |
| 147 |
File f = new File(fname); |
| 148 |
f.getParentFile().mkdirs(); |
| 149 |
PrintWriter pw = new PrintWriter(new FileWriter(fname + ".gpx")); |
| 150 |
pw.print("<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" creator=\"mkgmap\" "); |
| 151 |
pw.print("version=\"1.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "); |
| 152 |
pw.print("xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\"> "); |
| 153 |
|
| 154 |
if (singlePoints != null) { |
| 155 |
for (Coord c : singlePoints) { |
| 156 |
if (i == 0) |
| 157 |
addWptPoint(pw, c.getLatitude(), c.getLongitude()); |
| 158 |
else |
| 159 |
addWptPoint(pw, c); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if (polygonpoints != null && !polygonpoints.isEmpty()) { |
| 164 |
pw.print("<trk><name>"); |
| 165 |
pw.print(fname); |
| 166 |
pw.print("</name><trkseg>"); |
| 167 |
|
| 168 |
for (Coord c : polygonpoints) { |
| 169 |
if (i == 0) |
| 170 |
addTrkPoint(pw, c.getLatitude(), c.getLongitude()); |
| 171 |
else |
| 172 |
addTrkPoint(pw, c); |
| 173 |
} |
| 174 |
pw.print("</trkseg></trk>"); |
| 175 |
} |
| 176 |
pw.print("</gpx>"); |
| 177 |
pw.close(); |
| 178 |
} catch (Exception exp) { |
| 179 |
|
| 180 |
log.warn("Could not create gpx file", fname); |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
} |