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
  • Loading branch information
nyalldawson committed Jun 27, 2016
1 parent 2e168ef commit 4080aad
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 @@ -3073,6 +3073,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 @@ -116,6 +116,24 @@ def testMixOfLineStringCompoundCurve(self):
self.assertEqual(len(vl.dataProvider().subLayers()), 1)
self.assertEqual(vl.dataProvider().subLayers()[0], '0:testMixOfLineStringCompoundCurve:5:CompoundCurve')

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 4080aad

Please sign in to comment.