Geographic Information Systems Asked by Tolotos on June 10, 2021
Is there a way to execute either a python script or pyhton commands from within a powershell script to do some QGIS geoprocessing on a Windows 10 machine (64 bit)?
I have a powershell script that downloads gazetteer data with cURL and copies the data into a geopackage. I want to enrich the address data with a number of other attributes from other layers. As of now, I use python processing commands for this task that need to be executed from the python console of QGIS. A third script (powershell again) exports the data to csv files, 7z-ips and copies them to the clipboard in preparation for mail delivery.
Example of processing:
processing.run("native:joinattributesbylocation", {'INPUT':'C:/Users/myusername/Documents/Parkrauminventur.gpkg|layername=Hauskoordinaten','JOIN':'//some_server/Projekte/PRM.gdb|layername=Kontrollgebiete','PREDICATE':[0,1,2,3,4,5,6],'JOIN_FIELDS':['Gebiet','Region'],'METHOD':1,'DISCARD_NONMATCHING':False,'PREFIX':'KG_','OUTPUT':'ogr:dbname='C:/Users/myusername/Spatial_Joins.gpkg' table="SJ_HK" (geom)'})
However, I would like to write one script to include all steps that runs without the need to start QGIS. I read many instructions on how to write standalone scripts but none of them seem to be assuming powershell. So I am not sure how to set the required environmental variables in powershell and how to interact with python/QGIS.
Are there directions for this scenario?
You do not even need to use Python.
Since QGIS 3.14 (Pi) you are able to exexute QGIS with command line by running the qgis_process
.
To get all available processings list, just type qgis_process list
in terminal. Then, you will see you can execute Join Attributes by Location by typing :
qgis_process run native:joinattributesbylocation
If you need complementary information regarding a command, just type :
qgis_process help native:joinattributesbylocation
Join attributes by location (native:joinattributesbylocation)
---------------- Description
---------------- Join attributes from one vector layer to another by location. This algorithm takes an input vector layer and creates a new vector layer that is an extended version of the input one, with additional attributes in its attribute table.
The additional attributes and their values are taken from a second vector layer. A spatial criteria is applied to select the values from the second layer that are added to each feature from the first layer in the resulting one.
---------------- Arguments
----------------
INPUT: Base Layer Argument type: source Acceptable values:
- Path to a vector layer
JOIN: Join Layer Argument type: source Acceptable values:
- Path to a vector layer
PREDICATE: Geometric predicate Argument type: enum Available values:
- 0: intersects
- 1: contains
- 2: equals
- 3: touches
- 4: overlaps
- 5: within
- 6: crosses Acceptable values:
- Number of selected option, e.g. '1'
- Comma separated list of options, e.g. '1,3'
JOIN_FIELDS: Fields to add (leave empty to use all fields) Argument type: field Acceptable values:
- The name of an existing field
- ; delimited list of existing field names
METHOD: Join type Argument type: enum Available values:
- 0: Create separate feature for each matching feature (one-to-many)
- 1: Take attributes of the first matching feature only (one-to-one)
- 2: Take attributes of the feature with largest overlap only (one-to-one) Acceptable values:
- Number of selected option, e.g. '1'
- Comma separated list of options, e.g. '1,3'
DISCARD_NONMATCHING: Discard records which could not be joined Argument type: boolean Acceptable values:
- 1 for true/yes
- 0 for false/no
PREFIX: Joined field prefix Argument type: string Acceptable values:
- String value
OUTPUT: Joined layer Argument type: sink Acceptable values:
- Path for new vector layer
NON_MATCHING: Unjoinable features from first layer Argument type: sink Acceptable values:
- Path for new vector layer
---------------- Outputs
----------------
OUTPUT: <outputVector> Joined layer NON_MATCHING: <outputVector> Unjoinable features from first layer JOINED_COUNT: <outputNumber> Number of joined features from input table
to pass an argument just use the following syntax : --PARAM=value
Full sample :
qgis_process run native:joinattributesbylocation --INPUT="/my/folder/input.shp" --JOIN="/my/folder/tojoin.shp" --METHOD="0" --PREDICATE="1" --OUTPUT="/my/folder/output.shp"
Have a look on complementary resources :
Correct answer by simo on June 10, 2021
simo, you made my day - thank you very much, indeed! Converting your example into PowerShell I got the desired result. I used "splatting" to pass the parameters like so:
##Preparation
# Locate the qgis_process.bat and store location in variable
$bat_qgis_process = Get-ChildItem -Path 'C:Program Files' -File -Recurse -Include qgis_process-qgis.bat
# Definition of parameters for qgis_process in variable (this is called "Splatting")
$Args_qgis_process =
'run',
'native:joinattributesbylocation',
'--INPUT="C:/Users/myusername/Parkrauminventur.gpkg|layername=Hauskoordinaten"',
'--JOIN="//some_server/ArcGIS-Ablage/Projekte/PRM.gdb|layername=Kontrollgebiete"',
'--METHOD="1"',
'--PREDICATE="0,1,2,3,4,5,6"',
'--JOIN_FIELDS="Gebiet";"Region"',
'--DISCARD_NONMATCHING=0',
'--PREFIX="KG_"',
'--OUTPUT="C:/Users/myusername/Spatial_Joins.gpkg"'
# Execute qgis_process script with above parameters
& $bat_qgis_process.FullName @Args_qgis_process
Write-Host 'We are done here :-)!'
Read-Host -Prompt "Hit Enter to leave the script!"
The only minor drawback: As of now it seems impossible to define a specific layer name for the output geodatabase. The syntax for --INPUT is different form --OUTPUT in this regard. There is a feature request concerning this (see here).
Answered by Tolotos on June 10, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP