Skip to content

Commit 1b56055

Browse files
committed
Non spatial layers do not have a CRS - API changes only, needs UI change
Refs #19689
1 parent c87b625 commit 1b56055

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

src/core/qgsmaplayer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ void QgsMapLayer::setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSign
738738
{
739739
mCRS = srs;
740740

741-
if ( !mCRS.isValid() )
741+
if ( isSpatial() && !mCRS.isValid() )
742742
{
743743
mCRS.setValidationHint( tr( "Specify CRS for layer %1" ).arg( name() ) );
744744
mCRS.validate();

src/core/qgsvectorlayer.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3010,20 +3010,10 @@ bool QgsVectorLayer::addFeatures( QgsFeatureList &features, Flags )
30103010

30113011
void QgsVectorLayer::setCoordinateSystem()
30123012
{
3013-
QgsDebugMsgLevel( QStringLiteral( "Computing Coordinate System" ), 4 );
3014-
3015-
if ( isSpatial() )
3016-
{
3017-
// get CRS directly from provider
3018-
setCrs( mDataProvider->crs() );
3019-
}
3020-
else
3021-
{
3022-
setCrs( QgsCoordinateReferenceSystem( GEO_EPSG_CRS_AUTHID ) );
3023-
}
3013+
// if layer is not spatial, it has not CRS!
3014+
setCrs( isSpatial() ? mDataProvider->crs() : QgsCoordinateReferenceSystem() );
30243015
}
30253016

3026-
30273017
QString QgsVectorLayer::displayField() const
30283018
{
30293019
QgsExpression exp( mDisplayExpression );

tests/src/python/test_qgsvectorlayer.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,24 @@ def testSetDataSource(self):
336336
self.assertNotEqual(layer.renderer(), r)
337337
self.assertEqual(layer.renderer().symbol().type(), QgsSymbol.Line)
338338

339+
def test_layer_crs(self):
340+
"""
341+
Test that spatial layers have CRS, and non-spatial don't
342+
"""
343+
vl = QgsVectorLayer('Point?crs=epsg:3111&field=pk:integer', 'test', 'memory')
344+
self.assertTrue(vl.isSpatial())
345+
self.assertTrue(vl.crs().isValid())
346+
self.assertEqual(vl.crs().authid(), 'EPSG:3111')
347+
348+
vl = QgsVectorLayer('None?field=pk:integer', 'test', 'memory')
349+
self.assertFalse(vl.isSpatial())
350+
self.assertFalse(vl.crs().isValid())
351+
352+
# even if provider has a crs - we don't respect it for non-spatial layers!
353+
vl = QgsVectorLayer('None?crs=epsg:3111field=pk:integer', 'test', 'memory')
354+
self.assertFalse(vl.isSpatial())
355+
self.assertFalse(vl.crs().isValid())
356+
339357
# ADD FEATURE
340358

341359
def test_AddFeature(self):

0 commit comments

Comments
 (0)