Skip to content
Browse files
Port processing Densify Geometries algorithm to use c++ densify
Rough benchmark using 50k geometry polygon shapefile

Before: 1m25s
After:  21s
  • Loading branch information
nyalldawson committed Mar 25, 2017
1 parent 249c6fc commit b90aa916e620888cad9285946062206bd965c458
@@ -132,7 +132,9 @@ qgis:deleteholes: >
An optional minimum area parameter allows removing only holes which are smaller than a specified area threshold. Leaving this parameter as 0.0 results in all holes being removed.

This algorithm takes a polygon or line layer and generaates a new one in which the geometries have a larger number of vertices than the original one.
This algorithm takes a polygon or line layer and generates a new one in which the geometries have a larger number of vertices than the original one.

If the geometries have z or m values present then these will be linearly interpolated at the added nodes.

The number of new vertices to add to each feature geometry is specified as an input parameter.

@@ -48,6 +48,7 @@ class DensifyGeometries(GeoAlgorithm):
def defineCharacteristics(self):, self.i18n_name = self.trAlgorithm('Densify geometries'), self.i18n_group = self.trAlgorithm('Vector geometry tools')
self.tags ='add,vertices,points')

self.addParameter(ParameterVector(self.INPUT,'Input layer'),
@@ -74,54 +75,9 @@ def processAlgorithm(self, feedback):
for current, f in enumerate(features):
feature = f
if feature.hasGeometry():
new_geometry = self.densifyGeometry(feature.geometry(), int(vertices),
new_geometry = feature.geometry().densifyByCount(int(vertices))
feedback.setProgress(int(current * total))

del writer

def densifyGeometry(self, geometry, pointsNumber, isPolygon):
output = []
if isPolygon:
if geometry.isMultipart():
polygons = geometry.asMultiPolygon()
for poly in polygons:
p = []
for ring in poly:
p.append(self.densify(ring, pointsNumber))
return QgsGeometry.fromMultiPolygon(output)
rings = geometry.asPolygon()
for ring in rings:
output.append(self.densify(ring, pointsNumber))
return QgsGeometry.fromPolygon(output)
if geometry.isMultipart():
lines = geometry.asMultiPolyline()
for points in lines:
output.append(self.densify(points, pointsNumber))
return QgsGeometry.fromMultiPolyline(output)
points = geometry.asPolyline()
output = self.densify(points, pointsNumber)
return QgsGeometry.fromPolyline(output)

def densify(self, polyline, pointsNumber):
output = []
multiplier = 1.0 / float(pointsNumber + 1)
for i in range(len(polyline) - 1):
p1 = polyline[i]
p2 = polyline[i + 1]
for j in range(pointsNumber):
delta = multiplier * (j + 1)
x = p1.x() + delta * (p2.x() - p1.x())
y = p1.y() + delta * (p2.y() - p1.y())
output.append(QgsPoint(x, y))
if j + 1 == pointsNumber:
output.append(polyline[len(polyline) - 1])
return output

0 comments on commit b90aa91

Please sign in to comment.