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,
|
|
|
|
QgsProcessingUtils)
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterNumber
|
|
|
|
from processing.core.outputs import OutputVector
|
2017-05-02 14:47:58 +10:00
|
|
|
from processing.tools import dataobjects
|
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 10:42:42 +10:00
|
|
|
def icon(self):
|
|
|
|
return QgsApplication.getThemeIcon("/providerQgis.svg")
|
|
|
|
|
|
|
|
def svgIconPath(self):
|
|
|
|
return QgsApplication.iconPath("providerQgis.svg")
|
|
|
|
|
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
|
|
|
self.addParameter(ParameterVector(self.INPUT,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Input layer'),
|
2016-08-23 19:33:42 +03:00
|
|
|
[dataobjects.TYPE_VECTOR_POLYGON, dataobjects.TYPE_VECTOR_LINE]))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterNumber(self.INTERVAL,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Interval between vertices to add'), 0.0, 10000000.0, 1.0))
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2015-05-22 15:56:45 +02:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Densified')))
|
2012-12-12 00:47:57 +01:00
|
|
|
|
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-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
|
2013-01-12 23:36:00 +01:00
|
|
|
interval = self.getParameterValue(self.INTERVAL)
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2016-08-04 09:10:08 +02:00
|
|
|
isPolygon = layer.geometryType() == QgsWkbTypes.PolygonGeometry
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
writer = self.getOutputFromName(
|
2017-05-02 21:15:03 +10:00
|
|
|
self.OUTPUT).getVectorWriter(layer.fields(), layer.wkbType(), layer.crs(), context)
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
2017-04-25 17:53:32 +10:00
|
|
|
total = 100.0 / QgsProcessingUtils.featureCount(layer, context)
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, f in enumerate(features):
|
2016-11-07 09:22:50 +10:00
|
|
|
feature = f
|
|
|
|
if feature.hasGeometry():
|
2017-03-25 16:18:50 +10:00
|
|
|
new_geometry = feature.geometry().densifyByCount(float(interval))
|
2016-11-07 09:22:50 +10:00
|
|
|
feature.setGeometry(new_geometry)
|
2013-01-01 23:52:00 +01:00
|
|
|
writer.addFeature(feature)
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2012-12-12 00:47:57 +01:00
|
|
|
|
|
|
|
del writer
|