Skip to content

Commit 1355b39

Browse files
committedJul 1, 2021
Add QgsProviderUtils with a method for determining whether a list
of sublayer details is incomplete and requires a more in-depth scan
1 parent b742b8f commit 1355b39

File tree

7 files changed

+195
-0
lines changed

7 files changed

+195
-0
lines changed
 
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/providers/qgsproviderutils.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
class QgsProviderUtils
12+
{
13+
%Docstring(signature="appended")
14+
15+
Contains utility functions for working with data providers.
16+
17+
.. versionadded:: 3.22
18+
%End
19+
20+
%TypeHeaderCode
21+
#include "qgsproviderutils.h"
22+
%End
23+
public:
24+
25+
static bool sublayerDetailsAreIncomplete( const QList< QgsProviderSublayerDetails > &details );
26+
%Docstring
27+
Returns ``True`` if the sublayer ``details`` are incomplete, and require a more in-depth
28+
scan.
29+
30+
For instance, if the details contain any vector sublayers with unknown geometry types
31+
then a query with the Qgis.SublayerQueryFlag.ResolveGeometryType flag is required.
32+
%End
33+
34+
};
35+
36+
37+
38+
39+
/************************************************************************
40+
* This file has been generated automatically from *
41+
* *
42+
* src/core/providers/qgsproviderutils.h *
43+
* *
44+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
45+
************************************************************************/

‎python/core/core_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@
516516
%Include auto_generated/providers/qgsproviderregistry.sip
517517
%Include auto_generated/providers/qgsprovidersublayerdetails.sip
518518
%Include auto_generated/providers/qgsprovidersublayertask.sip
519+
%Include auto_generated/providers/qgsproviderutils.sip
519520
%Include auto_generated/providers/arcgis/qgsarcgisportalutils.sip
520521
%Include auto_generated/providers/arcgis/qgsarcgisrestutils.sip
521522
%Include auto_generated/providers/memory/qgsmemoryproviderutils.sip

‎src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ set(QGIS_CORE_SRCS
234234
providers/qgsproviderregistry.cpp
235235
providers/qgsprovidersublayerdetails.cpp
236236
providers/qgsprovidersublayertask.cpp
237+
providers/qgsproviderutils.cpp
237238

238239
providers/arcgis/qgsarcgisportalutils.cpp
239240
providers/arcgis/qgsarcgisrestquery.cpp
@@ -1524,6 +1525,7 @@ set(QGIS_CORE_HDRS
15241525
providers/qgsproviderregistry.h
15251526
providers/qgsprovidersublayerdetails.h
15261527
providers/qgsprovidersublayertask.h
1528+
providers/qgsproviderutils.h
15271529

15281530
providers/arcgis/qgsarcgisportalutils.h
15291531
providers/arcgis/qgsarcgisrestquery.h
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/***************************************************************************
2+
qgsproviderutils.cpp
3+
----------------------------
4+
begin : June 2021
5+
copyright : (C) 2021 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
/***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
17+
#include "qgsproviderutils.h"
18+
#include "qgsprovidersublayerdetails.h"
19+
20+
bool QgsProviderUtils::sublayerDetailsAreIncomplete( const QList<QgsProviderSublayerDetails> &details )
21+
{
22+
for ( const QgsProviderSublayerDetails &sublayer : details )
23+
{
24+
switch ( sublayer.type() )
25+
{
26+
case QgsMapLayerType::VectorLayer:
27+
if ( sublayer.wkbType() == QgsWkbTypes::Unknown )
28+
return true;
29+
break;
30+
31+
case QgsMapLayerType::RasterLayer:
32+
case QgsMapLayerType::PluginLayer:
33+
case QgsMapLayerType::MeshLayer:
34+
case QgsMapLayerType::VectorTileLayer:
35+
case QgsMapLayerType::AnnotationLayer:
36+
case QgsMapLayerType::PointCloudLayer:
37+
break;
38+
}
39+
}
40+
41+
return false;
42+
}

‎src/core/providers/qgsproviderutils.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/***************************************************************************
2+
qgsproviderutils.h
3+
----------------------------
4+
begin : June 2021
5+
copyright : (C) 2021 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
/***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#ifndef QGSPROVIDERUTILS_H
17+
#define QGSPROVIDERUTILS_H
18+
19+
#include "qgis_core.h"
20+
#include "qgis.h"
21+
#include <QList>
22+
23+
class QgsProviderSublayerDetails;
24+
25+
/**
26+
* \class QgsProviderUtils
27+
* \ingroup core
28+
*
29+
* \brief Contains utility functions for working with data providers.
30+
*
31+
* \since QGIS 3.22
32+
*/
33+
class CORE_EXPORT QgsProviderUtils
34+
{
35+
public:
36+
37+
/**
38+
* Returns TRUE if the sublayer \a details are incomplete, and require a more in-depth
39+
* scan.
40+
*
41+
* For instance, if the details contain any vector sublayers with unknown geometry types
42+
* then a query with the Qgis::SublayerQueryFlag::ResolveGeometryType flag is required.
43+
*/
44+
static bool sublayerDetailsAreIncomplete( const QList< QgsProviderSublayerDetails > &details );
45+
46+
};
47+
48+
#endif //QGSPROVIDERUTILS_H
49+
50+
51+

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ ADD_PYTHON_TEST(PyQgsPolymorphicRelation test_qgspolymorphicrelation.py)
269269
ADD_PYTHON_TEST(PyQgsRelationManager test_qgsrelationmanager.py)
270270
ADD_PYTHON_TEST(PyQgsProjectRelationManager test_qgsprojectrelationmanager.py)
271271
ADD_PYTHON_TEST(PyQgsProviderSublayerTask test_qgsprovidersublayertask.py)
272+
ADD_PYTHON_TEST(PyQgsProviderUtils test_qgsproviderutils.py)
272273
ADD_PYTHON_TEST(PyQgsRenderContext test_qgsrendercontext.py)
273274
ADD_PYTHON_TEST(PyQgsRenderer test_qgsrenderer.py)
274275
ADD_PYTHON_TEST(PyQgsReport test_qgsreport.py)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsProviderUtils.
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Nyall Dawson'
10+
__date__ = '30/06/2021'
11+
__copyright__ = 'Copyright 2021, The QGIS Project'
12+
13+
from qgis.core import (
14+
Qgis,
15+
QgsWkbTypes,
16+
QgsProviderRegistry,
17+
QgsProviderUtils
18+
)
19+
20+
from qgis.testing import (
21+
unittest,
22+
start_app
23+
)
24+
from utilities import unitTestDataPath
25+
26+
app = start_app()
27+
28+
29+
class TestQgsProviderUtils(unittest.TestCase):
30+
31+
def test_sublayerDetailsAreIncomplete(self):
32+
"""
33+
Test sublayerDetailsAreIncomplete
34+
"""
35+
uri = unitTestDataPath() + '/mixed_types.TAB'
36+
37+
# surface scan only
38+
sublayers = QgsProviderRegistry.instance().querySublayers(uri)
39+
self.assertEqual(len(sublayers), 1)
40+
self.assertEqual(sublayers[0].wkbType(), QgsWkbTypes.Unknown)
41+
42+
# need to resolve geometry types for complete details about this uri!
43+
self.assertTrue(QgsProviderUtils.sublayerDetailsAreIncomplete(sublayers))
44+
45+
# retry with retrieving geometry types
46+
sublayers = QgsProviderRegistry.instance().querySublayers(uri, Qgis.SublayerQueryFlag.ResolveGeometryType)
47+
# now we have all the details
48+
self.assertEqual(len(sublayers), 3)
49+
self.assertFalse(QgsProviderUtils.sublayerDetailsAreIncomplete(sublayers))
50+
51+
52+
if __name__ == '__main__':
53+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.