[Qgis-user] [QGIS-Developer] passing parameters to QGIS Python-Script started via Command Line

Ujaval Gandhi ujaval at spatialthoughts.com
Mon Aug 8 07:26:01 PDT 2022


One option is to set env variables before launching the qgis command. The env
variables can be read from python.


set QGIS_MESSAGE=Helllo World




Then your myscript.py can be something like


from qgis.utils import iface
import os

message = os.environ['QGIS_MESSAGE']
iface.messageBar().pushMessage(message)





---
Ujaval Gandhi
Spatial Thoughts
www.spatialthoughts.com [http://www.spatialthoughts.com]




[data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=3D]
On Mon, Aug 8, 2022 at 7:52 PM Matthias Kuhn via QGIS-Developer
<qgis-developer at lists.osgeo.org> wrote:

> Hi Peter,
> 
> 
> You are correct with the analysis of the situation.
> 
> 
> Even if `qgis --help` states the following:
>  [--] treat all following arguments as FILEs
> 
> 
> it does in fact already interpret parameters as files before the `--`
> parameter.
> Changing this behavior is risky (it will break many startup scripts out there)
> and can be considered for QGIS 4.
> For now, we could add another marker as a band aid, e.g. we add a `--ex`
> (extra-parameters) that indicates that any parameters that follow should NOT
> be interpreted as files:
> 
> 
> 
>     qgis -p 1234.qgs --code script.py [files could be here] --ex
> --myparam=1324 -- [more files can still be here]
> 
> 
> If that sounds like what you are looking for, we are happy to help you with
> that
> 
> 
> Best regards
> Matthias
> 
> On Mon, Aug 8, 2022 at 3:55 PM Drexel Peter via QGIS-Developer
> <qgis-developer at lists.osgeo.org [qgis-developer at lists.osgeo.org]> wrote:
> 
> 
> > Hi everyone,
> > 
> > My goal: I want to open QGIS using the Command-Line, load a specific
> > project, select specific features and zoom to the selected features.
> > 
> > What sounds rather difficult is actually mostly solvable using the
> > following:
> > 
> > "C:\Program Files\QGIS 3.16\bin\qgis-ltr-bin.exe" -p "d:\standardebenen.qgs"
> > --code myscript.py
> > 
> >  
> > 
> > This opens QGIS with the Project d:\standardebenen.qgs and after that it
> > runs the script “myscript.py”.
> > 
> > Within the scrip I can do everything what I can do in a Python-script, so
> > there I can select some features in a layer and show that selection.
> > 
> > But this selections are “hard-coded” in the script, I cannot pass over these
> > parameters to the myscript.py.
> > 
> > In “Pure Python” I can pass parameters to a Python Script using “python
> > myscript.py arg1 arg2 arg3”.
> > 
> > This seems to be not working in the QGIS-Command-Line  as the parameters are
> > interpreted as “layers to load”.
> > 
> > Am I right with my analysis?
> > 
> > One workaround is to write the parameters to al file and use this file
> > within the myscript.py to define the selections. But this is very unhandy…
> > 
> > Are there any ideas from any QGIS-Expert to solve my problem?
> > 
> >  
> > 
> > If this cannot be solved in the current version of QGIS is one of the
> > developers willing to get in contact with me to make an offer the implement
> > this feature ?
> > 
> > Thanks
> > 
> > Peter
> > 
> > _______________________________________________
> > QGIS-Developer mailing list
> > QGIS-Developer at lists.osgeo.org [QGIS-Developer at lists.osgeo.org]
> > List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> > [https://lists.osgeo.org/mailman/listinfo/qgis-developer]
> > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> > [https://lists.osgeo.org/mailman/listinfo/qgis-developer]
> 
> 
> https://qfield.org/get/
> [https://ci4.googleusercontent.com/proxy/GqvhkBbIsDNWtqe2HThZLEElIli5211Hog29CYort7mWquajzTRIucqyquJ4nK_KuFTbuEa2CCNNIfLx52nBCCQkb6EWyBAfIjM_iV06dJPuBZpmAolhpdzysf0=s0-d-e1-ft#https://www.opengis.ch/wp-content/uploads/2022/04/Qfield_Banner_email.png]
> [https://qfield.org/get/]
> 
> QFIELD 2.0 IS HERE! - Hold the power of QGIS in your hand - learn more
> [https://www.opengis.ch/2022/04/05/qfield-2-0-is-here/] - get it now
> [https://qfield.org/get]
> 
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org [QGIS-Developer at lists.osgeo.org]
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> [https://lists.osgeo.org/mailman/listinfo/qgis-developer]
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> [https://lists.osgeo.org/mailman/listinfo/qgis-developer]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20220808/102f4583/attachment.htm>


More information about the Qgis-user mailing list