Skip to content

Commit

Permalink
[ogr] Read GPX elevation values as geometry Z values
Browse files Browse the repository at this point in the history
(cherry-picked from 4080aad)
  • Loading branch information
nyalldawson committed Jul 1, 2016
1 parent 7e85d29 commit 4d8bb8b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2841,6 +2841,7 @@ void QgsOgrProvider::open( OpenMode mode )
QgsDebugMsg( "mLayerName: " + mLayerName );
QgsDebugMsg( "mSubsetString: " + mSubsetString );
CPLSetConfigOption( "OGR_ORGANIZE_POLYGONS", "ONLY_CCW" ); // "SKIP" returns MULTIPOLYGONs for multiringed POLYGONs
CPLSetConfigOption( "GPX_ELE_AS_25D", "YES" ); // use GPX elevation as z values

if ( mFilePath.startsWith( "MySQL:" ) && !mLayerName.isEmpty() && !mFilePath.endsWith( ",tables=" + mLayerName ) )
{
Expand Down
18 changes: 18 additions & 0 deletions tests/src/python/test_provider_ogr.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,23 @@ def testNoDanglingFileDescriptorAfterCloseVariant2(self):
os.unlink(datasource)
self.assertFalse(os.path.exists(datasource))

def testGpxElevation(self):
# GPX without elevation data
datasource = os.path.join(TEST_DATA_DIR, 'noelev.gpx')
vl = QgsVectorLayer(u'{}|layername=routes'.format(datasource), u'test', u'ogr')
self.assertTrue(vl.isValid())
f = next(vl.getFeatures())
self.assertEqual(f.constGeometry().geometry().wkbType(), QgsWKBTypes.LineString)

# GPX with elevation data
datasource = os.path.join(TEST_DATA_DIR, 'elev.gpx')
vl = QgsVectorLayer(u'{}|layername=routes'.format(datasource), u'test', u'ogr')
self.assertTrue(vl.isValid())
f = next(vl.getFeatures())
self.assertEqual(f.constGeometry().geometry().wkbType(), QgsWKBTypes.LineString25D)
self.assertEqual(f.constGeometry().geometry().pointN(0).z(), 1)
self.assertEqual(f.constGeometry().geometry().pointN(1).z(), 2)
self.assertEqual(f.constGeometry().geometry().pointN(2).z(), 3)

if __name__ == '__main__':
unittest.main()
15 changes: 15 additions & 0 deletions tests/testdata/elev.gpx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<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">
<metadata><bounds minlat="56.134840267383296" minlon="-3.877982991747363" maxlat="56.135514185147493" maxlon="-3.852565020533009"/></metadata>
<rte>
<rtept lat="56.134840267383296" lon="-3.877982991747363">
<ele>1</ele>
</rtept>
<rtept lat="56.134933668432737" lon="-3.865962243986197">
<ele>2</ele>
</rtept>
<rtept lat="56.135514185147493" lon="-3.852565020533009">
<ele>3</ele>
</rtept>
</rte>
</gpx>
12 changes: 12 additions & 0 deletions tests/testdata/noelev.gpx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<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">
<metadata><bounds minlat="56.134840267383240" minlon="-3.877982991747359" maxlat="56.135514185147436" maxlon="-3.852565020533005"/></metadata>
<rte>
<rtept lat="56.13484026738324" lon="-3.877982991747359">
</rtept>
<rtept lat="56.13493366843268" lon="-3.865962243986193">
</rtept>
<rtept lat="56.135514185147436" lon="-3.852565020533005">
</rtept>
</rte>
</gpx>

0 comments on commit 4d8bb8b

Please sign in to comment.