Skip to content
Permalink
Browse files

[FEATURE][processing] Optimised points along geometry algorithm

Supports also polygon geometries, handles null geometries,
and records the original line angle along with the distance
for each point.
  • Loading branch information
nyalldawson committed Aug 29, 2016
1 parent 9a9a49c commit 8db9284cb304618c91c22c380711a0d4dd02f707
@@ -284,6 +284,11 @@ qgis:orientedminimumboundingbox: >

As an alternative, the output layer can contain not just a single rectangle, but one for each input feature, representing the minimum rectangle that covers each of them.

qgis:pointsalonglines: >
Creates points at regular intervals along line or polygon geometries. Created points will have new attributes added for the distance along the geometry and the angle of the line at the point.

An optional start and end offset can be specified, which controls how far from the start and end of the geometry the points should be created.

qgis:pointsdisplacement:


@@ -0,0 +1,113 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
PointsAlongGeometry.py
---------------------
Date : August 2016
Copyright : (C) 2016 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""

__author__ = 'Nyall Dawson'
__date__ = 'August 2016'
__copyright__ = '(C) 2016, Nyall Dawson'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

import os
import math

from qgis.PyQt.QtCore import QVariant
from qgis.PyQt.QtGui import QIcon

from qgis.core import QgsFeature, QgsGeometry, QgsWkbTypes, QgsField

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector, ParameterNumber
from processing.core.outputs import OutputVector
from processing.tools import dataobjects, vector

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]


class PointsAlongGeometry(GeoAlgorithm):

INPUT = 'INPUT'
OUTPUT = 'OUTPUT'
DISTANCE = 'DISTANCE'
START_OFFSET = 'START_OFFSET'
END_OFFSET = 'END_OFFSET'

def getIcon(self):
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'extract_nodes.png'))

def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Points along lines')
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools')

self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer'),
[ParameterVector.VECTOR_TYPE_POLYGON, ParameterVector.VECTOR_TYPE_LINE]))
self.addParameter(ParameterNumber(self.DISTANCE,
self.tr('Distance'), default=1.0))
self.addParameter(ParameterNumber(self.START_OFFSET,
self.tr('Start offset'), default=0.0))
self.addParameter(ParameterNumber(self.END_OFFSET,
self.tr('End offset'), default=0.0))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Points')))

def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT))
distance = self.getParameterValue(self.DISTANCE)
start_offset = self.getParameterValue(self.START_OFFSET)
end_offset = self.getParameterValue(self.END_OFFSET)

fields = layer.fields()
fields.append(QgsField('distance', QVariant.Double))
fields.append(QgsField('angle', QVariant.Double))

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
fields, QgsWkbTypes.Point, layer.crs())

features = vector.features(layer)
total = 100.0 / len(features)
for current, input_feature in enumerate(features):
input_geometry = input_feature.geometry()
if not input_geometry:
writer.addFeature(input_feature)
else:
if input_geometry.type == QgsWkbTypes.PolygonGeometry:
length = input_geometry.geometry().perimeter()
else:
length = input_geometry.length() - end_offset
current_distance = start_offset

while current_distance <= length:
point = input_geometry.interpolate(current_distance)
angle = math.degrees(input_geometry.interpolateAngle(current_distance))

output_feature = QgsFeature()
output_feature.setGeometry(point)
attrs = input_feature.attributes()
attrs.append(current_distance)
attrs.append(angle)
output_feature.setAttributes(attrs)
writer.addFeature(output_feature)

current_distance += distance

progress.setPercentage(int(current * total))

del writer
@@ -154,6 +154,7 @@
from .PolygonCentroids import PolygonCentroids
from .Translate import Translate
from .SingleSidedBuffer import SingleSidedBuffer
from .PointsAlongGeometry import PointsAlongGeometry

pluginPath = os.path.normpath(os.path.join(
os.path.split(os.path.dirname(__file__))[0], os.pardir))
@@ -208,7 +209,8 @@ def __init__(self):
RectanglesOvalsDiamondsFixed(), MergeLines(),
BoundingBox(), Boundary(), PointOnSurface(),
OffsetLine(), PolygonCentroids(),
Translate(), SingleSidedBuffer()
Translate(), SingleSidedBuffer(),
PointsAlongGeometry()
]

if hasMatplotlib:
@@ -23,7 +23,7 @@ tests:
OUTPUT:
name: expected/polys_deleteholes.gml
type: vector

- algorithm: qgis:clip
name: Clip lines by polygons
params:
@@ -37,8 +37,8 @@ tests:
OUTPUT:
name: expected/clip_lines_by_polygon.gml
type: vector


- algorithm: qgis:clip
name: Clip lines by multipolygon
params:
@@ -52,7 +52,7 @@ tests:
OUTPUT:
name: expected/clip_lines_by_multipolygon.gml
type: vector

- algorithm: qgis:clip
name: Clip polygons by multipolygons
params:
@@ -66,7 +66,7 @@ tests:
OUTPUT:
name: expected/clip_polys_by_multipolygon.gml
type: vector

- algorithm: qgis:clip
name: Clip multipolygons by polygons
params:
@@ -80,7 +80,7 @@ tests:
OUTPUT:
name: expected/clip_multipolygons_by_polygons.gml
type: vector

- algorithm: qgis:clip
name: Clip points by polygons
params:
@@ -94,7 +94,7 @@ tests:
OUTPUT:
name: expected/clip_points_by_polygons.gml
type: vector

- algorithm: qgis:clip
name: Clip points by multipolygons
params:
@@ -108,8 +108,8 @@ tests:
OUTPUT:
name: expected/clip_points_by_multipolygons.gml
type: vector


# These datasets should produce a geometry collection and not a polygon only
# dataset. If the algorithm is fixed, a new test should be introduced to
# check this behavior.
@@ -439,7 +439,7 @@ tests:
OUTPUT:
name: expected/multi_to_single.gml
type: vector

- algorithm: qgis:boundingboxes
name: Bounding boxes for lines
params:
@@ -808,7 +808,7 @@ tests:
OUTPUT_LAYER:
name: expected/centroid_polys.gml
type: vector

- algorithm: qgis:translategeometry
name: Lines translated
params:
@@ -822,7 +822,7 @@ tests:
name: expected/lines_translated.gml
type: vector


- algorithm: qgis:singlesidedbuffer
name: Single sided buffer lines (left, round)
params:
@@ -869,4 +869,4 @@ tests:
results:
OUTPUT_LAYER:
name: expected/single_sided_buffer_multiline_bevel.gml
type: vector
type: vector

0 comments on commit 8db9284

Please sign in to comment.
You can’t perform that action at this time.