Skip to content
Permalink
Browse files
[processing] added some new core algs as scripts
  • Loading branch information
volaya committed Jul 11, 2014
1 parent 784b46b commit e845e7896dc0ad846a117a349a3391f1124197c1
@@ -0,0 +1,46 @@
##Lines=group
##lines=vector
##distance=number 1
##startpoint=number 0
##endpoint=number 0
##output=output vector

from qgis.core import *
from PyQt4.QtCore import *
from processing.core.VectorWriter import VectorWriter


def create_points(feat):
geom = feat.geometry()
length = geom.length()
currentdistance = 0

if endpoint > 0:
length = endpoint

out = QgsFeature()

while startpoint + currentdistance <= length:
point = geom.interpolate(startpoint + currentdistance)
currentdistance = currentdistance + distance
out.setGeometry(point)
attrs = feat.attributes()
attrs.append(currentdistance)
out.setAttributes(attrs)
writer.addFeature(out)


layer = processing.getObject(lines)
fields = layer.dataProvider().fields()
fields.append(QgsField('Distance', QVariant.Double))
writer = VectorWriter(output, None, fields, QGis.WKBPoint,
layer.crs())

feats = processing.features(layer)
nFeat = len(feats)
for i, feat in enumerate(feats):
progress.setPercentage(int(100 * i / nFeat))
create_points(feat)

del writer

@@ -0,0 +1,46 @@
##Vector geometry tools=group
##Polygons=vector
##Max_area=number 100000
##Results=output vector

from qgis.core import *
from PyQt4.QtCore import *
from processing.core.VectorWriter import VectorWriter
from shapely.geometry import Polygon, MultiPolygon
from shapely.wkb import loads
from shapely.wkt import dumps


polyLayer = processing.getObject(Polygons)
polyPrder = polyLayer.dataProvider()
n = polyLayer.featureCount()
l = 0

writer = VectorWriter(Results, None, polyPrder.fields(),
QGis.WKBMultiPolygon, polyPrder.crs())


resgeom = QgsGeometry()
resfeat = QgsFeature()

for feat in processing.features(polyLayer):
progress.setPercentage(int(100*l/n))
l+=1

g = loads(feat.geometry().asWkb())

if g.geom_type == 'MultiPolygon':
resg = [Polygon(p.exterior,
[r for r in p.interiors if Polygon(r).area > Max_area]) for p in g]

else:
resg = [Polygon(g.exterior,
[r for r in g.interiors if Polygon(r).area > Max_area])]

resgeom = QgsGeometry().fromWkt(dumps(MultiPolygon(resg)))

resfeat.setAttributes(feat.attributes())
resfeat.setGeometry(resgeom)
writer.addFeature(resfeat)

del writer
@@ -0,0 +1 @@
{"ALG_CREATOR": "Chourmo - v1", "ALG_DESC": "Delete all holes (or \"rings\") in polygons or multipolygons over an area limit", "ALG_HELP_CREATOR": "Chourmo - v1", "Area": "Maximum area for holes in result"}
@@ -0,0 +1,35 @@
##Vector table tools=group
##Input=vector
##Fields=string
##Output=output table

from qgis.core import *
from PyQt4.QtCore import *
from processing.core.TableWriter import TableWriter
from collections import defaultdict

layer = processing.getObject(input)
inputFields = layer.pendingFields()
fieldIdxs = []
fields = fields.split(',')
for f in fields:
idx = inputFields.indexFromName(f)
if idx == -1:
raise GeoAlgorithmExecutionException('Field not found:' + f)
fieldIdxs.append(idx)
writer = TableWriter(output, None, fields + ['FREQ'])

counts = {}
feats = processing.features(layer)
nFeats = len(feats)
counts = defaultdict(int)
for i, feat in enumerate(feats):
progress.setPercentage(int(100 * i / nFeats))
attrs = feat.attributes()
clazz = tuple([attrs[idx] for idx in fieldIdxs])
print clazz
counts[clazz] += 1

for c in counts:
writer.addRecord(list(c) + [counts[c]])

@@ -0,0 +1,44 @@
##Vector geometry tools=group
##Polygons=vector polygon
##To_keep=number 1
##Results=output vector

from qgis.core import *
from PyQt4.QtCore import *
from processing.core.VectorWriter import VectorWriter
from operator import itemgetter

To_keep = int(To_keep)
if To_keep < 1:
progress.setInfo("'To keep' value has been modified to be at least 1.")
To_keep = 1


polyLayer = processing.getObject(Polygons)
polyPrder = polyLayer.dataProvider()
count = polyLayer.featureCount()
writer = VectorWriter(Results, None, polyPrder.fields(),
QGis.WKBMultiPolygon, polyPrder.crs())


for n, feat in enumerate(processing.features(polyLayer)):
progress.setPercentage(int(100*n/count))
geom = feat.geometry()
if geom.isMultipart():
featres = feat
geoms = geom.asGeometryCollection()
geomarea = [(i, geoms[i].area()) for i in range(len(geoms))]
geomarea.sort(key=itemgetter(1))
if To_keep == 1:
featres.setGeometry(geoms[geomrea[-1][0]])
else if To_keep > len(geoms):
featres.setGeometry(geom)
else:
featres.setGeometry(geom)
geomres = [geoms[i].asPolygon() for i,a in geomarea[-1 * To_keep]]
featres.setGeometry(QgsGeometry.fromMultiPolygon(geomres))
writer.addFeature(featres)
else:
writer.addFeature(feat)

del writer
@@ -0,0 +1 @@
{"ALG_CREATOR": "Chourmo - v1", "To_keep": "Number of parts to keep, must be larger than 0", "ALG_DESC": "Keep n biggest sub polygon in a multipolygon, without exploding it...", "ALG_HELP_CREATOR": "Chourmo - v1", "Polygons": "Input layer"}
@@ -0,0 +1,46 @@
##Vector table tools=group
##input=vector
##class_field=field input
##value_field=field input
##output=output vector

from qgis.core import *
from PyQt4.QtCore import *
from processing.core.VectorWriter import VectorWriter

layer = processing.getObject(input)
provider = layer.dataProvider()
fields = provider.fields()
fields.append(QgsField('UNIQ_COUNT', QVariant.Int))
writer = VectorWriter(output, None, fields, provider.geometryType(),
layer.crs())

class_field_index = layer.fieldNameIndex(class_field)
value_field_index = layer.fieldNameIndex(value_field)

outFeat = QgsFeature()
classes = {}
feats = processing.features(layer)
nFeat = len(feats)
for n, inFeat in enumerate(feats):
progress.setPercentage(int(100 * n / nFeat))
attrs = inFeat.attributes()
clazz = attrs[class_field_index]
value = attrs[value_field_index]
if clazz not in classes:
classes[clazz] = []
if value not in classes[clazz]:
classes[clazz].append(value)

feats = processing.features(layer)
for n, inFeat in enumerate(feats):
progress.setPercentage(int(100 * n / nFeat))
inGeom = inFeat.geometry()
outFeat.setGeometry(inGeom)
attrs = inFeat.attributes()
clazz = attrs[class_field_index]
attrs.append(len(classes[clazz]))
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)

del writer

0 comments on commit e845e78

Please sign in to comment.