Merging gpx files

Objective: collect all GPS waypoints and tracks for the Tombstone hikes on YukonHiking.ca into a single map.

screenshot of Tombstone Hikes page

(Prep: download the GPX files. Thank them for the raw material by buying the book.)

Merge tracks

ogrmerge -o merged.gpkg *.gpx -single -update -overwrite_layer ^
  -src_layer_field_name Source -src_layer_field_content {AUTO_NAME} ^
  -src_geom_type multilinestring -nln Tracks

Merge all points

ogrmerge -o merged.gpkg *.gpx -single -update -overwrite_layer ^
  -src_layer_field_name Source -src_layer_field_content {AUTO_NAME} ^
  -src_geom_type point -nln Points

Extract waypoints from all points into own layer

ogr2ogr merged.gpkg merged.gpkg -nln Waypoints ^
  -sql "SELECT * FROM Points WHERE Source LIKE '%Way%'" -update

<<manually delete empty columns>>

Convert only tracks and waypoints back to GPX for adding Google Maps

ogr2ogr merged.gpx merged.gpkg -dsco GPX_USE_EXTENSIONS=YES waypoints tracks

Result

Tombstone Hikes – Hikes within Tombstone Territorial Park from www.yukonhiking.ca (circa July 2021).preview of the Tombstone Hikes google map

Thanks for the help

Ogrmerge does the heavy lifting. I came back to this page twenty times while building out the recipe.

Concatenate the content of france.shp and germany.shp in merged.shp, and adds a ‘country’ field to each feature whose value is ‘france’ or ‘germany’ depending where it comes from.

ogrmerge.py -single -o merged.shp france.shp germany.shp ^
  -src_layer_field_name country

From < https://gdal.org/programs/ogrmerge.html#ogrmerge>

Perform a join between 2 GeoPackage databases:

ogrinfo my_spatial.gpkg \
  -sql "SELECT poly.id, other.foo FROM poly JOIN other_schema.other USING (id)" \
  -oo PRELUDE_STATEMENTS="ATTACH DATABASE 'other.gpkg' AS other_schema"

From < https://gdal.org/drivers/vector/gpkg.html#vector-gpkg>

I needed help refining my sql syntax. This example got me to the end:

query='SELECT * FROM lut_data WHERE LUT ="CHF"'

From < https://gis.stackexchange.com/questions/341718/how-do-i-read-a-layer-from-a-gpkg-file-whilst-selecting-on-an-attribute>

This is a little less wordy than the ogrinfo sql approach, but I didn’t find this method until after. Noting it here to remind myself to try it later.

ogr2ogr can also filter and edit geodata. A common application is the limitation of a data set to a certain area. This can be created with the option -spat:

ogr2ogr \
  -spat -13.931 34.886 46.23 74.12 \
  -f GPKG output.gpkg \
  natural_earth_vector.gpkg

If you only want certain features of a layer, you can define them with -where. This example only returns countries with less than 1 million inhabitants:

ogr2ogr \
  -where "\"POP_EST\" < 1000000" \
  -f GPKG output.gpkg \
  natural_earth_vector.gpkg \
  ne_10m_admin_0_countries

From < https://jakobmiksch.eu/post/gdal_ogr/>

I needed this plus “-dsco GPX_USE_EXTENSIONS=YES” to avoid ERROR6

The ogr2ogr utility can be used to do GPX to GPX translation:

ogr2ogr -f GPX output.gpx input.gpx waypoints routes tracks

Note : in the case of GPX to GPX translation, you need to specify the layer names, in order to discard the route_points and track_points layers.

From < https://gdal.org/drivers/vector/gpx.html>

Further exploration

I have a Shapefile with a lot of empty columns but many of them are empty. Using QGIS or ogr2ogr, how do I remove all attribute columns where everything is NULL?

From < https://gis.stackexchange.com/questions/113471/how-to-delete-empty-fields-from-a-shapefile-with-qgis-or-ogr2ogr>

This question has been asked before but I couldn’t get the solution to work. I have a shapefile with lots of empty columns and I would like to remove them all with a script or some tools.

From < https://gis.stackexchange.com/questions/225010/how-do-i-delete-empty-fields-from-a-shapefile-with-qgis?noredirect=1&lq=1>

 

Leave a Reply

Your email address will not be published. Required fields are marked *