1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
|
package tim.prune.save.xml;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import tim.prune.GpsPrune;
import tim.prune.data.Coordinate;
import tim.prune.data.DataPoint;
import tim.prune.data.Field;
import tim.prune.data.FieldGpx;
import tim.prune.data.FileInfo;
import tim.prune.data.MediaObject;
import tim.prune.data.Selection;
import tim.prune.data.Timestamp;
import tim.prune.data.Track;
import tim.prune.data.TrackInfo;
import tim.prune.data.UnitSetLibrary;
import tim.prune.gui.ProgressDialog;
import tim.prune.save.SettingsForExport;
/**
* Class to generate the Gpx contents for any kind of GPX output
* (whether to file or not)
*/
public abstract class GpxWriter
{
private final ProgressDialog _progress;
private final SettingsForExport _settings;
private int _selectionStart = -1, _selectionEnd = -1;
/** this program name */
protected static final String GPX_CREATOR = "GpsPrune v" + GpsPrune.VERSION_NUMBER + " activityworkshop.net";
/** Constructor */
public GpxWriter(ProgressDialog inProgress, SettingsForExport inSettings)
{
_progress = inProgress;
_settings = inSettings;
}
/** @return the default gpx header string without extensions */
protected abstract String getDefaultGpxHeader();
/** @return the default gpx header string with extensions from the loaded files */
protected abstract String getGpxHeader(FileInfo inInfo);
/**
* Export the information to the given writer
* @param inWriter writer object
* @param inInfo track info object
* @param inName name of track (optional)
* @param inDesc description of track (optional)
* @return number of points written
* @throws IOException if io errors occur on write
*/
public int exportData(OutputStreamWriter inWriter, TrackInfo inInfo, String inName,
String inDesc, FileInfo inFileInfo) throws IOException
{
// Write or copy headers
inWriter.write(getXmlHeaderString(inWriter));
final String gpxHeader = getGpxHeaderString(inFileInfo);
inWriter.write(gpxHeader);
// name and description
String trackName = (isEmpty(inName) ? "GpsPruneTrack" : XmlUtils.fixCdata(inName));
String desc = (isEmpty(inDesc) ? "Export from GpsPrune" : XmlUtils.fixCdata(inDesc));
writeMetadata(inWriter, trackName, desc);
setSelectionRange(inInfo.getSelection());
Track track = inInfo.getTrack();
final int numPoints = track.getNumPoints();
if (_progress != null) {
_progress.setMaximumValue(numPoints);
}
int numSaved = 0;
// Export waypoints
if (_settings.getExportWaypoints()) {
numSaved += writeWaypoints(inWriter, track);
}
// Export both route points and then track points
if (_settings.getExportTrackPoints() || _settings.getExportPhotoPoints() || _settings.getExportAudioPoints())
{
// Output track points, if any
numSaved += writeTrackPoints(inWriter, track, trackName);
}
inWriter.write("</gpx>\n");
return numSaved;
}
private void setSelectionRange(Selection inSelection)
{
_selectionStart = _settings.getExportJustSelection() ? inSelection.getStart() : -1;
_selectionEnd = _settings.getExportJustSelection() ? inSelection.getEnd() : -1;
}
private boolean shouldExportIndex(int inIndex)
{
if (_selectionStart == -1 || _selectionEnd == -1) {
return true;
}
return inIndex >= _selectionStart && inIndex <= _selectionEnd;
}
/** Write the metadata including name and description (depends on file version) */
protected abstract void writeMetadata(OutputStreamWriter inWriter,
String inName, String inDesc) throws IOException;
/**
* Write the name and description according to the GPX version number
* @param inWriter writer object
* @param inName name, or null if none supplied
* @param inDesc description, or null if none supplied
* @param inIndent indentation string to use
*/
protected static void writeNameAndDescription(OutputStreamWriter inWriter,
String inName, String inDesc, String inIndent) throws IOException
{
if (!isEmpty(inName))
{
inWriter.write(inIndent);
inWriter.write("<name>");
inWriter.write(inName);
inWriter.write("</name>\n");
}
inWriter.write(inIndent);
inWriter.write("<desc>");
inWriter.write(inDesc);
inWriter.write("</desc>\n");
}
/**
* Get the header string for the xml document including encoding
* @param inWriter writer object
* @return header string defining encoding
*/
private static String getXmlHeaderString(OutputStreamWriter inWriter)
{
return "<?xml version=\"1.0\" encoding=\"" + XmlUtils.getEncoding(inWriter) + "\"?>\n";
}
/**
* Get the header string for the gpx tag
* @return header string from existing tags or from the default
*/
private String getGpxHeaderString(FileInfo inInfo)
{
String gpxHeader = null;
if (inInfo != null)
{
// Build header according to selected version _and_ the existing extensions, if any
gpxHeader = getGpxHeader(inInfo);
}
if (gpxHeader == null || gpxHeader.length() < 5) {
gpxHeader = getDefaultGpxHeader();
}
return gpxHeader + "\n";
}
/** @return the number of waypoints written */
private int writeWaypoints(Writer inWriter, Track inTrack)
throws IOException
{
// Loop over waypoints
final int numPoints = inTrack.getNumPoints();
int numSaved = 0;
for (int i=0; i<numPoints; i++)
{
if (!shouldExportIndex(i)) {
continue;
}
DataPoint point = inTrack.getPoint(i);
if (!point.isWaypoint()) {
continue;
}
if (_progress != null)
{
if (_progress.wasCancelled()) {
return -1;
}
_progress.setValue(i);
}
// Make a wpt element for each waypoint
exportWaypoint(point, inWriter);
numSaved++;
}
return numSaved;
}
/**
* Export the specified waypoint into the file
* @param inPoint waypoint to export
* @param inWriter writer object
* @throws IOException on write failure
*/
private void exportWaypoint(DataPoint inPoint, Writer inWriter)
throws IOException
{
inWriter.write("\t<wpt lat=\"");
inWriter.write(inPoint.getLatitude().output(Coordinate.Format.DECIMAL_FORCE_POINT));
inWriter.write("\" lon=\"");
inWriter.write(inPoint.getLongitude().output(Coordinate.Format.DECIMAL_FORCE_POINT));
inWriter.write("\">\n");
// altitude if available
if (inPoint.hasAltitude() || _settings.getExportMissingAltitudesAsZero())
{
inWriter.write("\t\t<ele>");
inWriter.write(inPoint.hasAltitude() ? inPoint.getAltitude().getStringValue(UnitSetLibrary.UNITS_METRES) : "0");
inWriter.write("</ele>\n");
}
// timestamp if available (some waypoints have timestamps, some not)
if (_settings.getExportTimestamps())
{
Timestamp waypointTimestamp = getPointTimestamp(inPoint);
if (waypointTimestamp != null && waypointTimestamp.isValid())
{
inWriter.write("\t\t<time>");
inWriter.write(getPointTimestamp(inPoint).getText(Timestamp.Format.ISO8601, null));
inWriter.write("</time>\n");
}
}
// magvar, geoidheight
for (FieldGpx field : FieldGpx.getFirstFields()) {
writeGpxTag(inWriter, "\t\t", inPoint, field);
}
// write waypoint name after elevation and time
inWriter.write("\t\t<name>");
inWriter.write(XmlUtils.fixCdata(inPoint.getWaypointName().trim()));
inWriter.write("</name>\n");
// comment, if any
String comment = XmlUtils.fixCdata(inPoint.getFieldValue(Field.COMMENT));
final String desc = XmlUtils.fixCdata(inPoint.getFieldValue(Field.DESCRIPTION));
if (isEmpty(comment) && _settings.getCopyDescriptionsToComments()) {
comment = desc;
}
if (!isEmpty(comment))
{
inWriter.write("\t\t<cmt>");
inWriter.write(comment);
inWriter.write("</cmt>\n");
}
// description, if any
if (!isEmpty(desc))
{
inWriter.write("\t\t<desc>");
inWriter.write(desc);
inWriter.write("</desc>\n");
}
if (versionSupportsPointLinks())
{
// Media links, if any
if (_settings.getExportPhotoPoints() && inPoint.getPhoto() != null)
{
inWriter.write("\t\t");
inWriter.write(makeMediaLink(inPoint.getPhoto()));
inWriter.write('\n');
}
if (_settings.getExportAudioPoints() && inPoint.getAudio() != null)
{
inWriter.write("\t\t");
inWriter.write(makeMediaLink(inPoint.getAudio()));
inWriter.write('\n');
}
}
// symbol, if any
final String symbol = XmlUtils.fixCdata(inPoint.getFieldValue(Field.SYMBOL));
if (!isEmpty(symbol))
{
inWriter.write("\t\t<sym>");
inWriter.write(symbol);
inWriter.write("</sym>\n");
}
// write waypoint type if any
String type = inPoint.getFieldValue(Field.WAYPT_TYPE);
if (!isEmpty(type))
{
type = type.trim();
if (!type.equals(""))
{
inWriter.write("\t\t<type>");
inWriter.write(type);
inWriter.write("</type>\n");
}
}
// fix, sat, hdop, vdop, pdop, ageofdgpsdata, dgpsid
for (FieldGpx field : FieldGpx.getSecondFields()) {
writeGpxTag(inWriter, "\t\t", inPoint, field);
}
exportWaypointExtensions(inPoint, inWriter);
inWriter.write("\t</wpt>\n");
}
/** @return true if the output version supports the link tag for points */
protected abstract boolean versionSupportsPointLinks();
/** Export the extension tags from the given waypoint to the writer */
protected abstract void exportWaypointExtensions(DataPoint inPoint, Writer inWriter) throws IOException;
/** Export the extension tags from the given trackpoint to the writer */
protected abstract void exportTrackpointExtensions(DataPoint inPoint, Writer inWriter) throws IOException;
/**
* Loop through the track outputting the relevant track points
* @param inWriter writer object for output
* @param inTrack track object
* @param inTrackName name of track
*/
private int writeTrackPoints(OutputStreamWriter inWriter, Track inTrack, String inTrackName)
throws IOException
{
int numPoints = inTrack.getNumPoints();
int numSaved = 0;
final boolean exportTrackPoints = _settings.getExportTrackPoints();
final boolean exportPhotos = _settings.getExportPhotoPoints();
final boolean exportAudios = _settings.getExportAudioPoints();
// Loop over track points
for (int i=0; i<numPoints; i++)
{
if (!shouldExportIndex(i)) {
continue;
}
DataPoint point = inTrack.getPoint(i);
if (point.isWaypoint()) {
continue;
}
if (_progress != null) {
_progress.setValue(i);
}
if ((point.getPhoto()==null && exportTrackPoints) || (point.getPhoto()!=null && exportPhotos)
|| (point.getAudio()!=null && exportAudios))
{
// restart track segment if necessary
if ((numSaved > 0) && point.getSegmentStart()) {
inWriter.write("\t\t</trkseg>\n\t\t<trkseg>\n");
}
if (numSaved == 0)
{
String trackStart = "\t<trk>\n\t\t<name>" + inTrackName + "</name>\n\t\t<number>1</number>\n\t\t<trkseg>\n";
inWriter.write(trackStart);
}
exportTrackpoint(point, inWriter);
numSaved++;
}
}
if (numSaved > 0) {
inWriter.write("\t\t</trkseg>\n\t</trk>\n");
}
return numSaved;
}
/**
* Export the specified trackpoint into the file
* @param inPoint trackpoint to export
* @param inWriter writer object
*/
private void exportTrackpoint(DataPoint inPoint, Writer inWriter)
throws IOException
{
inWriter.write("\t\t\t<trkpt lat=\"");
inWriter.write(inPoint.getLatitude().output(Coordinate.Format.DECIMAL_FORCE_POINT));
inWriter.write("\" lon=\"");
inWriter.write(inPoint.getLongitude().output(Coordinate.Format.DECIMAL_FORCE_POINT));
inWriter.write("\">\n");
// altitude
if (inPoint.hasAltitude() || _settings.getExportMissingAltitudesAsZero())
{
inWriter.write("\t\t\t\t<ele>");
inWriter.write(inPoint.hasAltitude() ? inPoint.getAltitude().getStringValue(UnitSetLibrary.UNITS_METRES) : "0");
inWriter.write("</ele>\n");
}
// Maybe take timestamp from photo if the point hasn't got one
Timestamp pointTimestamp = getPointTimestamp(inPoint);
// timestamp if available (and selected)
if (pointTimestamp != null && _settings.getExportTimestamps())
{
inWriter.write("\t\t\t\t<time>");
inWriter.write(pointTimestamp.getText(Timestamp.Format.ISO8601, null));
inWriter.write("</time>\n");
}
for (FieldGpx field : FieldGpx.getFirstFields()) {
writeGpxTag(inWriter, "\t\t\t\t", inPoint, field);
}
for (FieldGpx field : FieldGpx.getSecondFields()) {
writeGpxTag(inWriter, "\t\t\t\t", inPoint, field);
}
// photo, audio
if (inPoint.getPhoto() != null && _settings.getExportPhotoPoints())
{
inWriter.write("\t\t\t\t");
inWriter.write(makeMediaLink(inPoint.getPhoto()));
inWriter.write("\n");
}
if (inPoint.getAudio() != null && _settings.getExportAudioPoints()) {
inWriter.write(makeMediaLink(inPoint.getAudio()));
}
exportTrackpointExtensions(inPoint, inWriter);
inWriter.write("\t\t\t</trkpt>\n");
}
private void writeGpxTag(Writer inWriter, String inIndent, DataPoint inPoint, FieldGpx field)
throws IOException
{
String value = inPoint.getFieldValue(field);
if (value != null && versionSupportsTag(field))
{
inWriter.write(inIndent);
inWriter.write(field.getOpenTag());
inWriter.write(value);
inWriter.write(field.getCloseTag());
inWriter.write('\n');
}
}
/** @return true if this Gpx format supports the specified tag */
protected abstract boolean versionSupportsTag(FieldGpx inField);
/**
* Get the timestamp from the point or its media
* @param inPoint point object
* @return Timestamp object if available, or null
*/
private Timestamp getPointTimestamp(DataPoint inPoint)
{
if (inPoint.hasTimestamp()) {
return inPoint.getTimestamp();
}
if (inPoint.getPhoto() != null && _settings.getExportPhotoPoints())
{
if (inPoint.getPhoto().hasTimestamp()) {
return inPoint.getPhoto().getTimestamp();
}
}
if (inPoint.getAudio() != null && _settings.getExportAudioPoints())
{
if (inPoint.getAudio().hasTimestamp()) {
return inPoint.getAudio().getTimestamp();
}
}
return null;
}
/** @return true if the given string is empty */
protected static boolean isEmpty(String inString) {
return inString == null || inString.isEmpty();
}
/** @return xml fragment linking to the file or Url for the given media */
protected abstract String makeMediaLink(MediaObject inMedia);
}
|