1
+ # -*- coding: utf-8 -*-
2
+
3
+ """
4
+ ***************************************************************************
5
+ JoinAttributes.py
6
+ ---------------------
7
+ Date : August 2012
8
+ Copyright : (C) 2012 by Victor Olaya
9
+ Email : volayaf at gmail dot com
10
+ ***************************************************************************
11
+ * *
12
+ * This program is free software; you can redistribute it and/or modify *
13
+ * it under the terms of the GNU General Public License as published by *
14
+ * the Free Software Foundation; either version 2 of the License, or *
15
+ * (at your option) any later version. *
16
+ * *
17
+ ***************************************************************************
18
+ """
19
+ from sextante .core .SextanteVectorWriter import SextanteVectorWriter
20
+ from sextante .parameters .ParameterTableField import ParameterTableField
21
+
22
+ __author__ = 'Victor Olaya'
23
+ __date__ = 'August 2012'
24
+ __copyright__ = '(C) 2012, Victor Olaya'
25
+ # This will get replaced with a git SHA1 when you do a git archive
26
+ __revision__ = '$Format:%H$'
27
+
28
+ from sextante .core .GeoAlgorithm import GeoAlgorithm
29
+ from sextante .outputs .OutputVector import OutputVector
30
+ from sextante .parameters .ParameterVector import ParameterVector
31
+ from qgis .core import *
32
+ from PyQt4 .QtCore import *
33
+ from PyQt4 .QtGui import *
34
+ from sextante .core .QGisLayers import QGisLayers
35
+
36
+
37
+ class JoinAttributes (GeoAlgorithm ):
38
+
39
+ OUTPUT_LAYER = "OUTPUT_LAYER"
40
+ INPUT_LAYER = "INPUT_LAYER"
41
+ INPUT_LAYER_2 = "INPUT_LAYER_2"
42
+ TABLE_FIELD = "TABLE_FIELD"
43
+ TABLE_FIELD_2 = "TABLE_FIELD_2"
44
+
45
+ def defineCharacteristics (self ):
46
+ self .name = "Create new layer with selected features"
47
+ self .group = "Vector general tools"
48
+ self .addParameter (ParameterVector (self .INPUT_LAYER , "Input layer" , ParameterVector .VECTOR_TYPE_ANY , False ))
49
+ self .addParameter (ParameterVector (self .INPUT_LAYER_2 , "Input layer 2" , ParameterVector .VECTOR_TYPE_ANY , False ))
50
+ self .addParameter (ParameterTableField (self .TABLE_FIELD , "Table field" , self .INPUT_LAYER ))
51
+ self .addParameter (ParameterTableField (self .TABLE_FIELD_2 , "Table field 2" , self .INPUT_LAYER_2 ))
52
+ self .addOutput (OutputVector (self .OUTPUT_LAYER , "Output layer" ))
53
+
54
+ def processAlgorithm (self , progress ):
55
+ input = self .getParameterValue (self .INPUT_LAYER )
56
+ input2 = self .getParameterValue (self .INPUT_LAYER_2 )
57
+ output = self .getOutputFromName (self .OUTPUT_LAYER )
58
+ field = self .getParameterValue (self .TABLE_FIELD )
59
+ field2 = self .getParameterValue (self .TABLE_FIELD_2 )
60
+
61
+ # Layer 1
62
+ layer = QGisLayers .getObjectFromUri (input )
63
+ provider = layer .dataProvider ()
64
+ allAttrs = provider .attributeIndexes ()
65
+ provider .select ( allAttrs )
66
+ join_field1_index = provider .fieldNameIndex (field )
67
+ # Layer 2
68
+ layer2 = QGisLayers .getObjectFromUri (input2 )
69
+ provider2 = layer2 .dataProvider ()
70
+ allAttrs = provider2 .attributeIndexes ()
71
+ provider2 .select ( allAttrs )
72
+ fields2 = provider2 .fields ()
73
+ join_field2_index = provider2 .fieldNameIndex (field2 )
74
+
75
+ # Output
76
+ outFields = provider .fields ()
77
+ for (i ,f ) in fields2 .iteritems ():
78
+ f .setName ("x_" + f .name ())
79
+ outFields [len (outFields )] = f
80
+
81
+ writer = output .getVectorWriter (outFields , provider .geometryType (), provider .crs ())
82
+
83
+ inFeat = QgsFeature ()
84
+ inFeat2 = QgsFeature ()
85
+ outFeat = QgsFeature ()
86
+
87
+ # Create output vector layer with additional attribute
88
+ while provider .nextFeature (inFeat ):
89
+ inGeom = inFeat .geometry ()
90
+ atMap = inFeat .attributeMap ()
91
+ join_value1 = atMap [join_field1_index ].toString ()
92
+ while provider2 .nextFeature (inFeat2 ):
93
+ atMap2 = inFeat2 .attributeMap ()
94
+ join_value2 = atMap2 [join_field2_index ].toString ()
95
+ if join_value1 == join_value2 :
96
+ # create the new feature
97
+ outFeat .setGeometry ( inGeom )
98
+ outFeat .setAttributeMap ( atMap )
99
+ l = len (provider .fields ())
100
+ for (i ,a ) in atMap2 .iteritems ():
101
+ outFeat .addAttribute ( l + i , a )
102
+
103
+ writer .addFeature ( outFeat )
104
+
105
+ del writer
0 commit comments