[Qgis-developer] Python: creating a QgsPolygonV2 from the result of exteriorRing() or interiorRing(n) [QgsCurveV2]

Mark Johnson mj10777 at googlemail.com
Sat Oct 22 05:04:54 PDT 2016


For a python test script I would like to loop through a MultiPolygon,
calculation the areas of the single Polygons, exteriorRing and where they
exist the interiorRing and compare the results from another source
(spatialite).

Sample code:
  test_polygon = test_geom.geometryN(index)
  self.assertEqual(test_polygon.wkbType(), QgsWKBTypes.Polygon)
  count_rings=test_polygon.numInteriorRings()
  if (count_rings > 0):
   polygon_ring_area_sum+=polygon_area
   exterior_ring = test_polygon.exteriorRing()
   print('-I-> Polygon[%d] : exteriorRing[%s] isClosed[%d] isRing[%d] '%
(index,
exterior_ring.geometryType(),exterior_ring.isClosed(),exterior_ring.isRing()))
   i_rc=exterior_ring.convertTo(QgsWKBTypes.Polygon)
   exterior_ring_area=round(exterior_ring.area(),6)

-I-> Polygon[11] : exteriorRing[LineString] isClosed[1] isRing[1]
-I-> Polygon[11] : exteriorRing[LineString] exterior_ring_area[0.000000]
rc=0

To calculate the area, the geometry must be a Polygon but I have not found
a way to do this properly.

The convertTo() method seems to only change to a Z or M type
- the documentation is a bit vague 'Converts the geometry to a specified
type.'

the next attempt:
line = QgsGeometry.fromWkt(exterior_ring.asWkt())
exterior_ring_polygon = line.convertToType(QGis.Polygon, False)

also did not bring the desired result (fails when creating 'line').

Since the returned LineString is closed and a ring, there should be no
problem.

--
A second question:

How can the srid of a geometry retrieved from a QgsVectorLayer be
determined?

Mark Johnson
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20161022/62b24dc6/attachment-0001.html>


More information about the Qgis-developer mailing list