3

I'm trying to get the total area for a reprojected layer made by number of features selected by clicking on a layer with PyQGIS, but when I use iface.activeLayer().selectedFeatures() a list of QgsFeatures is returned, not a QgsVectorLayer, so I have to convert back again to QgsVectorLayer or add these features to a layer, but so far I haven't been able to find how to do this.

The code I've been using:

layer = iface.activeLayer().selectedFeatures()
parameter = {'INPUT': layer, 'TARGET_CRS': 'EPSG:32615','OUTPUT': 'memory:Reprojected'}
result = processing.run('native:reprojectlayer', parameter)
areatot = 0
for elem in result['OUTPUT'].getFeatures():
     geom = elem.geometry()
     areatot += geom.area()

1 Answer 1

3

The approach to what I was trying to achieve was to use the QgsProcessingFeatureSourceDefinition(layer.id(), True), this I found by reprojecting a layer using clicking menu, and checking the log, then I added this to the code I had to end up with this:

layer = iface.activeLayer()
parameter = { 'INPUT' : QgsProcessingFeatureSourceDefinition(layer.id(), True), 'OUTPUT' : 'memory:', 'TARGET_CRS' : QgsCoordinateReferenceSystem('EPSG:32615') }
result = processing.run('native:reprojectlayer', parameter)
areatot = 0
for elem in result['OUTPUT'].getFeatures():
     geom = elem.geometry()
     areatot += geom.area()

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.