2

My script:

from qgis.core import *
import qgis.utils
import processing, os, sys, glob

polys = QgsVectorLayer("/Users/john/shapefile/layer.shp","intersection","ogr")
output = "/Users/john/shapefile/intersection.shp"
processing.run("saga:polygonselfintersection", polys, output)

Generates the return message:

if param not in parameters or parameters[param.name()] is None:
TypeError: argument of type 'QgsVectorLayer' is not iterable
  • If you want to use 'saga:polygonselfintersection' algorithm you need to get main name parameters by using processing.algorithmHelp('saga:polygonselfintersection') command at Python Console. Please, see my answer. – xunilk Jan 9 at 17:56
1

Tested in QGIS 3.6.0. I also needed to check some layers for its intersecting features and found that for me raw geometry loop works much faster than saga algorithm. In my case I had three polygon layers which I had to check. Here is a code:

import time
import processing
layers_list = iface.mapCanvas().layers()

start = time.time()
for lyr in layers_list:
    tech_layer = QgsVectorLayer("MultiPolygon?crs=EPSG:102014" , "tech_test", "memory")
    features = [f for f in lyr.getFeatures()]
    for n in features:
        geom = n.geometry()
        if any(geom.overlaps(init.geometry())==True for init in features if n.id()!=init.id()):
            tech_layer.dataProvider().addFeatures([n])
end = time.time()
elapsed_time = end - start  
et_str_1 = time.strftime("%H:%M:%S", time.gmtime(elapsed_time)) 

start = time.time()
for lyr in layers_list:
    out = processing.run('saga:polygonselfintersection', {'POLYGONS':lyr, 'INTERSECT': 'memory:tmp'})
end = time.time()
elapsed_time = end - start  
msec_str = str(int(elapsed_time%1*60))
et_str_2 = time.strftime("%H:%M:%S", time.gmtime(elapsed_time))

print ("Raw geometry check: \t{}".format(et_str_1))
print ("SAGA geometry check: \t{}".format(et_str_2))

Here are the results:

Raw geometry check:     00:00:13
SAGA geometry check:    00:00:24

In general this might not be too significant here but in case of checking much more layers raw geometry loop can be faster.

1

If you want to use 'saga:polygonselfintersection' algorithm you need to get mandatory name parameters by using following command at Python Console:

processing.algorithmHelp('saga:polygonselfintersection') 

After that, your code, adapted for my paths in a Linux system, it will be modified to:

import processing    
polys = QgsVectorLayer("/home/zeito/pyqgis_data/layer.shp","intersection","ogr")
output = "/home/zeito/pyqgis_data/intersection.shp"
processing.runAndLoadResults("saga:polygonselfintersection", 
                            {'POLYGONS':polys, 
                             'INTERSECT':output})

I created layer.shp and it looks as at following image:

enter image description here

After running above code, layer resulting (Intersection) was automatically added to Map Canvas:

enter image description here

and its behavior is as expected.

  • after processing finished I want to change layers name from intersection to another. How to ? – p wat Jan 10 at 9:38
  • By code or manually? In last case, you can do that by placing cursor on layer name (layers panel), click on mouse right button and select 'Rename layer' at contextual menu. – xunilk Jan 11 at 22:11
  • In my case have many layers so I want to use script to change layers name after processing finished. – p wat Jan 13 at 1:56

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.