/
qgsoverlayanalyzer.cpp
212 lines (189 loc) · 6.04 KB
/
qgsoverlayanalyzer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/***************************************************************************
qgsoverlayanalyzer.cpp - QGIS Tools for vector geometry analysis
-------------------
begin : 8 Nov 2009
copyright : (C) Carson J. Q. Farmer
email : carson.farmer@gmail.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsoverlayanalyzer.h"
#include "qgsapplication.h"
#include "qgsfeatureiterator.h"
#include "qgsfield.h"
#include "qgsfeature.h"
#include "qgsgeometry.h"
#include "qgslogger.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsspatialindex.h"
#include "qgsvectorfilewriter.h"
#include "qgsvectordataprovider.h"
#include "qgsdistancearea.h"
#include <QProgressDialog>
bool QgsOverlayAnalyzer::intersection( QgsVectorLayer* layerA, QgsVectorLayer* layerB,
const QString& shapefileName, bool onlySelectedFeatures,
QProgressDialog* p )
{
if ( !layerA || !layerB )
{
return false;
}
QgsVectorDataProvider *dpA = layerA->dataProvider();
QgsVectorDataProvider *dpB = layerB->dataProvider();
if ( !dpA || !dpB )
{
return false;
}
QgsWkbTypes::Type outputType = dpA->wkbType();
QgsCoordinateReferenceSystem crs = layerA->crs();
QgsFields fieldsA = layerA->fields();
QgsFields fieldsB = layerB->fields();
combineFieldLists( fieldsA, fieldsB );
QgsVectorFileWriter vWriter( shapefileName, dpA->encoding(), fieldsA, outputType, crs );
QgsFeature currentFeature;
QgsSpatialIndex index;
//take only selection
if ( onlySelectedFeatures )
{
const QgsFeatureIds selectionB = layerB->selectedFeaturesIds();
QgsFeatureIds::const_iterator it = selectionB.constBegin();
for ( ; it != selectionB.constEnd(); ++it )
{
if ( !layerB->getFeatures( QgsFeatureRequest().setFilterFid( *it ) ).nextFeature( currentFeature ) )
{
continue;
}
index.insertFeature( currentFeature );
}
//use QgsVectorLayer::featureAtId
const QgsFeatureIds selectionA = layerA->selectedFeaturesIds();
if ( p )
{
p->setMaximum( selectionA.size() );
}
QgsFeature currentFeature;
int processedFeatures = 0;
it = selectionA.constBegin();
for ( ; it != selectionA.constEnd(); ++it )
{
if ( p )
{
p->setValue( processedFeatures );
}
if ( p && p->wasCanceled() )
{
break;
}
if ( !layerA->getFeatures( QgsFeatureRequest().setFilterFid( *it ) ).nextFeature( currentFeature ) )
{
continue;
}
intersectFeature( currentFeature, &vWriter, layerB, &index );
++processedFeatures;
}
if ( p )
{
p->setValue( selectionA.size() );
}
}
//take all features
else
{
QgsFeatureIterator fit = layerB->getFeatures();
while ( fit.nextFeature( currentFeature ) )
{
index.insertFeature( currentFeature );
}
int featureCount = layerA->featureCount();
if ( p )
{
p->setMaximum( featureCount );
}
int processedFeatures = 0;
fit = layerA->getFeatures();
QgsFeature currentFeature;
while ( fit.nextFeature( currentFeature ) )
{
if ( p )
{
p->setValue( processedFeatures );
}
if ( p && p->wasCanceled() )
{
break;
}
intersectFeature( currentFeature, &vWriter, layerB, &index );
++processedFeatures;
}
if ( p )
{
p->setValue( featureCount );
}
}
return true;
}
void QgsOverlayAnalyzer::intersectFeature( QgsFeature& f, QgsVectorFileWriter* vfw,
QgsVectorLayer* vl, QgsSpatialIndex* index )
{
if ( !f.hasGeometry() )
{
return;
}
QgsGeometry featureGeometry = f.geometry();
QgsGeometry intersectGeometry;
QgsFeature overlayFeature;
QList<QgsFeatureId> intersects;
intersects = index->intersects( featureGeometry.boundingBox() );
QList<QgsFeatureId>::const_iterator it = intersects.constBegin();
QgsFeature outFeature;
for ( ; it != intersects.constEnd(); ++it )
{
if ( !vl->getFeatures( QgsFeatureRequest().setFilterFid( *it ) ).nextFeature( overlayFeature ) )
{
continue;
}
if ( featureGeometry.intersects( overlayFeature.geometry() ) )
{
intersectGeometry = featureGeometry.intersection( overlayFeature.geometry() );
outFeature.setGeometry( intersectGeometry );
QgsAttributes attributesA = f.attributes();
QgsAttributes attributesB = overlayFeature.attributes();
combineAttributeMaps( attributesA, attributesB );
outFeature.setAttributes( attributesA );
//add it to vector file writer
if ( vfw )
{
vfw->addFeature( outFeature );
}
}
}
}
void QgsOverlayAnalyzer::combineFieldLists( QgsFields& fieldListA, const QgsFields& fieldListB )
{
QList<QString> names;
Q_FOREACH ( const QgsField& field, fieldListA )
names.append( field.name() );
for ( int idx = 0; idx < fieldListB.count(); ++idx )
{
QgsField field = fieldListB.at( idx );
int count = 0;
while ( names.contains( field.name() ) )
{
QString name = QString( "%1_%2" ).arg( field.name() ).arg( count );
field = QgsField( name, field.type() );
++count;
}
fieldListA.append( field );
names.append( field.name() );
}
}
void QgsOverlayAnalyzer::combineAttributeMaps( QgsAttributes& attributesA, const QgsAttributes& attributesB )
{
attributesA += attributesB;
}