A simple gpx parser and builder between GPX string and JavaScript object. It is dependent on isomorphic-xml2js.
gpx-parser-builder is written with ECMAScript 6. You can leverage Babel and Webpack to make all browsers available.
npm install gpx-parser-builder --save
v1.0.0+ is a breaking change for v0.2.2-. v1.0.0+ fully supports gpx files including waypoints, routes, and tracks. Every gpx type is 1-1 corresponding to a JavaScript class.
import GPX from 'gpx-parser-builder';
// Parse gpx
const gpx = GPX.parse('GPX_STRING');
window.console.dir(gpx.metadata);
window.console.dir(gpx.wpt);
window.console.dir(gpx.trk);
// Build gpx
window.console.log(gpx.toString());
Get more details about usage with the unit tests.
The GPX JavaScript object.
constructor(object)
const gpx = new Gpx({$:{...}, metadat: {...}, wpt:[{...},{...}]}, trk: {...}, rte: {...})
$
the attributes for the gpx element. Default value:
{
'version': '1.1',
'creator': 'gpx-parser-builder',
'xmlns': 'http://www.topografix.com/GPX/1/1',
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'xsi:schemaLocation': 'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd'
}
metadata
the metadata for the gpx.
wpt
array of waypoints. It is corresponded to <wpt>
. The type of all elements in wpt
is Waypoint
;
rte
array of routes. It is corresponded to <rte>
. The type of all elements in rte
is Route
;
trk
array of tracks. It is corresponded to <trk>
. The type of all elements in trk
is Track
;
parse(gpxString)
parse gpx string to Gpx object. return null
if parsing failed.
toString(options)
GPX object to gpx string. The options is for isomorphic-xml2js.
You can leverage StreamSaver.js or FileSaver.js to save as GPX file.
Zheng-Xiang Ke, [email protected]
gpx-parser-builder is available under the MIT license. See the LICENSE file for more info.