@@ -46,10 +46,11 @@ bool QgsOverlayAnalyzer::intersection( QgsVectorLayer* layerA, QgsVectorLayer* l
46
46
47
47
QGis::WkbType outputType = dpA->geometryType ();
48
48
const QgsCoordinateReferenceSystem crs = layerA->srs ();
49
- QgsFieldMap fields;
50
- // fields = combineFieldLists( dpA->fields(), dpB->fields() );
49
+ QgsFieldMap fieldsA = dpA->fields ();
50
+ QgsFieldMap fieldsB = dpB->fields ();
51
+ combineFieldLists ( fieldsA, fieldsB );
51
52
52
- QgsVectorFileWriter vWriter ( shapefileName, dpA->encoding (), fields , outputType, &crs );
53
+ QgsVectorFileWriter vWriter ( shapefileName, dpA->encoding (), fieldsA , outputType, &crs );
53
54
QgsFeature currentFeature;
54
55
QgsGeometry* dissolveGeometry; // dissolve geometry (if dissolve enabled)
55
56
QgsSpatialIndex index;
@@ -73,7 +74,7 @@ bool QgsOverlayAnalyzer::intersection( QgsVectorLayer* layerA, QgsVectorLayer* l
73
74
{
74
75
p->setMaximum ( selectionA.size () );
75
76
}
76
-
77
+ QgsFeature currentFeature;
77
78
int processedFeatures = 0 ;
78
79
it = selectionA.constBegin ();
79
80
for ( ; it != selectionA.constEnd (); ++it )
@@ -104,11 +105,11 @@ bool QgsOverlayAnalyzer::intersection( QgsVectorLayer* layerA, QgsVectorLayer* l
104
105
else
105
106
{
106
107
layerB->select ( layerB->pendingAllAttributesList (), QgsRectangle (), true , false );
107
- while ( dpB ->nextFeature ( currentFeature ) )
108
+ while ( layerB ->nextFeature ( currentFeature ) )
108
109
{
109
110
index.insertFeature ( currentFeature );
110
111
}
111
-
112
+ QgsFeature currentFeature;
112
113
layerA->select ( layerA->pendingAllAttributesList (), QgsRectangle (), true , false );
113
114
114
115
int featureCount = layerA->featureCount ();
@@ -144,7 +145,7 @@ void QgsOverlayAnalyzer::intersectFeature( QgsFeature& f, QgsVectorFileWriter* v
144
145
{
145
146
QgsGeometry* featureGeometry = f.geometry ();
146
147
QgsGeometry* intersectGeometry = 0 ;
147
- QgsFeature currentFeature ;
148
+ QgsFeature overlayFeature ;
148
149
149
150
if ( !featureGeometry )
150
151
{
@@ -154,20 +155,23 @@ void QgsOverlayAnalyzer::intersectFeature( QgsFeature& f, QgsVectorFileWriter* v
154
155
QList<int > intersects;
155
156
intersects = index->intersects ( featureGeometry->boundingBox () );
156
157
QList<int >::const_iterator it = intersects.constBegin ();
158
+ QgsFeature outFeature;
157
159
for ( ; it != intersects.constEnd (); ++it )
158
160
{
159
- if ( !vl->featureAtId ( *it, currentFeature , true , true ) )
161
+ if ( !vl->featureAtId ( *it, overlayFeature , true , true ) )
160
162
{
161
163
continue ;
162
164
}
163
165
164
- if ( featureGeometry->intersects ( currentFeature .geometry () ) )
166
+ if ( featureGeometry->intersects ( overlayFeature .geometry () ) )
165
167
{
166
- intersectGeometry = featureGeometry->intersection ( currentFeature .geometry () );
168
+ intersectGeometry = featureGeometry->intersection ( overlayFeature .geometry () );
167
169
168
- QgsFeature outFeature;
169
170
outFeature.setGeometry ( intersectGeometry );
170
- outFeature.setAttributeMap ( f.attributeMap () );
171
+ QgsAttributeMap attributeMapA = f.attributeMap ();
172
+ QgsAttributeMap attributeMapB = overlayFeature.attributeMap ();
173
+ combineAttributeMaps ( attributeMapA, attributeMapB );
174
+ outFeature.setAttributeMap ( attributeMapA );
171
175
172
176
// add it to vector file writer
173
177
if ( vfw )
@@ -178,27 +182,47 @@ void QgsOverlayAnalyzer::intersectFeature( QgsFeature& f, QgsVectorFileWriter* v
178
182
}
179
183
}
180
184
181
- void QgsOverlayAnalyzer::combineFieldLists ( QgsFieldMap fieldListA, QgsFieldMap fieldListB )
185
+ void QgsOverlayAnalyzer::combineFieldLists ( QgsFieldMap& fieldListA, QgsFieldMap fieldListB )
182
186
{
187
+ QList<QString> names;
188
+ QMap<int , QgsField>::const_iterator j = fieldListA.constBegin ();
189
+ while ( j != fieldListA.constEnd () )
190
+ {
191
+ names.append ( j.value ().name () );
192
+ ++j;
193
+ }
183
194
QMap<int , QgsField>::const_iterator i = fieldListB.constBegin ();
184
195
int count = 0 ;
196
+ int fcount = fieldListA.size ();
197
+ QgsField field;
185
198
while ( i != fieldListB.constEnd () )
186
199
{
187
- if ( !fieldListA.contains ( i.key () ) )
188
- {
189
- fieldListA.insert ( fieldListA.size ()-1 , i.value () );
190
- count = 0 ;
191
- }
192
- else
200
+ field = i.value ();
201
+ while ( names.contains ( field.name () ) )
193
202
{
194
- QgsField field;
195
- field = i.value ();
196
203
QString name = field.name ();
197
204
name.append ( " _" ).append ( QString ( count ) );
198
- fieldListA. insert ( fieldListA. size ()- 1 , QgsField ( name, field.type () ) );
205
+ field = QgsField ( name, field.type () );
199
206
++count;
200
- continue ;
201
207
}
208
+ fieldListA.insert ( fcount, field );
209
+ count = 0 ;
210
+ ++fcount;
202
211
++i;
203
212
}
204
213
}
214
+
215
+ void QgsOverlayAnalyzer::combineAttributeMaps ( QgsAttributeMap& attributeMapA, QgsAttributeMap attributeMapB )
216
+ {
217
+ QMap<int , QVariant>::const_iterator i = attributeMapB.constBegin ();
218
+ QVariant attribute;
219
+ int fcount = attributeMapA.size ();
220
+ while ( i != attributeMapB.constEnd () )
221
+ {
222
+ attribute = i.value ();
223
+ attributeMapA.insert ( fcount, attribute );
224
+ ++i;
225
+ ++fcount;
226
+ }
227
+ }
228
+
0 commit comments