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']
1 Answer 1
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
)
)
...
-
perfekt, it worked out. Thanks a lot!miraculix– miraculix2022年03月18日 11:13:25 +00:00Commented Mar 18, 2022 at 11:13
-
Happy to help, accept the answer if it worked, thank youKalak– Kalak2022年03月18日 11:16:57 +00:00Commented Mar 18, 2022 at 11:16
-
-
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_WEAmiraculix– miraculix2022年04月08日 08:24:23 +00:00Commented Apr 8, 2022 at 8:24
-
The path seems good, are you trying to load it through qgsvectorlayer() or the processing algorithms input?Kalak– Kalak2022年04月08日 12:34:10 +00:00Commented Apr 8, 2022 at 12:34
Explore related questions
See similar questions with these tags.