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$'
|
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from qgis.core import QgsFeature, QgsGeometry
|
2016-01-15 14:33:17 +02:00
|
|
|
|
|
|
|
from processing.core.ProcessingLog import ProcessingLog
|
2013-09-20 12:44:47 +02:00
|
|
|
from processing.tools import vector
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
|
|
def buffering(progress, writer, distance, field, useField, layer, dissolve,
|
|
|
|
segments):
|
2012-10-03 19:18:28 +03:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
if useField:
|
2012-10-03 19:18:28 +03:00
|
|
|
field = layer.fieldNameIndex(field)
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
outFeat = QgsFeature()
|
|
|
|
inFeat = QgsFeature()
|
|
|
|
inGeom = QgsGeometry()
|
|
|
|
outGeom = QgsGeometry()
|
2012-10-03 19:18:28 +03:00
|
|
|
|
|
|
|
current = 0
|
2013-09-12 13:19:00 +02:00
|
|
|
features = vector.features(layer)
|
2013-01-01 23:52:00 +01:00
|
|
|
total = 100.0 / float(len(features))
|
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:
|
|
|
|
first = True
|
|
|
|
for inFeat in features:
|
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
|
|
|
|
2013-09-17 12:33:57 +02:00
|
|
|
inGeom = QgsGeometry(inFeat.geometry())
|
2016-07-22 15:36:41 +03:00
|
|
|
if inGeom.isGeosEmpty():
|
|
|
|
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING, 'Feature {} has empty geometry. Skipping...'.format(inFeat.id()))
|
|
|
|
continue
|
|
|
|
if not inGeom.isGeosValid():
|
|
|
|
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING, 'Feature {} has invalid geometry. Skipping...'.format(inFeat.id()))
|
2016-01-15 14:33:17 +02:00
|
|
|
continue
|
2013-09-12 13:19:00 +02:00
|
|
|
outGeom = inGeom.buffer(float(value), segments)
|
|
|
|
if first:
|
|
|
|
tempGeom = QgsGeometry(outGeom)
|
|
|
|
first = False
|
2013-09-17 12:33:57 +02:00
|
|
|
else:
|
|
|
|
tempGeom = tempGeom.combine(outGeom)
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2013-01-01 23:52:00 +01:00
|
|
|
current += 1
|
|
|
|
progress.setPercentage(int(current * total))
|
2013-09-17 12:33:57 +02:00
|
|
|
|
2013-09-12 13:19:00 +02:00
|
|
|
outFeat.setGeometry(tempGeom)
|
|
|
|
outFeat.setAttributes(attrs)
|
|
|
|
writer.addFeature(outFeat)
|
2013-01-01 23:52:00 +01:00
|
|
|
else:
|
2013-10-01 20:52:22 +03:00
|
|
|
# Without dissolve
|
2013-01-01 23:52:00 +01:00
|
|
|
for inFeat in features:
|
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-09-17 12:33:57 +02:00
|
|
|
inGeom = QgsGeometry(inFeat.geometry())
|
2016-01-15 14:33:17 +02:00
|
|
|
if inGeom.isGeosEmpty() or not inGeom.isGeosValid():
|
2016-04-26 15:22:52 +03:00
|
|
|
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING, 'Feature {} has empty or invalid geometry. Skipping...'.format(inFeat.id()))
|
2016-01-15 14:33:17 +02:00
|
|
|
continue
|
|
|
|
|
2013-09-17 12:33:57 +02:00
|
|
|
outGeom = inGeom.buffer(float(value), segments)
|
2013-09-12 13:19:00 +02:00
|
|
|
outFeat.setGeometry(outGeom)
|
|
|
|
outFeat.setAttributes(attrs)
|
2013-09-17 12:33:57 +02:00
|
|
|
writer.addFeature(outFeat)
|
2013-01-01 23:52:00 +01:00
|
|
|
current += 1
|
|
|
|
progress.setPercentage(int(current * total))
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
del writer
|