[GIS] Creating standalone python Script using PyQGIS

pyqgispyqgis-3python-2.7

I have written a code which solve my problem for Demand Allocation. I have Input (.csv)File and Output is also (.csv) File. However I have to integrate this script
in Java Application.(https://stackoverflow.com/questions/10097491/call-and-receive-output-from-python-script-in-java), So i am thinking to write Standalone python script.I dont't have any idea how to use it.Can someone help me in this code. Any path or Variable that need to be set.Please point that too.I am working on windows 7& with QGIS 3.0 Version

from qgis.core import QgsProject
uri='file:///C:/Test/points_40.csv? 
delimiter=,&crs=epsg:4326&id=id&xField=Lat&yField=Long'
layer=QgsVectorLayer(uri,'Points','delimitedtext')
root = QgsProject.instance().layerTreeRoot()
QgsProject.instance().addMapLayer(layer)
vectorLyr=QgsVectorLayer("C:/Test/demand/Demand.shp","BuildingLayer","ogr")
vectorLyr.isValid()
QgsProject.instance().addMapLayer(vectorLyr)
uri='file:///C:/Test/data.csv?delimiter=,'
infoLyr=QgsVectorLayer(uri,'Population','delimitedtext')
infoLyr.isValid()
QgsProject.instance().addMapLayer(infoLyr)
csvField='FID_1'
shpField='FID'
joinObject=QgsVectorLayerJoinInfo()
joinObject.setJoinFieldName(csvField)
joinObject.setTargetFieldName(shpField)
joinObject.setJoinLayerId(infoLyr.id())
joinObject.setUsingMemoryCache(True)
joinObject.setJoinLayer(infoLyr)
vectorLyr.addJoin(joinObject)
import processing
parameters={'INPUT' : 'file:///C:/Test/points_40.csv delimiter=,&crs=epsg:4326&id=id&xField=Lat&yField=Long', 'BUFFER' : 0, 'OUTPUT' : 'C:/Test/demand/vor_points.shp' }
processing.run('qgis:voronoipolygons',parameters)
vor_points=QgsVectorLayer("C:/Test/demand/vor_points.shp","VorLayer","ogr")
vor_points.isValid()
QgsProject.instance().addMapLayer(vor_points)
vor_layer = vor_points.clone()
root.insertChildNode(-1, QgsLayerTreeLayer(vor_layer))
root.removeLayer(vor_points)
import processing
processing.run('qgis:checkvalidity',{ 'INPUT_LAYER' : 'C:/Test/demand/Demand.shp', 'METHOD' : 2, 'VALID_OUTPUT' : 'C:/Test/demand/valid.shp', 'INVALID_OUTPUT' : 'C:/Test/demand/invalid.shp', 'ERROR_OUTPUT' : 'C:/Test/demand/error.shp' })
ValidHouse=QgsVectorLayer("C:/Test/demand/valid.shp","validHouse","ogr")
ValidHouse.isValid()
QgsProject.instance().addMapLayer(ValidHouse)
processing.run("native:centroids", {'INPUT':'C:/Test/demand/valid.shp','ALL_PARTS':True,'OUTPUT':'C:/Test/demand/centroid.shp'})

 centroidLyr=QgsVectorLayer("C:/Test/demand/centroid.shp", 
"CentroidLayer","ogr")
centroidLyr.isValid()
QgsProject.instance().addMapLayer(centroidLyr)
processing.run("qgis:distancematrix",{ 'INPUT' : 'C:/Test/demand/centroid.shp', 'INPUT_FIELD' : 'FID', 'TARGET' : 'file:///C:/Test/points_40.csv?delimiter=,&crs=epsg:4326&id=id&xField=Lat&yField=Long', 'TARGET_FIELD' : 'id', 'MATRIX_TYPE' : 0, 'NEAREST_POINTS' : 1, 'OUTPUT' : 'C:/Test/finalDistance.csv' })

Best Answer

First, for QGIS3 it is important to have a proper environment. You'll find a solution in the osgeo4w/bin directory. Inside there is a template for calling python.exe with that environment (it is called python-qgis.bat.tmpl or, if it is already renamed, python-qgis.bat).

Second, a standalone pyqgis script has to "prepare" or init a pyqgis app, analog to a qt5 app. There is a description for writing standalone scripts in the pyqgis developer cookbook

And finally call that script through the batch file created in the first step.

Related Question