1

I need to write QGIS script, which removes features with same value of specific attribute - in output will be only features with first occurrence of value. I know, how to check duplicity, but I don't know, how properly create output from my script, when I need it to use in graphical modeler.

Also, what is the best way to get input into the script?

I am beginer in QGIS scripting, but I noticed that the basic scripts deal with inputs differently than any downloaded user scripts (which follow this documentation: https://docs.qgis.org/2.6/en/docs/user_manual/processing/scripts.html).

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 12, 2015 at 2:13
1
  • 1
    Last time I looked QGIS used QT forms, though I suppose any UI for python would work. Is this running as an add-in? Commented Jun 12, 2015 at 2:42

1 Answer 1

2

The following script can be used in the Processing Toolbox (either directly or through the modeler) which allows the user to select a field in the relevant layer and will only output the features with values which occur first:

##Delete first occurence=name
##input=vector
##field=field input
##output=output vector
from qgis.core import *
layer = processing.getObject(input)
provider = layer.dataProvider()
fields = provider.fields()
writer = QgsVectorFileWriter(output, 'UTF-8', fields, provider.geometryType(), layer.crs(), "ESRI Shapefile")
in_feat = QgsFeature()
out_feat = QgsFeature()
geom = QgsGeometry()
values = {}
value_field_index = layer.fieldNameIndex(field)
for in_feat in layer.getFeatures():
 geom = in_feat.geometry()
 attrs = in_feat.attributes()
 value = attrs[value_field_index]
 if value not in values:
 values[value]=[]
 out_feat.setGeometry(geom)
 out_feat.setAttributes(attrs)
 writer.addFeature(out_feat)
del writer

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Mar 7, 2016 at 14:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.