is there a possibility to extract the area covered by one geometry of a shapefile (polygon)?
I tried with
thepoly.area()
but get "nan" as result.
Ok. I think you are right and there is something wrong with thepoly, so I'll need to post some code. I have a buildings vector (polygon) layer containing three polygons (which I created for testing). This layer is called "build" in the code. To extract its geometries, I wrote a little function:
def extract_poly_geometry(self, layer):
feature = QgsFeature()
geom_list = []
while layer.dataProvider().nextFeature(feature):
geom_list.append(feature.geometry())
return geom_list
In the main part of my program it is called by
theINPolyShapeField = self.extract_poly_geometry(build)
Later I iterate over the polygons and try to run some code for each of them:
for a in range(0, (len(theINPolyShapeField) - 1), 1): # going through buildings
thepoly = theINPolyShapeField[a] # extract building number a
QMessageBox.information(None, "DEBUG:", str(thepoly))
# polyarea = float(thepoly.area())
QMessageBox.information(None, "DEBUG:", str(thepoly.asPolygon()))
I use the QMessageBoxes for debuging. The first says
qgis.core.QgsGeometry object at 0xbbe889c
(thanks for the reminder, andytillia), which I assume is correct. The second one however deliveres an empty list, "[]".
I tried to convert to MultyPoint by
QMessageBox.information(None, "DEBUG:", str(thepoly.asMultiPoint()))
to have a look at the points, but this crashes qGIS with segmentation fault…
I checked the type of thepoly on the console by
print(thepoly.wkbType())
which returns "3". According to Quantum GIS API Documentation this means "WKBPolygon". So thepoly is a polygon, is it?
Hm, i tried thepoly.validateGeometry() on the python console. When I tried it for my first geometry, it returned "[]". A call for the second one crashed QGIS with segmentation fault…
The theINPolyShapeField list should not be the problem, as the issue also occures on the console, where I use geom_list directly.
Yes, exactly, I see three polygons (that I drew before). I just created them using the "New shapefile" tool. I think I selected the right layer as I checked with build.name() and got the right "test_buildings", what is correct.
len(theINPolyShapeField) returnes 3.
Unfortunatelly thepoly.area() does not return anything as QGIS crashes with segmentation fault… Oo
Thanks for the loop. It really looks lot nicer than mine. However I think this is not the problem, as I diden't use the loop on the console and the same issue occures.