Delete features with PyQGIS in a loop

Viewed 25

I have been for months trying unsuccesfully to make a function which can compare a feature with the other ones in a shp layer and if some conditions fulfill, the feature in the loop is deleted. I need it for my final work. The conditions to delete a feature have to be two: if the feature overlay the other one and if it is older than the feature that is in the loop.

def superpuestos2(capa, fecha, geom, id):
    listaborrar = []
    
    fecha_feat = fecha
    fechainf_feat = datetime.strptime(fecha_feat, "%d/%m/%Y")
    feat_geom = geom
    
    
    features = capa.getFeatures()
    for f in features:
        
        id_feat = f.attribute('id')
        
        if id != id_feat:
            fecha_f = f.attribute('fecha')
            fechainf_f = datetime.strptime(fecha_f, "%d/%m/%Y")
            
            f_geom = f.geometry()
            inters = f_geom.intersection(feat_geom)
            areageom = feat_geom.area()
            interarea = inters.area()
            fraccion = interarea/areageom
            
            if fraccion > 0.3:
                if fechainf_feat >= fechainf_f:#si la fecha es mas antigua:
                    print("intersecta")#en vez de print que borre el feature de f.
                    listaborrar.append(f.id())
                    
    print(listaborrar)
    capa.dataProvider().deleteFeatures(listaborrar)
0 Answers
Related