I'm creating a Processing algorithm that requires the user to input several parameters including an input layer and an output CRS. At present, the tool defaults to SRID 4326. I'd like for the CRS parameter to default to whatever layer is selected as the input layer. Is this possible?
I don't see anything in processing.core. Would I have to get into the Qt internals and hook up an event handler for a change in the layer selection?
Current code:
self.addParameter(ParameterCrs(self.TARGET_CRS,
self.tr('Target CRS'), 'EPSG:4326'))
Desired pseudo code:
self.addParameter(ParameterCrs(self.TARGET_CRS,
self.tr('Target CRS'),
processing.parameters.getCrsFromInputLayer(INPUT_LAYER)))
1 Answer 1
I try to do this in my Multi Ring Buffer Plugin, but there is probably a more elegant solution.
This is done after getting a selected layer and applying the selected layers CRS, through the AuthID, to a newly created layer. If the AuthID is not recognised, QGIS prompts for a valid one.
# Check the current CRS of active layer
buffer_crs_object = self.iface.activeLayer().crs()
# Get the AuthID
buffer_crs = buffer_crs_object.authid()
# Apply that to the created layer if recognised
buffer_input_crs = "Polygon?crs=%s" % buffer_crs
# Create empty memory vector layer
layer_name = active_vl.name()
vl = QgsVectorLayer(buffer_input_crs, "%s_MultiRingBuffer" % layer_name, "memory")
Full code: https://github.com/HeikkiVesanto/QGIS_Multi_Ring_Buffer/blob/master/multi_ring_buffer.py
Explore related questions
See similar questions with these tags.
InputLayer.setCrs(QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId))