Tuesday 30 April 2019

qgis - How can I add output vector to a list to loop a geoprocessing on it?


I have about 18000 circular buffers that I want to transform in some kind of a wind rose. To do this, I transform the buffer polygons to lines, create points along the lines on the wind directions that I'm interested in (N, NW, W, SW, S, SE, E, NE), create voronoi polygons from theses points then intersect them with the original buffer polygons.


The problem is that some polygons are superposed, which breaks the voronoi processing.


To resolve it, I split the buffer layer into as many layers as I have polygons (so about 18000), and I want to loop my processing on each layer and then merge them into one.



To do this, I wrote this code :


#creation_rose_vents=name
#buffers=vector
#buffer_vent=output vector


import os, processing

path = "H:\\donnees_geo\\buffers_individuels"


shpfiles = [os.path.join(d, x)
for d, dirs, files in os.walk(path)
for x in files if x.endswith(".shp")]

list_buffers_vent = []

for buffers in shpfiles:
outputs_QGISPOLYGONSTOLINES_1=processing.runalg('qgis:polygonstolines', buffers,None)
outputs_QGISCREATEPOINTSALONGLINES_1=processing.runalg('qgis:createpointsalonglines', outputs_QGISPOLYGONSTOLINES_1['OUTPUT'],785.0,0.0,5495.0,None)
outputs_QGISVORONOIPOLYGONS_1=processing.runalg('qgis:voronoipolygons', outputs_QGISCREATEPOINTSALONGLINES_1['output'],0.0,None)

outputs_QGISINTERSECTION_1=processing.runalg('qgis:clip', outputs_QGISVORONOIPOLYGONS_1['OUTPUT'],buffers,buffer_vent)
list_buffer_vent.append(buffer_vent)

So far it is just supposed to add the result of my processing to a new list (for which I will try to merge all objects, but this is another problem), but there is an error saying : "buffer_vent is not defined" and I don't understand what is wrong.


Does anyone see what I'm doing wrong?


Bonus question: Is it possible to merge all objects from a list to one single shapefile or should I write my output to shapefiles first and then merge them?



Answer





  • You need to use 2 hash symbols # to assign the parameters (which gets highlighted by blue text), single # is used for comments (which gets highlighted by red text).





  • You could also shorten the paths for shpfiles by using the glob module.




  • I have modified your code a bit which includes an output folder to save all your clipped shapefiles and added a SAGA merge tool at the end to merge all your clipped shapefiles into one.


    ##creation_rose_vents=name
    ##output_folder=folder
    ##buffer_vent=output vector


    import glob, os, processing

    os.chdir("H:/donnees_geo/buffers_individuels")
    for buffers in glob.glob("*.shp"):
    outputs_QGISPOLYGONSTOLINES_1=processing.runalg('qgis:polygonstolines', buffers,None)
    outputs_QGISCREATEPOINTSALONGLINES_1=processing.runalg('qgis:createpointsalonglines', outputs_QGISPOLYGONSTOLINES_1['OUTPUT'],785.0,0.0,5495.0,None)
    outputs_QGISVORONOIPOLYGONS_1=processing.runalg('qgis:voronoipolygons', outputs_QGISCREATEPOINTSALONGLINES_1['output'],0.0,None)
    outputs_QGISINTERSECTION_1=processing.runalg('qgis:clip', outputs_QGISVORONOIPOLYGONS_1['OUTPUT'],buffers,output_folder + "/clipped_" + buffers)

    os.chdir(output_folder)

    output = glob.glob('*.shp')
    processing.runalg("saga:mergelayers", ";".join(output), False, False, buffer_vent)


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...