1

I am learning QGIS 3.XX and currently working on Voronoi Polygons.

I have three layers

  1. Point layer
  2. Building Layer
  3. Population CSV file.

These points layer are basically water tanks and building layer is full of small Polygon in YY area. I need to find which building will take water from the nearest water tanks.

I need to make Voronoi polygon on Point Layer. I tried manually by going through Layer-> Geometry Tools->Voronoi Polygon. And I got the below output.

enter image description here

After doing this, the newly created Voronoi Layer is placed at top of Layer Panel. I need to add this layer at lowest so that all the layers above can be put simultaneously on canvas. By now I am doing manually by placing point layer and building layer above voronoi layer, but how to achieve this using python? My expected output should be

enter image description here

until now my code looks like

from qgis.core import QgsProject
uri='file:///C:/Test/points.csv? 
delimiter=,&crs=epsg:4326&xField=Lat&yField=Long'
layer=QgsVectorLayer(uri,'Points','delimitedtext')
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)
  • 2
    Apart from your problem with the layer order, I think that Voronoi polygons as an interpolation method is an unsuitable approach for determining the water tank that supplies certain buildings. I think this should be done by a proximity/reachability anaylsis based on water supply infrasctructure or ways network in case of water is to be carried from the tanks to the buildings. – Jochen Schwarze Sep 4 '18 at 6:39
  • Related (as mentioned by OP in chat): gis.stackexchange.com/questions/294996/…. – Andre Silva Sep 6 '18 at 22:35
1

You can use processing.run()to execute algorithms from python. This is fully explained in this question but essentially your line will look something like this... processing.run('qgis:voronoipolygons', input, buffer, output).

You can then use QgsLayerTree.setCustomLayerOrder() to order the layers how you'd like. Just use a list of QgsMapLayers for the parameter. See the API for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.