2

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)))
2
  • Are you creating a modeler or a script? Not sure how to do this through the modeler but if it's via scripting (which when you run the script, you still get an interface to select layers), you coud add something like this: InputLayer.setCrs(QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId))
    – Joseph
    Aug 10, 2015 at 9:16
  • Sorry - the question isn't as clear as it could be. I know how to set the default CRS for the input. What I need is for the tool dialog to parse the CRS of a selected input layer and default to that. I'll modify the question. Aug 11, 2015 at 11:28

1 Answer 1

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

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.