/
qgsoverlayanalyzer.cpp
227 lines (206 loc) · 6.46 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/***************************************************************************
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. *
* *
***************************************************************************/
/* $Id: qgis.h 9774 2008-12-12 05:41:24Z timlinux $ */
#include "qgsoverlayanalyzer.h"
#include "qgsapplication.h"
#include "qgsfield.h"
#include "qgsfeature.h"
#include "qgslogger.h"
#include "qgscoordinatereferencesystem.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;
}
QGis::WkbType outputType = dpA->geometryType();
const QgsCoordinateReferenceSystem crs = layerA->srs();
QgsFieldMap fieldsA = dpA->fields();
QgsFieldMap fieldsB = dpB->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->featureAtId( *it, currentFeature, true, true ) )
{
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->featureAtId( *it, currentFeature, true, true ) )
{
continue;
}
intersectFeature( currentFeature, &vWriter, layerB, &index );
++processedFeatures;
}
if ( p )
{
p->setValue( selectionA.size() );
}
}
//take all features
else
{
layerB->select( layerB->pendingAllAttributesList(), QgsRectangle(), true, false );
while ( layerB->nextFeature( currentFeature ) )
{
index.insertFeature( currentFeature );
}
QgsFeature currentFeature;
layerA->select( layerA->pendingAllAttributesList(), QgsRectangle(), true, false );
int featureCount = layerA->featureCount();
if ( p )
{
p->setMaximum( featureCount );
}
int processedFeatures = 0;
while ( layerA->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 )
{
QgsGeometry* featureGeometry = f.geometry();
QgsGeometry* intersectGeometry = 0;
QgsFeature overlayFeature;
if ( !featureGeometry )
{
return;
}
QList<int> intersects;
intersects = index->intersects( featureGeometry->boundingBox() );
QList<int>::const_iterator it = intersects.constBegin();
QgsFeature outFeature;
for ( ; it != intersects.constEnd(); ++it )
{
if ( !vl->featureAtId( *it, overlayFeature, true, true ) )
{
continue;
}
if ( featureGeometry->intersects( overlayFeature.geometry() ) )
{
intersectGeometry = featureGeometry->intersection( overlayFeature.geometry() );
outFeature.setGeometry( intersectGeometry );
QgsAttributeMap attributeMapA = f.attributeMap();
QgsAttributeMap attributeMapB = overlayFeature.attributeMap();
combineAttributeMaps( attributeMapA, attributeMapB );
outFeature.setAttributeMap( attributeMapA );
//add it to vector file writer
if ( vfw )
{
vfw->addFeature( outFeature );
}
}
}
}
void QgsOverlayAnalyzer::combineFieldLists( QgsFieldMap& fieldListA, QgsFieldMap fieldListB )
{
QList<QString> names;
QMap<int, QgsField>::const_iterator j = fieldListA.constBegin();
while ( j != fieldListA.constEnd() )
{
names.append( j.value().name() );
++j;
}
QMap<int, QgsField>::const_iterator i = fieldListB.constBegin();
int count = 0;
int fcount = fieldListA.size();
QgsField field;
while ( i != fieldListB.constEnd() )
{
field = i.value();
while ( names.contains( field.name() ) )
{
QString name = field.name();
name.append( "_" ).append( QString( count ) );
field = QgsField( name, field.type() );
++count;
}
fieldListA.insert( fcount, field );
count = 0;
++fcount;
++i;
}
}
void QgsOverlayAnalyzer::combineAttributeMaps( QgsAttributeMap& attributeMapA, QgsAttributeMap attributeMapB )
{
QMap<int, QVariant>::const_iterator i = attributeMapB.constBegin();
QVariant attribute;
int fcount = attributeMapA.size();
while ( i != attributeMapB.constEnd() )
{
attribute = i.value();
attributeMapA.insert( fcount, attribute );
++i;
++fcount;
}
}