Skip to content

Commit f38ed07

Browse files
author
cfarmer
committed
Updates to QgsOverlayAnalyzer, fields and attributes are now properly
appended to the new layer git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12052 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 9c487fa commit f38ed07

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
lines changed

src/analysis/vector/qgsoverlayanalyzer.cpp

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ bool QgsOverlayAnalyzer::intersection( QgsVectorLayer* layerA, QgsVectorLayer* l
4646

4747
QGis::WkbType outputType = dpA->geometryType();
4848
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 );
5152

52-
QgsVectorFileWriter vWriter( shapefileName, dpA->encoding(), fields, outputType, &crs );
53+
QgsVectorFileWriter vWriter( shapefileName, dpA->encoding(), fieldsA, outputType, &crs );
5354
QgsFeature currentFeature;
5455
QgsGeometry* dissolveGeometry; //dissolve geometry (if dissolve enabled)
5556
QgsSpatialIndex index;
@@ -73,7 +74,7 @@ bool QgsOverlayAnalyzer::intersection( QgsVectorLayer* layerA, QgsVectorLayer* l
7374
{
7475
p->setMaximum( selectionA.size() );
7576
}
76-
77+
QgsFeature currentFeature;
7778
int processedFeatures = 0;
7879
it = selectionA.constBegin();
7980
for ( ; it != selectionA.constEnd(); ++it )
@@ -104,11 +105,11 @@ bool QgsOverlayAnalyzer::intersection( QgsVectorLayer* layerA, QgsVectorLayer* l
104105
else
105106
{
106107
layerB->select( layerB->pendingAllAttributesList(), QgsRectangle(), true, false );
107-
while ( dpB->nextFeature( currentFeature ) )
108+
while ( layerB->nextFeature( currentFeature ) )
108109
{
109110
index.insertFeature( currentFeature );
110111
}
111-
112+
QgsFeature currentFeature;
112113
layerA->select( layerA->pendingAllAttributesList(), QgsRectangle(), true, false );
113114

114115
int featureCount = layerA->featureCount();
@@ -144,7 +145,7 @@ void QgsOverlayAnalyzer::intersectFeature( QgsFeature& f, QgsVectorFileWriter* v
144145
{
145146
QgsGeometry* featureGeometry = f.geometry();
146147
QgsGeometry* intersectGeometry = 0;
147-
QgsFeature currentFeature;
148+
QgsFeature overlayFeature;
148149

149150
if ( !featureGeometry )
150151
{
@@ -154,20 +155,23 @@ void QgsOverlayAnalyzer::intersectFeature( QgsFeature& f, QgsVectorFileWriter* v
154155
QList<int> intersects;
155156
intersects = index->intersects( featureGeometry->boundingBox() );
156157
QList<int>::const_iterator it = intersects.constBegin();
158+
QgsFeature outFeature;
157159
for ( ; it != intersects.constEnd(); ++it )
158160
{
159-
if ( !vl->featureAtId( *it, currentFeature, true, true ) )
161+
if ( !vl->featureAtId( *it, overlayFeature, true, true ) )
160162
{
161163
continue;
162164
}
163165

164-
if ( featureGeometry->intersects( currentFeature.geometry() ) )
166+
if ( featureGeometry->intersects( overlayFeature.geometry() ) )
165167
{
166-
intersectGeometry = featureGeometry->intersection( currentFeature.geometry() );
168+
intersectGeometry = featureGeometry->intersection( overlayFeature.geometry() );
167169

168-
QgsFeature outFeature;
169170
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 );
171175

172176
//add it to vector file writer
173177
if ( vfw )
@@ -178,27 +182,47 @@ void QgsOverlayAnalyzer::intersectFeature( QgsFeature& f, QgsVectorFileWriter* v
178182
}
179183
}
180184

181-
void QgsOverlayAnalyzer::combineFieldLists( QgsFieldMap fieldListA, QgsFieldMap fieldListB )
185+
void QgsOverlayAnalyzer::combineFieldLists( QgsFieldMap& fieldListA, QgsFieldMap fieldListB )
182186
{
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+
}
183194
QMap<int, QgsField>::const_iterator i = fieldListB.constBegin();
184195
int count = 0;
196+
int fcount = fieldListA.size();
197+
QgsField field;
185198
while ( i != fieldListB.constEnd() )
186199
{
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() ) )
193202
{
194-
QgsField field;
195-
field = i.value();
196203
QString name = field.name();
197204
name.append( "_" ).append( QString( count ) );
198-
fieldListA.insert( fieldListA.size()-1 , QgsField( name, field.type() ) );
205+
field = QgsField( name, field.type() );
199206
++count;
200-
continue;
201207
}
208+
fieldListA.insert( fcount, field );
209+
count = 0;
210+
++fcount;
202211
++i;
203212
}
204213
}
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+

src/analysis/vector/qgsoverlayanalyzer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ class ANALYSIS_EXPORT QgsOverlayAnalyzer
9999

100100
private:
101101

102-
void combineFieldLists( QgsFieldMap fieldListA, QgsFieldMap fieldListB );
102+
void combineFieldLists( QgsFieldMap& fieldListA, QgsFieldMap fieldListB );
103103
void intersectFeature( QgsFeature& f, QgsVectorFileWriter* vfw, QgsVectorLayer* dp, QgsSpatialIndex* index );
104+
void combineAttributeMaps( QgsAttributeMap& attributeMapA, QgsAttributeMap attributeMapB );
104105
};
105106
#endif //QGSVECTORANALYZER

0 commit comments

Comments
 (0)