@@ -132,13 +132,16 @@ QStringList QgsDelimitedTextProvider::splitLine( QString line )
132
132
}
133
133
134
134
QgsDelimitedTextProvider::QgsDelimitedTextProvider ( QString uri )
135
- : QgsVectorDataProvider( uri ),
136
- mHasWktField( false ), mFieldCount(0 ),
137
- mXFieldIndex( -1 ), mYFieldIndex( -1 ),
138
- mWktFieldIndex( -1 ), mWktHasZM( false ),
139
- mWktZMRegexp(" \\ s+(?:z|m|zm)(?=\\ s*\\ ()" ,Qt::CaseInsensitive),
140
- mWktCrdRegexp(" (\\ -?\\ d+(?:\\ .\\ d*)?\\ s+\\ -?\\ d+(?:\\ .\\ d*)?)\\ s[\\ s\\ d\\ .\\ -]+" ),
141
- mShowInvalidLines( true ), mWkbType( QGis::WKBNoGeometry )
135
+ : QgsVectorDataProvider( uri )
136
+ , mHasWktField( false )
137
+ , mFieldCount( 0 )
138
+ , mXFieldIndex( -1 ), mYFieldIndex( -1 )
139
+ , mWktFieldIndex( -1 )
140
+ , mWktHasZM( false )
141
+ , mWktZMRegexp( " \\ s+(?:z|m|zm)(?=\\ s*\\ ()" , Qt::CaseInsensitive )
142
+ , mWktCrdRegexp( " (\\ -?\\ d+(?:\\ .\\ d*)?\\ s+\\ -?\\ d+(?:\\ .\\ d*)?)\\ s[\\ s\\ d\\ .\\ -]+" )
143
+ , mShowInvalidLines( true )
144
+ , mWkbType( QGis::WKBNoGeometry )
142
145
{
143
146
// Get the file name and mDelimiter out of the uri
144
147
mFileName = uri.left ( uri.indexOf ( " ?" ) );
@@ -169,7 +172,7 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
169
172
yField = QUrl::fromPercentEncoding ( yField.toUtf8 () );
170
173
171
174
mHasWktField = wktField != " " ;
172
-
175
+
173
176
skipLines = QUrl::fromPercentEncoding ( skipLines.toUtf8 () );
174
177
175
178
mSkipLines = skipLines.toInt ();
@@ -247,13 +250,13 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
247
250
// fields vector
248
251
QStringList fieldList = splitLine ( line );
249
252
250
- mFieldCount = fieldList.count ();
253
+ mFieldCount = fieldList.count ();
251
254
252
255
// We don't know anything about a text based field other
253
256
// than its name. All fields are assumed to be text
254
257
int fieldPos = 0 ;
255
- for ( int column = 0 ; column < mFieldCount ; column++ )
256
- {
258
+ for ( int column = 0 ; column < mFieldCount ; column++ )
259
+ {
257
260
QString field = fieldList[column];
258
261
if ( field.length () > 0 )
259
262
{
@@ -275,20 +278,20 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
275
278
mYFieldIndex = column;
276
279
}
277
280
278
- // WKT geometry field won't be displayed in attribute tables
279
- if ( column == mWktFieldIndex ) continue ;
281
+ // WKT geometry field won't be displayed in attribute tables
282
+ if ( column == mWktFieldIndex ) continue ;
280
283
281
284
QgsDebugMsg ( " Adding field: " + ( field ) );
282
285
// assume that the field could be integer or double
283
286
// for now, let's set field type as text
284
- attributeColumns.append (column);
287
+ attributeColumns.append ( column );
285
288
attributeFields[fieldPos] = QgsField ( field, QVariant::String, " Text" );
286
289
couldBeInt.insert ( fieldPos, true );
287
290
couldBeDouble.insert ( fieldPos, true );
288
291
fieldPos++;
289
292
}
290
293
}
291
- if ( mWktFieldIndex >= 0 ) { mXFieldIndex = -1 ; mYFieldIndex = -1 ; }
294
+ if ( mWktFieldIndex >= 0 ) { mXFieldIndex = -1 ; mYFieldIndex = -1 ; }
292
295
QgsDebugMsg ( " Field count for the delimited text file is " + QString::number ( attributeFields.size () ) );
293
296
hasFields = true ;
294
297
}
@@ -304,83 +307,83 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
304
307
continue ;
305
308
}
306
309
307
- if ( mHasWktField && mWktFieldIndex >= 0 )
308
- {
309
- // Get the wkt - confirm it is valid, get the type, and
310
- // if compatible with the rest of file, add to the extents
311
-
312
- QString sWkt = parts[mWktFieldIndex ];
313
- QgsGeometry *geom = 0 ;
314
- try
315
- {
316
- if ( ! mWktHasZM && sWkt .indexOf (mWktZMRegexp ) >= 0 ) mWktHasZM = true ;
317
- if ( mWktHasZM )
318
- {
319
- sWkt .remove (mWktZMRegexp ).replace (mWktCrdRegexp ," \\ 1" );
320
- }
321
- geom = QgsGeometry::fromWkt (sWkt );
322
- }
323
- catch ( ...)
324
- {
325
- geom = 0 ;
326
- }
327
-
328
- if ( geom )
329
- {
330
- QGis::WkbType type = geom->wkbType ();
331
- if ( type != QGis::WKBNoGeometry )
332
- {
333
- if ( mNumberFeatures == 0 )
334
- {
335
- mNumberFeatures ++;
336
- mWkbType = type;
337
- mExtent = geom->boundingBox ();
338
- }
339
- else if ( type == mWkbType )
340
- {
341
- mNumberFeatures ++;
342
- QgsRectangle bbox ( geom->boundingBox ());
343
- mExtent .combineExtentWith ( &bbox );
344
- }
345
- }
346
- delete geom;
347
- }
348
- }
349
-
350
- else if ( ! mHasWktField && mXFieldIndex >= 0 && mYFieldIndex >= 0 )
351
- {
352
-
353
- // Get the x and y values, first checking to make sure they
354
- // aren't null.
355
-
356
- QString sX = parts[mXFieldIndex ];
357
- QString sY = parts[mYFieldIndex ];
358
-
359
- bool xOk = true ;
360
- bool yOk = true ;
361
- double x = sX .toDouble ( &xOk );
362
- double y = sY .toDouble ( &yOk );
363
-
364
- if ( xOk && yOk )
365
- {
366
- if ( mNumberFeatures > 0 )
367
- {
368
- mExtent .combineExtentWith ( x, y );
369
- }
370
- else
371
- {
372
- // Extent for the first point is just the first point
373
- mExtent .set ( x, y, x, y );
374
- mWkbType = QGis::WKBPoint;
375
- }
376
- mNumberFeatures ++;
377
- }
378
- }
379
-
380
- for ( int i = 0 ; i < attributeFields.size (); i++ )
381
- {
382
- QString &value = parts[attributeColumns[i]];
383
- if ( value.isEmpty ()) continue ;
310
+ if ( mHasWktField && mWktFieldIndex >= 0 )
311
+ {
312
+ // Get the wkt - confirm it is valid, get the type, and
313
+ // if compatible with the rest of file, add to the extents
314
+
315
+ QString sWkt = parts[mWktFieldIndex ];
316
+ QgsGeometry *geom = 0 ;
317
+ try
318
+ {
319
+ if ( ! mWktHasZM && sWkt .indexOf ( mWktZMRegexp ) >= 0 ) mWktHasZM = true ;
320
+ if ( mWktHasZM )
321
+ {
322
+ sWkt .remove ( mWktZMRegexp ).replace ( mWktCrdRegexp , " \\ 1" );
323
+ }
324
+ geom = QgsGeometry::fromWkt ( sWkt );
325
+ }
326
+ catch ( ... )
327
+ {
328
+ geom = 0 ;
329
+ }
330
+
331
+ if ( geom )
332
+ {
333
+ QGis::WkbType type = geom->wkbType ();
334
+ if ( type != QGis::WKBNoGeometry )
335
+ {
336
+ if ( mNumberFeatures == 0 )
337
+ {
338
+ mNumberFeatures ++;
339
+ mWkbType = type;
340
+ mExtent = geom->boundingBox ();
341
+ }
342
+ else if ( type == mWkbType )
343
+ {
344
+ mNumberFeatures ++;
345
+ QgsRectangle bbox ( geom->boundingBox () );
346
+ mExtent .combineExtentWith ( &bbox );
347
+ }
348
+ }
349
+ delete geom;
350
+ }
351
+ }
352
+
353
+ else if ( ! mHasWktField && mXFieldIndex >= 0 && mYFieldIndex >= 0 )
354
+ {
355
+
356
+ // Get the x and y values, first checking to make sure they
357
+ // aren't null.
358
+
359
+ QString sX = parts[mXFieldIndex ];
360
+ QString sY = parts[mYFieldIndex ];
361
+
362
+ bool xOk = true ;
363
+ bool yOk = true ;
364
+ double x = sX .toDouble ( &xOk );
365
+ double y = sY .toDouble ( &yOk );
366
+
367
+ if ( xOk && yOk )
368
+ {
369
+ if ( mNumberFeatures > 0 )
370
+ {
371
+ mExtent .combineExtentWith ( x, y );
372
+ }
373
+ else
374
+ {
375
+ // Extent for the first point is just the first point
376
+ mExtent .set ( x, y, x, y );
377
+ mWkbType = QGis::WKBPoint;
378
+ }
379
+ mNumberFeatures ++;
380
+ }
381
+ }
382
+
383
+ for ( int i = 0 ; i < attributeFields.size (); i++ )
384
+ {
385
+ QString &value = parts[attributeColumns[i]];
386
+ if ( value.isEmpty () ) continue ;
384
387
// try to convert attribute values to integer and double
385
388
if ( couldBeInt[i] )
386
389
{
@@ -433,78 +436,76 @@ bool QgsDelimitedTextProvider::nextFeature( QgsFeature& feature )
433
436
feature.setValid ( false );
434
437
while ( ! mStream ->atEnd () )
435
438
{
436
- double x = 0.0 ;
437
- double y = 0.0 ;
438
439
QString line = readLine ( mStream ); // Default local 8 bit encoding
439
440
if ( line.isEmpty () )
440
441
continue ;
441
442
442
443
// lex the tokens from the current data line
443
444
QStringList tokens = splitLine ( line );
444
445
445
- QgsGeometry *geom = 0 ;
446
-
447
- if ( mHasWktField && mWktFieldIndex >= 0 )
448
- {
449
- try
450
- {
451
- QString &sWkt = tokens[mWktFieldIndex ];
452
- if ( mWktHasZM )
453
- {
454
- sWkt .remove (mWktZMRegexp ).replace (mWktCrdRegexp ," \\ 1" );
455
- }
456
-
457
- geom = QgsGeometry::fromWkt (sWkt );
458
- }
459
- catch ( ...)
460
- {
461
- geom = 0 ;
462
- }
463
-
464
- if ( geom && geom->wkbType () != mWkbType )
465
- {
466
- delete geom;
467
- geom = 0 ;
468
- }
469
- mFid ++;
470
- if ( ! boundsCheck (geom) )
471
- {
472
- delete geom;
473
- geom = 0 ;
474
- }
475
- }
476
- else if ( ! mHasWktField && mXFieldIndex >= 0 && mYFieldIndex >= 0 )
477
- {
478
- bool xOk, yOk;
479
- double x = tokens[mXFieldIndex ].toDouble (&xOk);
480
- double y = tokens[mYFieldIndex ].toDouble (&yOk);
481
- if ( xOk && yOk )
482
- {
483
- mFid ++;
484
- if ( boundsCheck (x,y ) )
485
- {
486
- geom = QgsGeometry::fromPoint (QgsPoint (x,y) );
487
- }
488
- }
489
- }
490
-
491
- // If no valid geometry skip to the next line
492
-
493
- if ( ! geom ) continue ;
446
+ QgsGeometry *geom = 0 ;
447
+
448
+ if ( mHasWktField && mWktFieldIndex >= 0 )
449
+ {
450
+ try
451
+ {
452
+ QString &sWkt = tokens[mWktFieldIndex ];
453
+ if ( mWktHasZM )
454
+ {
455
+ sWkt .remove ( mWktZMRegexp ).replace ( mWktCrdRegexp , " \\ 1" );
456
+ }
457
+
458
+ geom = QgsGeometry::fromWkt ( sWkt );
459
+ }
460
+ catch ( ... )
461
+ {
462
+ geom = 0 ;
463
+ }
464
+
465
+ if ( geom && geom->wkbType () != mWkbType )
466
+ {
467
+ delete geom;
468
+ geom = 0 ;
469
+ }
470
+ mFid ++;
471
+ if ( ! boundsCheck ( geom ) )
472
+ {
473
+ delete geom;
474
+ geom = 0 ;
475
+ }
476
+ }
477
+ else if ( ! mHasWktField && mXFieldIndex >= 0 && mYFieldIndex >= 0 )
478
+ {
479
+ bool xOk, yOk;
480
+ double x = tokens[mXFieldIndex ].toDouble ( &xOk );
481
+ double y = tokens[mYFieldIndex ].toDouble ( &yOk );
482
+ if ( xOk && yOk )
483
+ {
484
+ mFid ++;
485
+ if ( boundsCheck ( x, y ) )
486
+ {
487
+ geom = QgsGeometry::fromPoint ( QgsPoint ( x, y ) );
488
+ }
489
+ }
490
+ }
491
+
492
+ // If no valid geometry skip to the next line
493
+
494
+ if ( ! geom ) continue ;
494
495
495
496
// At this point the current feature values are valid
496
497
497
498
feature.setValid ( true );
498
499
499
500
feature.setFeatureId ( mFid );
500
501
501
- feature.setGeometry ( geom );
502
+ feature.setGeometry ( geom );
502
503
503
504
for ( QgsAttributeList::const_iterator i = mAttributesToFetch .begin ();
504
505
i != mAttributesToFetch .end ();
505
506
++i )
506
507
{
507
- QString &value = tokens[attributeColumns[*i]];
508
+ QString &value = tokens[attributeColumns[*i]];
508
509
QVariant val;
509
510
switch ( attributeFields[*i].type () )
510
511
{
@@ -568,6 +569,7 @@ void QgsDelimitedTextProvider::select( QgsAttributeList fetchAttributes,
568
569
mSelectionRectangle = rect;
569
570
mAttributesToFetch = fetchAttributes;
570
571
mFetchGeom = fetchGeometry;
572
+ mUseIntersect = useIntersect;
571
573
if ( rect.isEmpty () )
572
574
{
573
575
mSelectionRectangle = mExtent ;
@@ -644,7 +646,7 @@ bool QgsDelimitedTextProvider::boundsCheck( double x, double y )
644
646
if ( mSelectionRectangle .isEmpty () || !mFetchGeom )
645
647
return true ;
646
648
647
- return mSelectionRectangle .contains ( QgsPoint (x,y ) );
649
+ return mSelectionRectangle .contains ( QgsPoint ( x, y ) );
648
650
}
649
651
/* *
650
652
* Check to see if the geometry is within the selection rectangle
@@ -655,8 +657,8 @@ bool QgsDelimitedTextProvider::boundsCheck( QgsGeometry *geom )
655
657
if ( mSelectionRectangle .isEmpty () || !mFetchGeom )
656
658
return true ;
657
659
658
- return geom->boundingBox ().intersects ( mSelectionRectangle )
659
- && geom->intersects (mSelectionRectangle ); ;
660
+ return geom->boundingBox ().intersects ( mSelectionRectangle ) &&
661
+ ( ! mUseIntersect || geom->intersects ( mSelectionRectangle ) ) ;
660
662
}
661
663
662
664
int QgsDelimitedTextProvider::capabilities () const
0 commit comments