|
1 |
| -from sextante.core.GeoAlgorithm import GeoAlgorithm |
2 | 1 | import os.path
|
| 2 | + |
3 | 3 | from PyQt4 import QtGui
|
4 | 4 | from PyQt4.QtCore import *
|
5 |
| -from PyQt4.QtGui import * |
| 5 | + |
6 | 6 | from qgis.core import *
|
7 |
| -from sextante.parameters.ParameterVector import ParameterVector |
8 |
| -from sextante.core.QGisLayers import QGisLayers |
9 |
| -from sextante.outputs.OutputVector import OutputVector |
10 |
| -from sextante.ftools import ftools_utils |
| 7 | + |
| 8 | +from sextante.core.GeoAlgorithm import GeoAlgorithm |
11 | 9 | from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
| 10 | +from sextante.core.QGisLayers import QGisLayers |
| 11 | + |
| 12 | +from sextante.parameters.ParameterVector import ParameterVector |
12 | 13 | from sextante.parameters.ParameterTableField import ParameterTableField
|
13 | 14 |
|
| 15 | +from sextante.outputs.OutputVector import OutputVector |
| 16 | + |
| 17 | +from sextante.ftools import FToolsUtils as utils |
| 18 | + |
14 | 19 | class LinesIntersection(GeoAlgorithm):
|
15 | 20 |
|
16 |
| - INPUT1 = "INPUT1" |
17 |
| - INPUT2 = "INPUT2" |
| 21 | + INPUT_A = "INPUT_A" |
| 22 | + INPUT_B = "INPUT_B" |
| 23 | + FIELD_A = "FIELD_A" |
| 24 | + FIELD_B = "FIELD_B" |
| 25 | + |
18 | 26 | OUTPUT = "OUTPUT"
|
19 |
| - FIELD1 = "FIELD1" |
20 |
| - FIELD2 = "FIELD2" |
21 | 27 |
|
22 | 28 | def getIcon(self):
|
23 | 29 | return QtGui.QIcon(os.path.dirname(__file__) + "/icons/intersections.png")
|
24 | 30 |
|
| 31 | + def defineCharacteristics(self): |
| 32 | + self.name = "Line intersections" |
| 33 | + self.group = "Analysis tools" |
| 34 | + |
| 35 | + self.addParameter(ParameterVector(self.INPUT_A, "Input layer", ParameterVector.VECTOR_TYPE_LINE)) |
| 36 | + self.addParameter(ParameterVector(self.INPUT_B, "Intersect layer", ParameterVector.VECTOR_TYPE_LINE)) |
| 37 | + self.addParameter(ParameterTableField(self.FIELD_A, "Input unique ID field", self.INPUT_A)) |
| 38 | + self.addParameter(ParameterTableField(self.FIELD_B, "Intersect unique ID field", self.INPUT_B)) |
| 39 | + |
| 40 | + self.addOutput(OutputVector(self.OUTPUT, "Output layer")) |
| 41 | + |
25 | 42 | def processAlgorithm(self, progress):
|
26 |
| - layer1 = QGisLayers.getObjectFromUri(self.getParameterValue(LinesIntersection.INPUT1)) |
27 |
| - layer2 = QGisLayers.getObjectFromUri(self.getParameterValue(LinesIntersection.INPUT2)) |
28 |
| - field1 = self.getParameterValue(LinesIntersection.FIELD1) |
29 |
| - field2 = self.getParameterValue(LinesIntersection.FIELD2) |
30 |
| - provider1 = layer1.dataProvider() |
31 |
| - provider2 = layer2.dataProvider() |
32 |
| - allAttrs = provider1.attributeIndexes() |
33 |
| - provider1.select(allAttrs) |
34 |
| - allAttrs = provider2.attributeIndexes() |
35 |
| - provider2.select(allAttrs) |
36 |
| - fieldList = ftools_utils.getFieldList(layer1) |
37 |
| - index1 = provider1.fieldNameIndex(field1) |
38 |
| - field1 = fieldList[index1] |
39 |
| - field1.setName(unicode(field1.name()) + "_1") |
40 |
| - fieldList = ftools_utils.getFieldList(layer2) |
41 |
| - index2 = provider2.fieldNameIndex(field2) |
42 |
| - field2 = fieldList[index2] |
43 |
| - field2.setName(unicode(field2.name()) + "_2") |
44 |
| - fieldList = {0:field1, 1:field2} |
45 |
| - sRs = provider1.crs() |
46 |
| - writer = self.getOutputFromName(LinesIntersection.OUTPUT).getVectorWriter(fieldList, QGis.WKBPoint, sRs) |
47 |
| - #writer = QgsVectorFileWriter(outPath, "UTF-8", fieldList, QGis.WKBPoint, sRs) |
48 |
| - inFeat = QgsFeature() |
| 43 | + settings = QSettings() |
| 44 | + encoding = settings.value( "/UI/encoding", "System" ).toString() |
| 45 | + |
| 46 | + layerA = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_A)) |
| 47 | + layerB = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_B)) |
| 48 | + fieldA = self.getParameterValue(self.FIELD_A) |
| 49 | + fieldB = self.getParameterValue(self.FIELD_B) |
| 50 | + |
| 51 | + output = self.getOutputValue(self.OUTPUT) |
| 52 | + |
| 53 | + providerA = layerA.dataProvider() |
| 54 | + providerB = layerB.dataProvider() |
| 55 | + |
| 56 | + |
| 57 | + idxA = layerA.fieldNameIndex(fieldA) |
| 58 | + idxB = layerB.fieldNameIndex(fieldB) |
| 59 | + |
| 60 | + fieldList = {0 : layerA.pendingFields()[idxA], |
| 61 | + 1 : layerB.pendingFields()[idxB] |
| 62 | + } |
| 63 | + |
| 64 | + writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList, |
| 65 | + QGis.WKBPoint, providerA.crs()) |
| 66 | + |
| 67 | + spatialIndex = utils.createSpatialIndex(providerB) |
| 68 | + |
| 69 | + providerA.rewind() |
| 70 | + layerA.select([idxA]) |
| 71 | + providerB.rewind() |
| 72 | + layerB.select([idxB]) |
| 73 | + |
| 74 | + inFeatA = QgsFeature() |
49 | 75 | inFeatB = QgsFeature()
|
50 | 76 | outFeat = QgsFeature()
|
51 | 77 | inGeom = QgsGeometry()
|
52 |
| - tempGeom = QgsGeometry() |
53 |
| - start = 15.00 |
54 |
| - add = 85.00 / layer1.featureCount() |
55 |
| - index = ftools_utils.createIndex( provider2 ) |
56 |
| - while provider1.nextFeature(inFeat): |
57 |
| - inGeom = inFeat.geometry() |
58 |
| - lineList = [] |
59 |
| - #(check, lineList) = layer2.featuresInRectangle(inGeom.boundingBox(), True, True) |
60 |
| - # Below is a work-around for featuresInRectangle |
61 |
| - # Which appears to have been removed for QGIS version 1.0 |
62 |
| - #layer2.select(inGeom.boundingBox(), False) |
63 |
| - #lineList = layer2.selectedFeatures() |
64 |
| - lineList = index.intersects( inGeom.boundingBox() ) |
65 |
| - if len(lineList) > 0: check = 0 |
66 |
| - else: check = 1 |
67 |
| - if check == 0: |
68 |
| - for i in lineList: |
69 |
| - provider2.featureAtId( int( i ), inFeatB , True, allAttrs ) |
70 |
| - tmpGeom = QgsGeometry( inFeatB.geometry() ) |
71 |
| - #tempGeom = i.geometry() |
72 |
| - tempList = [] |
73 |
| - atMap1 = inFeat.attributeMap() |
74 |
| - atMap2 = inFeatB.attributeMap() |
| 78 | + tmpGeom = QgsGeometry() |
| 79 | + |
| 80 | + current = 0 |
| 81 | + total = 100.0 / float(providerA.featureCount()) |
| 82 | + hasIntersections = False |
| 83 | + |
| 84 | + while layerA.nextFeature(inFeatA): |
| 85 | + inGeom = inFeatA.geometry() |
| 86 | + hasIntersections = False |
| 87 | + lines = spatialIndex.intersects(inGeom.boundingBox()) |
| 88 | + |
| 89 | + if len(lines) > 0: |
| 90 | + hasIntersections = True |
| 91 | + |
| 92 | + if hasIntersections: |
| 93 | + layerB.select([idxB]) |
| 94 | + for i in lines: |
| 95 | + layerB.featureAtId(int(i), inFeatB) |
| 96 | + tmpGeom = QgsGeometry(inFeatB.geometry()) |
| 97 | + |
| 98 | + points = [] |
| 99 | + atMapA = inFeatA.attributeMap() |
| 100 | + atMapB = inFeatB.attributeMap() |
| 101 | + |
75 | 102 | if inGeom.intersects(tmpGeom):
|
76 | 103 | tempGeom = inGeom.intersection(tmpGeom)
|
77 | 104 | if tempGeom.type() == QGis.Point:
|
78 | 105 | if tempGeom.isMultipart():
|
79 |
| - tempList = tempGeom.asMultiPoint() |
| 106 | + points = tempGeom.asMultiPoint() |
80 | 107 | else:
|
81 |
| - tempList.append(tempGeom.asPoint()) |
82 |
| - for j in tempList: |
| 108 | + points.append(tempGeom.asPoint()) |
| 109 | + |
| 110 | + for j in points: |
83 | 111 | outFeat.setGeometry(tempGeom.fromPoint(j))
|
84 |
| - outFeat.addAttribute(0, atMap1[index1]) |
85 |
| - outFeat.addAttribute(1, atMap2[index2]) |
| 112 | + outFeat.addAttribute(0, atMapA[idxA]) |
| 113 | + outFeat.addAttribute(1, atMapB[idxB]) |
86 | 114 | writer.addFeature(outFeat)
|
87 |
| - start = start + add |
88 |
| - progress.setPercentage(start) |
89 |
| - del writer |
90 | 115 |
|
| 116 | + current += 1 |
| 117 | + progress.setPercentage(int(current * total)) |
91 | 118 |
|
92 |
| - def defineCharacteristics(self): |
93 |
| - self.name = "Line intersections" |
94 |
| - self.group = "Analysis tools" |
95 |
| - self.addParameter(ParameterVector(LinesIntersection.INPUT1, "Input layer", ParameterVector.VECTOR_TYPE_LINE)) |
96 |
| - self.addParameter(ParameterVector(LinesIntersection.INPUT2, "Intersect layer", ParameterVector.VECTOR_TYPE_LINE)) |
97 |
| - self.addParameter(ParameterTableField(LinesIntersection.FIELD1, "Input unique ID field", LinesIntersection.INPUT1)) |
98 |
| - self.addParameter(ParameterTableField(LinesIntersection.FIELD2, "Intersect unique ID field", LinesIntersection.INPUT2)) |
99 |
| - self.addOutput(OutputVector(LinesIntersection.OUTPUT, "Output layer")) |
100 |
| - #========================================================= |
| 119 | + del writer |
0 commit comments