├── .travis.yml ├── .gitignore ├── ChangeLog ├── test ├── gpx │ ├── two-segments.gpx │ ├── only-wp.gpx │ ├── simple-route.gpx │ ├── simple-track.gpx │ └── simple-track-garmin-etrex.gpx └── j4gpx │ └── GPXText.java ├── .project ├── README.md ├── .classpath ├── src └── com │ └── urizev │ └── gpx │ ├── GPXParserOptions.java │ ├── beans │ ├── RoutePoint.java │ ├── TrackPoint.java │ ├── Extension.java │ ├── GPX.java │ ├── Route.java │ ├── Track.java │ └── Waypoint.java │ ├── types │ └── FixType.java │ ├── extensions │ ├── DummyExtensionParser.java │ └── IExtensionParser.java │ ├── GPXConstants.java │ └── GPXParser.java ├── pom.xml └── license.txt /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.class 3 | .settings 4 | # Package Files # 5 | *.jar 6 | *.war 7 | *.ear 8 | /bin/ 9 | /target/ 10 | .DS_Store 11 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | ### j4gpx-0.2 (2015-05-19) 2 | * Track segment support. A segment is an list of waypoints. After parsing a segment adds all its waypoints to the track list waypoints and add a new segment to the track segment list. 3 | * Add minimal test suite (needs to be completed) 4 | 5 | ### j4gpx-0.1 (2014-01-10) 6 | * Initial release -------------------------------------------------------------------------------- /test/gpx/two-segments.gpx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | j4gpx 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.m2e.core.maven2Nature 21 | org.eclipse.jdt.core.javanature 22 | 23 | 24 | -------------------------------------------------------------------------------- /test/gpx/only-wp.gpx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Test file by Patrick 5 | 6 | 7 | 0.0 8 | Position 1 9 | 10 | 11 | 0.0 12 | Position 2 13 | 14 | 15 | 0.0 16 | Position 3 17 | 18 | 19 | 0.0 20 | Position 4 21 | 22 | -------------------------------------------------------------------------------- /test/gpx/simple-route.gpx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Test file by Patrick 5 | 6 | 7 | Patrick's Route 8 | 9 | 0.0 10 | Position 1 11 | 12 | 13 | 0.0 14 | Position 2 15 | 16 | 17 | 0.0 18 | Position 3 19 | 20 | 21 | 0.0 22 | Position 4 23 | 24 | 25 | -------------------------------------------------------------------------------- /test/gpx/simple-track.gpx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Test file by Patrick 5 | 6 | 7 | Patrick's Track 8 | 9 | 10 | 0.0 11 | Position 1 12 | 13 | 14 | 0.0 15 | Position 2 16 | 17 | 18 | 0.0 19 | Position 3 20 | 21 | 22 | 0.0 23 | Position 4 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # j4gpx 2 | 3 | A simple GPX reader/writer for Java with non external dependencies and compatible with Android. Based on the work GPXParser by ghitabot (http://sourceforge.net/projects/gpxparser/) 4 | 5 | [![Build Status](https://travis-ci.org/urizev/j4gpx.svg?branch=master)](https://travis-ci.org/urizev/j4gpx) 6 | [![License](https://img.shields.io/badge/License-LGPL-brightgreen.svg)](license.txt) 7 | [![Release](https://img.shields.io/github/release/jitpack/gradle-simple.svg?label=maven)](https://jitpack.io/#urizev/j4gpx) 8 | ### Releases 9 | 10 | ###### j4gpx-0.3 (2021-07-06) 11 | * Security fix caused by vulnerable version of junit. 12 | * GPXParser support writing with indentation 13 | * Bug fix writing GPX with no waypoints 14 | * Minor fix 15 | 16 | Thanks to @reciprocum 17 | 18 | ###### j4gpx-0.2 (2015-05-19) 19 | * Track segment support. A segment is an list of waypoints. After parsing a segment adds all its waypoints to the track list waypoints and add a new segment to the track segment list. 20 | * Add minimal test suite (needs to be completed) 21 | 22 | ###### j4gpx-0.1 (2014-01-10) 23 | * Initial release 24 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /src/com/urizev/gpx/GPXParserOptions.java: -------------------------------------------------------------------------------- 1 | package com.urizev.gpx; 2 | 3 | public class 4 | GPXParserOptions 5 | { 6 | public final boolean skipWaypoints ; 7 | public final boolean skipRoutes ; 8 | public final boolean skipTracks ; 9 | 10 | public static final GPXParserOptions PARSE_ALL = new GPXParserOptions( false, false, false ) ; 11 | public static final GPXParserOptions SKIP_WAYPOINTS = new GPXParserOptions( true , false, false ) ; 12 | public static final GPXParserOptions SKIP_ROUTES = new GPXParserOptions( false, true , false ) ; 13 | public static final GPXParserOptions SKIP_TRACKS = new GPXParserOptions( false, false, true ) ; 14 | public static final GPXParserOptions ONLY_WAYPOINTS = new GPXParserOptions( false, true , true ) ; 15 | public static final GPXParserOptions ONLY_ROUTES = new GPXParserOptions( true , false, true ) ; 16 | public static final GPXParserOptions ONLY_TRACKS = new GPXParserOptions( true , true , false ) ; 17 | 18 | public 19 | GPXParserOptions( final boolean skipWaypoints, final boolean skipRoutes, final boolean skipTracks ) 20 | { 21 | super( ) ; 22 | this.skipWaypoints = skipWaypoints ; 23 | this.skipRoutes = skipRoutes ; 24 | this.skipTracks = skipTracks ; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | com.urizev.j4lsi 4 | j4gpx 5 | 0.3-SNAPSHOT 6 | j4lsi 7 | A simple GPX reader/writer for Java with non external dependencies and compatible with Android 8 | 9 | src 10 | test 11 | 12 | 13 | test 14 | 15 | **/*.java 16 | 17 | 18 | 19 | 20 | 21 | maven-compiler-plugin 22 | 3.1 23 | 24 | 1.7 25 | 1.7 26 | 27 | 28 | 29 | 30 | 31 | 32 | junit 33 | junit 34 | 4.13.1 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /src/com/urizev/gpx/beans/RoutePoint.java: -------------------------------------------------------------------------------- 1 | /* 2 | * RoutePoint.java 3 | * 4 | * Copyright (c) 2012, AlternativeVision. All rights reserved. 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301 USA 20 | */ 21 | 22 | package com.urizev.gpx.beans; 23 | 24 | /** 25 | * Defines additional route point information that {@link Waypoint} doesn't 26 | * define.
27 | * It is currently not used. 28 | */ 29 | public class RoutePoint extends Waypoint { 30 | 31 | public RoutePoint(String name, float latitude, float longitude) { 32 | super(name, latitude, longitude); 33 | // TODO Auto-generated constructor stub 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/com/urizev/gpx/beans/TrackPoint.java: -------------------------------------------------------------------------------- 1 | /* 2 | * TrackPoint.java 3 | * 4 | * Copyright (c) 2012, AlternativeVision. All rights reserved. 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301 USA 20 | */ 21 | 22 | package com.urizev.gpx.beans; 23 | 24 | /** 25 | * Defines additional track point information that {@link Waypoint} doesn't 26 | * define.
27 | * It is currently not used. 28 | */ 29 | public class TrackPoint extends Waypoint { 30 | 31 | public TrackPoint(String name, float latitude, float longitude) { 32 | super(name, latitude, longitude); 33 | // TODO Auto-generated constructor stub 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /test/gpx/simple-track-garmin-etrex.gpx: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | Garmin International 9 | 10 | 11 | 12 | 13 | 14 | 15 | 24-JUL-09 16 | 17 | 18 | DarkRed 19 | 20 | 21 | 22 | 23 | 143.94 24 | 25 | 26 | 27 | 136.06 28 | 29 | 30 | 31 | 138.03 32 | 33 | 34 | 35 | 148.75 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /src/com/urizev/gpx/types/FixType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * FixType.java 3 | * 4 | * Copyright (c) 2012, AlternativeVision. All rights reserved. 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301 USA 20 | */ 21 | 22 | package com.urizev.gpx.types; 23 | /** 24 | * 25 | *

Type of GPS fix. Value comes from list: {'none'|'2d'|'3d'|'dgps'|'pps'}

26 | *
27 | * 31 | *

To signify "the fix info is unknown", leave out fixType entirely.

32 | * 33 | */ 34 | public class FixType { 35 | 36 | private String value; 37 | 38 | private FixType(String value) { 39 | this.value = value; 40 | } 41 | 42 | public String getValue() { 43 | return value; 44 | } 45 | 46 | public static FixType returnType(String value) { 47 | if(NONE.getValue().equals(value)) { 48 | return NONE; 49 | } else if(TWO_D.getValue().equals(value)) { 50 | return TWO_D; 51 | } else if(THREE_D.getValue().equals(value)) { 52 | return THREE_D; 53 | } else if(DGPS.getValue().equals(value)) { 54 | return DGPS; 55 | } else if(PPS.getValue().equals(value)) { 56 | return PPS; 57 | } 58 | return null; 59 | } 60 | 61 | //'none'|'2d'|'3d'|'dgps'|'pps' 62 | 63 | /** 64 | * Constant that defines 'none' Fix type 65 | */ 66 | public static FixType NONE = new FixType("none"); 67 | /** 68 | * Constant that defines '2d' Fix type 69 | */ 70 | public static FixType TWO_D = new FixType("2d"); 71 | /** 72 | * Constant that defines '3d' Fix type 73 | */ 74 | public static FixType THREE_D = new FixType("3d"); 75 | /** 76 | * Constant that defines 'dgps' Fix type 77 | */ 78 | public static FixType DGPS = new FixType("dgps"); 79 | /** 80 | * Constant that defines 'pps' Fix type 81 | */ 82 | public static FixType PPS = new FixType("pps"); 83 | 84 | public String toString() { 85 | return value; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/com/urizev/gpx/extensions/DummyExtensionParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BasicExtensionParser.java 3 | * 4 | * Copyright (c) 2012, AlternativeVision. All rights reserved. 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301 USA 20 | */ 21 | 22 | package com.urizev.gpx.extensions; 23 | 24 | import org.w3c.dom.Document; 25 | import org.w3c.dom.Node; 26 | 27 | import com.urizev.gpx.beans.GPX; 28 | import com.urizev.gpx.beans.Route; 29 | import com.urizev.gpx.beans.Track; 30 | import com.urizev.gpx.beans.Waypoint; 31 | 32 | public class DummyExtensionParser implements IExtensionParser { 33 | 34 | public String getId() { 35 | return "Basic Extension Parser"; 36 | } 37 | 38 | public Object parseWaypointExtension(Node node) { 39 | return "Parsed Waypoint data"; 40 | } 41 | 42 | public Object parseTrackExtension(Node node) { 43 | return "Parsed Track data"; 44 | } 45 | 46 | public Object parseGPXExtension(Node node) { 47 | return "Parsed GPX data"; 48 | } 49 | 50 | public Object parseRouteExtension(Node node) { 51 | return "Parsed Route data"; 52 | } 53 | 54 | public void writeGPXExtensionData(Node node, GPX wpt, Document doc) { 55 | // TODO Auto-generated method stub 56 | } 57 | 58 | public void writeWaypointExtensionData(Node node, Waypoint wpt, Document doc) { 59 | Node sampleNode = doc.createElement("mySampleExtension"); 60 | sampleNode.setNodeValue("mySampleWaypointValue"); 61 | node.appendChild(sampleNode); 62 | } 63 | 64 | public void writeTrackExtensionData(Node node, Track wpt, Document doc) { 65 | Node sampleNode = doc.createElement("mySampleExtension"); 66 | sampleNode.setNodeValue("mySampleTrackValue"); 67 | node.appendChild(sampleNode); 68 | 69 | } 70 | 71 | public void writeRouteExtensionData(Node node, Route wpt, Document doc) { 72 | Node sampleNode = doc.createElement("mySampleExtension"); 73 | sampleNode.setNodeValue("mySampleRouteValue"); 74 | node.appendChild(sampleNode); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/com/urizev/gpx/GPXConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * GPXConstants.java 3 | * 4 | * Copyright (c) 2012, AlternativeVision. All rights reserved. 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301 USA 20 | */ 21 | 22 | package com.urizev.gpx; 23 | 24 | public interface GPXConstants { 25 | /*GPX nodes and attributes*/ 26 | public static final String GPX_NODE = "gpx"; 27 | public static final String WPT_NODE = "wpt"; 28 | public static final String TRK_NODE = "trk"; 29 | public static final String VERSION_ATTR = "version"; 30 | public static final String CREATOR_ATTR = "creator"; 31 | /*End GPX nodes and attributes*/ 32 | 33 | /*Waypoint nodes and attributes*/ 34 | public static final String LAT_ATTR = "lat"; 35 | public static final String LON_ATTR = "lon"; 36 | public static final String ELE_NODE = "ele"; 37 | public static final String TIME_NODE = "time"; 38 | public static final String NAME_NODE = "name"; 39 | public static final String CMT_NODE = "cmt"; 40 | public static final String DESC_NODE = "desc"; 41 | public static final String SRC_NODE = "src"; 42 | public static final String MAGVAR_NODE = "magvar"; 43 | public static final String GEOIDHEIGHT_NODE = "geoidheight"; 44 | public static final String LINK_NODE = "link"; 45 | public static final String SYM_NODE = "sym"; 46 | public static final String TYPE_NODE = "type"; 47 | public static final String FIX_NODE = "fix"; 48 | public static final String SAT_NODE = "sat"; 49 | public static final String HDOP_NODE = "hdop"; 50 | public static final String VDOP_NODE = "vdop"; 51 | public static final String PDOP_NODE = "pdop"; 52 | public static final String AGEOFGPSDATA_NODE = "ageofdgpsdata"; 53 | public static final String DGPSID_NODE = "dgpsid"; 54 | public static final String EXTENSIONS_NODE = "extensions"; 55 | /*End Waypoint nodes and attributes*/ 56 | 57 | /*Track nodes and attributes*/ 58 | public static final String NUMBER_NODE = "number"; 59 | public static final String TRKSEG_NODE = "trkseg"; 60 | public static final String TRKPT_NODE = "trkpt"; 61 | /*End Track nodes and attributes*/ 62 | 63 | /*Route Nodes*/ 64 | public static final String RTE_NODE = "rte"; 65 | public static final String RTEPT_NODE = "rtept"; 66 | /*End route nodes*/ 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/com/urizev/gpx/beans/Extension.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Extension.java 3 | * 4 | * Copyright (c) 2012, AlternativeVision. All rights reserved. 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301 USA 20 | */ 21 | 22 | package com.urizev.gpx.beans; 23 | 24 | import java.util.HashMap; 25 | 26 | /** 27 | * This class holds generic extension information from any node that can have extensions defined. 28 | *
29 | *

Any custom extension parser used when parsing a gpx file will return a custom object 30 | * instance that will be kept in properties defined in this class.

31 | *
32 | *

Multiple extension parsers can be used when parsing. Every extension parser defines 33 | * an unique id that will be used as a HashMap key for the parsed object values.

34 | */ 35 | public class Extension { 36 | protected HashMap extensionData; 37 | 38 | public HashMap getExtensionData() { 39 | return extensionData; 40 | } 41 | 42 | /** 43 | * Setter for extension HashMap. 44 | * @param extensionData - A HashMap with extensions parser ids as keys and parsed objects as values. 45 | */ 46 | public void setExtensionData(HashMap extensionData) { 47 | this.extensionData = extensionData; 48 | } 49 | 50 | 51 | /** 52 | * Adds a new parsed extension object into the extension data with the key set by parserId. 53 | * 54 | * @param parserId a unique key representing the id of he extension parser used. 55 | * @param data an object holding the parsed information. This can be any object type and it is 56 | * the extension parser's job to set it properly. 57 | */ 58 | public void addExtensionData(String parserId, Object data) { 59 | if(extensionData == null) { 60 | extensionData = new HashMap(); 61 | } 62 | extensionData.put(parserId, data); 63 | } 64 | 65 | /** 66 | * Returns the extension data parsed by the extension parser with id parserId 67 | * @param parserId a String representing the id of an extension parser 68 | * @return the extension data parsed by the extension parser with id parserId 69 | */ 70 | public Object getExtensionData(String parserId) { 71 | if(extensionData != null) { 72 | return extensionData.get(parserId); 73 | } else { 74 | return null; 75 | } 76 | } 77 | 78 | /** 79 | * Returns the number of extension data objects that are currently set. 80 | * @return the number of extension data objects that are currently set. 81 | */ 82 | public int getExtensionsParsed() { 83 | if(extensionData != null) { 84 | return extensionData.size(); 85 | } else { 86 | return 0; 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/com/urizev/gpx/extensions/IExtensionParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * IExtensionParser 3 | * 4 | * Copyright (c) 2012, AlternativeVision. All rights reserved. 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301 USA 20 | */ 21 | 22 | package com.urizev.gpx.extensions; 23 | 24 | import org.w3c.dom.Document; 25 | import org.w3c.dom.Node; 26 | 27 | import com.urizev.gpx.beans.GPX; 28 | import com.urizev.gpx.beans.Route; 29 | import com.urizev.gpx.beans.Track; 30 | import com.urizev.gpx.beans.Waypoint; 31 | 32 | /** 33 | * This interface defines extension parsers methods. 34 | *
35 | *

All custom extension parser must implement this interface.

36 | *

Any custom parser must be added to {@link com.urizev.gpx.GPXParser} as an extension parser 37 | * before parsing a gpx file, or writing a {@link GPX} to a file. This is done by 38 | * calling addExtensionParser() method of {@link com.urizev.gpx.GPXParser} 39 | *

{@link com.urizev.gpx.GPXParser} parseGPX method calls several methods from the registered 40 | * extension parsers added at different steps of processing:

41 | *
    42 | *
  • parseGPXExtension() for parsing <extensions> of a <gpx> node
  • 43 | *
  • parseTrackExtension() for parsing <extensions> of a <trk> node
  • 44 | *
  • parseRouteExtension() for parsing <extensions> of a <rte> node
  • 45 | *
  • parseWaypointExtension() for parsing <extensions> of a <wpt> node
  • 46 | *
47 | *
48 | * 49 | *

{@link com.urizev.gpx.GPXParser} writeGPX method also calls several methods from the registered 50 | * extensions parsers at different steps of writing data:

51 | *
    52 | *
  • writeGPXExtensionData() when writing the <extensions> from the {@link GPX}
  • 53 | *
  • writeTrackExtensionData() when writing the <extensions> from the {@link Track}
  • 54 | *
  • writeRouteExtensionData() when writing the <extensions> from the {@link Route}
  • 55 | *
  • writeWaypointExtensionData() when writing the <extensions> from the {@link Waypoint}
  • 56 | *
57 | */ 58 | public interface IExtensionParser { 59 | 60 | public String getId(); 61 | 62 | public Object parseWaypointExtension(Node node); 63 | 64 | public Object parseTrackExtension(Node node); 65 | 66 | public Object parseGPXExtension(Node node); 67 | 68 | public Object parseRouteExtension(Node node); 69 | 70 | public void writeGPXExtensionData(Node node, GPX wpt, Document doc); 71 | 72 | public void writeWaypointExtensionData(Node node, Waypoint wpt, Document doc); 73 | 74 | public void writeTrackExtensionData(Node node, Track wpt, Document doc); 75 | 76 | public void writeRouteExtensionData(Node node, Route wpt, Document doc); 77 | 78 | } 79 | -------------------------------------------------------------------------------- /test/j4gpx/GPXText.java: -------------------------------------------------------------------------------- 1 | package j4gpx; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.io.FileInputStream; 6 | import java.io.FileNotFoundException; 7 | import java.util.ArrayList; 8 | import java.util.HashSet; 9 | import java.util.Set; 10 | 11 | import org.junit.BeforeClass; 12 | import org.junit.Test; 13 | 14 | import com.urizev.gpx.GPXParser; 15 | import com.urizev.gpx.beans.GPX; 16 | import com.urizev.gpx.beans.Route; 17 | import com.urizev.gpx.beans.Track; 18 | import com.urizev.gpx.beans.Waypoint; 19 | 20 | public class GPXText { 21 | private static GPXParser parser; 22 | 23 | @BeforeClass 24 | public static void setUpBeforeClass() { 25 | parser = new GPXParser(); 26 | } 27 | 28 | @Test 29 | public void testTwoSegments() { 30 | try { 31 | GPX gpx = parser.parseGPX(new FileInputStream("test/gpx/two-segments.gpx")); 32 | assertEquals(0, gpx.getWaypoints().size()); 33 | 34 | Set tracks = gpx.getTracks(); 35 | assertEquals(1, tracks.size()); 36 | 37 | Track track = tracks.iterator().next(); 38 | ArrayList points = track.getTrackPoints(); 39 | assertEquals(3, points.size()); 40 | 41 | ArrayList> segments = track.getTrackSegments(); 42 | assertEquals(2, segments.size()); 43 | 44 | } catch (FileNotFoundException e) { 45 | e.printStackTrace(); 46 | fail(e.getMessage()); 47 | } catch (Exception e) { 48 | e.printStackTrace(); 49 | fail(e.getMessage()); 50 | } 51 | } 52 | 53 | @Test 54 | public void testOnlyWayPoints() { 55 | try { 56 | GPX gpx = parser.parseGPX(new FileInputStream("test/gpx/only-wp.gpx")); 57 | 58 | Set tracks = gpx.getTracks(); 59 | assertEquals(0, tracks.size()); 60 | 61 | assertEquals(4, gpx.getWaypoints().size()); 62 | } catch (FileNotFoundException e) { 63 | e.printStackTrace(); 64 | fail(e.getMessage()); 65 | } catch (Exception e) { 66 | e.printStackTrace(); 67 | fail(e.getMessage()); 68 | } 69 | } 70 | 71 | @Test 72 | public void textSimpleRoute() { 73 | try { 74 | GPX gpx = parser.parseGPX(new FileInputStream("test/gpx/simple-route.gpx")); 75 | 76 | assertEquals(0, gpx.getWaypoints().size()); 77 | 78 | Set tracks = gpx.getTracks(); 79 | assertEquals(0, tracks.size()); 80 | 81 | Set routes = gpx.getRoutes(); 82 | assertEquals(1, routes.size()); 83 | 84 | assertEquals(4, routes.iterator().next().getRoutePoints().size()); 85 | } catch (FileNotFoundException e) { 86 | e.printStackTrace(); 87 | fail(e.getMessage()); 88 | } catch (Exception e) { 89 | e.printStackTrace(); 90 | fail(e.getMessage()); 91 | } 92 | } 93 | 94 | @Test 95 | public void textSimpleTrack() { 96 | try { 97 | GPX gpx = parser.parseGPX(new FileInputStream("test/gpx/simple-track.gpx")); 98 | 99 | assertEquals(0, gpx.getWaypoints().size()); 100 | 101 | Set tracks = gpx.getTracks(); 102 | assertEquals(1, tracks.size()); 103 | assertEquals(4, tracks.iterator().next().getTrackPoints().size()); 104 | 105 | Set routes = gpx.getRoutes(); 106 | assertEquals(0, routes.size()); 107 | } catch (FileNotFoundException e) { 108 | e.printStackTrace(); 109 | fail(e.getMessage()); 110 | } catch (Exception e) { 111 | e.printStackTrace(); 112 | fail(e.getMessage()); 113 | } 114 | } 115 | 116 | @Test 117 | public void textSimpleTrackGarminEtrex() { 118 | try { 119 | GPX gpx = parser.parseGPX(new FileInputStream("test/gpx/simple-track-garmin-etrex.gpx")); 120 | 121 | assertEquals(0, gpx.getWaypoints().size()); 122 | 123 | Set tracks = gpx.getTracks(); 124 | assertEquals(1, tracks.size()); 125 | assertEquals(4, tracks.iterator().next().getTrackPoints().size()); 126 | assertEquals(1, tracks.iterator().next().getTrackSegments().size()); 127 | 128 | Set routes = gpx.getRoutes(); 129 | assertEquals(0, routes.size()); 130 | } catch (FileNotFoundException e) { 131 | e.printStackTrace(); 132 | fail(e.getMessage()); 133 | } catch (Exception e) { 134 | e.printStackTrace(); 135 | fail(e.getMessage()); 136 | } 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /src/com/urizev/gpx/beans/GPX.java: -------------------------------------------------------------------------------- 1 | /* 2 | * GPX.java 3 | * 4 | * Copyright (c) 2012, AlternativeVision. All rights reserved. 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301 USA 20 | */ 21 | 22 | package com.urizev.gpx.beans; 23 | 24 | import java.util.HashSet; 25 | 26 | /** 27 | * This class holds gpx information from a <gpx> node.
28 | *

29 | * GPX specification for this tag: 30 | *

31 | * 32 | * <gpx version="1.1" creator=""xsd:string [1]">
33 | *    <metadata> xsd:string </metadata> [0..1]
34 | *    <wpt> xsd:string </wpt> [0..1]
35 | *    <rte> xsd:string </rte> [0..1]
36 | *    <trk> xsd:string </trk> [0..1]
37 | *    <extensions> extensionsType </extensions> [0..1]
38 | * </gpx>
39 | *
40 | */ 41 | public class GPX extends Extension { 42 | 43 | private String creator; 44 | private HashSet routes; 45 | private HashSet tracks; 46 | private String version; 47 | private HashSet waypoints; 48 | 49 | public GPX() { 50 | this.waypoints = new HashSet(); 51 | this.tracks = new HashSet(); 52 | this.routes = new HashSet(); 53 | } 54 | 55 | /** 56 | * Adds a new Route to a gpx object 57 | * 58 | * @param route 59 | * a {@link Route} 60 | */ 61 | public void addRoute(Route route) { 62 | if (this.routes == null) { 63 | this.routes = new HashSet(); 64 | } 65 | this.routes.add(route); 66 | } 67 | 68 | /** 69 | * Adds a new track to a gpx object 70 | * 71 | * @param track 72 | * a {@link Track} 73 | */ 74 | public void addTrack(Track track) { 75 | if (this.tracks == null) { 76 | this.tracks = new HashSet(); 77 | } 78 | this.tracks.add(track); 79 | } 80 | 81 | /** 82 | * Adds a new waypoint to a gpx object 83 | * 84 | * @param waypoint 85 | * a {@link Waypoint} 86 | */ 87 | public void addWaypoint(Waypoint waypoint) { 88 | if (this.waypoints == null) { 89 | this.waypoints = new HashSet(); 90 | } 91 | this.waypoints.add(waypoint); 92 | 93 | } 94 | 95 | /** 96 | * Returns the creator of this gpx object 97 | * 98 | * @return A String representing the creator of a gpx object 99 | */ 100 | public String getCreator() { 101 | return this.creator; 102 | } 103 | 104 | /** 105 | * Getter for the list of routes from a gpx object 106 | * 107 | * @return a HashSet of {@link Route} 108 | */ 109 | public HashSet getRoutes() { 110 | return this.routes; 111 | } 112 | 113 | /** 114 | * Getter for the list of Tracks from a gpx objecty 115 | * 116 | * @return a HashSet of {@link Track} 117 | */ 118 | public HashSet getTracks() { 119 | return this.tracks; 120 | } 121 | 122 | /** 123 | * Returns the version of a gpx object 124 | * 125 | * @return A String representing the version of this gpx object 126 | */ 127 | public String getVersion() { 128 | return this.version; 129 | } 130 | 131 | /** 132 | * Getter for the list of waypoints from a gpx objecty 133 | * 134 | * @return a HashSet of {@link Waypoint} 135 | */ 136 | public HashSet getWaypoints() { 137 | return this.waypoints; 138 | } 139 | 140 | /** 141 | * Setter for gpx creator property. This maps to creator attribute 142 | * value. 143 | * 144 | * @param creator 145 | * A String representing the creator of a gpx file. 146 | */ 147 | public void setCreator(String creator) { 148 | this.creator = creator; 149 | } 150 | 151 | /** 152 | * Setter for the list of routes from a gpx object 153 | * 154 | * @param routes 155 | * a HashSet of {@link Route} 156 | */ 157 | public void setRoutes(HashSet routes) { 158 | this.routes = routes; 159 | } 160 | 161 | /** 162 | * Setter for the list of tracks from a gpx object 163 | * 164 | * @param tracks 165 | * a HashSet of {@link Track} 166 | */ 167 | public void setTracks(HashSet tracks) { 168 | this.tracks = tracks; 169 | } 170 | 171 | /** 172 | * Setter for gpx version property. This maps to version attribute 173 | * value. 174 | * 175 | * @param version 176 | * A String representing the version of a gpx file. 177 | */ 178 | public void setVersion(String version) { 179 | this.version = version; 180 | } 181 | 182 | /** 183 | * Setter for the list of waypoints from a gpx object 184 | * 185 | * @param waypoints 186 | * a HashSet of {@link Waypoint} 187 | */ 188 | public void setWaypoints(HashSet waypoints) { 189 | this.waypoints = waypoints; 190 | } 191 | } 192 | -------------------------------------------------------------------------------- /src/com/urizev/gpx/beans/Route.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Route.java 3 | * 4 | * Copyright (c) 2012, AlternativeVision. All rights reserved. 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301 USA 20 | */ 21 | 22 | package com.urizev.gpx.beans; 23 | 24 | import java.util.ArrayList; 25 | 26 | /** 27 | * This class holds route information from a <rte> node. 28 | *
29 | *

GPX specification for this tag:

30 | * 31 | * <rte>
32 | *    <name> xsd:string </name> [0..1]
33 | *    <cmt> xsd:string </cmt> [0..1]
34 | *    <desc> xsd:string </desc> [0..1]
35 | *    <src> xsd:string </src> [0..1]
36 | *    <link> linkType </link> [0..*]
37 | *    <number> xsd:nonNegativeInteger </number> [0..1]
38 | *    <type> xsd:string </type> [0..1]
39 | *    <extensions> extensionsType </extensions> [0..1]
40 | *    <rtept> wptType </rtept> [0..*]
41 | * </rte>
42 | *
43 | */ 44 | public class Route extends Extension { 45 | private String name; 46 | private String comment; 47 | private String description; 48 | private String src; 49 | private Integer number; 50 | private String type; 51 | private ArrayList routePoints; 52 | 53 | /** 54 | * Returns the name of this route. 55 | * @return A String representing the name of this route. 56 | */ 57 | public String getName() { 58 | return name; 59 | } 60 | 61 | /** 62 | * Setter for route name property. This maps to <name> tag value. 63 | * @param name A String representing the name of this route. 64 | */ 65 | public void setName(String name) { 66 | this.name = name; 67 | } 68 | 69 | /** 70 | * Returns the comment of this route. 71 | * @return A String representing the comment of this route. 72 | */ 73 | public String getComment() { 74 | return comment; 75 | } 76 | 77 | /** 78 | * Setter for route comment property. This maps to <comment> tag value. 79 | * @param comment A String representing the comment of this route. 80 | */ 81 | public void setComment(String comment) { 82 | this.comment = comment; 83 | } 84 | 85 | /** 86 | * Returns the description of this route. 87 | * @return A String representing the description of this route. 88 | */ 89 | public String getDescription() { 90 | return description; 91 | } 92 | 93 | /** 94 | * Setter for route description property. This maps to <description> tag value. 95 | * @param description A String representing the description of this route. 96 | */ 97 | public void setDescription(String description) { 98 | this.description = description; 99 | } 100 | 101 | /** 102 | * Returns the src of this route. 103 | * @return A String representing the src of this route. 104 | */ 105 | public String getSrc() { 106 | return src; 107 | } 108 | 109 | /** 110 | * Setter for src type property. This maps to <src> tag value. 111 | * @param src A String representing the src of this route. 112 | */ 113 | public void setSrc(String src) { 114 | this.src = src; 115 | } 116 | 117 | /** 118 | * Returns the number of this route. 119 | * @return A String representing the number of this route. 120 | */ 121 | public Integer getNumber() { 122 | return number; 123 | } 124 | 125 | /** 126 | * Setter for route number property. This maps to <number> tag value. 127 | * @param number An Integer representing the number of this route. 128 | */ 129 | public void setNumber(Integer number) { 130 | this.number = number; 131 | } 132 | 133 | /** 134 | * Returns the type of this route. 135 | * @return A String representing the type of this route. 136 | */ 137 | public String getType() { 138 | return type; 139 | } 140 | 141 | /** 142 | * Setter for route type property. This maps to <type> tag value. 143 | * @param type A String representing the type of this route. 144 | */ 145 | public void setType(String type) { 146 | this.type = type; 147 | } 148 | 149 | /** 150 | * Getter for the list of waypoints of this route. 151 | * @return an ArrayList of {@link Waypoint} representing the points of the route. 152 | */ 153 | public ArrayList getRoutePoints() { 154 | return routePoints; 155 | } 156 | 157 | /** 158 | * Setter for the list of waypoints of this route. 159 | * @param routePoints an ArrayList of {@link Waypoint} representing the points of the route. 160 | */ 161 | public void setRoutePoints(ArrayList routePoints) { 162 | this.routePoints = routePoints; 163 | } 164 | 165 | /** 166 | * Adds this new waypoint to this route. 167 | * @param waypoint a {@link Waypoint}. 168 | */ 169 | public void addRoutePoint(Waypoint waypoint) { 170 | if(routePoints == null) { 171 | routePoints = new ArrayList(); 172 | } 173 | routePoints.add(waypoint); 174 | } 175 | 176 | /** 177 | * Returns a String representation of this track. 178 | */ 179 | public String toString() { 180 | StringBuffer sb = new StringBuffer(); 181 | sb.append("rte["); 182 | sb.append("name:" + name +" "); 183 | int points = 0; 184 | if(routePoints!= null) { 185 | points = routePoints.size(); 186 | } 187 | sb.append("rtepts:" + points +" "); 188 | sb.append("]"); 189 | return sb.toString(); 190 | } 191 | } 192 | -------------------------------------------------------------------------------- /src/com/urizev/gpx/beans/Track.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Track.java 3 | * 4 | * Copyright (c) 2012, AlternativeVision. All rights reserved. 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301 USA 20 | */ 21 | 22 | package com.urizev.gpx.beans; 23 | 24 | import java.util.ArrayList; 25 | /** 26 | * This class holds track information from a <trk> node. 27 | *
28 | *

GPX specification for this tag:

29 | * 30 | * <trk>
31 | *    <name> xsd:string </name> [0..1]
32 | *    <cmt> xsd:string </cmt> [0..1]
33 | *    <desc> xsd:string </desc> [0..1]
34 | *    <src> xsd:string </src> [0..1]
35 | *    <link> linkType </link> [0..*]
36 | *    <number> xsd:nonNegativeInteger </number> [0..1]
37 | *    <type> xsd:string </type> [0..1]
38 | *    <extensions> extensionsType </extensions> [0..1]
39 | *    <trkseg> trksegType </trkseg> [0..*]
40 | * </trk>
41 | *
42 | */ 43 | public class Track extends Extension{ 44 | /* 45 | xsd:string [0..1] ? 46 | xsd:string [0..1] ? 47 | xsd:string [0..1] ? 48 | xsd:string [0..1] ? 49 | linkType [0..*] ? 50 | xsd:nonNegativeInteger [0..1] ? 51 | xsd:string [0..1] ? 52 | extensionsType [0..1] ? 53 | trksegType [0..*] ? 54 | */ 55 | private String name; 56 | private String comment; 57 | private String description; 58 | private String src; 59 | private Integer number; 60 | private String type; 61 | private ArrayList trackPoints; 62 | private ArrayList> trackSegments; 63 | 64 | /** 65 | * Returns the name of this track. 66 | * @return A String representing the name of this track. 67 | */ 68 | public String getName() { 69 | return name; 70 | } 71 | 72 | /** 73 | * Setter for track name property. This maps to <name> tag value. 74 | * @param name A String representing the name of this track. 75 | */ 76 | public void setName(String name) { 77 | this.name = name; 78 | } 79 | 80 | /** 81 | * Returns the comment of this track. 82 | * @return A String representing the comment of this track. 83 | */ 84 | public String getComment() { 85 | return comment; 86 | } 87 | 88 | /** 89 | * Setter for track comment property. This maps to <comment> tag value. 90 | * @param comment A String representing the comment of this track. 91 | */ 92 | public void setComment(String comment) { 93 | this.comment = comment; 94 | } 95 | 96 | /** 97 | * Returns the description of this track. 98 | * @return A String representing the description of this track. 99 | */ 100 | public String getDescription() { 101 | return description; 102 | } 103 | 104 | /** 105 | * Setter for track description property. This maps to <description> tag value. 106 | * @param description A String representing the description of this track. 107 | */ 108 | public void setDescription(String description) { 109 | this.description = description; 110 | } 111 | 112 | /** 113 | * Returns the src of this track. 114 | * @return A String representing the src of this track. 115 | */ 116 | public String getSrc() { 117 | return src; 118 | } 119 | 120 | /** 121 | * Setter for src type property. This maps to <src> tag value. 122 | * @param src A String representing the src of this track. 123 | */ 124 | public void setSrc(String src) { 125 | this.src = src; 126 | } 127 | 128 | /** 129 | * Returns the number of this track. 130 | * @return A String representing the number of this track. 131 | */ 132 | public Integer getNumber() { 133 | return number; 134 | } 135 | 136 | /** 137 | * Setter for track number property. This maps to <number> tag value. 138 | * @param number An Integer representing the number of this track. 139 | */ 140 | public void setNumber(Integer number) { 141 | this.number = number; 142 | } 143 | 144 | /** 145 | * Returns the type of this track. 146 | * @return A String representing the type of this track. 147 | */ 148 | public String getType() { 149 | return type; 150 | } 151 | 152 | /** 153 | * Setter for track type property. This maps to <type> tag value. 154 | * @param type A String representing the type of this track. 155 | */ 156 | public void setType(String type) { 157 | this.type = type; 158 | } 159 | 160 | /** 161 | * Getter for the list of waypoints of a track. 162 | * @return an ArrayList of {@link Waypoint} representing the points of the track. 163 | */ 164 | public ArrayList getTrackPoints() { 165 | return trackPoints; 166 | } 167 | 168 | /** 169 | * Setter for the list of waypoints of a track. 170 | * @param trackPoints an ArrayList of {@link Waypoint} representing the points of the track. 171 | */ 172 | public void setTrackPoints(ArrayList trackPoints) { 173 | this.trackPoints = trackPoints; 174 | } 175 | 176 | /** 177 | * Adds this new track point to this track. 178 | * @param trackPoint a {@link Waypoint}. 179 | */ 180 | public void addTrackPoint(Waypoint trackPoint) 181 | { 182 | if (trackPoints == null) 183 | trackPoints = new ArrayList(); 184 | 185 | trackPoints.add(trackPoint); 186 | } 187 | 188 | /** 189 | * Adds new track points to the list of waypoints of a track. 190 | * @param trackPoints an ArrayList of {@link Waypoint} representing the points of the track. 191 | */ 192 | public void addTrackPoints(ArrayList trackPoints) { 193 | if (this.trackPoints == null) { 194 | this.trackPoints = new ArrayList(); 195 | } 196 | this.trackPoints.addAll(trackPoints); 197 | } 198 | 199 | /** 200 | * Setter for the list of {@link Waypoint} of a {@link Track}. 201 | * @param trackPoints an ArrayList of {@link Waypoint} representing the points of the {@link Track}. 202 | */ 203 | public void addTrackSegment(ArrayList segment) { 204 | if (this.trackSegments == null) { 205 | this.trackSegments = new ArrayList<>(); 206 | } 207 | this.trackSegments.add(segment); 208 | } 209 | 210 | /** 211 | * Returns the segments of the track 212 | * @param trackPoints an ArrayList of {@link Waypoint} representing the points of the {@link Track}. 213 | * 214 | * @return A list of segments. Each segment is a list of {@link Waypoint} 215 | */ 216 | public ArrayList> getTrackSegments() { 217 | return this.trackSegments; 218 | } 219 | 220 | /** 221 | * Returns a String representation of this track. 222 | */ 223 | public String toString() { 224 | StringBuffer sb = new StringBuffer(); 225 | sb.append("trk["); 226 | sb.append("name:" + name +" "); 227 | int points = 0; 228 | if(trackPoints!= null) { 229 | points = trackPoints.size(); 230 | } 231 | sb.append("trkseg:" + points +" "); 232 | sb.append("]"); 233 | return sb.toString(); 234 | } 235 | } 236 | -------------------------------------------------------------------------------- /src/com/urizev/gpx/beans/Waypoint.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Waypoint.java 3 | * 4 | * Copyright (c) 2012, AlternativeVision. All rights reserved. 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301 USA 20 | */ 21 | 22 | package com.urizev.gpx.beans; 23 | 24 | import java.text.SimpleDateFormat; 25 | import java.util.Date; 26 | import java.util.Iterator; 27 | 28 | import com.urizev.gpx.types.FixType; 29 | 30 | /** 31 | * This class holds waypoint information from a <wpt> node.
32 | *

33 | * GPX specification for this tag: 34 | *

35 | * 36 | * <wpt
37 | *    lat="latitudeType [1]"
38 | *    lon="longitudeType [1]">
39 | *    <ele> xsd:decimal </ele> [0..1]
40 | *    <time> xsd:dateTime </time> [0..1]
41 | *    <magvar> degreesType </magvar> [0..1]
42 | *    <geoidheight> xsd:decimal </geoidheight> [0..1]
43 | *    <name> xsd:string </name> [0..1]
44 | *    <cmt> xsd:string </cmt> [0..1]
45 | *    <desc> xsd:string </desc> [0..1]
46 | *    <src> xsd:string </src> [0..1]
47 | *    <link> linkType </link> [0..*]
48 | *    <sym> xsd:string </sym> [0..1]
49 | *    <type> xsd:string </type> [0..1]
50 | *    <fix> fixType </fix> [0..1]
51 | *    <sat> xsd:nonNegativeInteger </sat> [0..1]
52 | *    <hdop> xsd:decimal </hdop> [0..1]
53 | *    <vdop> xsd:decimal </vdop> [0..1]
54 | *    <pdop> xsd:decimal </pdop> [0..1]
55 | *    <ageofdgpsdata> xsd:decimal </ageofdgpsdata> [0..1]
56 | *    <dgpsid> dgpsStationType </dgpsid> [0..1]
57 | *    <extensions> extensionsType </extensions> [0..1]
58 | * </wpt>
59 | *
60 | */ 61 | public class Waypoint extends Extension { 62 | 63 | private Double ageOfGPSData; 64 | private String comment; 65 | private String description; 66 | private Integer dgpsid; 67 | private Double elevation; 68 | private FixType fix; 69 | private Double geoidHeight; 70 | private Double hdop; 71 | /* 72 | * lat="latitudeType [1] ?" lon="longitudeType [1] ?"> xsd:decimal 73 | * [0..1] ? [0..1] ? degreesType 74 | * [0..1] ? xsd:decimal [0..1] ? 75 | * xsd:string [0..1] ? xsd:string [0..1] ? 76 | * xsd:string [0..1] ? xsd:string [0..1] ? 77 | * linkType [0..*] ? xsd:string [0..1] ? 78 | * xsd:string [0..1] ? fixType [0..1] ? 79 | * xsd:nonNegativeInteger [0..1] ? xsd:decimal [0..1] 80 | * ? xsd:decimal [0..1] ? xsd:decimal [0..1] ? 81 | * xsd:decimal [0..1] ? 82 | * dgpsStationType [0..1] ? extensionsType 83 | * [0..1] ? 84 | */ 85 | private Double latitude; 86 | private Double longitude; 87 | private Double magneticDeclination; 88 | private String name; 89 | private Double pdop; 90 | private Integer sat; 91 | private String src; 92 | private String sym; 93 | private Date time; 94 | private String type; 95 | private Double vdop; 96 | 97 | public Waypoint(String name, float latitude, float longitude) { 98 | this.name = name; 99 | this.latitude = (double) latitude; 100 | this.longitude = (double) longitude; 101 | } 102 | 103 | /** 104 | * Returns the ageOfGPSData of this waypoint. 105 | * 106 | * @return a Double representing the ageOfGPSData of this waypoint. 107 | */ 108 | public Double getAgeOfGPSData() { 109 | return this.ageOfGPSData; 110 | } 111 | 112 | /** 113 | * Returns the comment of this waypoint. 114 | * 115 | * @return a String representing the comment of this waypoint. 116 | */ 117 | public String getComment() { 118 | return this.comment; 119 | } 120 | 121 | /** 122 | * Returns the description of this waypoint. 123 | * 124 | * @return a String representing the description of this waypoint. 125 | */ 126 | public String getDescription() { 127 | return this.description; 128 | } 129 | 130 | /** 131 | * Returns the dgpsid of this waypoint. 132 | * 133 | * @return an Integer representing the dgpsid of this waypoint. 134 | */ 135 | public Integer getDgpsid() { 136 | return this.dgpsid; 137 | } 138 | 139 | /** 140 | * Returns the elevation of this waypoint. 141 | * 142 | * @return A Double representing the elevation of this waypoint. 143 | */ 144 | public Double getElevation() { 145 | return this.elevation; 146 | } 147 | 148 | /** 149 | * Returns the fix of this waypoint. 150 | * 151 | * @return A {@link FixType} representing the fix of this waypoint. 152 | */ 153 | public FixType getFix() { 154 | return this.fix; 155 | } 156 | 157 | /** 158 | * Returns the geoid height of this waypoint. 159 | * 160 | * @return A String representing the geoid height of this waypoint. 161 | */ 162 | public Double getGeoidHeight() { 163 | return this.geoidHeight; 164 | } 165 | 166 | /** 167 | * Returns the hdop of this waypoint. 168 | * 169 | * @return a Double representing the hdop of this waypoint. 170 | */ 171 | public Double getHdop() { 172 | return this.hdop; 173 | } 174 | 175 | /** 176 | * Returns the latitude of this waypoint. 177 | * 178 | * @return a Double value representing the latitude of this waypoint. 179 | */ 180 | public Double getLatitude() { 181 | return this.latitude; 182 | } 183 | 184 | /** 185 | * Returns the longitude of this waypoint. 186 | * 187 | * @return a Double value representing the longitude of this waypoint. 188 | */ 189 | public Double getLongitude() { 190 | return this.longitude; 191 | } 192 | 193 | /** 194 | * Returns the magnetic declination of this waypoint. 195 | * 196 | * @return A Double representing the magnetic declination of this waypoint. 197 | */ 198 | public Double getMagneticDeclination() { 199 | return this.magneticDeclination; 200 | } 201 | 202 | /** 203 | * Returns the name of this waypoint. 204 | * 205 | * @return A String representing the name of this waypoint. 206 | */ 207 | public String getName() { 208 | return this.name; 209 | } 210 | 211 | /** 212 | * Returns the pdop of this waypoint. 213 | * 214 | * @return a Double representing the pdop of this waypoint. 215 | */ 216 | public Double getPdop() { 217 | return this.pdop; 218 | } 219 | 220 | /** 221 | * Returns the sat of this waypoint. 222 | * 223 | * @return an Integer representing the sat of this waypoint. 224 | */ 225 | public Integer getSat() { 226 | return this.sat; 227 | } 228 | 229 | /** 230 | * Returns the src of this waypoint. 231 | * 232 | * @return A String representing the src of this waypoint. 233 | */ 234 | public String getSrc() { 235 | return this.src; 236 | } 237 | 238 | /** 239 | * Returns the sym of this waypoint. 240 | * 241 | * @return A String representing the sym of this waypoint. 242 | */ 243 | public String getSym() { 244 | return this.sym; 245 | } 246 | 247 | /** 248 | * Returns the time of this waypoint. 249 | * 250 | * @return a Date representing the name of this waypoint. 251 | */ 252 | public Date getTime() { 253 | return this.time; 254 | } 255 | 256 | /** 257 | * Returns the type of this waypoint. 258 | * 259 | * @return A String representing the type of this waypoint. 260 | */ 261 | public String getType() { 262 | return this.type; 263 | } 264 | 265 | /** 266 | * Returns the vdop of this waypoint. 267 | * 268 | * @return a Double representing the vdop of this waypoint. 269 | */ 270 | public Double getVdop() { 271 | return this.vdop; 272 | } 273 | 274 | /** 275 | * Setter for waypoint ageOfGPSData property. This maps to 276 | * <ageOfGPSData> tag value. 277 | * 278 | * @param ageOfGPSData 279 | * A String representing the ageOfGPSData of this waypoint. 280 | */ 281 | public void setAgeOfGPSData(Double ageOfGPSData) { 282 | this.ageOfGPSData = ageOfGPSData; 283 | } 284 | 285 | /** 286 | * Setter for waypoint comment property. This maps to <cmt> tag value. 287 | * 288 | * @param comment 289 | * A String representing the comment of this waypoint. 290 | */ 291 | public void setComment(String comment) { 292 | this.comment = comment; 293 | } 294 | 295 | /** 296 | * Setter for waypoint description property. This maps to <desc> tag 297 | * value. 298 | * 299 | * @param description 300 | * A String representing the description of this waypoint. 301 | */ 302 | public void setDescription(String description) { 303 | this.description = description; 304 | } 305 | 306 | /** 307 | * Setter for waypoint dgpsid property. This maps to <dgpsid> tag 308 | * value. 309 | * 310 | * @param dgpsid 311 | * an Integer representing the dgpsid of this waypoint. 312 | */ 313 | public void setDgpsid(Integer dgpsid) { 314 | this.dgpsid = dgpsid; 315 | } 316 | 317 | /** 318 | * Setter for waypoint elevation property. This maps to <ele> tag 319 | * value. 320 | * 321 | * @param elevation 322 | * a Double value representing the elevation of this waypoint. 323 | */ 324 | public void setElevation(Double elevation) { 325 | this.elevation = elevation; 326 | } 327 | 328 | /** 329 | * Setter for waypoint fix property. This maps to <fix> tag value. 330 | * 331 | * @param fix 332 | * a {@link FixType} representing the fix of this waypoint. 333 | */ 334 | public void setFix(FixType fix) { 335 | this.fix = fix; 336 | } 337 | 338 | /** 339 | * Setter for waypoint geoid height property. This maps to 340 | * <geoidheight> tag value. 341 | * 342 | * @param geoidHeight 343 | * A String representing the geoid height of this waypoint. 344 | */ 345 | public void setGeoidHeight(Double geoidHeight) { 346 | this.geoidHeight = geoidHeight; 347 | } 348 | 349 | /** 350 | * Setter for waypoint hdop property. This maps to <hdop> tag value. 351 | * 352 | * @param hdop 353 | * a Double representing the name of this waypoint. 354 | */ 355 | public void setHdop(Double hdop) { 356 | this.hdop = hdop; 357 | } 358 | 359 | /** 360 | * Setter for waypoint latitude property. This maps to "lat" attribute 361 | * value. 362 | * 363 | * @param latitude 364 | * a Doube value representing the latitude of this waypoint. 365 | */ 366 | public void setLatitude(Double latitude) { 367 | this.latitude = latitude; 368 | } 369 | 370 | /** 371 | * Setter for waypoint longitude property. This maps to "long" attribute 372 | * value. 373 | * 374 | * @param longitude 375 | * a Doube value representing the longitude of this waypoint. 376 | */ 377 | public void setLongitude(Double longitude) { 378 | this.longitude = longitude; 379 | } 380 | 381 | /** 382 | * Setter for waypoint magnetic declination property. This maps to 383 | * <magvar> tag value. 384 | * 385 | * @param magneticDeclination 386 | * A String representing the magnetic declination of this 387 | * waypoint. 388 | */ 389 | public void setMagneticDeclination(Double magneticDeclination) { 390 | this.magneticDeclination = magneticDeclination; 391 | } 392 | 393 | /** 394 | * Setter for waypoint name property. This maps to <name> tag value. 395 | * 396 | * @param name 397 | * A String representing the name of this waypoint. 398 | */ 399 | public void setName(String name) { 400 | this.name = name; 401 | } 402 | 403 | /** 404 | * Setter for waypoint pdop property. This maps to <pdop> tag value. 405 | * 406 | * @param pdop 407 | * a Double representing the pdop of this waypoint. 408 | */ 409 | public void setPdop(Double pdop) { 410 | this.pdop = pdop; 411 | } 412 | 413 | /** 414 | * Setter for waypoint sat property. This maps to <sat> tag value. 415 | * 416 | * @param sat 417 | * an Integer representing the sat of this waypoint. 418 | */ 419 | public void setSat(Integer sat) { 420 | this.sat = sat; 421 | } 422 | 423 | /** 424 | * Setter for waypoint src property. This maps to <src> tag value. 425 | * 426 | * @param src 427 | * a String representing the src of this waypoint. 428 | */ 429 | public void setSrc(String src) { 430 | this.src = src; 431 | } 432 | 433 | /** 434 | * Setter for waypoint sym property. This maps to <sym> tag value. 435 | * 436 | * @param sym 437 | * a String representing the sym of this waypoint. 438 | */ 439 | public void setSym(String sym) { 440 | this.sym = sym; 441 | } 442 | 443 | /** 444 | * Setter for waypoint time property. This maps to <time> tag value. 445 | * 446 | * @param time 447 | * a Date representing the time of this waypoint. 448 | */ 449 | public void setTime(Date time) { 450 | this.time = time; 451 | } 452 | 453 | /** 454 | * Setter for waypoint type property. This maps to <type> tag value. 455 | * 456 | * @param type 457 | * a String representing the type of this waypoint. 458 | */ 459 | public void setType(String type) { 460 | this.type = type; 461 | } 462 | 463 | /** 464 | * Setter for waypoint vdop property. This maps to <vdop> tag value. 465 | * 466 | * @param vdop 467 | * A String representing the vdop of this waypoint. 468 | */ 469 | public void setVdop(Double vdop) { 470 | this.vdop = vdop; 471 | } 472 | 473 | /** 474 | * Returns a String representation of this waypoint. 475 | */ 476 | @Override 477 | public String toString() { 478 | StringBuffer sb = new StringBuffer(); 479 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 480 | String date = ""; 481 | if (this.time != null) { 482 | date = sdf.format(this.time); 483 | } 484 | sb.append("["); 485 | sb.append("name:'" + this.name + "' "); 486 | sb.append("lat:" + this.latitude + " "); 487 | sb.append("lon:" + this.longitude + " "); 488 | sb.append("elv:" + this.elevation + " "); 489 | sb.append("time:" + date + " "); 490 | sb.append("fix:" + this.fix + " "); 491 | if (this.extensionData != null) { 492 | sb.append("extensions:{"); 493 | Iterator it = this.extensionData.keySet().iterator(); 494 | while (it.hasNext()) { 495 | sb.append(it.next()); 496 | if (it.hasNext()) { 497 | sb.append(","); 498 | } 499 | } 500 | sb.append("}"); 501 | } 502 | sb.append("]"); 503 | return sb.toString(); 504 | } 505 | } 506 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.] 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS -------------------------------------------------------------------------------- /src/com/urizev/gpx/GPXParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * GPXParser.java 3 | * 4 | * Copyright (c) 2012, AlternativeVision. All rights reserved. 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301 USA 20 | */ 21 | 22 | package com.urizev.gpx; 23 | 24 | import java.io.InputStream; 25 | import java.io.OutputStream; 26 | import java.text.ParseException; 27 | import java.text.SimpleDateFormat; 28 | import java.util.ArrayList; 29 | import java.util.Date; 30 | import java.util.Iterator; 31 | 32 | import javax.xml.parsers.DocumentBuilder; 33 | import javax.xml.parsers.DocumentBuilderFactory; 34 | import javax.xml.parsers.ParserConfigurationException; 35 | import javax.xml.transform.OutputKeys; 36 | import javax.xml.transform.Transformer; 37 | import javax.xml.transform.TransformerException; 38 | import javax.xml.transform.TransformerFactory; 39 | import javax.xml.transform.dom.DOMSource; 40 | import javax.xml.transform.stream.StreamResult; 41 | 42 | import org.w3c.dom.DOMException; 43 | import org.w3c.dom.Document; 44 | import org.w3c.dom.NamedNodeMap; 45 | import org.w3c.dom.Node; 46 | import org.w3c.dom.NodeList; 47 | 48 | import com.urizev.gpx.beans.GPX; 49 | import com.urizev.gpx.beans.Route; 50 | import com.urizev.gpx.beans.Track; 51 | import com.urizev.gpx.beans.Waypoint; 52 | import com.urizev.gpx.extensions.IExtensionParser; 53 | import com.urizev.gpx.types.FixType; 54 | 55 | /** 56 | *

57 | * This class defines methods for parsing and writing gpx files. 58 | *

59 | *
60 | * Usage for parsing a gpx file into a {@link GPX} object:
61 | * 62 | * GPXParser p = new GPXParser();
63 | * FileInputStream in = new FileInputStream("inFile.gpx");
64 | * GPX gpx = p.parseGPX(in);
65 | *

66 | * Usage for writing a {@link GPX} object to a file:
67 | * 68 | * GPXParser p = new GPXParser();
69 | * FileOutputStream out = new FileOutputStream("outFile.gpx");
70 | * p.writeGPX(gpx, out);
71 | * out.close();
72 | *
73 | */ 74 | public class GPXParser { 75 | 76 | private final ArrayList extensionParsers = new ArrayList(); 77 | 78 | /** 79 | * Adds a new extension parser to be used when parsing a gpx steam 80 | * 81 | * @param parser 82 | * an instance of a {@link IExtensionParser} implementation 83 | */ 84 | public void addExtensionParser(IExtensionParser parser) { 85 | this.extensionParsers.add(parser); 86 | } 87 | 88 | /** 89 | * Parses a stream containing GPX data 90 | * 91 | * @param in 92 | * the input stream 93 | * @return {@link GPX} object containing parsed data, or null if no gpx data 94 | * was found in the seream 95 | * @throws Exception 96 | */ 97 | public GPX parseGPX(InputStream in) throws Exception { 98 | DocumentBuilder builder = DocumentBuilderFactory.newInstance() 99 | .newDocumentBuilder(); 100 | Document doc = builder.parse(in); 101 | Node firstChild = doc.getFirstChild(); 102 | if (firstChild != null 103 | && GPXConstants.GPX_NODE.equals(firstChild.getNodeName())) { 104 | GPX gpx = new GPX(); 105 | NamedNodeMap attrs = firstChild.getAttributes(); 106 | for (int idx = 0; idx < attrs.getLength(); idx++) { 107 | Node attr = attrs.item(idx); 108 | if (GPXConstants.VERSION_ATTR.equals(attr.getNodeName())) { 109 | gpx.setVersion(attr.getNodeValue()); 110 | } else if (GPXConstants.CREATOR_ATTR.equals(attr.getNodeName())) { 111 | gpx.setCreator(attr.getNodeValue()); 112 | } 113 | } 114 | NodeList nodes = firstChild.getChildNodes(); 115 | 116 | if (nodes != null) 117 | for (int idx = 0; idx < nodes.getLength(); idx++) { 118 | Node currentNode = nodes.item(idx); 119 | if (GPXConstants.WPT_NODE.equals(currentNode.getNodeName())) { 120 | Waypoint w = this.parseWaypoint(currentNode); 121 | if (w != null) { 122 | gpx.addWaypoint(w); 123 | } 124 | } else if (GPXConstants.TRK_NODE.equals(currentNode 125 | .getNodeName())) { 126 | Track trk = this.parseTrack(currentNode); 127 | if (trk != null) { 128 | gpx.addTrack(trk); 129 | } 130 | } else if (GPXConstants.EXTENSIONS_NODE.equals(currentNode 131 | .getNodeName())) { 132 | for (IExtensionParser parser : this.extensionParsers) { 133 | Object data = parser.parseGPXExtension(currentNode); 134 | gpx.addExtensionData(parser.getId(), data); 135 | } 136 | } else if (GPXConstants.RTE_NODE.equals(currentNode 137 | .getNodeName())) { 138 | Route rte = this.parseRoute(currentNode); 139 | if (rte != null) { 140 | gpx.addRoute(rte); 141 | } 142 | } 143 | } 144 | // TODO: parse route node 145 | return gpx; 146 | } else { 147 | } 148 | return null; 149 | } 150 | 151 | /** 152 | * Removes an extension parser previously added 153 | * 154 | * @param parser 155 | * an instance of a {@link IExtensionParser} implementation 156 | */ 157 | public void removeExtensionParser(IExtensionParser parser) { 158 | this.extensionParsers.remove(parser); 159 | } 160 | 161 | public void writeGPX(GPX gpx, OutputStream out) throws ParserConfigurationException, TransformerException { 162 | this.writeGPX(gpx, out, false); 163 | } 164 | 165 | public void writeGPX(GPX gpx, OutputStream out, boolean indent) throws ParserConfigurationException, TransformerException { 166 | DocumentBuilder builder = DocumentBuilderFactory.newInstance() 167 | .newDocumentBuilder(); 168 | Document doc = builder.newDocument(); 169 | Node gpxNode = doc.createElement(GPXConstants.GPX_NODE); 170 | this.addBasicGPXInfoToNode(gpx, gpxNode, doc); 171 | 172 | if (gpx.getWaypoints() != null) 173 | for (Waypoint wp : gpx.getWaypoints()) 174 | this.addWaypointToGPXNode(wp, gpxNode, doc); 175 | 176 | if (gpx.getRoutes() != null) 177 | for (Route route : gpx.getRoutes()) 178 | this.addRouteToGPXNode(route, gpxNode, doc); 179 | 180 | if (gpx.getTracks() != null) 181 | for (Track track : gpx.getTracks()) 182 | this.addTrackToGPXNode(track, gpxNode, doc); 183 | 184 | doc.appendChild(gpxNode); 185 | 186 | // Use a Transformer for output 187 | TransformerFactory tFactory = TransformerFactory.newInstance(); 188 | Transformer transformer = tFactory.newTransformer(); 189 | if (indent) { 190 | transformer.setOutputProperty (OutputKeys.INDENT, "yes"); 191 | } 192 | 193 | DOMSource source = new DOMSource(doc); 194 | StreamResult result = new StreamResult(out); 195 | transformer.transform(source, result); 196 | } 197 | 198 | private void addBasicGPXInfoToNode(GPX gpx, Node gpxNode, Document doc) { 199 | NamedNodeMap attrs = gpxNode.getAttributes(); 200 | if (gpx.getVersion() != null) { 201 | Node verNode = doc.createAttribute(GPXConstants.VERSION_ATTR); 202 | verNode.setNodeValue(gpx.getVersion()); 203 | attrs.setNamedItem(verNode); 204 | } 205 | if (gpx.getCreator() != null) { 206 | Node creatorNode = doc.createAttribute(GPXConstants.CREATOR_ATTR); 207 | creatorNode.setNodeValue(gpx.getCreator()); 208 | attrs.setNamedItem(creatorNode); 209 | } 210 | 211 | if (gpx.getExtensionsParsed() > 0) { 212 | Node node = doc.createElement(GPXConstants.EXTENSIONS_NODE); 213 | for (IExtensionParser parser : this.extensionParsers) { 214 | parser.writeGPXExtensionData(node, gpx, doc); 215 | } 216 | gpxNode.appendChild(node); 217 | } 218 | } 219 | 220 | private void addGenericWaypointToGPXNode(String tagName, Waypoint wpt, 221 | Node gpxNode, Document doc) { 222 | Node wptNode = doc.createElement(tagName); 223 | NamedNodeMap attrs = wptNode.getAttributes(); 224 | if (wpt.getLatitude() != null) { 225 | Node latNode = doc.createAttribute(GPXConstants.LAT_ATTR); 226 | latNode.setNodeValue(wpt.getLatitude().toString()); 227 | attrs.setNamedItem(latNode); 228 | } 229 | if (wpt.getLongitude() != null) { 230 | Node longNode = doc.createAttribute(GPXConstants.LON_ATTR); 231 | longNode.setNodeValue(wpt.getLongitude().toString()); 232 | attrs.setNamedItem(longNode); 233 | } 234 | if (wpt.getElevation() != null) { 235 | Node node = doc.createElement(GPXConstants.ELE_NODE); 236 | node.appendChild(doc.createTextNode(wpt.getElevation().toString())); 237 | wptNode.appendChild(node); 238 | } 239 | if (wpt.getTime() != null) { 240 | Node node = doc.createElement(GPXConstants.TIME_NODE); 241 | SimpleDateFormat sdf = new SimpleDateFormat( 242 | "yyyy-MM-dd'T'kk:mm:ss'Z'"); 243 | node.appendChild(doc.createTextNode(sdf.format(wpt.getTime()))); 244 | wptNode.appendChild(node); 245 | } 246 | if (wpt.getMagneticDeclination() != null) { 247 | Node node = doc.createElement(GPXConstants.MAGVAR_NODE); 248 | node.appendChild(doc.createTextNode(wpt.getMagneticDeclination() 249 | .toString())); 250 | wptNode.appendChild(node); 251 | } 252 | if (wpt.getGeoidHeight() != null) { 253 | Node node = doc.createElement(GPXConstants.GEOIDHEIGHT_NODE); 254 | node.appendChild(doc 255 | .createTextNode(wpt.getGeoidHeight().toString())); 256 | wptNode.appendChild(node); 257 | } 258 | if (wpt.getName() != null) { 259 | Node node = doc.createElement(GPXConstants.NAME_NODE); 260 | node.appendChild(doc.createTextNode(wpt.getName())); 261 | wptNode.appendChild(node); 262 | } 263 | if (wpt.getComment() != null) { 264 | Node node = doc.createElement(GPXConstants.CMT_NODE); 265 | node.appendChild(doc.createTextNode(wpt.getComment())); 266 | wptNode.appendChild(node); 267 | } 268 | if (wpt.getDescription() != null) { 269 | Node node = doc.createElement(GPXConstants.DESC_NODE); 270 | node.appendChild(doc.createTextNode(wpt.getDescription())); 271 | wptNode.appendChild(node); 272 | } 273 | if (wpt.getSrc() != null) { 274 | Node node = doc.createElement(GPXConstants.SRC_NODE); 275 | node.appendChild(doc.createTextNode(wpt.getSrc())); 276 | wptNode.appendChild(node); 277 | } 278 | // TODO: write link node 279 | if (wpt.getSym() != null) { 280 | Node node = doc.createElement(GPXConstants.SYM_NODE); 281 | node.appendChild(doc.createTextNode(wpt.getSym())); 282 | wptNode.appendChild(node); 283 | } 284 | if (wpt.getType() != null) { 285 | Node node = doc.createElement(GPXConstants.TYPE_NODE); 286 | node.appendChild(doc.createTextNode(wpt.getType())); 287 | wptNode.appendChild(node); 288 | } 289 | if (wpt.getFix() != null) { 290 | Node node = doc.createElement(GPXConstants.FIX_NODE); 291 | node.appendChild(doc.createTextNode(wpt.getFix().toString())); 292 | wptNode.appendChild(node); 293 | } 294 | if (wpt.getSat() != null) { 295 | Node node = doc.createElement(GPXConstants.SAT_NODE); 296 | node.appendChild(doc.createTextNode(wpt.getSat().toString())); 297 | wptNode.appendChild(node); 298 | } 299 | if (wpt.getHdop() != null) { 300 | Node node = doc.createElement(GPXConstants.HDOP_NODE); 301 | node.appendChild(doc.createTextNode(wpt.getHdop().toString())); 302 | wptNode.appendChild(node); 303 | } 304 | if (wpt.getVdop() != null) { 305 | Node node = doc.createElement(GPXConstants.VDOP_NODE); 306 | node.appendChild(doc.createTextNode(wpt.getVdop().toString())); 307 | wptNode.appendChild(node); 308 | } 309 | if (wpt.getPdop() != null) { 310 | Node node = doc.createElement(GPXConstants.PDOP_NODE); 311 | node.appendChild(doc.createTextNode(wpt.getPdop().toString())); 312 | wptNode.appendChild(node); 313 | } 314 | if (wpt.getAgeOfGPSData() != null) { 315 | Node node = doc.createElement(GPXConstants.AGEOFGPSDATA_NODE); 316 | node.appendChild(doc.createTextNode(wpt.getAgeOfGPSData() 317 | .toString())); 318 | wptNode.appendChild(node); 319 | } 320 | if (wpt.getDgpsid() != null) { 321 | Node node = doc.createElement(GPXConstants.DGPSID_NODE); 322 | node.appendChild(doc.createTextNode(wpt.getDgpsid().toString())); 323 | wptNode.appendChild(node); 324 | } 325 | if (wpt.getExtensionsParsed() > 0) { 326 | Node node = doc.createElement(GPXConstants.EXTENSIONS_NODE); 327 | Iterator it = this.extensionParsers.iterator(); 328 | while (it.hasNext()) { 329 | it.next().writeWaypointExtensionData(node, wpt, doc); 330 | } 331 | wptNode.appendChild(node); 332 | } 333 | gpxNode.appendChild(wptNode); 334 | } 335 | 336 | private void addRouteToGPXNode(Route rte, Node gpxNode, Document doc) { 337 | Node trkNode = doc.createElement(GPXConstants.RTE_NODE); 338 | 339 | if (rte.getName() != null) { 340 | Node node = doc.createElement(GPXConstants.NAME_NODE); 341 | node.appendChild(doc.createTextNode(rte.getName())); 342 | trkNode.appendChild(node); 343 | } 344 | if (rte.getComment() != null) { 345 | Node node = doc.createElement(GPXConstants.CMT_NODE); 346 | node.appendChild(doc.createTextNode(rte.getComment())); 347 | trkNode.appendChild(node); 348 | } 349 | if (rte.getDescription() != null) { 350 | Node node = doc.createElement(GPXConstants.DESC_NODE); 351 | node.appendChild(doc.createTextNode(rte.getDescription())); 352 | trkNode.appendChild(node); 353 | } 354 | if (rte.getSrc() != null) { 355 | Node node = doc.createElement(GPXConstants.SRC_NODE); 356 | node.appendChild(doc.createTextNode(rte.getSrc())); 357 | trkNode.appendChild(node); 358 | } 359 | // TODO: write link 360 | if (rte.getNumber() != null) { 361 | Node node = doc.createElement(GPXConstants.NUMBER_NODE); 362 | node.appendChild(doc.createTextNode(rte.getNumber().toString())); 363 | trkNode.appendChild(node); 364 | } 365 | if (rte.getType() != null) { 366 | Node node = doc.createElement(GPXConstants.TYPE_NODE); 367 | node.appendChild(doc.createTextNode(rte.getType())); 368 | trkNode.appendChild(node); 369 | } 370 | if (rte.getExtensionsParsed() > 0) { 371 | Node node = doc.createElement(GPXConstants.EXTENSIONS_NODE); 372 | Iterator it = this.extensionParsers.iterator(); 373 | while (it.hasNext()) { 374 | it.next().writeRouteExtensionData(node, rte, doc); 375 | } 376 | trkNode.appendChild(node); 377 | } 378 | if (rte.getRoutePoints() != null) { 379 | Iterator it = rte.getRoutePoints().iterator(); 380 | while (it.hasNext()) { 381 | this.addGenericWaypointToGPXNode(GPXConstants.RTEPT_NODE, 382 | it.next(), trkNode, doc); 383 | } 384 | } 385 | gpxNode.appendChild(trkNode); 386 | } 387 | 388 | private void addTrackToGPXNode(Track trk, Node gpxNode, Document doc) { 389 | Node trkNode = doc.createElement(GPXConstants.TRK_NODE); 390 | 391 | if (trk.getName() != null) { 392 | Node node = doc.createElement(GPXConstants.NAME_NODE); 393 | node.appendChild(doc.createTextNode(trk.getName())); 394 | trkNode.appendChild(node); 395 | } 396 | if (trk.getComment() != null) { 397 | Node node = doc.createElement(GPXConstants.CMT_NODE); 398 | node.appendChild(doc.createTextNode(trk.getComment())); 399 | trkNode.appendChild(node); 400 | } 401 | if (trk.getDescription() != null) { 402 | Node node = doc.createElement(GPXConstants.DESC_NODE); 403 | node.appendChild(doc.createTextNode(trk.getDescription())); 404 | trkNode.appendChild(node); 405 | } 406 | if (trk.getSrc() != null) { 407 | Node node = doc.createElement(GPXConstants.SRC_NODE); 408 | node.appendChild(doc.createTextNode(trk.getSrc())); 409 | trkNode.appendChild(node); 410 | } 411 | // TODO: write link 412 | if (trk.getNumber() != null) { 413 | Node node = doc.createElement(GPXConstants.NUMBER_NODE); 414 | node.appendChild(doc.createTextNode(trk.getNumber().toString())); 415 | trkNode.appendChild(node); 416 | } 417 | if (trk.getType() != null) { 418 | Node node = doc.createElement(GPXConstants.TYPE_NODE); 419 | node.appendChild(doc.createTextNode(trk.getType())); 420 | trkNode.appendChild(node); 421 | } 422 | if (trk.getExtensionsParsed() > 0) { 423 | Node node = doc.createElement(GPXConstants.EXTENSIONS_NODE); 424 | Iterator it = this.extensionParsers.iterator(); 425 | while (it.hasNext()) { 426 | it.next().writeTrackExtensionData(node, trk, doc); 427 | } 428 | trkNode.appendChild(node); 429 | } 430 | if (trk.getTrackPoints() != null) { 431 | Node trksegNode = doc.createElement(GPXConstants.TRKSEG_NODE); 432 | Iterator it = trk.getTrackPoints().iterator(); 433 | while (it.hasNext()) { 434 | this.addGenericWaypointToGPXNode(GPXConstants.TRKPT_NODE, 435 | it.next(), trksegNode, doc); 436 | } 437 | trkNode.appendChild(trksegNode); 438 | } 439 | gpxNode.appendChild(trkNode); 440 | } 441 | 442 | private void addWaypointToGPXNode(Waypoint wpt, Node gpxNode, Document doc) { 443 | this.addGenericWaypointToGPXNode(GPXConstants.WPT_NODE, wpt, gpxNode, 444 | doc); 445 | } 446 | 447 | private Date getNodeValueAsDate(Node node) throws DOMException, 448 | ParseException { 449 | // 2012-02-25T09:28:45Z 450 | Date val = null; 451 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss"); 452 | val = sdf.parse(node.getFirstChild().getNodeValue()); 453 | 454 | return val; 455 | } 456 | 457 | private Double getNodeValueAsDouble(Node node) { 458 | return Double.parseDouble(node.getFirstChild().getNodeValue()); 459 | } 460 | 461 | private FixType getNodeValueAsFixType(Node node) { 462 | return FixType.returnType(node.getFirstChild().getNodeValue()); 463 | } 464 | 465 | private Integer getNodeValueAsInteger(Node node) { 466 | return Integer.parseInt(node.getFirstChild().getNodeValue()); 467 | } 468 | 469 | private String getNodeValueAsString(Node node) { 470 | if (node == null) { 471 | return null; 472 | } 473 | 474 | Node child = node.getFirstChild(); 475 | if (child == null) { 476 | return null; 477 | } 478 | return child.getNodeValue(); 479 | } 480 | 481 | private Route parseRoute(Node node) throws Exception { 482 | if (node == null) { 483 | return null; 484 | } 485 | Route rte = new Route(); 486 | NodeList nodes = node.getChildNodes(); 487 | if (nodes != null) { 488 | for (int idx = 0; idx < nodes.getLength(); idx++) { 489 | Node currentNode = nodes.item(idx); 490 | if (GPXConstants.NAME_NODE.equals(currentNode.getNodeName())) { 491 | rte.setName(this.getNodeValueAsString(currentNode)); 492 | } else if (GPXConstants.CMT_NODE.equals(currentNode 493 | .getNodeName())) { 494 | rte.setComment(this.getNodeValueAsString(currentNode)); 495 | } else if (GPXConstants.DESC_NODE.equals(currentNode 496 | .getNodeName())) { 497 | rte.setDescription(this.getNodeValueAsString(currentNode)); 498 | } else if (GPXConstants.SRC_NODE.equals(currentNode 499 | .getNodeName())) { 500 | rte.setSrc(this.getNodeValueAsString(currentNode)); 501 | } else if (GPXConstants.LINK_NODE.equals(currentNode 502 | .getNodeName())) { 503 | // TODO: parse link 504 | // rte.setLink(getNodeValueAsLink(currentNode)); 505 | } else if (GPXConstants.NUMBER_NODE.equals(currentNode 506 | .getNodeName())) { 507 | rte.setNumber(this.getNodeValueAsInteger(currentNode)); 508 | } else if (GPXConstants.TYPE_NODE.equals(currentNode 509 | .getNodeName())) { 510 | rte.setType(this.getNodeValueAsString(currentNode)); 511 | } else if (GPXConstants.RTEPT_NODE.equals(currentNode 512 | .getNodeName())) { 513 | Waypoint wp = this.parseWaypoint(currentNode); 514 | if (wp != null) { 515 | rte.addRoutePoint(wp); 516 | } 517 | } else if (GPXConstants.EXTENSIONS_NODE.equals(currentNode 518 | .getNodeName())) { 519 | for ( IExtensionParser extensionParser : extensionParsers ) 520 | { 521 | Object data = extensionParser.parseRouteExtension( currentNode ); 522 | if (data != null) rte.addExtensionData( extensionParser.getId( ), data ); 523 | } 524 | } 525 | } 526 | } 527 | 528 | return rte; 529 | } 530 | 531 | private Track parseTrack(Node node) throws Exception { 532 | if (node == null) { 533 | return null; 534 | } 535 | Track trk = new Track(); 536 | NodeList nodes = node.getChildNodes(); 537 | if (nodes != null) { 538 | for (int idx = 0; idx < nodes.getLength(); idx++) { 539 | Node currentNode = nodes.item(idx); 540 | if (GPXConstants.NAME_NODE.equals(currentNode.getNodeName())) { 541 | trk.setName(this.getNodeValueAsString(currentNode)); 542 | } else if (GPXConstants.CMT_NODE.equals(currentNode 543 | .getNodeName())) { 544 | trk.setComment(this.getNodeValueAsString(currentNode)); 545 | } else if (GPXConstants.DESC_NODE.equals(currentNode 546 | .getNodeName())) { 547 | trk.setDescription(this.getNodeValueAsString(currentNode)); 548 | } else if (GPXConstants.SRC_NODE.equals(currentNode 549 | .getNodeName())) { 550 | trk.setSrc(this.getNodeValueAsString(currentNode)); 551 | } else if (GPXConstants.LINK_NODE.equals(currentNode 552 | .getNodeName())) { 553 | // TODO: parse link 554 | // trk.setLink(getNodeValueAsLink(currentNode)); 555 | } else if (GPXConstants.NUMBER_NODE.equals(currentNode 556 | .getNodeName())) { 557 | trk.setNumber(this.getNodeValueAsInteger(currentNode)); 558 | } else if (GPXConstants.TYPE_NODE.equals(currentNode 559 | .getNodeName())) { 560 | trk.setType(this.getNodeValueAsString(currentNode)); 561 | } else if (GPXConstants.TRKSEG_NODE.equals(currentNode 562 | .getNodeName())) { 563 | ArrayList segment = this.parseTrackSeg(currentNode); 564 | trk.addTrackPoints(segment); 565 | trk.addTrackSegment(segment); 566 | } else if (GPXConstants.EXTENSIONS_NODE.equals(currentNode 567 | .getNodeName())) { 568 | for ( IExtensionParser extensionParser : extensionParsers ) 569 | { 570 | Object data = extensionParser.parseTrackExtension( currentNode ); 571 | if (data != null) trk.addExtensionData( extensionParser.getId( ), data ); 572 | } 573 | } 574 | } 575 | } 576 | 577 | return trk; 578 | } 579 | 580 | private ArrayList parseTrackSeg(Node node) throws Exception { 581 | if (node == null) { 582 | return null; 583 | } 584 | ArrayList trkpts = new ArrayList(); 585 | 586 | NodeList nodes = node.getChildNodes(); 587 | if (nodes != null) { 588 | for (int idx = 0; idx < nodes.getLength(); idx++) { 589 | Node currentNode = nodes.item(idx); 590 | if (GPXConstants.TRKPT_NODE.equals(currentNode.getNodeName())) { 591 | Waypoint wp = this.parseWaypoint(currentNode); 592 | if (wp != null) { 593 | trkpts.add(wp); 594 | } 595 | } else if (GPXConstants.EXTENSIONS_NODE.equals(currentNode 596 | .getNodeName())) { 597 | /* 598 | * Iterator it = 599 | * extensionParsers.iterator(); while(it.hasNext()) { 600 | * IExtensionParser parser = it.next(); Object data = 601 | * parser.parseWaypointExtension(currentNode); 602 | * //.addExtensionData(parser.getId(), data); } 603 | */ 604 | } 605 | } 606 | } 607 | return trkpts; 608 | } 609 | 610 | /** 611 | * Parses a wpt node into a Waypoint object 612 | * 613 | * @param node 614 | * @return Waypoint object with info from the received node 615 | * @throws Exception 616 | */ 617 | private Waypoint parseWaypoint(Node node) throws Exception { 618 | if (node == null) { 619 | return null; 620 | } 621 | Waypoint w = new Waypoint(null, 0, 0); 622 | NamedNodeMap attrs = node.getAttributes(); 623 | // check for lat attribute 624 | Node latNode = attrs.getNamedItem(GPXConstants.LAT_ATTR); 625 | if (latNode != null) { 626 | Double latVal = null; 627 | latVal = Double.parseDouble(latNode.getNodeValue()); 628 | w.setLatitude(latVal); 629 | } else { 630 | throw new Exception("no lat value in waypoint data."); 631 | } 632 | // check for lon attribute 633 | Node lonNode = attrs.getNamedItem(GPXConstants.LON_ATTR); 634 | if (lonNode != null) { 635 | Double lonVal = Double.parseDouble(lonNode.getNodeValue()); 636 | w.setLongitude(lonVal); 637 | } else { 638 | throw new Exception("no lon value in waypoint data."); 639 | } 640 | 641 | NodeList childNodes = node.getChildNodes(); 642 | if (childNodes != null) { 643 | for (int idx = 0; idx < childNodes.getLength(); idx++) { 644 | Node currentNode = childNodes.item(idx); 645 | if (GPXConstants.ELE_NODE.equals(currentNode.getNodeName())) { 646 | w.setElevation(this.getNodeValueAsDouble(currentNode)); 647 | } else if (GPXConstants.TIME_NODE.equals(currentNode 648 | .getNodeName())) { 649 | w.setTime(this.getNodeValueAsDate(currentNode)); 650 | } else if (GPXConstants.NAME_NODE.equals(currentNode 651 | .getNodeName())) { 652 | w.setName(this.getNodeValueAsString(currentNode)); 653 | } else if (GPXConstants.CMT_NODE.equals(currentNode 654 | .getNodeName())) { 655 | w.setComment(this.getNodeValueAsString(currentNode)); 656 | } else if (GPXConstants.DESC_NODE.equals(currentNode 657 | .getNodeName())) { 658 | w.setDescription(this.getNodeValueAsString(currentNode)); 659 | } else if (GPXConstants.SRC_NODE.equals(currentNode 660 | .getNodeName())) { 661 | w.setSrc(this.getNodeValueAsString(currentNode)); 662 | } else if (GPXConstants.MAGVAR_NODE.equals(currentNode 663 | .getNodeName())) { 664 | w.setMagneticDeclination(this 665 | .getNodeValueAsDouble(currentNode)); 666 | } else if (GPXConstants.GEOIDHEIGHT_NODE.equals(currentNode 667 | .getNodeName())) { 668 | w.setGeoidHeight(this.getNodeValueAsDouble(currentNode)); 669 | } else if (GPXConstants.LINK_NODE.equals(currentNode 670 | .getNodeName())) { 671 | // TODO: parse link 672 | // w.setGeoidHeight(getNodeValueAsDouble(currentNode)); 673 | } else if (GPXConstants.SYM_NODE.equals(currentNode 674 | .getNodeName())) { 675 | w.setSym(this.getNodeValueAsString(currentNode)); 676 | } else if (GPXConstants.FIX_NODE.equals(currentNode 677 | .getNodeName())) { 678 | w.setFix(this.getNodeValueAsFixType(currentNode)); 679 | } else if (GPXConstants.TYPE_NODE.equals(currentNode 680 | .getNodeName())) { 681 | w.setType(this.getNodeValueAsString(currentNode)); 682 | } else if (GPXConstants.SAT_NODE.equals(currentNode 683 | .getNodeName())) { 684 | w.setSat(this.getNodeValueAsInteger(currentNode)); 685 | } else if (GPXConstants.HDOP_NODE.equals(currentNode 686 | .getNodeName())) { 687 | w.setHdop(this.getNodeValueAsDouble(currentNode)); 688 | } else if (GPXConstants.VDOP_NODE.equals(currentNode 689 | .getNodeName())) { 690 | w.setVdop(this.getNodeValueAsDouble(currentNode)); 691 | } else if (GPXConstants.PDOP_NODE.equals(currentNode 692 | .getNodeName())) { 693 | w.setPdop(this.getNodeValueAsDouble(currentNode)); 694 | } else if (GPXConstants.AGEOFGPSDATA_NODE.equals(currentNode 695 | .getNodeName())) { 696 | w.setAgeOfGPSData(this.getNodeValueAsDouble(currentNode)); 697 | } else if (GPXConstants.DGPSID_NODE.equals(currentNode 698 | .getNodeName())) { 699 | w.setDgpsid(this.getNodeValueAsInteger(currentNode)); 700 | } else if (GPXConstants.EXTENSIONS_NODE.equals(currentNode 701 | .getNodeName())) { 702 | Iterator it = this.extensionParsers 703 | .iterator(); 704 | while (it.hasNext()) { 705 | IExtensionParser parser = it.next(); 706 | Object data = parser 707 | .parseWaypointExtension(currentNode); 708 | w.addExtensionData(parser.getId(), data); 709 | } 710 | } 711 | } 712 | } 713 | 714 | return w; 715 | } 716 | } 717 | --------------------------------------------------------------------------------