95 lines
4.0 KiB
Python
Raw Normal View History

2012-10-04 19:33:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
Intersection.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. *
* *
***************************************************************************
"""
2012-10-04 19:33:47 +02:00
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
2013-08-12 20:44:27 +02:00
from processing.core.GeoAlgorithm import GeoAlgorithm
2012-09-15 18:25:25 +03:00
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
2013-08-12 20:44:27 +02:00
from processing.parameters.ParameterVector import ParameterVector
from processing.core.QGisLayers import QGisLayers
from processing.outputs.OutputVector import OutputVector
from processing.tools import vector as utils
2012-09-15 18:25:25 +03:00
class Intersection(GeoAlgorithm):
INPUT = "INPUT"
INPUT2 = "INPUT2"
OUTPUT = "OUTPUT"
#===========================================================================
# def getIcon(self):
# return QtGui.QIcon(os.path.dirname(__file__) + "/icons/intersect.png")
#===========================================================================
2012-09-15 18:25:25 +03:00
def processAlgorithm(self, progress):
vlayerA = QGisLayers.getObjectFromUri(self.getParameterValue(Intersection.INPUT))
vlayerB = QGisLayers.getObjectFromUri(self.getParameterValue(Intersection.INPUT2))
2013-02-07 01:09:39 +01:00
vproviderA = vlayerA.dataProvider()
fields = utils.combineVectorFields(vlayerA, vlayerB)
2012-09-15 18:25:25 +03:00
writer = self.getOutputFromName(Intersection.OUTPUT).getVectorWriter(fields, vproviderA.geometryType(), vproviderA.crs() )
inFeatA = QgsFeature()
inFeatB = QgsFeature()
outFeat = QgsFeature()
index = utils.spatialindex(vlayerB)
2012-09-15 18:25:25 +03:00
nElement = 0
selectionA = QGisLayers.features(vlayerA)
2013-02-07 01:09:39 +01:00
nFeat = len(selectionA)
for inFeatA in selectionA:
nElement += 1
progress.setPercentage(nElement/float(nFeat) * 100)
geom = QgsGeometry( inFeatA.geometry() )
atMapA = inFeatA.attributes()
intersects = index.intersects( geom.boundingBox() )
for i in intersects:
request = QgsFeatureRequest().setFilterFid(i)
2013-03-26 14:15:12 +01:00
inFeatB = vlayerB.getFeatures(request).next()
tmpGeom = QgsGeometry(inFeatB.geometry())
2013-07-09 22:30:51 +02:00
if geom.intersects( tmpGeom ):
atMapB = inFeatB.attributes()
int_geom = QgsGeometry( geom.intersection( tmpGeom ) )
if int_geom.wkbType() == 7:
int_com = geom.combine( tmpGeom )
int_sym = geom.symDifference( tmpGeom )
int_geom = QgsGeometry( int_com.difference( int_sym ) )
outFeat.setGeometry( int_geom )
attrs = []
attrs.extend(atMapA)
attrs.extend(atMapB)
outFeat.setAttributes(attrs)
writer.addFeature( outFeat )
2013-02-07 01:09:39 +01:00
2013-01-01 23:52:00 +01:00
2012-09-15 18:25:25 +03:00
del writer
2013-02-07 01:09:39 +01:00
2012-09-15 18:25:25 +03:00
def defineCharacteristics(self):
self.name = "Intersection"
self.group = "Vector overlay tools"
self.addParameter(ParameterVector(Intersection.INPUT, "Input layer", [ParameterVector.VECTOR_TYPE_ANY]))
self.addParameter(ParameterVector(Intersection.INPUT2, "Intersect layer", [ParameterVector.VECTOR_TYPE_ANY]))
2012-09-15 18:25:25 +03:00
self.addOutput(OutputVector(Intersection.OUTPUT, "Intersection"))