Skip to content

Commit 9da6a03

Browse files
committed
Add API for specifying fallback CRS and layer WKB type to use
for QgsVectorLayer, when constructing a layer with a URI which may be invalid These may be used for layers where the geometry type/CRS is known in advance, and where the layer path may not be initially resolvable. (E.g. layers with a URI pointing to a non-existant file). It is only ever used if the layer cannot be resolved, otherwise the actual layer geometry type will be detected and used for the layer.
1 parent bca78c0 commit 9da6a03

9 files changed

+62
-12
lines changed

python/core/auto_generated/qgsattributeeditorelement.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ class QgsAttributeEditorHtmlElement : QgsAttributeEditorElement
406406
%Docstring
407407
An attribute editor widget that will represent arbitrary HTML code.
408408

409-
.. versionadded:: 3.10
409+
.. versionadded:: 3.8
410410
%End
411411

412412
%TypeHeaderCode

python/core/auto_generated/qgsreadwritecontext.sip.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Returns data provider coordinate transform context
119119

120120
.. seealso:: :py:func:`setTransformContext`
121121

122-
.. versionadded:: 3.10
122+
.. versionadded:: 3.8
123123
%End
124124

125125
void setTransformContext( const QgsCoordinateTransformContext &transformContext );
@@ -128,7 +128,7 @@ Sets data coordinate transform context to ``transformContext``
128128

129129
.. seealso:: :py:func:`transformContext`
130130

131-
.. versionadded:: 3.10
131+
.. versionadded:: 3.8
132132
%End
133133

134134
public:

python/core/auto_generated/qgsvectorlayer.sip.in

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ Constructor for LayerOptions.
339339
%Docstring
340340
Constructor for LayerOptions.
341341

342-
.. versionadded:: 3.10
342+
.. versionadded:: 3.8
343343
%End
344344

345345
bool loadDefaultStyle;
@@ -348,8 +348,11 @@ Constructor for LayerOptions.
348348

349349
QgsCoordinateTransformContext transformContext;
350350

351-
};
351+
QgsWkbTypes::Type fallbackWkbType;
352352

353+
QgsCoordinateReferenceSystem fallbackCrs;
354+
355+
};
353356

354357
explicit QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(),
355358
const QString &providerLib = "ogr", const QgsVectorLayer::LayerOptions &options = QgsVectorLayer::LayerOptions() );

src/core/qgsattributeeditorelement.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ class CORE_EXPORT QgsAttributeEditorQmlElement : public QgsAttributeEditorElemen
474474
* \ingroup core
475475
* An attribute editor widget that will represent arbitrary HTML code.
476476
*
477-
* \since QGIS 3.10
477+
* \since QGIS 3.8
478478
*/
479479
class CORE_EXPORT QgsAttributeEditorHtmlElement : public QgsAttributeEditorElement
480480
{

src/core/qgsreadwritecontext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class CORE_EXPORT QgsReadWriteContext
120120
*
121121
* \see setTransformContext()
122122
*
123-
* \since QGIS 3.10
123+
* \since QGIS 3.8
124124
*/
125125
QgsCoordinateTransformContext transformContext() const;
126126

@@ -129,7 +129,7 @@ class CORE_EXPORT QgsReadWriteContext
129129
*
130130
* \see transformContext()
131131
*
132-
* \since QGIS 3.10
132+
* \since QGIS 3.8
133133
*/
134134
void setTransformContext( const QgsCoordinateTransformContext &transformContext );
135135

src/core/qgsvectorlayer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ QgsVectorLayer::QgsVectorLayer( const QString &vectorLayerPath,
148148
, mAuxiliaryLayerKey( QString() )
149149
, mReadExtentFromXml( options.readExtentFromXml )
150150
{
151+
if ( options.fallbackCrs.isValid() )
152+
setCrs( options.fallbackCrs, false );
153+
mWkbType = options.fallbackWkbType;
154+
151155
setProviderType( providerKey );
152156

153157
mGeometryOptions = qgis::make_unique<QgsGeometryOptions>();

src/core/qgsvectorlayer.h

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
409409

410410
/**
411411
* Constructor for LayerOptions.
412-
* \since QGIS 3.10
412+
* \since QGIS 3.8
413413
*/
414414
explicit LayerOptions( const QgsCoordinateTransformContext &transformContext,
415415
bool loadDefaultStyle = true,
@@ -431,12 +431,37 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
431431

432432
/**
433433
* Coordinate transform context
434-
* \since QGIS 3.10
434+
* \since QGIS 3.8
435435
*/
436436
QgsCoordinateTransformContext transformContext = QgsCoordinateTransformContext();
437437

438-
};
438+
/**
439+
* Fallback geometry type.
440+
*
441+
* This may be set for layers where the geometry type is known in advance, and where
442+
* the layer path may not be initially resolvable. (E.g. layers with a URI pointing
443+
* to a non-existent file). It is only ever used if the layer cannot be resolved,
444+
* otherwise the actual layer geometry type will be detected and used for the layer.
445+
*
446+
* \see fallbackCrs
447+
* \since QGIS 3.8
448+
*/
449+
QgsWkbTypes::Type fallbackWkbType = QgsWkbTypes::Unknown;
439450

451+
/**
452+
* Fallback layer coordinate reference system.
453+
*
454+
* This may be set for layers where the coordinate reference system is known in advance, and where
455+
* the layer path may not be initially resolvable. (E.g. layers with a URI pointing
456+
* to a non-existent file). It is only ever used if the layer cannot be resolved,
457+
* otherwise the actual layer CRS will be detected and used for the layer.
458+
*
459+
* \see fallbackWkbType
460+
* \since QGIS 3.8
461+
*/
462+
QgsCoordinateReferenceSystem fallbackCrs;
463+
464+
};
440465

441466
/**
442467
* Constructor - creates a vector layer

src/gui/editorwidgets/qgshtmlwidgetwrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class GUI_EXPORT QgsHtmlWidgetWrapper : public QgsWidgetWrapper
7373
/**
7474
* \ingroup gui
7575
* To pass the QgsExpression functionality and it's context to the context of the QWebView
76-
* \since QGIS 3.10
76+
* \since QGIS 3.8
7777
*/
7878
class HtmlExpression : public QObject
7979
{

tests/src/python/test_qgsvectorlayer.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,24 @@ def testStoreWkbTypeInvalidLayers(self):
434434

435435
shutil.rmtree(temp_path, True)
436436

437+
def testFallbackCrsWkbType(self):
438+
"""
439+
Test fallback CRS and WKB types are used when layer path is invalid
440+
"""
441+
vl = QgsVectorLayer('this is an outrage!!!')
442+
self.assertFalse(vl.isValid()) # i'd certainly hope so...
443+
self.assertEqual(vl.wkbType(), QgsWkbTypes.Unknown)
444+
self.assertFalse(vl.crs().isValid())
445+
446+
# with fallback
447+
options = QgsVectorLayer.LayerOptions()
448+
options.fallbackWkbType = QgsWkbTypes.CircularString
449+
options.fallbackCrs = QgsCoordinateReferenceSystem('EPSG:3111')
450+
vl = QgsVectorLayer("i'm the moon", options=options)
451+
self.assertFalse(vl.isValid())
452+
self.assertEqual(vl.wkbType(), QgsWkbTypes.CircularString)
453+
self.assertEqual(vl.crs().authid(), 'EPSG:3111')
454+
437455
def test_layer_crs(self):
438456
"""
439457
Test that spatial layers have CRS, and non-spatial don't

0 commit comments

Comments
 (0)