0

I'm trying to parse a shapefile with ogr and export the values to WKT. The geometries contain a m value but this is not showing in the WKT output.

Does ogr support 4D/m values?

# My code
from osgeo import ogr
shp = ogr.Open(shapefile.shp)
lyr = shp.GetLayer()
for feature in lyr:
   # output - 'LINESTRING (557729 4970394 1869,557024 4970372 1806, ...)'
   # It should be - 'LINESTRING (557729 4970394 1869 1,557024 4970372 1806 2, ...)'
   print(feature.GetGeometryRef().ExportToWkt())
1

Since the 2.1 version, ogr support 4D/m values

wkt = "POINT (557729 4970394 1869 1)"
point = ogr.CreateGeometryFromWkt(wkt)
point.GetPointZM()
(557729.0, 4970394.0, 1869.0, 1.0)
point.GetX()
557729.0
point.GetY()
4970394.0
point.GetZ()
1869.0
point.GetM()
1.0

But

point.ExportToWkt()
'POINT (557729 4970394 1869)'
# and GeoJson
point.ExportToJSON()
'{ "type": "Point", "coordinates": [ 557729.0, 4970394.0, 1869.0 ] }'
  • Upgrading to v2.x and realizing that support for M is still limited (not available in ExportToWkt etc) helped. I was able to extract the M value by breaking the line into points and using GetM(). Thanks! – Matt Jul 24 '17 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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