/
qgsrendererv2.cpp
299 lines (236 loc) · 7.54 KB
/
qgsrendererv2.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "qgsrendererv2.h"
#include "qgssymbolv2.h"
#include "qgssymbollayerv2utils.h"
#include "qgssinglesymbolrendererv2.h" // for default renderer
#include "qgsrendererv2registry.h"
#include "qgsrendercontext.h"
#include "qgsgeometry.h"
#include "qgsfeature.h"
#include "qgslogger.h"
#include <QDomElement>
#include <QDomDocument>
#include <QPolygonF>
static unsigned char* _getPoint(QPointF& pt, const QgsMapToPixel& mapToPixel, unsigned char* wkb)
{
wkb++; // jump over endian info
unsigned int wkbType = *(( int* ) wkb );
wkb += sizeof(unsigned int);
double x = *(( double * ) wkb ); wkb += sizeof(double);
double y = *(( double * ) wkb ); wkb += sizeof(double);
if ( wkbType == QGis::WKBPolygon25D )
wkb += sizeof(double);
mapToPixel.transformInPlace(x,y);
// TODO: coordinate transform
pt = QPointF(x,y);
return wkb;
}
static unsigned char* _getLineString(QPolygonF& pts, const QgsMapToPixel& mapToPixel, unsigned char* wkb)
{
wkb++; // jump over endian info
unsigned int wkbType = *(( int* ) wkb );
wkb += sizeof(unsigned int);
unsigned int nPoints = *(( int* ) wkb );
wkb += sizeof(unsigned int);
bool hasZValue = ( wkbType == QGis::WKBLineString25D );
double x,y;
pts.resize(nPoints);
for ( unsigned int i = 0; i < nPoints; ++i )
{
x = *(( double * ) wkb );
wkb += sizeof( double );
y = *(( double * ) wkb );
wkb += sizeof( double );
if ( hasZValue ) // ignore Z value
wkb += sizeof( double );
mapToPixel.transformInPlace(x,y);
pts[i] = QPointF(x,y);
// TODO: coordinate transform
}
return wkb;
}
static unsigned char* _getPolygon(QPolygonF& pts, QList<QPolygonF>& holes, const QgsMapToPixel& mapToPixel, unsigned char* wkb)
{
wkb++; // jump over endian info
unsigned int wkbType = *(( int* ) wkb );
wkb += sizeof(unsigned int); // jump over wkb type
unsigned int numRings = *(( int* ) wkb );
wkb += sizeof(unsigned int);
if ( numRings == 0 ) // sanity check for zero rings in polygon
return wkb;
bool hasZValue = ( wkbType == QGis::WKBPolygon25D );
double x,y;
holes.clear();
for ( unsigned int idx = 0; idx < numRings; idx++ )
{
unsigned int nPoints = *(( int* )wkb );
wkb += sizeof(unsigned int);
QPolygonF poly(nPoints);
// Extract the points from the WKB and store in a pair of vectors.
for ( unsigned int jdx = 0; jdx < nPoints; jdx++ )
{
x = *(( double * ) wkb ); wkb += sizeof( double );
y = *(( double * ) wkb ); wkb += sizeof( double );
mapToPixel.transformInPlace(x,y);
// TODO: coordinate transform
poly[jdx] = QPointF(x,y);
if ( hasZValue )
wkb += sizeof( double );
}
if ( nPoints < 1 )
continue;
if (idx == 0)
pts = poly;
else
holes.append(poly);
}
return wkb;
}
QgsFeatureRendererV2::QgsFeatureRendererV2(QString type)
: mType(type), mUsingSymbolLevels(false)
{
}
QgsFeatureRendererV2* QgsFeatureRendererV2::defaultRenderer(QGis::GeometryType geomType)
{
return new QgsSingleSymbolRendererV2( QgsSymbolV2::defaultSymbol(geomType) );
}
void QgsFeatureRendererV2::renderFeature(QgsFeature& feature, QgsRenderContext& context, int layer)
{
QgsSymbolV2* symbol = symbolForFeature(feature);
if (symbol == NULL)
return;
QgsSymbolV2::SymbolType symbolType = symbol->type();
QgsGeometry* geom = feature.geometry();
switch (geom->wkbType())
{
case QGis::WKBPoint:
{
if (symbolType != QgsSymbolV2::Marker)
{
QgsDebugMsg("point can be drawn only with marker symbol!");
break;
}
QPointF pt;
_getPoint(pt, context.mapToPixel(), geom->asWkb());
((QgsMarkerSymbolV2*)symbol)->renderPoint(pt, context, layer);
}
break;
case QGis::WKBLineString:
{
if (symbolType != QgsSymbolV2::Line)
{
QgsDebugMsg("linestring can be drawn only with line symbol!");
break;
}
QPolygonF pts;
_getLineString(pts, context.mapToPixel(), geom->asWkb());
((QgsLineSymbolV2*)symbol)->renderPolyline(pts, context, layer);
}
break;
case QGis::WKBPolygon:
{
if (symbolType != QgsSymbolV2::Fill)
{
QgsDebugMsg("polygon can be drawn only with fill symbol!");
break;
}
QPolygonF pts;
QList<QPolygonF> holes;
_getPolygon(pts, holes, context.mapToPixel(), geom->asWkb());
((QgsFillSymbolV2*)symbol)->renderPolygon(pts, (holes.count() ? &holes : NULL), context, layer);
}
break;
case QGis::WKBMultiPoint:
{
if (symbolType != QgsSymbolV2::Marker)
{
QgsDebugMsg("multi-point can be drawn only with marker symbol!");
break;
}
unsigned char* wkb = geom->asWkb();
unsigned int num = *(( int* )(wkb + 5) );
unsigned char* ptr = wkb + 9;
QPointF pt;
for (unsigned int i = 0; i < num; ++i)
{
ptr = _getPoint(pt, context.mapToPixel(), ptr);
((QgsMarkerSymbolV2*)symbol)->renderPoint(pt, context, layer);
}
}
break;
case QGis::WKBMultiLineString:
{
if (symbolType != QgsSymbolV2::Line)
{
QgsDebugMsg("multi-linestring can be drawn only with line symbol!");
break;
}
unsigned char* wkb = geom->asWkb();
unsigned int num = *(( int* )(wkb + 5) );
unsigned char* ptr = wkb + 9;
QPolygonF pts;
for (unsigned int i = 0; i < num; ++i)
{
ptr = _getLineString(pts, context.mapToPixel(), ptr);
((QgsLineSymbolV2*)symbol)->renderPolyline(pts, context, layer);
}
}
break;
case QGis::WKBMultiPolygon:
{
if (symbolType != QgsSymbolV2::Fill)
{
QgsDebugMsg("multi-polygon can be drawn only with fill symbol!");
break;
}
unsigned char* wkb = geom->asWkb();
unsigned int num = *(( int* )(wkb + 5) );
unsigned char* ptr = wkb + 9;
QPolygonF pts;
QList<QPolygonF> holes;
for (unsigned int i = 0; i < num; ++i)
{
ptr = _getPolygon(pts, holes, context.mapToPixel(), ptr);
((QgsFillSymbolV2*)symbol)->renderPolygon(pts, (holes.count() ? &holes : NULL), context, layer);
}
}
break;
default:
QgsDebugMsg("unsupported wkb type for rendering");
}
}
QString QgsFeatureRendererV2::dump()
{
return "UNKNOWN RENDERER\n";
}
QgsFeatureRendererV2* QgsFeatureRendererV2::load(QDomElement& element)
{
// <renderer-v2 type=""> ... </renderer-v2>
if (element.isNull())
return NULL;
// load renderer
QString rendererType = element.attribute("type");
QgsRendererV2CreateFunc pfCreate = QgsRendererV2Registry::instance()->rendererMetadata(rendererType).createFunction();
// unknown renderer type?
if (pfCreate == NULL)
return NULL;
return pfCreate(element);
}
QDomElement QgsFeatureRendererV2::save(QDomDocument& doc)
{
// create empty renderer element
return doc.createElement(RENDERER_TAG_NAME);
}
QgsLegendSymbologyList QgsFeatureRendererV2::legendSymbologyItems(QSize iconSize)
{
// empty list by default
return QgsLegendSymbologyList();
}
int QgsFeatureRendererV2::fieldNameIndex( const QgsFieldMap& fields, const QString& fieldName )
{
for ( QgsFieldMap::const_iterator it = fields.constBegin(); it != fields.constEnd(); ++it )
{
if ( it->name() == fieldName )
return it.key();
}
return -1;
}