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日

asked Nov 19, 2023 at 21:10

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
answered Nov 25, 2023 at 12:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.