Graphics Reference
In-Depth Information
For line geometries, we'll calculate the length of the line and update the total length:
if geometry.type() == QGis.Line:
length = int(calculator.measure (geometry) /
1000)
tot_length = tot_length + length
feature_info = "line of length %d kilometers" %
length
For polygon geometries, we'll calculate the area of the polygon and update the total area:
elif geometry.type() == QGis.Polygon:
area = int(calculator.measure (geometry) /
1000000)
tot_area = tot_area + area
feature_info = "polygon of area %d square
kilometers" % area
Finally, for the other types of geometries, we'll simply display the geometry's type:
else:
geom_type =
qgis.vectorGeometryType(geometry.type())
feature_info = "geometry of type %s" % geom_type
Now that we've done these calculations, we can display the feature's label together with
the information we calculated about this feature:
print "%s: %s" % (feature_label, feature_info)
Finally, when we've finished iterating over the features, we can display the total line
length and polygon area for all the features in the shapefile:
print "Total length of all line features: %d" %
tot_length
print "Total area of all polygon features: %d" %
tot_area
This completes our program for analyzing the contents of a shapefile. The full source for
this program is available in the code samples provided with this topic. To test our pro-
Search WWH ::




Custom Search