Skip to content

Commit

Permalink
[Server][WFS] Add CRS info to GeoJSON output
Browse files Browse the repository at this point in the history
when requested CRS is not WGS84
  • Loading branch information
troopa81 committed Mar 6, 2024
1 parent 533a717 commit f7d5feb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/server/services/wfs/qgswfsgetfeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}
Expand Down
4 changes: 4 additions & 0 deletions tests/src/python/test_qgsserver_wfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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"""
Expand Down

0 comments on commit f7d5feb

Please sign in to comment.