6

Please excuse my ignorance...I am new to Python OGR.

I am trying to create a simple square polygon and fail...this produces an empty polygon.

Where did I go wrong?

from osgeo import ogr
driver = ogr.GetDriverByName('ESRI Shapefile')
datasource = driver.CreateDataSource('c:/temp/testPolygon.shp')
layer = datasource.CreateLayer('layerName',geom_type=ogr.wkbPolygon)

#create ring and polygon object:
myRing = ogr.Geometry(type=ogr.wkbLinearRing)
myRing.AddPoint(0.0, 0.0)
myRing.AddPoint(0.0, 10.0)
myRing.AddPoint(10.0, 10.0)
myRing.AddPoint(0.0, 10.0)
myRing.AddPoint(0.0, 0.0)#close ring
myPoly = ogr.Geometry(type=ogr.wkbPolygon)
myPoly.AddGeometry
#this produces myPoly.GetArea() of zero!

#create feature polygon:
feature = ogr.Feature( layer.GetLayerDefn() )
feature.SetGeometry(myPoly)
layer.CreateFeature(feature)

Even the following returns an area of zero: from osgeo import ogr

#create polygon object:
myRing = ogr.Geometry(type=ogr.wkbLinearRing)
myRing.AddPoint(0.0, 0.0)#LowerLeft
myRing.AddPoint(0.0, 10.0)#UpperLeft
myRing.AddPoint(10.0, 10.0)#UpperRight
myRing.AddPoint(10.0, 0.0)#Lower Right
myRing.AddPoint(0.0, 0.0)#close ring
myPoly = ogr.Geometry(type=ogr.wkbPolygon)
myPoly.AddGeometry
print (myPoly.GetArea() ) #returns zero, not 100...
1
  • Do you have spaces myRing _ .AddPoint?
    – nadya
    Commented Dec 7, 2012 at 0:07

2 Answers 2

2

Thanks...I forgot to specify what geometry (myRing) to pass to myPoly myPoly.AddGeometry(myRing)

The following now works as a simple start:

#create simple square polygon shapefile:
from osgeo import ogr
driver = ogr.GetDriverByName('ESRI Shapefile')

datasource = driver.CreateDataSource('c:/temp/testPolygon.shp')
layer = datasource.CreateLayer('layerName',geom_type=ogr.wkbPolygon)

#create polygon object:
myRing = ogr.Geometry(type=ogr.wkbLinearRing)
myRing.AddPoint(0.0, 0.0)#LowerLeft
myRing.AddPoint(0.0, 10.0)#UpperLeft
myRing.AddPoint(10.0, 10.0)#UpperRight
myRing.AddPoint(10.0, 0.0)#Lower Right
myRing.AddPoint(0.0, 0.0)#close ring
myPoly = ogr.Geometry(type=ogr.wkbPolygon)
myPoly.AddGeometry(myRing)
print ('Polygon area =',myPoly.GetArea() )#returns correct area of 100.0

#create feature object with point geometry type from layer object:
feature = ogr.Feature( layer.GetLayerDefn() )
feature.SetGeometry(myPoly)
layer.CreateFeature(feature)

#flush memory
feature.Destroy()
datasource.Destroy()
0

Shouldn't the fourth point be (10.0,0.0) rather than (0.0,10.0) -- which is your second point?

1
  • Yes, but i still have the problem with even simpler code: #myPoly.GetArea() returns area of zero... from osgeo import ogr #create polygon object: myRing = ogr.Geometry(type=ogr.wkbLinearRing) myRing .AddPoint(0.0, 0.0)#LowerLeft myRing .AddPoint(0.0, 10.0)#UpperLeft myRing .AddPoint(10.0, 10.0)#UpperRight myRing .AddPoint(10.0, 0.0)#Lower Right myRing .AddPoint(0.0, 0.0)#close ring myPoly = ogr.Geometry(type=ogr.wkbPolygon) myPoly.AddGeometry print (myPoly.GetArea() )
    – Dave
    Commented Dec 7, 2012 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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