Graphics Reference
In-Depth Information
"Select
Shapefile",
"~", '*.shp')
if not filename:
print "Cancelled."
return
We can then open the selected shapefile:
registry = QgsProviderRegistry.instance()
provider = registry.provider("ogr",filename)
if not provider.isValid():
print "Invalid shapefile."
return
In order to identify a feature, we need to display a meaningful label for the feature. To do
this, we'll look for an attribute with a likely-looking name. If there is no suitable attribute,
we'll have to use the feature's ID instead.
Let's start by building a list of the various attributes stored in this shapefile:
attr_names = []
for field in provider.fields():
attr_names.append(field.name())
We're now ready to start scanning through the shapefile's features. Before we do this,
though, let's initialize a couple of variables to hold the totals we need to calculate:
tot_length = 0
tot_area = 0
We also need to set up a QgsDistanceArea object to do the distance and area calcula-
tions for us.
crs = provider.crs()
calculator = QgsDistanceArea()
calculator.setSourceCrs(crs)
calculator.setEllipsoid(crs.ellipsoidAcronym())
calculator.setEllipsoidalMode(crs.geographicFlag())
Search WWH ::




Custom Search