134 lines
5.3 KiB
Python
Raw Normal View History

2012-10-04 19:33:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
SumLines.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. *
* *
***************************************************************************
"""
2016-09-27 19:51:06 +02:00
from builtins import next
2012-10-04 19:33:47 +02:00
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
2012-10-04 19:33:47 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2012-10-04 19:33:47 +02:00
__revision__ = '$Format:%H$'
import os
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtGui import QIcon
from qgis.core import QgsFeature, QgsGeometry, QgsFeatureRequest, QgsDistanceArea
2013-08-12 20:44:27 +02:00
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterString
from processing.core.outputs import OutputVector
from processing.tools import dataobjects, vector
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
2012-09-15 18:25:25 +03:00
class SumLines(GeoAlgorithm):
LINES = 'LINES'
POLYGONS = 'POLYGONS'
LEN_FIELD = 'LEN_FIELD'
COUNT_FIELD = 'COUNT_FIELD'
OUTPUT = 'OUTPUT'
2012-09-15 18:25:25 +03:00
def getIcon(self):
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'sum_lines.png'))
def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Sum line lengths')
self.group, self.i18n_group = self.trAlgorithm('Vector analysis tools')
2015-01-15 20:41:15 +02:00
self.addParameter(ParameterVector(self.LINES,
self.tr('Lines'), [dataobjects.TYPE_VECTOR_LINE]))
2015-01-15 20:41:15 +02:00
self.addParameter(ParameterVector(self.POLYGONS,
self.tr('Polygons'), [dataobjects.TYPE_VECTOR_POLYGON]))
self.addParameter(ParameterString(self.LEN_FIELD,
self.tr('Lines length field name', 'LENGTH')))
self.addParameter(ParameterString(self.COUNT_FIELD,
self.tr('Lines count field name', 'COUNT')))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Line length'), datatype=[dataobjects.TYPE_VECTOR_POLYGON]))
2012-09-15 18:25:25 +03:00
def processAlgorithm(self, progress):
lineLayer = dataobjects.getObjectFromUri(self.getParameterValue(self.LINES))
polyLayer = dataobjects.getObjectFromUri(self.getParameterValue(self.POLYGONS))
lengthFieldName = self.getParameterValue(self.LEN_FIELD)
countFieldName = self.getParameterValue(self.COUNT_FIELD)
(idxLength, fieldList) = vector.findOrCreateField(polyLayer,
polyLayer.fields(), lengthFieldName)
(idxCount, fieldList) = vector.findOrCreateField(polyLayer, fieldList,
countFieldName)
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
fieldList.toList(), polyLayer.wkbType(), polyLayer.crs())
spatialIndex = vector.spatialindex(lineLayer)
ftLine = QgsFeature()
ftPoly = QgsFeature()
2012-09-15 18:25:25 +03:00
outFeat = QgsFeature()
inGeom = QgsGeometry()
outGeom = QgsGeometry()
distArea = QgsDistanceArea()
features = vector.features(polyLayer)
total = 100.0 / len(features)
hasIntersections = False
for current, ftPoly in enumerate(features):
inGeom = ftPoly.geometry()
attrs = ftPoly.attributes()
count = 0
2012-09-15 18:25:25 +03:00
length = 0
hasIntersections = False
lines = spatialIndex.intersects(inGeom.boundingBox())
engine = None
if len(lines) > 0:
hasIntersections = True
# use prepared geometries for faster intersection tests
engine = QgsGeometry.createGeometryEngine(inGeom.geometry())
engine.prepareGeometry()
if hasIntersections:
request = QgsFeatureRequest().setFilterFids(lines).setSubsetOfAttributes([])
for ftLine in lineLayer.getFeatures(request):
tmpGeom = ftLine.geometry()
if engine.intersects(tmpGeom.geometry()):
2012-09-15 18:25:25 +03:00
outGeom = inGeom.intersection(tmpGeom)
2016-09-15 18:26:43 +10:00
length += distArea.measureLength(outGeom)
count += 1
2012-09-15 18:25:25 +03:00
outFeat.setGeometry(inGeom)
if idxLength == len(attrs):
2013-06-03 21:25:22 +02:00
attrs.append(length)
else:
2013-06-03 21:25:22 +02:00
attrs[idxLength] = length
if idxCount == len(attrs):
2013-06-03 21:25:22 +02:00
attrs.append(count)
else:
2013-06-03 21:25:22 +02:00
attrs[idxCount] = count
outFeat.setAttributes(attrs)
2012-09-15 18:25:25 +03:00
writer.addFeature(outFeat)
progress.setPercentage(int(current * total))
2012-09-15 18:25:25 +03:00
del writer