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...
my_res = processing.getObject(result['OUTPUT'])bit yieldsAttributeError: SinglePartsToMultiparts instance has no attribute '__getitem__'!? Nevertheless,all_PA = QgsMapLayerRegistry.instance().mapLayersByName("Multipart")[0]did work, since setting the output directory toNonedid produce the desired output of a memory layer, such that this solved my problem. – nils Dec 08 '16 at 20:57my_PA_vllayer or that the"my_field_of_country_ISO3"field exists? – mgri Dec 08 '16 at 21:01resultobject 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 sincemy_res = processing.getObject(result['OUTPUT'])givesAttributeError: SinglePartsToMultiparts instance has no attribute '__getitem__'– nils Dec 08 '16 at 21:06