1

Using the GDAL/OGR command line, how could one add some fields to a GPX file ?

I simply need to add the gpx filename to its content, e.g. in a new field called datasource in each (there are several in a file):

<?xml version="1.0"?>
<gpx version="1.1" creator="GDAL 3.4.1" 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">
<metadata><bounds minlat="41.300000000000003" minlon="10.900000000000000" maxlat="69.700000000000000" maxlon="31.000000000000000"/></metadata>                   
<trk>
  <name>001: Test layer 1</name>
  <desc>Layer one</desc>
  <!-- I want to add the following 3 lines: --> 
  <extensions>
    <ogr:datasource>GPX_Filename</ogr:datasource>
  </extensions>
  <trkseg>
    ...
  </trkseg>
</trk>
<trk>
  <name>002 Test layer 2</name>
  <desc>Layer two</desc>
  <!-- I want to add the following 3 lines: --> 
  <extensions>
    <ogr:datasource>GPX_Filename</ogr:datasource>
  </extensions>
  <trkseg>
    ...
  </trkseg>
</trk>
...
</gpx>

How would can I automate this using the command line (on Linux) ?

GDAL version: GDAL 3.4.1, released 2021/12/27

1 Answer 1

0

I've implemented a quick workaround in bash, because I wasn't able to figure out a way to directly achieve that using GDAL. It's based on a tool called xmlstarlet which allow to manipulate an XML file using the command line.

#!/bin/bash

for f in *.gpx;
do
  xmlstarlet edit \
    --inplace \
    -N x="http://www.topografix.com/GPX/1/1" \
    --insert "/x:gpx" --type attr -n "xmlns:ogr" -v "http://osgeo.org/gdal" \
    --append "/x:gpx/x:trk/x:desc" --type  elem -n "extensions" \
    --subnode "///extensions" --type elem -n "ogr:datasource" -v "${f%%.*}" \
    "${f}"
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.