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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Anita Graser'
|
|
|
|
__date__ = 'Dec 2012'
|
|
|
|
__copyright__ = '(C) 2012, Anita Graser'
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
|
|
|
from math import sqrt
|
|
|
|
|
|
|
|
from PyQt4.QtCore import *
|
|
|
|
|
|
|
|
from qgis.core import *
|
|
|
|
|
|
|
|
from sextante.core.GeoAlgorithm import GeoAlgorithm
|
|
|
|
from sextante.core.QGisLayers import QGisLayers
|
|
|
|
from sextante.parameters.ParameterVector import ParameterVector
|
|
|
|
from sextante.parameters.ParameterNumber import ParameterNumber
|
|
|
|
from sextante.outputs.OutputVector import OutputVector
|
|
|
|
|
|
|
|
class DensifyGeometriesInterval(GeoAlgorithm):
|
|
|
|
|
2013-01-12 23:36:00 +01:00
|
|
|
INPUT = "INPUT"
|
2012-12-12 00:47:57 +01:00
|
|
|
INTERVAL = "INTERVAL"
|
|
|
|
OUTPUT = "OUTPUT"
|
|
|
|
|
|
|
|
def defineCharacteristics(self):
|
|
|
|
self.name = "Densify geometries given an interval"
|
2012-12-20 00:16:05 +01:00
|
|
|
self.group = "Vector geometry tools"
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2013-08-06 15:06:35 +03:00
|
|
|
self.addParameter(ParameterVector(self.INPUT, "Input layer", [ParameterVector.VECTOR_TYPE_POLYGON,ParameterVector.VECTOR_TYPE_LINE]))
|
2013-01-12 23:36:00 +01:00
|
|
|
self.addParameter(ParameterNumber(self.INTERVAL, "Interval between Vertices to add", 1, 10000000, 1))
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2013-02-07 23:40:43 +01:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT, "Densified layer"))
|
2012-12-12 00:47:57 +01:00
|
|
|
|
|
|
|
def processAlgorithm(self, progress):
|
2013-01-12 23:36:00 +01:00
|
|
|
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
|
|
|
|
interval = self.getParameterValue(self.INTERVAL)
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2013-02-07 01:09:39 +01:00
|
|
|
isPolygon = layer.geometryType() == QGis.Polygon
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2013-02-28 21:43:59 +04:00
|
|
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields().toList(),
|
2013-02-04 00:14:39 +01:00
|
|
|
layer.wkbType(), layer.crs())
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2013-01-01 23:52:00 +01:00
|
|
|
features = QGisLayers.features(layer)
|
|
|
|
total = 100.0 / float(len(features))
|
2012-12-12 00:47:57 +01:00
|
|
|
current = 0
|
2013-01-12 23:36:00 +01:00
|
|
|
for f in features:
|
2013-01-01 23:52:00 +01:00
|
|
|
featGeometry = QgsGeometry(f.geometry())
|
2013-02-28 21:43:59 +04:00
|
|
|
attrs = f.attributes()
|
2013-01-01 23:52:00 +01:00
|
|
|
newGeometry = self.densifyGeometry(featGeometry, interval, isPolygon)
|
|
|
|
feature = QgsFeature()
|
|
|
|
feature.setGeometry(newGeometry)
|
2013-02-28 21:43:59 +04:00
|
|
|
feature.setAttributes(attrs)
|
2013-01-01 23:52:00 +01:00
|
|
|
writer.addFeature(feature)
|
2012-12-12 00:47:57 +01:00
|
|
|
|
2013-01-01 23:52:00 +01:00
|
|
|
current += 1
|
|
|
|
progress.setPercentage(int(current * total))
|
2012-12-12 00:47:57 +01:00
|
|
|
|
|
|
|
del writer
|
|
|
|
|
|
|
|
def densifyGeometry(self, geometry, interval, isPolygon):
|
|
|
|
output = []
|
|
|
|
if isPolygon:
|
|
|
|
if geometry.isMultipart():
|
|
|
|
polygons = geometry.asMultiPolygon()
|
|
|
|
for poly in polygons:
|
|
|
|
p = []
|
|
|
|
for ring in poly:
|
|
|
|
p.append(self.densify(ring, interval))
|
|
|
|
output.append(p)
|
|
|
|
return QgsGeometry.fromMultiPolygon(output)
|
|
|
|
else:
|
|
|
|
rings = geometry.asPolygon()
|
|
|
|
for ring in rings:
|
|
|
|
output.append(self.densify(ring, interval))
|
|
|
|
return QgsGeometry.fromPolygon(output)
|
|
|
|
else:
|
|
|
|
if geometry.isMultipart():
|
|
|
|
lines = geometry.asMultiPolyline()
|
|
|
|
for points in lines:
|
|
|
|
output.append(self.densify(points, interval))
|
|
|
|
return QgsGeometry.fromMultiPolyline(output)
|
|
|
|
else:
|
|
|
|
points = geometry.asPolyline()
|
|
|
|
output = self.densify(points, interval)
|
|
|
|
return QgsGeometry.fromPolyline(output)
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2012-12-12 00:47:57 +01:00
|
|
|
def densify(self, polyline, interval):
|
|
|
|
output = []
|
|
|
|
for i in xrange(len(polyline) - 1):
|
|
|
|
p1 = polyline[i]
|
|
|
|
p2 = polyline[i + 1]
|
|
|
|
output.append(p1)
|
|
|
|
# calculate necessary number of points between p1 and p2
|
2013-01-12 23:36:00 +01:00
|
|
|
pointsNumber = sqrt(p1.sqrDist(p2)) / interval
|
2012-12-12 00:47:57 +01:00
|
|
|
if pointsNumber > 1:
|
|
|
|
multiplier = 1.0 / float(pointsNumber)
|
|
|
|
else:
|
|
|
|
multiplier = 1
|
|
|
|
for j in xrange(int(pointsNumber)):
|
|
|
|
delta = multiplier * (j + 1)
|
|
|
|
x = p1.x() + delta * (p2.x() - p1.x())
|
|
|
|
y = p1.y() + delta * (p2.y() - p1.y())
|
2013-01-01 23:52:00 +01:00
|
|
|
output.append(QgsPoint(x, y))
|
2012-12-12 00:47:57 +01:00
|
|
|
if j + 1 == pointsNumber:
|
|
|
|
break
|
|
|
|
output.append(polyline[len(polyline) - 1])
|
|
|
|
return output
|