|
22 | 22 | #include "gdal.h"
|
23 | 23 | #include "gdalwarper.h"
|
24 | 24 | #include "cpl_string.h"
|
| 25 | +#include "qgsapplication.h" |
25 | 26 |
|
26 | 27 | #include <QNetworkProxy>
|
27 | 28 | #include <QString>
|
28 | 29 | #include <QImage>
|
29 | 30 | #include <QFileInfo>
|
30 |
| - |
31 |
| -// File extensions for formats supported by GDAL which may contain multiple layers |
32 |
| -// and should be treated as a potential layer container |
33 |
| -const QStringList QgsGdalUtils::SUPPORTED_DB_LAYERS_EXTENSIONS |
34 |
| -{ |
35 |
| - QStringLiteral( "gpkg" ), |
36 |
| - QStringLiteral( "sqlite" ), |
37 |
| - QStringLiteral( "db" ), |
38 |
| - QStringLiteral( "gdb" ), |
39 |
| - QStringLiteral( "kml" ), |
40 |
| - QStringLiteral( "kmz" ), |
41 |
| - QStringLiteral( "osm" ), |
42 |
| - QStringLiteral( "mdb" ), |
43 |
| - QStringLiteral( "accdb" ), |
44 |
| - QStringLiteral( "xls" ), |
45 |
| - QStringLiteral( "xlsx" ), |
46 |
| - QStringLiteral( "gpx" ), |
47 |
| - QStringLiteral( "pdf" ), |
48 |
| - QStringLiteral( "pbf" ), |
49 |
| - QStringLiteral( "nc" ), |
50 |
| - QStringLiteral( "shp.zip" ) }; |
| 31 | +#include <mutex> |
51 | 32 |
|
52 | 33 | bool QgsGdalUtils::supportsRasterCreate( GDALDriverH driver )
|
53 | 34 | {
|
@@ -564,6 +545,90 @@ bool QgsGdalUtils::pathIsCheapToOpen( const QString &path, int smallFileSizeLimi
|
564 | 545 | return false;
|
565 | 546 | }
|
566 | 547 |
|
| 548 | +QStringList QgsGdalUtils::multiLayerFileExtensions() |
| 549 | +{ |
| 550 | +#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,4,0) |
| 551 | + // get supported extensions |
| 552 | + static std::once_flag initialized; |
| 553 | + static QStringList SUPPORTED_DB_LAYERS_EXTENSIONS; |
| 554 | + std::call_once( initialized, [ = ] |
| 555 | + { |
| 556 | + // iterate through all of the supported drivers, adding the corresponding file extensions for |
| 557 | + // types which advertise multilayer support |
| 558 | + GDALDriverH driver = nullptr; |
| 559 | + |
| 560 | + QSet< QString > extensions; |
| 561 | + |
| 562 | + for ( int i = 0; i < GDALGetDriverCount(); ++i ) |
| 563 | + { |
| 564 | + driver = GDALGetDriver( i ); |
| 565 | + if ( !driver ) |
| 566 | + { |
| 567 | + QgsLogger::warning( "unable to get driver " + QString::number( i ) ); |
| 568 | + continue; |
| 569 | + } |
| 570 | + |
| 571 | + bool isMultiLayer = false; |
| 572 | + if ( QString( GDALGetMetadataItem( driver, GDAL_DCAP_RASTER, nullptr ) ) == QLatin1String( "YES" ) ) |
| 573 | + { |
| 574 | + if ( GDALGetMetadataItem( driver, GDAL_DMD_SUBDATASETS, nullptr ) != nullptr ) |
| 575 | + { |
| 576 | + isMultiLayer = true; |
| 577 | + } |
| 578 | + } |
| 579 | + if ( !isMultiLayer && QString( GDALGetMetadataItem( driver, GDAL_DCAP_VECTOR, nullptr ) ) == QLatin1String( "YES" ) ) |
| 580 | + { |
| 581 | + if ( GDALGetMetadataItem( driver, GDAL_DCAP_MULTIPLE_VECTOR_LAYERS, nullptr ) != nullptr ) |
| 582 | + { |
| 583 | + isMultiLayer = true; |
| 584 | + } |
| 585 | + } |
| 586 | + |
| 587 | + if ( !isMultiLayer ) |
| 588 | + continue; |
| 589 | + |
| 590 | + const QString driverExtensions = GDALGetMetadataItem( driver, GDAL_DMD_EXTENSIONS, "" ); |
| 591 | + if ( driverExtensions.isEmpty() ) |
| 592 | + continue; |
| 593 | + |
| 594 | +#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0) |
| 595 | + const QStringList splitExtensions = driverExtensions.split( ' ', QString::SkipEmptyParts ); |
| 596 | +#else |
| 597 | + const QStringList splitExtensions = driverExtensions.split( ' ', Qt::SkipEmptyParts ); |
| 598 | +#endif |
| 599 | + |
| 600 | + for ( const QString &ext : splitExtensions ) |
| 601 | + extensions.insert( ext ); |
| 602 | + } |
| 603 | + |
| 604 | + SUPPORTED_DB_LAYERS_EXTENSIONS = qgis::setToList( extensions ); |
| 605 | + } ); |
| 606 | + return SUPPORTED_DB_LAYERS_EXTENSIONS; |
| 607 | + |
| 608 | +#else |
| 609 | + static const QStringList SUPPORTED_DB_LAYERS_EXTENSIONS |
| 610 | + { |
| 611 | + QStringLiteral( "gpkg" ), |
| 612 | + QStringLiteral( "sqlite" ), |
| 613 | + QStringLiteral( "db" ), |
| 614 | + QStringLiteral( "gdb" ), |
| 615 | + QStringLiteral( "kml" ), |
| 616 | + QStringLiteral( "kmz" ), |
| 617 | + QStringLiteral( "osm" ), |
| 618 | + QStringLiteral( "mdb" ), |
| 619 | + QStringLiteral( "accdb" ), |
| 620 | + QStringLiteral( "xls" ), |
| 621 | + QStringLiteral( "xlsx" ), |
| 622 | + QStringLiteral( "ods" ), |
| 623 | + QStringLiteral( "gpx" ), |
| 624 | + QStringLiteral( "pdf" ), |
| 625 | + QStringLiteral( "pbf" ), |
| 626 | + QStringLiteral( "nc" ), |
| 627 | + QStringLiteral( "shp.zip" ) }; |
| 628 | + return SUPPORTED_DB_LAYERS_EXTENSIONS; |
| 629 | +#endif |
| 630 | +} |
| 631 | + |
567 | 632 | bool QgsGdalUtils::vrtMatchesLayerType( const QString &vrtPath, QgsMapLayerType type )
|
568 | 633 | {
|
569 | 634 | CPLPushErrorHandler( CPLQuietErrorHandler );
|
|
0 commit comments