Skip to content

Commit 4d8bb8b

Browse files
committed
[ogr] Read GPX elevation values as geometry Z values
(cherry-picked from 4080aad)
1 parent 7e85d29 commit 4d8bb8b

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

src/providers/ogr/qgsogrprovider.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2841,6 +2841,7 @@ void QgsOgrProvider::open( OpenMode mode )
28412841
QgsDebugMsg( "mLayerName: " + mLayerName );
28422842
QgsDebugMsg( "mSubsetString: " + mSubsetString );
28432843
CPLSetConfigOption( "OGR_ORGANIZE_POLYGONS", "ONLY_CCW" ); // "SKIP" returns MULTIPOLYGONs for multiringed POLYGONs
2844+
CPLSetConfigOption( "GPX_ELE_AS_25D", "YES" ); // use GPX elevation as z values
28442845

28452846
if ( mFilePath.startsWith( "MySQL:" ) && !mLayerName.isEmpty() && !mFilePath.endsWith( ",tables=" + mLayerName ) )
28462847
{

tests/src/python/test_provider_ogr.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,23 @@ def testNoDanglingFileDescriptorAfterCloseVariant2(self):
153153
os.unlink(datasource)
154154
self.assertFalse(os.path.exists(datasource))
155155

156+
def testGpxElevation(self):
157+
# GPX without elevation data
158+
datasource = os.path.join(TEST_DATA_DIR, 'noelev.gpx')
159+
vl = QgsVectorLayer(u'{}|layername=routes'.format(datasource), u'test', u'ogr')
160+
self.assertTrue(vl.isValid())
161+
f = next(vl.getFeatures())
162+
self.assertEqual(f.constGeometry().geometry().wkbType(), QgsWKBTypes.LineString)
163+
164+
# GPX with elevation data
165+
datasource = os.path.join(TEST_DATA_DIR, 'elev.gpx')
166+
vl = QgsVectorLayer(u'{}|layername=routes'.format(datasource), u'test', u'ogr')
167+
self.assertTrue(vl.isValid())
168+
f = next(vl.getFeatures())
169+
self.assertEqual(f.constGeometry().geometry().wkbType(), QgsWKBTypes.LineString25D)
170+
self.assertEqual(f.constGeometry().geometry().pointN(0).z(), 1)
171+
self.assertEqual(f.constGeometry().geometry().pointN(1).z(), 2)
172+
self.assertEqual(f.constGeometry().geometry().pointN(2).z(), 3)
173+
156174
if __name__ == '__main__':
157175
unittest.main()

tests/testdata/elev.gpx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<gpx version="1.1" creator="GDAL 1.11.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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">
3+
<metadata><bounds minlat="56.134840267383296" minlon="-3.877982991747363" maxlat="56.135514185147493" maxlon="-3.852565020533009"/></metadata>
4+
<rte>
5+
<rtept lat="56.134840267383296" lon="-3.877982991747363">
6+
<ele>1</ele>
7+
</rtept>
8+
<rtept lat="56.134933668432737" lon="-3.865962243986197">
9+
<ele>2</ele>
10+
</rtept>
11+
<rtept lat="56.135514185147493" lon="-3.852565020533009">
12+
<ele>3</ele>
13+
</rtept>
14+
</rte>
15+
</gpx>

tests/testdata/noelev.gpx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<gpx version="1.1" creator="GDAL 1.11.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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">
3+
<metadata><bounds minlat="56.134840267383240" minlon="-3.877982991747359" maxlat="56.135514185147436" maxlon="-3.852565020533005"/></metadata>
4+
<rte>
5+
<rtept lat="56.13484026738324" lon="-3.877982991747359">
6+
</rtept>
7+
<rtept lat="56.13493366843268" lon="-3.865962243986193">
8+
</rtept>
9+
<rtept lat="56.135514185147436" lon="-3.852565020533005">
10+
</rtept>
11+
</rte>
12+
</gpx>

0 commit comments

Comments
 (0)