2012-12-12 00:47:57 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
DensifyGeometriesInterval.py by Anita Graser, Dec 2012
|
2013-02-28 21:43:59 +04:00
|
|
|
based on DensifyGeometries.py
|
2012-12-12 00:47:57 +01:00
|
|
|
---------------------
|
|
|
|
Date : October 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf 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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-09-21 18:24:26 +02:00
|
|
|
from builtins import range
|
2012-12-12 00:47:57 +01:00
|
|
|
|
|
|
|
__author__ = 'Anita Graser'
|
|
|
|
__date__ = 'Dec 2012'
|
|
|
|
__copyright__ = '(C) 2012, Anita Graser'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-12-12 00:47:57 +01:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-12-12 00:47:57 +01:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
|
|
|
from math import sqrt
|
|
|
|
|
2017-05-02 14:47:58 +10:00
|
|
|
from qgis.core import (QgsWkbTypes,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsApplication,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-06-21 22:11:31 +10:00
|
|
|
QgsProcessingParameterFeatureSource,
|
|
|
|
QgsProcessingParameterNumber,
|
|
|
|
QgsProcessingParameterFeatureSink,
|
|
|
|
QgsProcessingOutputVectorLayer,
|
|
|
|
QgsProcessingParameterDefinition)
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class DensifyGeometriesInterval(QgisAlgorithm):
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
INTERVAL = 'INTERVAL'
|
|
|
|
OUTPUT = 'OUTPUT'
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector geometry tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2017-06-21 22:11:31 +10:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
|
|
|
|
self.tr('Input layer'), [QgsProcessingParameterDefinition.TypeVectorPolygon, QgsProcessingParameterDefinition.TypeVectorLine]))
|
|
|
|
self.addParameter(QgsProcessingParameterNumber(self.INTERVAL,
|
|
|
|
self.tr('Interval between vertices to add'), QgsProcessingParameterNumber.Double,
|
|
|
|
1, False, 0, 10000000))
|
|
|
|
|
|
|
|
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Densified')))
|
|
|
|
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Densified')))
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
self.source = None
|
|
|
|
self.interval = None
|
|
|
|
self.sink = None
|
|
|
|
self.dest_id = None
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'densifygeometriesgivenaninterval'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Densify geometries given an interval')
|
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
def prepareAlgorithm(self, parameters, context, feedback):
|
|
|
|
self.source = self.parameterAsSource(parameters, self.INPUT, context)
|
|
|
|
self.interval = self.parameterAsDouble(parameters, self.INTERVAL, context)
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
(self.sink, self.dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
|
|
|
self.source.fields(), self.source.wkbType(), self.source.sourceCrs())
|
|
|
|
return True
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
def processAlgorithm(self, context, feedback):
|
|
|
|
features = self.source.getFeatures()
|
|
|
|
total = 100.0 / self.source.featureCount() if self.source.featureCount() else 0
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, f in enumerate(features):
|
2017-06-21 22:11:31 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
|
2016-11-07 09:22:50 +10:00
|
|
|
feature = f
|
|
|
|
if feature.hasGeometry():
|
2017-06-28 19:55:08 +10:00
|
|
|
new_geometry = feature.geometry().densifyByDistance(float(self.interval))
|
2016-11-07 09:22:50 +10:00
|
|
|
feature.setGeometry(new_geometry)
|
2017-06-28 19:55:08 +10:00
|
|
|
self.sink.addFeature(feature, QgsFeatureSink.FastInsert)
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2017-06-28 19:55:08 +10:00
|
|
|
return True
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
def postProcessAlgorithm(self, context, feedback):
|
|
|
|
return {self.OUTPUT: self.dest_id}
|