Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Merge pull request #3435 from nyalldawson/interpolate_angle
Expression functions to Interpolate angle (+ related processing improvements)
- Loading branch information
Showing
25 changed files
with
1,701 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<GMLFeatureClassList> | ||
<GMLFeatureClass> | ||
<Name>extract_nodes_lines</Name> | ||
<ElementPath>extract_nodes_lines</ElementPath> | ||
<GeometryType>1</GeometryType> | ||
<SRSName>EPSG:4326</SRSName> | ||
<DatasetSpecificInfo> | ||
<FeatureCount>17</FeatureCount> | ||
<ExtentXMin>-1.00000</ExtentXMin> | ||
<ExtentXMax>11.00000</ExtentXMax> | ||
<ExtentYMin>-3.00000</ExtentYMin> | ||
<ExtentYMax>5.00000</ExtentYMax> | ||
</DatasetSpecificInfo> | ||
<PropertyDefn> | ||
<Name>distance</Name> | ||
<ElementPath>distance</ElementPath> | ||
<Type>Real</Type> | ||
</PropertyDefn> | ||
<PropertyDefn> | ||
<Name>angle</Name> | ||
<ElementPath>angle</ElementPath> | ||
<Type>Real</Type> | ||
</PropertyDefn> | ||
</GMLFeatureClass> | ||
</GMLFeatureClassList> |
Oops, something went wrong.