6

I need to test if a vector layer, chosen by the user, is in geographic coordinates, according to the code below:

class RoadSlopeCalculatorAlgorithm(QgsProcessingAlgorithm):
    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterRasterLayer('MDT', 'Digital Elevation Model (DEM)', defaultValue=None))
        self.addParameter(QgsProcessingParameterFeatureSink('CalculatedRoadSlopes', 'Calculated road slopes', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, supportsAppend=True, defaultValue=None))
        self.addParameter(QgsProcessingParameterBoolean('VERBOSE_LOG', 'Verbose logging', optional=True, defaultValue=False))
        self.addParameter(QgsProcessingParameterVectorLayer('VectorLineLayer', 'Road network vector layer', types=[QgsProcessing.TypeVectorLine], defaultValue=None))
        self.addParameter(QgsProcessingParameterNumber('SegmentLength', 'Segment length (=> 5 times pixel size)', type=QgsProcessingParameterNumber.Double, maxValue=1.79769e+308, defaultValue=250))

    def processAlgorithm(self, parameters, context, model_feedback):
        # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
        # overall progress through the model
        feedback = QgsProcessingMultiStepFeedback(5, model_feedback)
        results = {}
        outputs = {}

        alg_params = {
            'INPUT': parameters['VectorLineLayer'],
            'LENGTH': parameters['SegmentLength'],
            'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
        }

        # Check if vector line layer 'VectorLineLayer' is in geographic coordinates

        if alg_params['INPUT'].crs.isGeographic() :  #this is line 61 on the script
            # and so on .........

And there is the following error message:

*Traceback (most recent call last):
File "<string>", line 61, in processAlgorithm
AttributeError: 'str' object has no attribute 'crs'

How can I refer to the vector line layer object in [VectorLineLayer].crs.isGeographic()?

1 Answer 1

10

In your script, parameters['VectorLineLayer'] returns the layer's id which is a string. You need to use parameterAsVectorLayer to get the reference of the vector layer.

def processAlgorithm(self, parameters, context, model_feedback):

    # previous lines

    vector_layer = self.parameterAsVectorLayer(parameters, 'VectorLineLayer', context)

    if vector_layer.crs().isGeographic():
        # and so on

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.