Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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" )

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? Thank you very much.

share|improve this question
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
up vote 4 down vote accepted

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'
share|improve this answer
Ooops. Thank you very much! :-) – SonOfabox Jun 26 '12 at 9:25

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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