2

I try to save Points with OGR. It works well but I lose the z dimension while saving/loading.

from osgeo import ogr
from osgeo import osr

shapePath="...xxx.shp"
X='1.0'
Y='2.0'
Z='3.0'

driver = ogr.GetDriverByName('ESRI Shapefile')

srs = osr.SpatialReference()
srs.ImportFromWkt(mySRSinwkt)

dataSource = driver.Open(shapePath, 1)
layer = dataSource.CreateLayer("points", srs, geom_type=ogr.wkbPoint)
layer.CreateField(ogr.FieldDefn("Point_ID", ogr.OFTInteger))

# Set feature fields 
feature = ogr.Feature(olayer.GetLayerDefn())
feature.SetField("Point_ID", 1)

# Set geometry
geom_wkt = 'POINT (' + X + ' ' + Y + ' ' + Z + ')'
point = ogr.CreateGeometryFromWkt(geom_wkt)
feature.SetGeometry(point)

#Point has z dimension
print str(feature.GetGeometryRef())

# Create feature            
layer.CreateFeature(feature)

# remove feature and datasource from memory
feature.Destroy()
dataSource.Destroy()

# Load
dataSource = driver.Open(shapePath, 0)
layer = dataSource.GetLayer()

# Point has no z dimension anymore
for feature in layer:
    print str(feature.GetGeometryRef())

dataSource = None
2

You should use

layer = dataSource.CreateLayer("points", srs, geom_type=ogr.wkbPoint25D)

since ogr.wkbPoint is 2D only.

|improve this answer|||||
  • Works nicely! Ty! Do you know why they 2.5D instead of 3D to name the geometries? Because it is really confusing! – Nono Mar 31 '16 at 9:22

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.