Skip to content

Commit

Permalink
Add QgsProviderUtils with a method for determining whether a list
Browse files Browse the repository at this point in the history
of sublayer details is incomplete and requires a more in-depth scan
  • Loading branch information
nyalldawson committed Jul 1, 2021
1 parent b742b8f commit 1355b39
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 0 deletions.
45 changes: 45 additions & 0 deletions python/core/auto_generated/providers/qgsproviderutils.sip.in
@@ -0,0 +1,45 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/providers/qgsproviderutils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/



class QgsProviderUtils
{
%Docstring(signature="appended")

Contains utility functions for working with data providers.

.. versionadded:: 3.22
%End

%TypeHeaderCode
#include "qgsproviderutils.h"
%End
public:

static bool sublayerDetailsAreIncomplete( const QList< QgsProviderSublayerDetails > &details );
%Docstring
Returns ``True`` if the sublayer ``details`` are incomplete, and require a more in-depth
scan.

For instance, if the details contain any vector sublayers with unknown geometry types
then a query with the Qgis.SublayerQueryFlag.ResolveGeometryType flag is required.
%End

};




/************************************************************************
* This file has been generated automatically from *
* *
* src/core/providers/qgsproviderutils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -516,6 +516,7 @@
%Include auto_generated/providers/qgsproviderregistry.sip
%Include auto_generated/providers/qgsprovidersublayerdetails.sip
%Include auto_generated/providers/qgsprovidersublayertask.sip
%Include auto_generated/providers/qgsproviderutils.sip
%Include auto_generated/providers/arcgis/qgsarcgisportalutils.sip
%Include auto_generated/providers/arcgis/qgsarcgisrestutils.sip
%Include auto_generated/providers/memory/qgsmemoryproviderutils.sip
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -234,6 +234,7 @@ set(QGIS_CORE_SRCS
providers/qgsproviderregistry.cpp
providers/qgsprovidersublayerdetails.cpp
providers/qgsprovidersublayertask.cpp
providers/qgsproviderutils.cpp

providers/arcgis/qgsarcgisportalutils.cpp
providers/arcgis/qgsarcgisrestquery.cpp
Expand Down Expand Up @@ -1524,6 +1525,7 @@ set(QGIS_CORE_HDRS
providers/qgsproviderregistry.h
providers/qgsprovidersublayerdetails.h
providers/qgsprovidersublayertask.h
providers/qgsproviderutils.h

providers/arcgis/qgsarcgisportalutils.h
providers/arcgis/qgsarcgisrestquery.h
Expand Down
42 changes: 42 additions & 0 deletions src/core/providers/qgsproviderutils.cpp
@@ -0,0 +1,42 @@
/***************************************************************************
qgsproviderutils.cpp
----------------------------
begin : June 2021
copyright : (C) 2021 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsproviderutils.h"
#include "qgsprovidersublayerdetails.h"

bool QgsProviderUtils::sublayerDetailsAreIncomplete( const QList<QgsProviderSublayerDetails> &details )
{
for ( const QgsProviderSublayerDetails &sublayer : details )
{
switch ( sublayer.type() )
{
case QgsMapLayerType::VectorLayer:
if ( sublayer.wkbType() == QgsWkbTypes::Unknown )
return true;
break;

case QgsMapLayerType::RasterLayer:
case QgsMapLayerType::PluginLayer:
case QgsMapLayerType::MeshLayer:
case QgsMapLayerType::VectorTileLayer:
case QgsMapLayerType::AnnotationLayer:
case QgsMapLayerType::PointCloudLayer:
break;
}
}

return false;
}
51 changes: 51 additions & 0 deletions src/core/providers/qgsproviderutils.h
@@ -0,0 +1,51 @@
/***************************************************************************
qgsproviderutils.h
----------------------------
begin : June 2021
copyright : (C) 2021 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSPROVIDERUTILS_H
#define QGSPROVIDERUTILS_H

#include "qgis_core.h"
#include "qgis.h"
#include <QList>

class QgsProviderSublayerDetails;

/**
* \class QgsProviderUtils
* \ingroup core
*
* \brief Contains utility functions for working with data providers.
*
* \since QGIS 3.22
*/
class CORE_EXPORT QgsProviderUtils
{
public:

/**
* Returns TRUE if the sublayer \a details are incomplete, and require a more in-depth
* scan.
*
* For instance, if the details contain any vector sublayers with unknown geometry types
* then a query with the Qgis::SublayerQueryFlag::ResolveGeometryType flag is required.
*/
static bool sublayerDetailsAreIncomplete( const QList< QgsProviderSublayerDetails > &details );

};

#endif //QGSPROVIDERUTILS_H



1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -269,6 +269,7 @@ ADD_PYTHON_TEST(PyQgsPolymorphicRelation test_qgspolymorphicrelation.py)
ADD_PYTHON_TEST(PyQgsRelationManager test_qgsrelationmanager.py)
ADD_PYTHON_TEST(PyQgsProjectRelationManager test_qgsprojectrelationmanager.py)
ADD_PYTHON_TEST(PyQgsProviderSublayerTask test_qgsprovidersublayertask.py)
ADD_PYTHON_TEST(PyQgsProviderUtils test_qgsproviderutils.py)
ADD_PYTHON_TEST(PyQgsRenderContext test_qgsrendercontext.py)
ADD_PYTHON_TEST(PyQgsRenderer test_qgsrenderer.py)
ADD_PYTHON_TEST(PyQgsReport test_qgsreport.py)
Expand Down
53 changes: 53 additions & 0 deletions tests/src/python/test_qgsproviderutils.py
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsProviderUtils.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Nyall Dawson'
__date__ = '30/06/2021'
__copyright__ = 'Copyright 2021, The QGIS Project'

from qgis.core import (
Qgis,
QgsWkbTypes,
QgsProviderRegistry,
QgsProviderUtils
)

from qgis.testing import (
unittest,
start_app
)
from utilities import unitTestDataPath

app = start_app()


class TestQgsProviderUtils(unittest.TestCase):

def test_sublayerDetailsAreIncomplete(self):
"""
Test sublayerDetailsAreIncomplete
"""
uri = unitTestDataPath() + '/mixed_types.TAB'

# surface scan only
sublayers = QgsProviderRegistry.instance().querySublayers(uri)
self.assertEqual(len(sublayers), 1)
self.assertEqual(sublayers[0].wkbType(), QgsWkbTypes.Unknown)

# need to resolve geometry types for complete details about this uri!
self.assertTrue(QgsProviderUtils.sublayerDetailsAreIncomplete(sublayers))

# retry with retrieving geometry types
sublayers = QgsProviderRegistry.instance().querySublayers(uri, Qgis.SublayerQueryFlag.ResolveGeometryType)
# now we have all the details
self.assertEqual(len(sublayers), 3)
self.assertFalse(QgsProviderUtils.sublayerDetailsAreIncomplete(sublayers))


if __name__ == '__main__':
unittest.main()

0 comments on commit 1355b39

Please sign in to comment.