From f7d5febf33faa6e63fffcc985487152b99004cd2 Mon Sep 17 00:00:00 2001 From: Julien Cabieces Date: Wed, 6 Mar 2024 19:05:13 +0100 Subject: [PATCH] [Server][WFS] Add CRS info to GeoJSON output when requested CRS is not WGS84 --- src/server/services/wfs/qgswfsgetfeature.cpp | 15 +++++++++++++++ tests/src/python/test_qgsserver_wfs.py | 4 ++++ 2 files changed, 19 insertions(+) diff --git a/src/server/services/wfs/qgswfsgetfeature.cpp b/src/server/services/wfs/qgswfsgetfeature.cpp index a484dd1d3bab..7d7149779fa4 100644 --- a/src/server/services/wfs/qgswfsgetfeature.cpp +++ b/src/server/services/wfs/qgswfsgetfeature.cpp @@ -1174,6 +1174,21 @@ namespace QgsWfs fcString = QStringLiteral( "{\"type\": \"FeatureCollection\",\n" ); fcString += " \"bbox\": [ " + qgsDoubleToString( rect->xMinimum(), prec ) + ", " + qgsDoubleToString( rect->yMinimum(), prec ) + ", " + qgsDoubleToString( rect->xMaximum(), prec ) + ", " + qgsDoubleToString( rect->yMaximum(), prec ) + "],\n"; + + const QString srsName {request.serverParameters().value( QStringLiteral( "SRSNAME" ) )}; + const QgsCoordinateReferenceSystem destinationCrs { srsName.isEmpty( ) ? QStringLiteral( "EPSG:4326" ) : srsName }; + if ( ! destinationCrs.isValid() ) + { + throw QgsRequestNotWellFormedException( QStringLiteral( "srsName error: '%1' is not valid." ).arg( srsName ) ); + } + + json value; + QgsJsonUtils::addCrsInfo( value, destinationCrs ); + for ( auto it : value.items() ) + { + fcString += " \"" + QString::fromStdString( it.key() ) + "\": " + QString::fromStdString( it.value().dump() ) + ",\n"; + } + fcString += QLatin1String( " \"features\": [\n" ); response.write( fcString.toUtf8() ); } diff --git a/tests/src/python/test_qgsserver_wfs.py b/tests/src/python/test_qgsserver_wfs.py index f97be0916b7d..ec5ffcfcb948 100644 --- a/tests/src/python/test_qgsserver_wfs.py +++ b/tests/src/python/test_qgsserver_wfs.py @@ -787,6 +787,7 @@ def test_getFeatureFeatureJsonCrs(self): jdata['features'][0]['geometry'] jdata['features'][0]['geometry']['coordinates'] self.assertEqual(jdata['features'][0]['geometry']['coordinates'], [807305, 5592878]) + self.assertEqual(jdata['crs']['properties']['name'], "urn:ogc:def:crs:EPSG:0:3857") query_string = "?" + "&".join(["%s=%s" % i for i in list({ "SERVICE": "WFS", @@ -803,6 +804,7 @@ def test_getFeatureFeatureJsonCrs(self): jdata['features'][0]['geometry'] jdata['features'][0]['geometry']['coordinates'] self.assertEqual([int(i) for i in jdata['features'][0]['geometry']['coordinates']], [7, 44]) + self.assertFalse('crs' in jdata) query_string = "?" + "&".join(["%s=%s" % i for i in list({ "SERVICE": "WFS", @@ -834,6 +836,7 @@ def test_getFeatureFeatureJsonCrs(self): jdata['features'][0]['geometry'] jdata['features'][0]['geometry']['coordinates'] self.assertEqual([int(i) for i in jdata['features'][0]['geometry']['coordinates']], [361806, 4964192]) + self.assertEqual(jdata['crs']['properties']['name'], "urn:ogc:def:crs:EPSG:0:32632") query_string = "?" + "&".join(["%s=%s" % i for i in list({ "SERVICE": "WFS", @@ -851,6 +854,7 @@ def test_getFeatureFeatureJsonCrs(self): jdata['features'][0]['geometry'] jdata['features'][0]['geometry']['coordinates'] self.assertEqual([int(i) for i in jdata['features'][0]['geometry']['coordinates']], [812191, 5589555]) + self.assertEqual(jdata['crs']['properties']['name'], "urn:ogc:def:crs:EPSG:0:3857") def test_insert_srsName(self): """Test srsName is respected when insering"""