2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
Delaunay.py
|
|
|
|
---------------------
|
|
|
|
Date : August 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__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 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
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
|
|
|
from qgis.PyQt.QtCore import QVariant
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2018-05-15 11:24:29 +07:00
|
|
|
from qgis.core import (QgsApplication,
|
|
|
|
QgsField,
|
2017-05-02 21:15:03 +10:00
|
|
|
QgsFeatureRequest,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-05-02 21:15:03 +10:00
|
|
|
QgsFeature,
|
|
|
|
QgsGeometry,
|
2017-06-01 12:18:43 +02:00
|
|
|
QgsPointXY,
|
2017-05-02 21:15:03 +10:00
|
|
|
QgsWkbTypes,
|
2017-07-08 14:43:07 +10:00
|
|
|
QgsProcessing,
|
2017-07-01 18:00:51 +10:00
|
|
|
QgsFields,
|
2017-07-13 08:55:17 +03:00
|
|
|
QgsProcessingException,
|
2017-07-12 07:50:13 +10:00
|
|
|
QgsProcessingParameterFeatureSource,
|
2017-07-12 07:39:43 +10:00
|
|
|
QgsProcessingParameterFeatureSink)
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2016-08-23 19:33:42 +03:00
|
|
|
|
2016-03-21 04:58:12 +01:00
|
|
|
from . import voronoi
|
2013-02-28 21:43:59 +04:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class Delaunay(QgisAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2018-05-15 11:24:29 +07:00
|
|
|
return QgsApplication.getThemeIcon("/algorithms/mAlgorithmDelaunay.svg")
|
|
|
|
|
|
|
|
def svgIconPath(self):
|
|
|
|
return QgsApplication.iconPath("/algorithms/mAlgorithmDelaunay.svg")
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
2017-08-22 23:36:42 +10:00
|
|
|
return self.tr('Vector geometry')
|
2017-03-29 12:04:09 +10:00
|
|
|
|
2017-12-14 14:03:56 +02:00
|
|
|
def groupId(self):
|
|
|
|
return 'vectorgeometry'
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2012-10-01 20:59:10 +03:00
|
|
|
|
2017-07-10 16:31:14 +10:00
|
|
|
def initAlgorithm(self, config=None):
|
2017-07-08 14:43:07 +10:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT, self.tr('Input layer'), [QgsProcessing.TypeVectorPoint]))
|
|
|
|
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Delaunay triangulation'), type=QgsProcessing.TypeVectorPolygon))
|
2012-10-01 20:59:10 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'delaunaytriangulation'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Delaunay triangulation')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-07-01 18:00:51 +10:00
|
|
|
source = self.parameterAsSource(parameters, self.INPUT, context)
|
2018-04-27 12:31:56 +10:00
|
|
|
if source is None:
|
|
|
|
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
|
2012-10-01 20:59:10 +03:00
|
|
|
|
2017-05-02 21:15:03 +10:00
|
|
|
fields = QgsFields()
|
|
|
|
fields.append(QgsField('POINTA', QVariant.Double, '', 24, 15))
|
|
|
|
fields.append(QgsField('POINTB', QVariant.Double, '', 24, 15))
|
|
|
|
fields.append(QgsField('POINTC', QVariant.Double, '', 24, 15))
|
2012-10-01 20:59:10 +03:00
|
|
|
|
2017-07-01 18:00:51 +10:00
|
|
|
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
|
|
|
fields, QgsWkbTypes.Polygon, source.sourceCrs())
|
2018-04-27 12:52:02 +10:00
|
|
|
if sink is None:
|
|
|
|
raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT))
|
2012-10-01 20:59:10 +03:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
pts = []
|
|
|
|
ptDict = {}
|
2013-01-12 23:36:00 +01:00
|
|
|
ptNdx = -1
|
2012-10-01 20:59:10 +03:00
|
|
|
c = voronoi.Context()
|
2017-07-01 18:00:51 +10:00
|
|
|
features = source.getFeatures()
|
|
|
|
total = 100.0 / source.featureCount() if source.featureCount() else 0
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, inFeat in enumerate(features):
|
2017-07-01 18:00:51 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
|
2016-10-17 10:47:20 +02:00
|
|
|
geom = QgsGeometry(inFeat.geometry())
|
2017-01-30 21:58:23 +10:00
|
|
|
if geom.isNull():
|
2016-10-17 13:45:51 +02:00
|
|
|
continue
|
2016-10-16 15:07:03 +02:00
|
|
|
if geom.isMultipart():
|
|
|
|
points = geom.asMultiPoint()
|
|
|
|
else:
|
2016-10-22 09:03:24 +10:00
|
|
|
points = [geom.asPoint()]
|
2016-10-16 15:07:03 +02:00
|
|
|
for n, point in enumerate(points):
|
|
|
|
x = point.x()
|
|
|
|
y = point.y()
|
|
|
|
pts.append((x, y))
|
|
|
|
ptNdx += 1
|
|
|
|
ptDict[ptNdx] = (inFeat.id(), n)
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2012-10-01 20:59:10 +03:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
if len(pts) < 3:
|
2017-07-13 08:55:17 +03:00
|
|
|
raise QgsProcessingException(
|
2015-01-15 20:41:15 +02:00
|
|
|
self.tr('Input file should contain at least 3 points. Choose '
|
|
|
|
'another file and try again.'))
|
2012-10-01 20:59:10 +03:00
|
|
|
|
2016-03-14 20:26:58 +01:00
|
|
|
uniqueSet = set(item for item in pts)
|
2012-09-15 18:25:25 +03:00
|
|
|
ids = [pts.index(item) for item in uniqueSet]
|
|
|
|
sl = voronoi.SiteList([voronoi.Site(*i) for i in uniqueSet])
|
|
|
|
c.triangulate = True
|
|
|
|
voronoi.voronoi(sl, c)
|
|
|
|
triangles = c.triangles
|
|
|
|
feat = QgsFeature()
|
2012-10-01 20:59:10 +03:00
|
|
|
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / len(triangles) if triangles else 1
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, triangle in enumerate(triangles):
|
2017-07-01 18:00:51 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
|
2017-01-16 15:13:30 +01:00
|
|
|
indices = list(triangle)
|
|
|
|
indices.append(indices[0])
|
2012-10-01 20:59:10 +03:00
|
|
|
polygon = []
|
2013-02-04 00:14:39 +01:00
|
|
|
attrs = []
|
2012-10-01 20:59:10 +03:00
|
|
|
step = 0
|
2017-01-16 15:13:30 +01:00
|
|
|
for index in indices:
|
2016-10-16 15:07:03 +02:00
|
|
|
fid, n = ptDict[ids[index]]
|
|
|
|
request = QgsFeatureRequest().setFilterFid(fid)
|
2017-07-01 18:00:51 +10:00
|
|
|
inFeat = next(source.getFeatures(request))
|
2016-10-17 10:47:20 +02:00
|
|
|
geom = QgsGeometry(inFeat.geometry())
|
2016-10-16 15:07:03 +02:00
|
|
|
if geom.isMultipart():
|
2017-06-01 12:18:43 +02:00
|
|
|
point = QgsPointXY(geom.asMultiPoint()[n])
|
2016-10-16 15:07:03 +02:00
|
|
|
else:
|
2017-06-01 12:18:43 +02:00
|
|
|
point = QgsPointXY(geom.asPoint())
|
2012-10-01 20:59:10 +03:00
|
|
|
polygon.append(point)
|
|
|
|
if step <= 3:
|
2013-06-09 16:04:44 +02:00
|
|
|
attrs.append(ids[index])
|
2012-10-01 20:59:10 +03:00
|
|
|
step += 1
|
2013-02-04 00:14:39 +01:00
|
|
|
feat.setAttributes(attrs)
|
2017-10-30 09:13:04 +01:00
|
|
|
geometry = QgsGeometry().fromPolygonXY([polygon])
|
2012-10-01 20:59:10 +03:00
|
|
|
feat.setGeometry(geometry)
|
2017-07-01 18:00:51 +10:00
|
|
|
sink.addFeature(feat, QgsFeatureSink.FastInsert)
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2012-10-01 20:59:10 +03:00
|
|
|
|
2017-07-01 18:00:51 +10:00
|
|
|
return {self.OUTPUT: dest_id}
|