3 mar 2020

ELIMINAR PUNTOS DUPLICADOS EN QGIS

PARA ELIMINAR PUNTOS DUPLICADOS, COPIAR EN CONSOLA DE PYTHON Y EJECUTAR:

layer = iface.activeLayer() # This command will load the layer currently selected


# Create the output layer
crs = layer.crs().toWkt()
outLayer = QgsVectorLayer('Point?crs='+ crs, 'cleaned' , 'memory')
prov = outLayer.dataProvider()
fields = layer.pendingFields() # Fields from the input layer
prov.addAttributes(fields) # Add input layer fields to the outLayer
outLayer.updateFields()

all_points = {}
index = QgsSpatialIndex() # Create a Spatial Index
for ft in layer.getFeatures():
    index.insertFeature(ft)
    all_points[ft.id()] = ft

ids_already_processed = [] # It will store all the feature ids already processed

for feat in layer.getFeatures():
    attrs_already_added = [] # It will store all the attributes already processed
    if feat.id() not in ids_already_processed:
        attrs_already_processed = []
        idsList = index.intersects(feat.geometry().boundingBox())
        attributes = [all_points[id].attributes() for id in idsList]
        geometries = [all_points[id].geometry() for id in idsList]
        for attr in attributes:
            if attr not in attrs_already_added:
                outGeom = QgsFeature()
                outGeom.setAttributes(attr)
                geom_index = attributes.index(attr)
                outGeom.setGeometry(geometries[geom_index])
                prov.addFeatures([outGeom])
                attrs_already_added.append(attr)
        ids_already_processed += [all_points[id].id() for id in idsList]

# Add the layer to the Layers Panel
QgsMapLayerRegistry.instance().addMapLayer(outLayer)

-------

Esto elimina los puntos duplicados excepto aquellos con atributos diferentes