Class: GPX

ol/format/GPX~GPX


import GPX from 'ol/format/GPX.js';

Feature format for reading and writing data in the GPX format.

Note that readFeature only reads the first feature of the source.

When reading, routes (<rte>) are converted into LineString geometries, and tracks (<trk>) into MultiLineString. Any properties on route and track waypoints are ignored.

When writing, LineString geometries are output as routes (<rte>), and MultiLineString as tracks (<trk>).

new GPX(options)

Name Type Description
readExtensions function | undefined

Callback function to process extensions nodes. To prevent memory leaks, this callback function must not store any references to the node. Note that the extensions node is not allowed in GPX 1.0. Moreover, only extensions nodes from wpt, rte and trk can be processed, as those are directly mapped to a feature.

Extends

Methods

readFeature(source, options){Feature} inherited

Read a single feature.

Name Type Description
source Document | Element | Object | string

Source.

options

Read options.

Name Type Description
dataProjection ProjectionLike | undefined

Projection of the data we are reading. If not provided, the projection will be derived from the data (where possible) or the dataProjection of the format is assigned (where set). If the projection can not be derived from the data and if no dataProjection is set for a format, the features will not be reprojected.

extent Extent | undefined

Tile extent in map units of the tile being read. This is only required when reading data with tile pixels as geometry units. When configured, a dataProjection with TILE_PIXELS as units and the tile's pixel extent as extent needs to be provided.

featureProjection ProjectionLike | undefined

Projection of the feature geometries created by the format reader. If not provided, features will be returned in the dataProjection.

Returns:
Feature.

readFeatures(source, options){Array<Feature>} inherited

Read all features from a feature collection.

Name Type Description
source Document | Element | Object | string

Source.

options

Options.

Name Type Description
dataProjection ProjectionLike | undefined

Projection of the data we are reading. If not provided, the projection will be derived from the data (where possible) or the dataProjection of the format is assigned (where set). If the projection can not be derived from the data and if no dataProjection is set for a format, the features will not be reprojected.

extent Extent | undefined

Tile extent in map units of the tile being read. This is only required when reading data with tile pixels as geometry units. When configured, a dataProjection with TILE_PIXELS as units and the tile's pixel extent as extent needs to be provided.

featureProjection ProjectionLike | undefined

Projection of the feature geometries created by the format reader. If not provided, features will be returned in the dataProjection.

Returns:
Features.

readMetadata(source){GPXMetadata | null}

Reads a GPX file's metadata tag, reading among other things:

  • the name and description of this GPX
  • its author
  • the copyright associated with this GPX file

Will return null if no metadata tag is present (or no valid source is given).

Name Type Description
source Document | Element | Object | string

Source.

Returns:
Metadata

Read the projection from the source.

Name Type Description
source Document | Element | Object | string

Source.

Returns:
Projection.

writeFeatures(features, options){string} inherited

Encode an array of features as string.

Name Type Description
features Array<Feature>

Features.

options

Write options.

Name Type Description
dataProjection ProjectionLike | undefined

Projection of the data we are writing. If not provided, the dataProjection of the format is assigned (where set). If no dataProjection is set for a format, the features will be returned in the featureProjection.

featureProjection ProjectionLike | undefined

Projection of the feature geometries that will be serialized by the format writer. If not provided, geometries are assumed to be in the dataProjection if that is set; in other words, they are not transformed.

rightHanded boolean | undefined

When writing geometries, follow the right-hand rule for linear ring orientation. This means that polygons will have counter-clockwise exterior rings and clockwise interior rings. By default, coordinates are serialized as they are provided at construction. If true, the right-hand rule will be applied. If false, the left-hand rule will be applied (clockwise for exterior and counter-clockwise for interior rings). Note that not all formats support this. The GeoJSON format does use this property when writing geometries.

decimals number | undefined

Maximum number of decimal places for coordinates. Coordinates are stored internally as floats, but floating-point arithmetic can create coordinates with a large number of decimal places, not generally wanted on output. Set a number here to round coordinates. Can also be used to ensure that coordinates read in can be written back out with the same number of decimals. Default is no rounding.

Returns:
Result.

writeFeaturesNode(features, options){Node}

Encode an array of features in the GPX format as an XML node. LineString geometries are output as routes (<rte>), and MultiLineString as tracks (<trk>).

Name Type Description
features Array<Feature>

Features.

options

Options.

Name Type Description
dataProjection ProjectionLike | undefined

Projection of the data we are writing. If not provided, the dataProjection of the format is assigned (where set). If no dataProjection is set for a format, the features will be returned in the featureProjection.

featureProjection ProjectionLike | undefined

Projection of the feature geometries that will be serialized by the format writer. If not provided, geometries are assumed to be in the dataProjection if that is set; in other words, they are not transformed.

rightHanded boolean | undefined

When writing geometries, follow the right-hand rule for linear ring orientation. This means that polygons will have counter-clockwise exterior rings and clockwise interior rings. By default, coordinates are serialized as they are provided at construction. If true, the right-hand rule will be applied. If false, the left-hand rule will be applied (clockwise for exterior and counter-clockwise for interior rings). Note that not all formats support this. The GeoJSON format does use this property when writing geometries.

decimals number | undefined

Maximum number of decimal places for coordinates. Coordinates are stored internally as floats, but floating-point arithmetic can create coordinates with a large number of decimal places, not generally wanted on output. Set a number here to round coordinates. Can also be used to ensure that coordinates read in can be written back out with the same number of decimals. Default is no rounding.

Returns:
Node.