2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
Clip.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$'
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
from PyQt4.QtCore import *
|
|
|
|
from qgis.core import *
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
|
|
|
from processing.core.ProcessingLog import ProcessingLog
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.outputs import OutputVector
|
2013-09-15 17:00:00 +03:00
|
|
|
from processing.tools import dataobjects, vector
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
class Clip(GeoAlgorithm):
|
2013-09-15 17:00:00 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
OVERLAY = 'OVERLAY'
|
|
|
|
OUTPUT = 'OUTPUT'
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-02-28 13:27:09 +04:00
|
|
|
def defineCharacteristics(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
self.name = 'Clip'
|
|
|
|
self.group = 'Vector overlay tools'
|
|
|
|
self.addParameter(ParameterVector(Clip.INPUT, 'Input layer',
|
|
|
|
[ParameterVector.VECTOR_TYPE_ANY]))
|
|
|
|
self.addParameter(ParameterVector(Clip.OVERLAY, 'Clip layer',
|
|
|
|
[ParameterVector.VECTOR_TYPE_ANY]))
|
|
|
|
self.addOutput(OutputVector(Clip.OUTPUT, 'Clipped'))
|
2013-02-28 13:27:09 +04:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
def processAlgorithm(self, progress):
|
2013-10-01 20:52:22 +03:00
|
|
|
layerA = dataobjects.getObjectFromUri(
|
|
|
|
self.getParameterValue(Clip.INPUT))
|
|
|
|
layerB = dataobjects.getObjectFromUri(
|
|
|
|
self.getParameterValue(Clip.OVERLAY))
|
2013-02-28 13:27:09 +04:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
|
|
|
|
layerA.pendingFields(),
|
|
|
|
layerA.dataProvider().geometryType(),
|
|
|
|
layerA.dataProvider().crs())
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
inFeatA = QgsFeature()
|
|
|
|
inFeatB = QgsFeature()
|
|
|
|
outFeat = QgsFeature()
|
2013-02-28 13:27:09 +04:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
index = vector.spatialindex(layerB)
|
2013-02-28 13:27:09 +04:00
|
|
|
|
2013-09-12 13:19:00 +02:00
|
|
|
selectionA = vector.features(layerA)
|
2013-02-28 13:27:09 +04:00
|
|
|
|
|
|
|
current = 0
|
|
|
|
total = 100.0 / float(len(selectionA))
|
|
|
|
|
2013-01-21 23:04:34 +01:00
|
|
|
for inFeatA in selectionA:
|
2013-02-28 13:27:09 +04:00
|
|
|
geom = QgsGeometry(inFeatA.geometry())
|
|
|
|
attrs = inFeatA.attributes()
|
2013-09-15 17:00:00 +03:00
|
|
|
intersects = index.intersects(geom.boundingBox())
|
2013-02-04 00:14:39 +01:00
|
|
|
first = True
|
2013-02-28 13:27:09 +04:00
|
|
|
found = False
|
2013-09-15 17:00:00 +03:00
|
|
|
if len(intersects) > 0:
|
|
|
|
for i in intersects:
|
2013-10-01 20:52:22 +03:00
|
|
|
layerB.getFeatures(
|
|
|
|
QgsFeatureRequest().setFilterFid(i)).nextFeature(
|
|
|
|
inFeatB)
|
2013-02-28 13:27:09 +04:00
|
|
|
tmpGeom = QgsGeometry(inFeatB.geometry())
|
|
|
|
if tmpGeom.intersects(geom):
|
|
|
|
found = True
|
|
|
|
if first:
|
|
|
|
outFeat.setGeometry(QgsGeometry(tmpGeom))
|
|
|
|
first = False
|
|
|
|
else:
|
2013-09-15 17:00:00 +03:00
|
|
|
try:
|
|
|
|
cur_geom = QgsGeometry(outFeat.geometry())
|
2013-10-01 20:52:22 +03:00
|
|
|
new_geom = QgsGeometry(
|
|
|
|
cur_geom.combine(tmpGeom))
|
2013-09-15 17:00:00 +03:00
|
|
|
outFeat.setGeometry(QgsGeometry(new_geom))
|
|
|
|
except:
|
2013-10-01 20:52:22 +03:00
|
|
|
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
|
|
|
|
'GEOS geoprocessing error: One or \
|
|
|
|
more input features have invalid \
|
|
|
|
geometry.'
|
|
|
|
)
|
2013-09-15 17:00:00 +03:00
|
|
|
break
|
|
|
|
if found:
|
|
|
|
try:
|
|
|
|
cur_geom = QgsGeometry(outFeat.geometry())
|
|
|
|
new_geom = QgsGeometry(geom.intersection(cur_geom))
|
|
|
|
if new_geom.wkbType() == 0:
|
|
|
|
int_com = QgsGeometry(geom.combine(cur_geom))
|
|
|
|
int_sym = QgsGeometry(geom.symDifference(cur_geom))
|
|
|
|
new_geom = QgsGeometry(int_com.difference(int_sym))
|
|
|
|
try:
|
|
|
|
outFeat.setGeometry(new_geom)
|
|
|
|
outFeat.setAttributes(attrs)
|
|
|
|
writer.addFeature(outFeat)
|
|
|
|
except:
|
2013-10-01 20:52:22 +03:00
|
|
|
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
|
|
|
|
'Feature geometry error: One or more \
|
|
|
|
output features ignored due to invalid \
|
|
|
|
geometry.'
|
|
|
|
)
|
2013-09-15 17:00:00 +03:00
|
|
|
continue
|
|
|
|
except:
|
2013-10-01 20:52:22 +03:00
|
|
|
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
|
|
|
|
'GEOS geoprocessing error: One or more input \
|
|
|
|
features have invalid geometry.'
|
|
|
|
)
|
2013-09-15 17:00:00 +03:00
|
|
|
continue
|
2013-02-28 13:27:09 +04:00
|
|
|
|
|
|
|
current += 1
|
|
|
|
progress.setPercentage(int(current * total))
|
|
|
|
|
|
|
|
del writer
|