Skip to content
Permalink
Browse files

Use json objects for server getfeatureinfo

  • Loading branch information
elpaso committed May 2, 2019
1 parent bbd836f commit 290909f9bb4a8d3c3242f2f07a8dd78931fd58b2
@@ -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" ) );
@@ -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(
@@ -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',
@@ -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"
}
@@ -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"
}
@@ -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"
}
@@ -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"
}
@@ -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.
You can’t perform that action at this time.