1

On QGIS 2.18.1, Python 2.7.12, Ubuntu 64 bit, I am struggling with setting a CRS for a memory output layer within an iteration over several years of observations for a protected area (PA) polygon layer (UNEP-WCMC World PA database). The solution I have found so far (see, e.g. this) builds upon generating a multipart vector layer for a sub-selection of PA per year as a memory layer in order to intersect with GADM country polygons. This, however, requires a manual input of selecting the CRS for the memory layer. Is there a way of doing this automatically?

The code that requires solution:

from qgis.core import *
from PyQt4.QtCore import *

# singlepart to multipart algorithm
processing.runandload('qgis:singlepartstomultipart', my_PA_vl, "my_field_of_country_ISO3", "memory:my_output_layer_name") # for this step I would like to set CRS for mem layer automatically
my_output_layer_name = QgsMapLayerRegistry.instance().mapLayersByName("Multipart")[0]

Setting the output layer to None - which should set the memory layer CRS to input layer CRS (?) - somehow does not generate any output.

Potentially, there might even be a better solution / approach for what I am intending...

0

Your output will have the same crs of your input layer. You will be obviously able to change the crs of the output layer, but only in a further step.

Probably I don't completely understand your question, however I think you essentially want to manage the output of the 'qgis:singlepartstomultipart' algorithm for an intersection. If so, you need to choose a name for the current operation and to set your output to None. Then, you will able to handle your result for the next step:

from qgis.core import *

# You can use any name instead of 'result'
result = processing.runalg('qgis:singlepartstomultipart', my_PA_vl, "my_field_of_country_ISO3", None) 

# You can use any name instead of 'my_res'
my_res = processing.getObject(result['OUTPUT']) 

# And then, for example:
intersection = processing.runalg("qgis:intersection", my_res, country_polygons ,False,None):
# where 'country_polygons' is the name of the layer you wish to intersect with 'my_res'

If you want to add your layer to the Layers panel, you may insert this in your preferred position:

QgsMapLayerRegistry.instance().addMapLayer(my_res)

I hope this will help you, otherwise I can edit my answer.

  • Thanks, you actually got what I wanted quite right. The my_res = processing.getObject(result['OUTPUT']) bit yields AttributeError: SinglePartsToMultiparts instance has no attribute '__getitem__' !? Nevertheless, all_PA = QgsMapLayerRegistry.instance().mapLayersByName("Multipart")[0] did work, since setting the output directory to None did produce the desired output of a memory layer, such that this solved my problem. – nils Dec 8 '16 at 20:57
  • I tested the code on sample data, and it worked for me... Are you sure that you properly load the my_PA_vl layer or that the "my_field_of_country_ISO3" field exists? – mgri Dec 8 '16 at 21:01
  • Yeah, the result object has successfully generated without requiring a manual CRS input. That solved my problem. I was referring to the second line, which did not work for me since my_res = processing.getObject(result['OUTPUT']) gives AttributeError: SinglePartsToMultiparts instance has no attribute '__getitem__' – nils Dec 8 '16 at 21:06
  • That's weird... However, I'm glad you solved your problem =) – mgri Dec 8 '16 at 21:14
  • Actually, you did ;D – nils Dec 8 '16 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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