Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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...
share|improve this question
    
Do you have spaces myRing _ .AddPoint? –  nadya Dec 7 '12 at 0:07
add comment

2 Answers

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

share|improve this answer
    
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 Dec 7 '12 at 0:37
add comment

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()
share|improve this answer
add comment

Your Answer

 
discard

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.