Friday, 17 August 2018

Creating standalone python Script using PyQGIS?


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' })


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...