2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
Buffer.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$'
|
|
|
|
|
2017-07-28 13:58:52 +10:00
|
|
|
from qgis.core import (QgsFeature,
|
|
|
|
QgsGeometry,
|
|
|
|
QgsFeatureRequest,
|
|
|
|
QgsFeatureSink)
|
2016-01-15 14:33:17 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2017-07-28 13:58:52 +10:00
|
|
|
def buffering(feedback, context, sink, distance, field, useField, source, dissolve, segments, endCapStyle=1,
|
2017-08-07 18:08:57 +10:00
|
|
|
joinStyle=1, miterLimit=2):
|
2012-10-03 19:18:28 +03:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
if useField:
|
2017-07-28 13:58:52 +10:00
|
|
|
field = source.fields().lookupField(field)
|
2012-10-03 19:18:28 +03:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
outFeat = QgsFeature()
|
2012-10-03 19:18:28 +03:00
|
|
|
|
|
|
|
current = 0
|
2017-07-28 13:58:52 +10:00
|
|
|
total = 100.0 / source.featureCount() if source.featureCount() else 0
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
# With dissolve
|
2013-01-01 23:52:00 +01:00
|
|
|
if dissolve:
|
2017-07-28 14:10:48 +10:00
|
|
|
attributes_to_fetch = []
|
|
|
|
if useField:
|
|
|
|
attributes_to_fetch.append(field)
|
2017-07-28 13:58:52 +10:00
|
|
|
|
2017-07-28 14:10:48 +10:00
|
|
|
features = source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes(attributes_to_fetch))
|
2016-08-11 18:35:19 +10:00
|
|
|
buffered_geometries = []
|
2013-01-01 23:52:00 +01:00
|
|
|
for inFeat in features:
|
2017-07-28 13:58:52 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
|
2013-02-28 21:43:59 +04:00
|
|
|
attrs = inFeat.attributes()
|
2013-01-01 23:52:00 +01:00
|
|
|
if useField:
|
2013-06-03 21:25:22 +02:00
|
|
|
value = attrs[field]
|
2013-01-01 23:52:00 +01:00
|
|
|
else:
|
|
|
|
value = distance
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2016-08-01 16:25:46 +10:00
|
|
|
inGeom = inFeat.geometry()
|
2016-12-15 12:25:20 +01:00
|
|
|
|
2017-08-07 18:08:57 +10:00
|
|
|
buffered_geometries.append(inGeom.buffer(float(value), segments, endCapStyle, joinStyle, miterLimit))
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2013-01-01 23:52:00 +01:00
|
|
|
current += 1
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2013-09-17 12:33:57 +02:00
|
|
|
|
2016-08-11 18:35:19 +10:00
|
|
|
final_geometry = QgsGeometry.unaryUnion(buffered_geometries)
|
|
|
|
outFeat.setGeometry(final_geometry)
|
2013-09-12 13:19:00 +02:00
|
|
|
outFeat.setAttributes(attrs)
|
2017-07-28 13:58:52 +10:00
|
|
|
sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
2013-01-01 23:52:00 +01:00
|
|
|
else:
|
2017-07-28 13:58:52 +10:00
|
|
|
|
|
|
|
features = source.getFeatures()
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
# Without dissolve
|
2013-01-01 23:52:00 +01:00
|
|
|
for inFeat in features:
|
2017-07-28 13:58:52 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
2013-02-28 21:43:59 +04:00
|
|
|
attrs = inFeat.attributes()
|
2013-01-01 23:52:00 +01:00
|
|
|
if useField:
|
2013-06-03 21:25:22 +02:00
|
|
|
value = attrs[field]
|
2013-01-01 23:52:00 +01:00
|
|
|
else:
|
|
|
|
value = distance
|
2016-08-01 16:25:46 +10:00
|
|
|
inGeom = inFeat.geometry()
|
2016-08-11 18:35:19 +10:00
|
|
|
outFeat = QgsFeature()
|
2017-08-07 18:08:57 +10:00
|
|
|
outGeom = inGeom.buffer(float(value), segments, endCapStyle, joinStyle, miterLimit)
|
2016-12-15 12:25:20 +01:00
|
|
|
outFeat.setGeometry(outGeom)
|
2013-09-12 13:19:00 +02:00
|
|
|
outFeat.setAttributes(attrs)
|
2017-07-28 13:58:52 +10:00
|
|
|
sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
2013-01-01 23:52:00 +01:00
|
|
|
current += 1
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|