@@ -43,6 +43,7 @@ QgsGml::QgsGml(
43
43
, mFinished( false )
44
44
, mFeatureCount( 0 )
45
45
, mCurrentWKBSize( 0 )
46
+ , mCurrentFeature( 0 )
46
47
{
47
48
mThematicAttributes .clear ();
48
49
for ( int i = 0 ; i < fields.size (); i++ )
@@ -140,7 +141,6 @@ int QgsGml::getFeatures( const QString& uri, QGis::WkbType* wkbType, QgsRectangl
140
141
141
142
int QgsGml::getFeatures ( const QByteArray &data, QGis::WkbType* wkbType, QgsRectangle* extent )
142
143
{
143
- QgsDebugMsg ( " Entered" );
144
144
mWkbType = wkbType;
145
145
mExtent .setMinimal ();
146
146
@@ -176,9 +176,11 @@ void QgsGml::handleProgressEvent( qint64 progress, qint64 totalSteps )
176
176
void QgsGml::startElement ( const XML_Char* el, const XML_Char** attr )
177
177
{
178
178
QString elementName ( el );
179
+ ParseMode theParseMode ( mParseModeStack .isEmpty () ? none : mParseModeStack .top () );
179
180
QStringList splitName = elementName.split ( NS_SEPARATOR );
180
181
QString localName = splitName.last ();
181
182
QString ns = splitName.size () > 1 ? splitName.first () : " " ;
183
+
182
184
if ( elementName == GML_NAMESPACE + NS_SEPARATOR + " coordinates" )
183
185
{
184
186
mParseModeStack .push ( QgsGml::coordinate );
@@ -203,16 +205,17 @@ void QgsGml::startElement( const XML_Char* el, const XML_Char** attr )
203
205
{
204
206
mParseModeStack .push ( QgsGml::boundingBox );
205
207
}
206
- else if ( localName == mTypeName )
208
+ else if ( theParseMode == none && localName == mTypeName )
207
209
{
210
+ Q_ASSERT ( !mCurrentFeature );
208
211
mCurrentFeature = new QgsFeature ( mFeatureCount );
209
212
QgsAttributes attributes ( mThematicAttributes .size () ); // add empty attributes
210
213
mCurrentFeature ->setAttributes ( attributes );
211
214
mParseModeStack .push ( QgsGml::feature );
212
215
mCurrentFeatureId = readAttribute ( " fid" , attr );
213
216
}
214
217
215
- else if ( elementName == GML_NAMESPACE + NS_SEPARATOR + " Box" && mParseModeStack . top () == QgsGml::boundingBox )
218
+ else if ( theParseMode == boundingBox && elementName == GML_NAMESPACE + NS_SEPARATOR + " Box" )
216
219
{
217
220
// read attribute srsName="EPSG:26910"
218
221
int epsgNr;
@@ -244,7 +247,7 @@ void QgsGml::startElement( const XML_Char* el, const XML_Char** attr )
244
247
{
245
248
mParseModeStack .push ( QgsGml::multiPolygon );
246
249
}
247
- else if (( mParseModeStack . size () > 0 ) && ( mParseModeStack . top () == QgsGml:: feature ) && ( mThematicAttributes .find ( localName ) != mThematicAttributes . end () ) )
250
+ else if ( theParseMode == feature && mThematicAttributes .contains ( localName ) )
248
251
{
249
252
mParseModeStack .push ( QgsGml::attribute );
250
253
mAttributeName = localName;
@@ -255,22 +258,18 @@ void QgsGml::startElement( const XML_Char* el, const XML_Char** attr )
255
258
void QgsGml::endElement ( const XML_Char* el )
256
259
{
257
260
QString elementName ( el );
261
+ ParseMode theParseMode ( mParseModeStack .isEmpty () ? none : mParseModeStack .top () );
258
262
QStringList splitName = elementName.split ( NS_SEPARATOR );
259
263
QString localName = splitName.last ();
260
264
QString ns = splitName.size () > 1 ? splitName.first () : " " ;
261
- if ( elementName == GML_NAMESPACE + NS_SEPARATOR + " coordinates" )
265
+
266
+ if ( theParseMode == coordinate && elementName == GML_NAMESPACE + NS_SEPARATOR + " coordinates" )
262
267
{
263
- if ( !mParseModeStack .empty () )
264
- {
265
- mParseModeStack .pop ();
266
- }
268
+ mParseModeStack .pop ();
267
269
}
268
- else if ( localName == mAttributeName ) // add a thematic attribute to the feature
270
+ else if ( theParseMode == attribute && localName == mAttributeName ) // add a thematic attribute to the feature
269
271
{
270
- if ( !mParseModeStack .empty () )
271
- {
272
- mParseModeStack .pop ();
273
- }
272
+ mParseModeStack .pop ();
274
273
275
274
// find index with attribute name
276
275
QMap<QString, QPair<int , QgsField> >::const_iterator att_it = mThematicAttributes .find ( mAttributeName );
@@ -292,31 +291,27 @@ void QgsGml::endElement( const XML_Char* el )
292
291
var = QVariant ( mStringCash );
293
292
break ;
294
293
}
294
+ Q_ASSERT ( mCurrentFeature );
295
295
mCurrentFeature ->setAttribute ( att_it.value ().first , QVariant ( mStringCash ) );
296
296
}
297
297
}
298
- else if ( localName == mGeometryAttribute )
298
+ else if ( theParseMode == geometry && localName == mGeometryAttribute )
299
299
{
300
- if ( !mParseModeStack .empty () )
301
- {
302
- mParseModeStack .pop ();
303
- }
300
+ mParseModeStack .pop ();
304
301
}
305
- else if ( ! mParseModeStack . empty () && mParseModeStack . top () == QgsGml:: boundingBox && elementName == GML_NAMESPACE + NS_SEPARATOR + " boundedBy" )
302
+ else if ( theParseMode == boundingBox && elementName == GML_NAMESPACE + NS_SEPARATOR + " boundedBy" )
306
303
{
307
304
// create bounding box from mStringCash
308
305
if ( createBBoxFromCoordinateString ( mCurrentExtent , mStringCash ) != 0 )
309
306
{
310
307
QgsDebugMsg ( " creation of bounding box failed" );
311
308
}
312
309
313
- if ( !mParseModeStack .empty () )
314
- {
315
- mParseModeStack .pop ();
316
- }
310
+ mParseModeStack .pop ();
317
311
}
318
- else if ( localName == mTypeName )
312
+ else if ( theParseMode == feature && localName == mTypeName )
319
313
{
314
+ Q_ASSERT ( mCurrentFeature );
320
315
if ( mCurrentWKBSize > 0 )
321
316
{
322
317
mCurrentFeature ->setGeometryAndOwnership ( mCurrentWKB , mCurrentWKBSize );
@@ -336,11 +331,9 @@ void QgsGml::endElement( const XML_Char* el )
336
331
{
337
332
mIdMap .insert ( mCurrentFeature ->id (), mCurrentFeatureId );
338
333
}
334
+ mCurrentFeature = 0 ;
339
335
++mFeatureCount ;
340
- if ( !mParseModeStack .empty () )
341
- {
342
- mParseModeStack .pop ();
343
- }
336
+ mParseModeStack .pop ();
344
337
}
345
338
else if ( elementName == GML_NAMESPACE + NS_SEPARATOR + " Point" )
346
339
{
@@ -350,7 +343,7 @@ void QgsGml::endElement( const XML_Char* el )
350
343
// error
351
344
}
352
345
353
- if ( mParseModeStack . top () != QgsGml::multiPoint )
346
+ if ( theParseMode == QgsGml::geometry )
354
347
{
355
348
// directly add WKB point to the feature
356
349
if ( getPointWKB ( &mCurrentWKB , &mCurrentWKBSize , *( pointList.begin () ) ) != 0 )
@@ -382,11 +375,6 @@ void QgsGml::endElement( const XML_Char* el )
382
375
{
383
376
QgsDebugMsg ( " No wkb fragments" );
384
377
}
385
-
386
- // wkbList.push_back(wkb);
387
- // wkbSizeList.push_back(wkbSize);
388
- // mCurrentWKBFragments.push_back(wkbList);
389
- // mCurrentWKBFragmentSizes.push_back(wkbSizeList);
390
378
}
391
379
}
392
380
else if ( elementName == GML_NAMESPACE + NS_SEPARATOR + " LineString" )
@@ -398,7 +386,7 @@ void QgsGml::endElement( const XML_Char* el )
398
386
{
399
387
// error
400
388
}
401
- if ( mParseModeStack . top () != QgsGml::multiLine )
389
+ if ( theParseMode == QgsGml::geometry )
402
390
{
403
391
if ( getLineWKB ( &mCurrentWKB , &mCurrentWKBSize , pointList ) != 0 )
404
392
{
@@ -429,13 +417,9 @@ void QgsGml::endElement( const XML_Char* el )
429
417
{
430
418
QgsDebugMsg ( " no wkb fragments" );
431
419
}
432
- // wkbList.push_back(wkb);
433
- // wkbSizeList.push_back(wkbSize);
434
- // mCurrentWKBFragments.push_back(wkbList);
435
- // mCurrentWKBFragmentSizes.push_back(wkbSizeList);
436
420
}
437
421
}
438
- else if ( elementName == GML_NAMESPACE + NS_SEPARATOR + " LinearRing" )
422
+ else if (( theParseMode == geometry || theParseMode == multiPolygon ) && elementName == GML_NAMESPACE + NS_SEPARATOR + " LinearRing" )
439
423
{
440
424
QList<QgsPoint> pointList;
441
425
if ( pointsFromCoordinateString ( pointList, mStringCash ) != 0 )
@@ -458,42 +442,34 @@ void QgsGml::endElement( const XML_Char* el )
458
442
QgsDebugMsg ( " no wkb fragments" );
459
443
}
460
444
}
461
- else if ( elementName == GML_NAMESPACE + NS_SEPARATOR + " Polygon" )
445
+ else if (( theParseMode == geometry || theParseMode == multiPolygon ) && elementName == GML_NAMESPACE + NS_SEPARATOR + " Polygon" )
462
446
{
463
447
if ( *mWkbType != QGis::WKBMultiPolygon )// keep multitype in case of geometry type mix
464
448
{
465
449
*mWkbType = QGis::WKBPolygon;
466
450
}
467
- if ( mParseModeStack .top () != QgsGml::multiPolygon )
451
+
452
+ if ( theParseMode == geometry )
468
453
{
469
454
createPolygonFromFragments ();
470
455
}
471
456
}
472
- else if ( elementName == GML_NAMESPACE + NS_SEPARATOR + " MultiPoint" )
457
+ else if ( theParseMode == multiPoint && elementName == GML_NAMESPACE + NS_SEPARATOR + " MultiPoint" )
473
458
{
474
459
*mWkbType = QGis::WKBMultiPoint;
475
- if ( !mParseModeStack .empty () )
476
- {
477
- mParseModeStack .pop ();
478
- }
460
+ mParseModeStack .pop ();
479
461
createMultiPointFromFragments ();
480
462
}
481
- else if ( elementName == GML_NAMESPACE + NS_SEPARATOR + " MultiLineString" )
463
+ else if ( theParseMode == multiLine && elementName == GML_NAMESPACE + NS_SEPARATOR + " MultiLineString" )
482
464
{
483
465
*mWkbType = QGis::WKBMultiLineString;
484
- if ( !mParseModeStack .empty () )
485
- {
486
- mParseModeStack .pop ();
487
- }
466
+ mParseModeStack .pop ();
488
467
createMultiLineFromFragments ();
489
468
}
490
- else if ( elementName == GML_NAMESPACE + NS_SEPARATOR + " MultiPolygon" )
469
+ else if ( theParseMode == multiPolygon && elementName == GML_NAMESPACE + NS_SEPARATOR + " MultiPolygon" )
491
470
{
492
471
*mWkbType = QGis::WKBMultiPolygon;
493
- if ( !mParseModeStack .empty () )
494
- {
495
- mParseModeStack .pop ();
496
- }
472
+ mParseModeStack .pop ();
497
473
createMultiPolygonFromFragments ();
498
474
}
499
475
}
0 commit comments