0

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' })
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Sep 25, 2018 at 10:19
0

1 Answer 1

2

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.

answered Sep 25, 2018 at 11:06
5
  • Can i get source from where i can learn this right from starting ? Commented Sep 25, 2018 at 13:29
  • 1
    The pyqgis developer cookbook is right place to start, i would say. Commented Sep 25, 2018 at 13:31
  • Okay thank you :) How long it will take to devlop this.? Like a month is it too heavy or lengthy to make? I have a deadline to be met that why Commented Sep 25, 2018 at 13:35
  • 2
    It isn't that complicated, in the best case it just wraps your code above. Commented Sep 25, 2018 at 13:39
  • Muller Hi, I am still stuck can you guide me in easy way gis.stackexchange.com/questions/297428/… please Commented Sep 29, 2018 at 9:25

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.