2016-11-04 17:13:59 +10:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
SnapGeometries.py
|
|
|
|
-----------------
|
|
|
|
Date : October 2016
|
|
|
|
Copyright : (C) 2016 by Nyall Dawson
|
|
|
|
Email : nyall dot dawson 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__ = 'Nyall Dawson'
|
|
|
|
__date__ = 'October 2016'
|
|
|
|
__copyright__ = '(C) 2016, Nyall Dawson'
|
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2017-03-30 13:31:06 +10:00
|
|
|
from qgis.analysis import (QgsGeometrySnapper,
|
|
|
|
QgsInternalGeometrySnapper)
|
2017-07-01 15:44:34 +10:00
|
|
|
from qgis.core import (QgsFeatureSink,
|
2017-07-08 14:43:07 +10:00
|
|
|
QgsProcessing,
|
2017-07-01 15:44:34 +10:00
|
|
|
QgsProcessingParameterFeatureSource,
|
|
|
|
QgsProcessingParameterFeatureSink,
|
|
|
|
QgsProcessingParameterNumber,
|
2017-07-12 07:39:43 +10:00
|
|
|
QgsProcessingParameterEnum)
|
2016-11-04 17:13:59 +10:00
|
|
|
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2016-11-04 17:13:59 +10:00
|
|
|
|
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class SnapGeometriesToLayer(QgisAlgorithm):
|
2016-11-04 17:13:59 +10:00
|
|
|
|
|
|
|
INPUT = 'INPUT'
|
|
|
|
REFERENCE_LAYER = 'REFERENCE_LAYER'
|
|
|
|
TOLERANCE = 'TOLERANCE'
|
|
|
|
OUTPUT = 'OUTPUT'
|
2016-12-19 10:52:29 +01:00
|
|
|
BEHAVIOR = 'BEHAVIOR'
|
2016-11-04 17:13:59 +10: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-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-07-01 15:44:34 +10: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, QgsProcessing.TypeVectorLine, QgsProcessing.TypeVectorPolygon]))
|
2017-07-01 15:44:34 +10:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSource(self.REFERENCE_LAYER, self.tr('Reference layer'),
|
2017-07-08 14:43:07 +10:00
|
|
|
[QgsProcessing.TypeVectorPoint,
|
|
|
|
QgsProcessing.TypeVectorLine,
|
|
|
|
QgsProcessing.TypeVectorPolygon]))
|
2017-07-01 15:44:34 +10:00
|
|
|
|
|
|
|
self.addParameter(QgsProcessingParameterNumber(self.TOLERANCE, self.tr('Tolerance (layer units)'), type=QgsProcessingParameterNumber.Double,
|
|
|
|
minValue=0.00000001, maxValue=9999999999, defaultValue=10.0))
|
2016-11-07 08:28:24 +10:00
|
|
|
|
|
|
|
self.modes = [self.tr('Prefer aligning nodes'),
|
2017-03-30 15:46:07 +10:00
|
|
|
self.tr('Prefer closest point'),
|
|
|
|
self.tr('Move end points only, prefer aligning nodes'),
|
|
|
|
self.tr('Move end points only, prefer closest point'),
|
|
|
|
self.tr('Snap end points to end points only')]
|
2017-07-01 15:44:34 +10:00
|
|
|
self.addParameter(QgsProcessingParameterEnum(
|
2016-12-19 10:52:29 +01:00
|
|
|
self.BEHAVIOR,
|
|
|
|
self.tr('Behavior'),
|
2017-07-01 15:44:34 +10:00
|
|
|
options=self.modes, defaultValue=0))
|
|
|
|
|
|
|
|
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Snapped geometry')))
|
2016-11-04 17:13:59 +10:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'snapgeometries'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Snap geometries to layer')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-07-01 15:44:34 +10:00
|
|
|
source = self.parameterAsSource(parameters, self.INPUT, context)
|
|
|
|
|
|
|
|
reference_source = self.parameterAsSource(parameters, self.REFERENCE_LAYER, context)
|
|
|
|
tolerance = self.parameterAsDouble(parameters, self.TOLERANCE, context)
|
|
|
|
mode = self.parameterAsEnum(parameters, self.BEHAVIOR, context)
|
|
|
|
|
|
|
|
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
|
|
|
source.fields(), source.wkbType(), source.sourceCrs())
|
|
|
|
|
|
|
|
features = source.getFeatures()
|
|
|
|
total = 100.0 / source.featureCount() if source.featureCount() else 0
|
|
|
|
|
|
|
|
if parameters[self.INPUT] != parameters[self.REFERENCE_LAYER]:
|
|
|
|
snapper = QgsGeometrySnapper(reference_source)
|
|
|
|
processed = 0
|
|
|
|
for f in features:
|
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
if f.hasGeometry():
|
|
|
|
out_feature = f
|
|
|
|
out_feature.setGeometry(snapper.snapGeometry(f.geometry(), tolerance, mode))
|
|
|
|
sink.addFeature(out_feature, QgsFeatureSink.FastInsert)
|
|
|
|
else:
|
|
|
|
sink.addFeature(f)
|
|
|
|
processed += 1
|
|
|
|
feedback.setProgress(processed * total)
|
2017-03-30 13:31:06 +10:00
|
|
|
else:
|
|
|
|
# snapping internally
|
|
|
|
snapper = QgsInternalGeometrySnapper(tolerance, mode)
|
|
|
|
processed = 0
|
|
|
|
for f in features:
|
2017-07-01 15:44:34 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
|
2017-03-30 13:31:06 +10:00
|
|
|
out_feature = f
|
|
|
|
out_feature.setGeometry(snapper.snapFeature(f))
|
2017-07-01 15:44:34 +10:00
|
|
|
sink.addFeature(out_feature, QgsFeatureSink.FastInsert)
|
2017-03-30 13:31:06 +10:00
|
|
|
processed += 1
|
2017-07-01 15:44:34 +10:00
|
|
|
feedback.setProgress(processed * total)
|
2016-11-04 17:13:59 +10:00
|
|
|
|
2017-07-01 15:44:34 +10:00
|
|
|
return {self.OUTPUT: dest_id}
|