2

I have a shapefile containing many thousands of polygons with values stored in an attribute field and ranging 0-105. I would like to create a much simplified file that contains classes for value ranges. E.g. values 30-50 = class 1, values 50-70 = class 2, values 70-100 = class 3. The classes can be integers. Would it then be possible to simplify this to a shapefile containing 3 feature classes (class 1, class 2, class 3) each class containing every polygon fitting those criteria?

There are no overlapping polygons. Attributes from the original shapefile do not need to be preserved. I'd like to use python and ogr to accomplish this.

2
  • Values ranging 0-105 are individually assigned in a specific field of attributes table for each polygon?
    – xunilk
    Commented Nov 21, 2015 at 13:33
  • Yes, exactly. Edited to be more specific and added desired output (though not sure if it is possible..)
    – geoeye
    Commented Nov 21, 2015 at 18:15

1 Answer 1

3

You can use PyQGIS code (I know that you use Ubuntu) at the Python Console of QGIS. To test my sugestion, I used the next code where a QgsExpression object has your class 1 values:

layer = iface.activeLayer()

expression = QgsExpression( u'"values" >= 30 AND "values" <= 50' )

idx = [ feat.attributes()[0] for feat in layer.getFeatures() 
        if expression.evaluate( feat ) ]

feats = [ feat for feat in layer.getFeatures() 
          if expression.evaluate( feat )]

for feat in feats:
    area = feat.geometry().area()
    print u"area{:.0f} = {:.2f}".format(idx[feats.index(feat)], area)

crs = layer.crs()

epsg = crs.postgisSrid()

uri = "Polygon?crs=epsg:" + str(epsg) + "&field=id:integer""&field=area&index=yes"

new_layer = QgsVectorLayer(uri, 
                           'class1', 
                           'memory')

QgsMapLayerRegistry.instance().addMapLayer(new_layer)

prov = new_layer.dataProvider()

n = len(feats)

for i in range(n):

    feats[i].setAttributes([i, feats[i].geometry().area()])

prov.addFeatures(feats)

I ran this code with the polygon vector layer of next image; where you can observe a 'values' field at the attributes table.

enter image description here

Result was a new memory layer (blue color) with the features that match the query for your class.

enter image description here

Areas were calculated for two different methods for corroborating this approach (features were selected as I expected).

1
  • Many thanks for your response. I will test this ASAP. I'm trying to automate this process though, so I am looking for a solution that can be executed via python scripting and not in the qgis console. I do not know much about using pyqgis within scripts- is this method still applicable? How do you know I use ubuntu :-P
    – geoeye
    Commented Nov 22, 2015 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.