Graphics Reference
In-Depth Information
calculator.setEllipsoid(crs.ellipsoidAcronym())
calculator.setEllipsoidalMode(crs.geographicFlag())
if geometry.isMultipart():
self.add(info, 'num_multi', 1)
parts = geometry.asGeometryCollection()
for sub_geometry in parts:
self.analyzeGeometry(sub_geometry, layer, info)
elif geometry.type() == QGis.Point:
self.add(info, 'num_points', 1)
elif geometry.type() == QGis.Line:
self.add(info, 'num_lines', 1)
self.add(info, 'tot_line_length',
calculator.measure(geometry))
elif geometry.type() == QGis.Polygon:
self.add(info, 'num_polygons', 1)
self.add(info, 'tot_poly_area',
calculator.measure(geometry))
self.add(info, 'tot_poly_perimeter',
calculator.measurePerimeter(geometry))
def add(self, info, key, n):
if key in info:
info[key] = info[key] + n
else:
info[key] = n
The add() method is just a helper method that adds a number to a dictionary entry if it
exists, and creates that entry if it doesn't. This allows us to use the info dictionary to
store the results as we calculate them.
As you can see, the analyzeGeometry() method makes use of a Qg-
sDistanceArea object to calculate the lengths and areas of a geometry. Note that our
analyzeGeometry() method is recursive; if a geometry has multiple parts, each sub-
geometry might also have multiple parts, so we call analyzeGeometry() recursively
on each part to allow these nested geometries to be handled correctly.
When we call analyzeGeometry() on a given QGSGeometry , the results of the
analysis will be stored in the info dictionary. Let's add some code to the end of our
Search WWH ::




Custom Search