Skip to content
Permalink
Browse files

review Line intersections tool

  • Loading branch information
alexbruy committed Oct 4, 2012
1 parent 0fa807d commit 330fa7da549ae9b1c1f00d7fd282327200f0573f
Showing with 92 additions and 73 deletions.
  1. +92 −73 python/plugins/sextante/ftools/LinesIntersection.py
@@ -1,100 +1,119 @@
from sextante.core.GeoAlgorithm import GeoAlgorithm
import os.path

from PyQt4 import QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *
from sextante.parameters.ParameterVector import ParameterVector
from sextante.core.QGisLayers import QGisLayers
from sextante.outputs.OutputVector import OutputVector
from sextante.ftools import ftools_utils

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from sextante.core.QGisLayers import QGisLayers

from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterTableField import ParameterTableField

from sextante.outputs.OutputVector import OutputVector

from sextante.ftools import FToolsUtils as utils

class LinesIntersection(GeoAlgorithm):

INPUT1 = "INPUT1"
INPUT2 = "INPUT2"
INPUT_A = "INPUT_A"
INPUT_B = "INPUT_B"
FIELD_A = "FIELD_A"
FIELD_B = "FIELD_B"

OUTPUT = "OUTPUT"
FIELD1 = "FIELD1"
FIELD2 = "FIELD2"

def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/icons/intersections.png")

def defineCharacteristics(self):
self.name = "Line intersections"
self.group = "Analysis tools"

self.addParameter(ParameterVector(self.INPUT_A, "Input layer", ParameterVector.VECTOR_TYPE_LINE))
self.addParameter(ParameterVector(self.INPUT_B, "Intersect layer", ParameterVector.VECTOR_TYPE_LINE))
self.addParameter(ParameterTableField(self.FIELD_A, "Input unique ID field", self.INPUT_A))
self.addParameter(ParameterTableField(self.FIELD_B, "Intersect unique ID field", self.INPUT_B))

self.addOutput(OutputVector(self.OUTPUT, "Output layer"))

def processAlgorithm(self, progress):
layer1 = QGisLayers.getObjectFromUri(self.getParameterValue(LinesIntersection.INPUT1))
layer2 = QGisLayers.getObjectFromUri(self.getParameterValue(LinesIntersection.INPUT2))
field1 = self.getParameterValue(LinesIntersection.FIELD1)
field2 = self.getParameterValue(LinesIntersection.FIELD2)
provider1 = layer1.dataProvider()
provider2 = layer2.dataProvider()
allAttrs = provider1.attributeIndexes()
provider1.select(allAttrs)
allAttrs = provider2.attributeIndexes()
provider2.select(allAttrs)
fieldList = ftools_utils.getFieldList(layer1)
index1 = provider1.fieldNameIndex(field1)
field1 = fieldList[index1]
field1.setName(unicode(field1.name()) + "_1")
fieldList = ftools_utils.getFieldList(layer2)
index2 = provider2.fieldNameIndex(field2)
field2 = fieldList[index2]
field2.setName(unicode(field2.name()) + "_2")
fieldList = {0:field1, 1:field2}
sRs = provider1.crs()
writer = self.getOutputFromName(LinesIntersection.OUTPUT).getVectorWriter(fieldList, QGis.WKBPoint, sRs)
#writer = QgsVectorFileWriter(outPath, "UTF-8", fieldList, QGis.WKBPoint, sRs)
inFeat = QgsFeature()
settings = QSettings()
encoding = settings.value( "/UI/encoding", "System" ).toString()

layerA = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_A))
layerB = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_B))
fieldA = self.getParameterValue(self.FIELD_A)
fieldB = self.getParameterValue(self.FIELD_B)

output = self.getOutputValue(self.OUTPUT)

providerA = layerA.dataProvider()
providerB = layerB.dataProvider()


idxA = layerA.fieldNameIndex(fieldA)
idxB = layerB.fieldNameIndex(fieldB)

fieldList = {0 : layerA.pendingFields()[idxA],
1 : layerB.pendingFields()[idxB]
}

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList,
QGis.WKBPoint, providerA.crs())

spatialIndex = utils.createSpatialIndex(providerB)

providerA.rewind()
layerA.select([idxA])
providerB.rewind()
layerB.select([idxB])

inFeatA = QgsFeature()
inFeatB = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
tempGeom = QgsGeometry()
start = 15.00
add = 85.00 / layer1.featureCount()
index = ftools_utils.createIndex( provider2 )
while provider1.nextFeature(inFeat):
inGeom = inFeat.geometry()
lineList = []
#(check, lineList) = layer2.featuresInRectangle(inGeom.boundingBox(), True, True)
# Below is a work-around for featuresInRectangle
# Which appears to have been removed for QGIS version 1.0
#layer2.select(inGeom.boundingBox(), False)
#lineList = layer2.selectedFeatures()
lineList = index.intersects( inGeom.boundingBox() )
if len(lineList) > 0: check = 0
else: check = 1
if check == 0:
for i in lineList:
provider2.featureAtId( int( i ), inFeatB , True, allAttrs )
tmpGeom = QgsGeometry( inFeatB.geometry() )
#tempGeom = i.geometry()
tempList = []
atMap1 = inFeat.attributeMap()
atMap2 = inFeatB.attributeMap()
tmpGeom = QgsGeometry()

current = 0
total = 100.0 / float(providerA.featureCount())
hasIntersections = False

while layerA.nextFeature(inFeatA):
inGeom = inFeatA.geometry()
hasIntersections = False
lines = spatialIndex.intersects(inGeom.boundingBox())

if len(lines) > 0:
hasIntersections = True

if hasIntersections:
layerB.select([idxB])
for i in lines:
layerB.featureAtId(int(i), inFeatB)
tmpGeom = QgsGeometry(inFeatB.geometry())

points = []
atMapA = inFeatA.attributeMap()
atMapB = inFeatB.attributeMap()

if inGeom.intersects(tmpGeom):
tempGeom = inGeom.intersection(tmpGeom)
if tempGeom.type() == QGis.Point:
if tempGeom.isMultipart():
tempList = tempGeom.asMultiPoint()
points = tempGeom.asMultiPoint()
else:
tempList.append(tempGeom.asPoint())
for j in tempList:
points.append(tempGeom.asPoint())

for j in points:
outFeat.setGeometry(tempGeom.fromPoint(j))
outFeat.addAttribute(0, atMap1[index1])
outFeat.addAttribute(1, atMap2[index2])
outFeat.addAttribute(0, atMapA[idxA])
outFeat.addAttribute(1, atMapB[idxB])
writer.addFeature(outFeat)
start = start + add
progress.setPercentage(start)
del writer

current += 1
progress.setPercentage(int(current * total))

def defineCharacteristics(self):
self.name = "Line intersections"
self.group = "Analysis tools"
self.addParameter(ParameterVector(LinesIntersection.INPUT1, "Input layer", ParameterVector.VECTOR_TYPE_LINE))
self.addParameter(ParameterVector(LinesIntersection.INPUT2, "Intersect layer", ParameterVector.VECTOR_TYPE_LINE))
self.addParameter(ParameterTableField(LinesIntersection.FIELD1, "Input unique ID field", LinesIntersection.INPUT1))
self.addParameter(ParameterTableField(LinesIntersection.FIELD2, "Intersect unique ID field", LinesIntersection.INPUT2))
self.addOutput(OutputVector(LinesIntersection.OUTPUT, "Output layer"))
#=========================================================
del writer

0 comments on commit 330fa7d

Please sign in to comment.
You can’t perform that action at this time.