1

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?

1

1 Answer 1

0
  • 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) 
    
1
  • @bliblablo35 - Most welcome, glad it works :)
    – Joseph
    Jun 10, 2016 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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