2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
DensifyGeometries.py
|
|
|
|
---------------------
|
|
|
|
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-10-04 19:33:47 +02:00
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'October 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
|
|
|
|
2017-05-02 14:47:58 +10:00
|
|
|
from qgis.core import (QgsWkbTypes,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsApplication,
|
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
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2012-10-03 18:46:01 +03:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class DensifyGeometries(QgisAlgorithm):
|
2012-10-03 18:46:01 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
VERTICES = 'VERTICES'
|
|
|
|
OUTPUT = 'OUTPUT'
|
2012-10-03 18:46:01 +03:00
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
|
|
|
return QgsApplication.getThemeIcon("/providerQgis.svg")
|
|
|
|
|
|
|
|
def svgIconPath(self):
|
|
|
|
return QgsApplication.iconPath("providerQgis.svg")
|
|
|
|
|
2017-03-29 09:51:13 +10:00
|
|
|
def tags(self):
|
|
|
|
return self.tr('add,vertices,points').split(',')
|
|
|
|
|
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__()
|
2015-01-15 20:41:15 +02: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.VERTICES,
|
|
|
|
self.tr('Vertices to add'), QgsProcessingParameterNumber.Integer,
|
|
|
|
1, False, 1, 10000000))
|
|
|
|
|
|
|
|
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Densified')))
|
|
|
|
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Densified')))
|
2012-10-03 18:46:01 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'densifygeometries'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Densify geometries')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-06-21 22:11:31 +10:00
|
|
|
source = self.parameterAsSource(parameters, self.INPUT, context)
|
|
|
|
vertices = self.parameterAsInt(parameters, self.VERTICES, context)
|
2012-10-03 18:46:01 +03:00
|
|
|
|
2017-06-21 22:11:31 +10:00
|
|
|
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
|
|
|
source.fields(), source.wkbType(), source.sourceCrs())
|
2012-10-03 18:46:01 +03:00
|
|
|
|
2017-06-21 22:11:31 +10:00
|
|
|
features = source.getFeatures()
|
|
|
|
total = 100.0 / source.featureCount()
|
2012-10-03 18:46:01 +03:00
|
|
|
|
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-21 22:11:31 +10:00
|
|
|
new_geometry = feature.geometry().densifyByCount(vertices)
|
2016-11-07 09:22:50 +10:00
|
|
|
feature.setGeometry(new_geometry)
|
2017-06-21 22:11:31 +10:00
|
|
|
sink.addFeature(feature)
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2012-10-03 18:46:01 +03:00
|
|
|
|
2017-06-21 22:11:31 +10:00
|
|
|
return {self.OUTPUT: dest_id}
|