Skip to content

Commit

Permalink
Use json objects for server getfeatureinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed May 2, 2019
1 parent bbd836f commit 290909f
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 185 deletions.
48 changes: 19 additions & 29 deletions src/server/services/wms/qgswmsrenderer.cpp
Expand Up @@ -2194,10 +2194,11 @@ namespace QgsWms

QByteArray QgsRenderer::convertFeatureInfoToJson( const QList<QgsMapLayer *> &layers, const QDomDocument &doc ) const
{
QString json;
json.append( QStringLiteral( "{\"type\": \"FeatureCollection\",\n" ) );
json.append( QStringLiteral( " \"features\":[\n" ) );

json json
{
{ "type", "FeatureCollection" },
{ "features", json::array() },
};
const bool withGeometry = ( QgsServerProjectUtils::wmsFeatureInfoAddWktGeometry( *mProject ) && mWmsParameters.withGeometry() );

const QDomNodeList layerList = doc.elementsByTagName( QStringLiteral( "Layer" ) );
Expand Down Expand Up @@ -2279,46 +2280,35 @@ namespace QgsWms

for ( const auto &feature : qgis::as_const( features ) )
{
if ( json.right( 1 ).compare( QStringLiteral( "}" ) ) == 0 )
{
json.append( QStringLiteral( "," ) );
}

const QString id = QStringLiteral( "%1.%2" ).arg( layer->name(), QgsJsonUtils::encodeValue( feature.id() ) );
json.append( exporter.exportFeature( feature, QVariantMap(), id ) );
json["features"].push_back( exporter.exportFeatureToJsonObject( feature, QVariantMap(), id ) );
}
}
else // raster layer
{
if ( json.right( 1 ).compare( QStringLiteral( "}" ) ) == 0 )
{
json.append( QStringLiteral( "," ) );
}
json.append( QStringLiteral( "{" ) );
json.append( QStringLiteral( "\"type\":\"Feature\",\n" ) );
json.append( QStringLiteral( "\"id\":\"%1\",\n" ).arg( layer->name() ) );
json.append( QStringLiteral( "\"properties\":{\n" ) );

auto properties = json::object();
const QDomNodeList attributesNode = layerElem.elementsByTagName( QStringLiteral( "Attribute" ) );
for ( int j = 0; j < attributesNode.size(); ++j )
{
const QDomElement attrElmt = attributesNode.at( j ).toElement();
const QString name = attrElmt.attribute( QStringLiteral( "name" ) );
const QString value = attrElmt.attribute( QStringLiteral( "value" ) );

if ( j > 0 )
json.append( QStringLiteral( ",\n" ) );

json.append( QStringLiteral( " \"%1\": \"%2\"" ).arg( name, value ) );
properties[name.toStdString()] = value.toStdString();
}

json.append( QStringLiteral( "\n}\n}" ) );
json["features"].push_back(
{
{"type", "Feature" },
{"id", layer->name().toStdString() },
{"properties", properties }
} );
}
}

json.append( QStringLiteral( "]}" ) );

return json.toUtf8();
#ifdef QGISDEBUG
return QByteArray::fromStdString( json.dump( 2 ) );
#else
return QByteArray::fromStdString( json.dump() );
#endif
}

QDomElement QgsRenderer::createFeatureGML(
Expand Down
3 changes: 2 additions & 1 deletion tests/src/python/test_qgsserver_wms_getfeatureinfo.py
Expand Up @@ -40,9 +40,10 @@


class TestQgsServerWMSGetFeatureInfo(TestQgsServerWMSTestBase):

"""QGIS Server WMS Tests for GetFeatureInfo request"""

#regenerate_reference = True

def testGetFeatureInfo(self):
# Test getfeatureinfo response xml
self.wms_request_compare('GetFeatureInfo',
Expand Down
25 changes: 14 additions & 11 deletions tests/testdata/qgis_server/wms_getfeatureinfo_alias_json.txt
@@ -1,15 +1,18 @@
*****
Content-Type: application/json; charset=utf-8

{"type": "FeatureCollection",
"features":[
{
"type":"Feature",
"id":"fields_alias.2",
"geometry":null,
"properties":{
"alias_id":3,
"alias_name":"three",
"utf8nameè":"three èé↓"
}
}]}
"features": [
{
"geometry": null,
"id": "fields_alias.2",
"properties": {
"alias_id": 3,
"alias_name": "three",
"utf8nameè": "three èé↓"
},
"type": "Feature"
}
],
"type": "FeatureCollection"
}
@@ -1,14 +1,17 @@
*****
Content-Type: application/json; charset=utf-8

{"type": "FeatureCollection",
"features":[
{
"type":"Feature",
"id":"exclude_attribute.2",
"geometry":null,
"properties":{
"id":3,
"utf8nameè":"three èé↓"
}
}]}
"features": [
{
"geometry": null,
"id": "exclude_attribute.2",
"properties": {
"id": 3,
"utf8nameè": "three èé↓"
},
"type": "Feature"
}
],
"type": "FeatureCollection"
}
25 changes: 14 additions & 11 deletions tests/testdata/qgis_server/wms_getfeatureinfo_geojson.txt
@@ -1,15 +1,18 @@
*****
Content-Type: application/geo+json; charset=utf-8

{"type": "FeatureCollection",
"features":[
{
"type":"Feature",
"id":"testlayer èé.2",
"geometry":null,
"properties":{
"id":3,
"name":"three",
"utf8nameè":"three èé↓"
}
}]}
"features": [
{
"geometry": null,
"id": "testlayer èé.2",
"properties": {
"id": 3,
"name": "three",
"utf8nameè": "three èé↓"
},
"type": "Feature"
}
],
"type": "FeatureCollection"
}
32 changes: 20 additions & 12 deletions tests/testdata/qgis_server/wms_getfeatureinfo_geometry_json.txt
@@ -1,16 +1,24 @@
*****
Content-Type: application/json; charset=utf-8

{"type": "FeatureCollection",
"features":[
{
"type":"Feature",
"id":"testlayer èé.2",
"geometry":
{"type": "Point", "coordinates": [913204.9128, 5606011.4565]},
"properties":{
"id":3,
"name":"three",
"utf8nameè":"three èé↓"
}
}]}
"features": [
{
"geometry": {
"coordinates": [
913204.9128,
5606011.4565
],
"type": "Point"
},
"id": "testlayer èé.2",
"properties": {
"id": 3,
"name": "three",
"utf8nameè": "three èé↓"
},
"type": "Feature"
}
],
"type": "FeatureCollection"
}
65 changes: 34 additions & 31 deletions tests/testdata/qgis_server/wms_getfeatureinfo_group_name_areas.txt
@@ -1,34 +1,37 @@
Content-Length: 753
*****
Content-Type: application/json; charset=utf-8

{"type": "FeatureCollection",
"features":[
{
"type":"Feature",
"id":"as_areas.18",
"geometry":null,
"properties":{
"fid":18,
"gid":34,
"datum":"2013-06-11",
"bearbeiter":"scholle-b",
"veranstaltung":"",
"beschriftung":"",
"name":"",
"flaechentyp":"Schraffur",
"farbe":"255 0 0",
"schraff_width":"2",
"schraff_width_prt":"6.00",
"schraff_size":"10",
"schraff_size_prt":"30.00",
"schraff_winkel":"45",
"umrissfarbe":"0 0 0",
"umrisstyp":"durchgezogen",
"umrissstaerke":"1",
"umrissstaerke_prt":"3.00",
"umfang":758.4703,
"flaeche":12879,
"bemerkung":"",
"last_change":"2013-06-11 10:03:45.52301+02"
}
}]}
"features": [
{
"geometry": null,
"id": "as_areas.18",
"properties": {
"bearbeiter": "scholle-b",
"bemerkung": "",
"beschriftung": "",
"datum": "2013-06-11",
"farbe": "255 0 0",
"fid": 18,
"flaeche": 12879.0,
"flaechentyp": "Schraffur",
"gid": 34,
"last_change": "2013-06-11 10:03:45.52301+02",
"name": "",
"schraff_size": "10",
"schraff_size_prt": "30.00",
"schraff_width": "2",
"schraff_width_prt": "6.00",
"schraff_winkel": "45",
"umfang": 758.4703,
"umrissfarbe": "0 0 0",
"umrissstaerke": "1",
"umrissstaerke_prt": "3.00",
"umrisstyp": "durchgezogen",
"veranstaltung": ""
},
"type": "Feature"
}
],
"type": "FeatureCollection"
}
25 changes: 14 additions & 11 deletions tests/testdata/qgis_server/wms_getfeatureinfo_json.txt
@@ -1,15 +1,18 @@
*****
Content-Type: application/json; charset=utf-8

{"type": "FeatureCollection",
"features":[
{
"type":"Feature",
"id":"testlayer èé.2",
"geometry":null,
"properties":{
"id":3,
"name":"three",
"utf8nameè":"three èé↓"
}
}]}
"features": [
{
"geometry": null,
"id": "testlayer èé.2",
"properties": {
"id": 3,
"name": "three",
"utf8nameè": "three èé↓"
},
"type": "Feature"
}
],
"type": "FeatureCollection"
}

0 comments on commit 290909f

Please sign in to comment.