1

I'm using Python and OGR to extract the elevation of lines within a DXF file.

import ogr

driver = ogr.GetDriverByName('DXF')
datasource = driver.Open('test1.dxf', 0)

layers=datasource.ExecuteSQL( "SELECT DISTINCT Layer FROM entities" )
layer=datasource.GetLayerByIndex(0)

for i in range(0, layers.GetFeatureCount()):
        layerName = layers.GetFeature(i).GetFieldAsString(0)
        layer.SetAttributeFilter( "Layer='%s'" % layerName)
        print 'Layer=%s|Features=%s' % (layerName, layer.GetFeatureCount())

    for j in range(0, layer.GetFeatureCount()):
      myFeature = layer.GetNextFeature()
      myElevation = myFeature.GetGeometryRef().getZ(0)

But I get an "AttributeError: getZ" error when executing it.

What am I doing wrong?

  • It would help if you would edit your question to include exactly what error you are receiving. – Get Spatial Jun 24 '12 at 19:30
4

Python is case sensitive and gdal/ogr doesn't really follow the Python PEP8 naming conventions. "getZ" is not an ogr.Geometry method, "GetZ" is what you are looking for.

>>> help(ogr.Geometry.GetZ)
Help on method GetZ in module osgeo.ogr:

GetZ(self, *args, **kwargs) unbound osgeo.ogr.Geometry method
    GetZ(self, int point = 0) -> double

>>> help(ogr.Geometry.getZ)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
AttributeError: type object 'Geometry' has no attribute 'getZ'
0

Finally I tried with this workaround

myGeomRefWkt = myFeature.GetGeometryRef().ExportToWkt()
myElevation = myGeomRefWkt[12:][:-1].split(",")[0].split(" ")[2]

to handle the string after "LINESTRING" and between parentheses,`and it works

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.