3

I am writing a script and I would like to let the user choose the layer but i would like to default a layer (when it is open in the project) in the input field and if the user changes it he can choose another one from the list. I can specify a the drop down list or a specific layer but then the dropdown option is no longer available. Can I specify a default layer like I can do it with numbers?

This gives me the dropdown list with my layer options from the list

alg_params = {
 'INPUT': parameters['Input_name'],
 'INPUT_FIELDS': [''],
 'OVERLAY': parameters['Overlay_name'],
 'OVERLAY_FIELDS': [''],
 'OVERLAY_FIELDS_PREFIX': '',
 'OUTPUT': parameters['result_name']

In this I choose the layer, but its not a default, it is hard set and my dropdown list disappears.

alg_params = {
 'INPUT': 'Input_name_d9798079_c0d4_4e60_a3f7_ce2ca427d004',
 'INPUT_FIELDS': [''],
 'OVERLAY': parameters['Overlay_name'],
 'OVERLAY_FIELDS': [''],
 'OVERLAY_FIELDS_PREFIX': '',
 'OUTPUT': parameters['result_name']
asked Mar 18, 2022 at 9:00

1 Answer 1

4

What you want can be done when you add the parameters to your processing script class in the initAlgorithm function

...
 def initAlgorithm(self, config=None):
 """
 Here we define the inputs and output of the algorithm, along
 with some other properties.
 """
 # We add the input vector features source. It can have any kind of
 # geometry.
 self.addParameter(
 QgsProcessingParameterFeatureSource(
 self.INPUT,
 self.tr('Input layer'),
 [QgsProcessing.TypeVectorAnyGeometry],
 defaultValue="the layer name in legend or the layer.shp file path" # HERE
 )
 )
 ...
answered Mar 18, 2022 at 11:09
8
  • perfekt, it worked out. Thanks a lot! Commented Mar 18, 2022 at 11:13
  • Happy to help, accept the answer if it worked, thank you Commented Mar 18, 2022 at 11:16
  • Sure, good point. Commented Mar 18, 2022 at 11:46
  • Hi Louis, do you happen to know how to define the path to a layer in a geopackage? It not working with just the path I tried it with QgsProject.instance().readPath but I can't get it. In another Step i load a layer from the GeoPackage by the name with this: C:/Users/.../../Geopacke.gpkg|layername=Potenzial_WEA Commented Apr 8, 2022 at 8:24
  • The path seems good, are you trying to load it through qgsvectorlayer() or the processing algorithms input? Commented Apr 8, 2022 at 12:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.