OGR has support for GPX reading (if GDAL is build with expat library support) and writing.
Version supported are GPX 1.0 and 1.1 for reading, GPX 1.1 for writing.
The OGR driver supports reading and writing of all the GPX feature types :
In addition to its GPX attributes, each route point of a route has a route_fid
(foreign key to the FID of its belonging route)
and a route_point_id which is its sequence number in the route.
The same applies for track points with track_fid, track_seg_id and track_seg_point_id.
All coordinates are relative to the WGS84 datum (EPSG:4326).
If the environment variable GPX_ELE_AS_25D is set to YES, the elevation element will be used to set the Z coordinates of waypoints, route points and track points.
The OGR/GPX reads and writes the GPX attributes for the waypoints, routes and tracks.
By default, up to 2 <link> elements can be taken into account by feature. This default number can be changed with the GPX_N_MAX_LINKS environment variable.
The following sequence GPX content :
<extensions> <navaid:name>TOTAL RF</navaid:name> <navaid:address>BENSALEM</navaid:address> <navaid:state>PA</navaid:state> <navaid:country>US</navaid:country> <navaid:frequencies> <navaid:frequency type="CTAF" frequency="122.900" name="CTAF"/> </navaid:frequencies> <navaid:runways> <navaid:runway designation="H1" length="80" width="80" surface="ASPH-G"> </navaid:runway> </navaid:runways> <navaid:magvar>12</navaid:magvar> </extensions>will be interpreted in the OGR SF model as :
navaid_name (String) = TOTAL RF navaid_address (String) = BENSALEM navaid_state (String) = PA navaid_country (String) = US navaid_frequencies (String) = <navaid:frequency type="CTAF" frequency="122.900" name="CTAF" ></navaid:frequency> navaid_runways (String) = <navaid:runway designation="H1" length="80" width="80" surface="ASPH-G" ></navaid:runway> navaid_magvar (Integer) = 12
Note : the GPX driver will output content of the extensions element only if it is found in the first records of the GPX file.
If extensions appear later, you can force an explicit parsing of the whole file with the GPX_USE_EXTENSIONS environment variable.
If the output file already exits, the writing will not occur. You have to delete the existing file first.
Supported geometries :
For route points and tracks points, if there is a Z coordinate, it is used to fill the elevation element of the corresponding points.
The GPX writer supports the following layer creation options:
The GPX writer supports the following dataset creation options:
Waypoints, routes and tracks must be written into that order to be valid against the XML Schema.
When translating from a source dataset, it may be necessary to rename the field names from the source dataset to the expected GPX attribute names, such as <name>, <desc>, etc... This can be done with a OGR VRT dataset, or by using the "-sql" option of the ogr2ogr utility.
ogrinfo -ro -al input.gpx
ogr2ogr -f GPX output.gpx input.gpx waypoints routes tracks
ogr2ogr -f GPX -dsco GPX_USE_EXTENSIONS=YES output.gpx inputwhich will give an output like the following one :
<?xml version="1.0"?> <gpx version="1.1" creator="GDAL 1.5dev" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ogr="http://osgeo.org/gdal" xmlns="http://www.topografix.com/GPX/1/1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"> <wpt lat="1" lon="2"> <extensions> <ogr:Primary_ID>PID5</ogr:Primary_ID> <ogr:Secondary_ID>SID5</ogr:Secondary_ID> </extensions> </wpt> <wpt lat="3" lon="4"> <extensions> <ogr:Primary_ID>PID4</ogr:Primary_ID> <ogr:Secondary_ID>SID4</ogr:Secondary_ID> </extensions> </wpt> </gpx>
ogr2ogr -f GPX output.gpx input.shp -sql "SELECT field1 AS name, field2 AS desc FROM source_layer"