@@ -251,6 +251,20 @@ bool QgsOgrFeatureIterator::fetchFeatureWithId( QgsFeatureId id, QgsFeature &fea
251251 return true ;
252252}
253253
254+ bool QgsOgrFeatureIterator::checkFeature ( gdal::ogr_feature_unique_ptr &fet, QgsFeature &feature )
255+ {
256+ if ( !readFeature ( std::move ( fet ), feature ) )
257+ return false ;
258+
259+ if ( !mFilterRect .isNull () && ( !feature.hasGeometry () || feature.geometry ().isEmpty () ) )
260+ return false ;
261+
262+ // we have a feature, end this cycle
263+ feature.setValid ( true );
264+ geometryToDestinationCrs ( feature, mTransform );
265+ return true ;
266+ }
267+
254268bool QgsOgrFeatureIterator::fetchFeature ( QgsFeature &feature )
255269{
256270 QMutexLocker locker ( mSharedDS ? &mSharedDS ->mutex () : nullptr );
@@ -282,20 +296,41 @@ bool QgsOgrFeatureIterator::fetchFeature( QgsFeature &feature )
282296
283297 gdal::ogr_feature_unique_ptr fet;
284298
285- while ( fet.reset ( OGR_L_GetNextFeature ( mOgrLayer ) ), fet )
286- {
287- if ( !readFeature ( std::move ( fet ), feature ) )
288- continue ;
289-
290- if ( !mFilterRect .isNull () && ( !feature.hasGeometry () || feature.geometry ().isEmpty () ) )
291- continue ;
299+ // OSM layers (especially large ones) need the GDALDataset::GetNextFeature() call rather than OGRLayer::GetNextFeature()
300+ // see more details here: https://trac.osgeo.org/gdal/wiki/rfc66_randomlayerreadwrite
292301
293- // we have a feature, end this cycle
294- feature.setValid ( true );
295- geometryToDestinationCrs ( feature, mTransform );
296- return true ;
302+ #if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,2,0)
303+ if ( mSource ->mDriverName == QLatin1String ( " OSM" ) )
304+ {
305+ OGRLayerH nextFeatureBelongingLayer;
306+ while ( fet.reset ( GDALDatasetGetNextFeature ( mConn ->ds , &nextFeatureBelongingLayer, nullptr , nullptr , nullptr ) ), fet )
307+ {
308+ if ( nextFeatureBelongingLayer == mOgrLayer && checkFeature ( fet, feature ) )
309+ {
310+ return true ;
311+ }
312+ }
313+ }
314+ else
315+ {
297316
298- } // while
317+ while ( fet.reset ( OGR_L_GetNextFeature ( mOgrLayer ) ), fet )
318+ {
319+ if ( checkFeature ( fet, feature ) )
320+ {
321+ return true ;
322+ }
323+ }
324+ }
325+ #else
326+ while ( fet.reset ( OGR_L_GetNextFeature ( mOgrLayer ) ), fet )
327+ {
328+ if ( checkFeature ( fet, feature ) )
329+ {
330+ return true ;
331+ }
332+ }
333+ #endif
299334
300335 close ();
301336 return false ;
0 commit comments