Skip to content

Commit

Permalink
Merge pull request #41787 from elpaso/bugfix-server-fids
Browse files Browse the repository at this point in the history
Fix #41786 wrong server feature ids when composite PKs
  • Loading branch information
elpaso authored and github-actions[bot] committed Feb 24, 2021
1 parent b514863 commit 450f5c9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/server/qgsserverfeatureid.cpp
Expand Up @@ -29,10 +29,9 @@ QString QgsServerFeatureId::getServerFid( const QgsFeature &feature, const QgsAt
}

QStringList pkValues;
QgsAttributeList::const_iterator it = pkAttributes.constBegin();
if ( it != pkAttributes.constEnd() )
for ( const auto &attrIdx : qgis::as_const( pkAttributes ) )
{
pkValues.append( feature.attribute( *it ).toString() );
pkValues.append( feature.attribute( attrIdx ).toString() );
}
return pkValues.join( pkSeparator() );
}
Expand Down
42 changes: 42 additions & 0 deletions tests/src/python/test_qgsserver_wms_getfeatureinfo_postgres.py
Expand Up @@ -25,6 +25,7 @@

import re
import urllib.parse
import json

from qgis.testing import unittest

Expand Down Expand Up @@ -59,6 +60,18 @@ def setUpClass(cls):

cls.vlconn = cls.dbconn + ' sslmode=disable key=\'pk\' checkPrimaryKeyUnicity=0 srid=4326 type=POINT table="qgis_test"."someDataLong" (geom) sql='

# Create another layer with multiple PKs
conn.executeSql('DROP TABLE IF EXISTS "qgis_test"."multiple_pks"')
conn.executeSql(
'CREATE TABLE "qgis_test"."multiple_pks" ( pk1 bigint not null, pk2 bigint not null, name text not null, geom geometry(POINT,4326), PRIMARY KEY ( pk1, pk2 ) )')
conn.executeSql(
'INSERT INTO "qgis_test"."multiple_pks" VALUES ( 1, 1, \'1-1\', ST_GeomFromText(\'point(7 45)\', 4326))')
conn.executeSql(
'INSERT INTO "qgis_test"."multiple_pks" VALUES ( 1, 2, \'1-2\', ST_GeomFromText(\'point(8 46)\', 4326))')

cls.vlconn_multiplepks = cls.dbconn + \
" sslmode=disable key='pk1,pk2' estimatedmetadata=true srid=4326 type=Point checkPrimaryKeyUnicity='0' table=\"qgis_test\".\"multiple_pks\" (geom)"

def _baseFilterTest(self, info_format):

vl = QgsVectorLayer(self.vlconn, 'someData', 'postgres')
Expand Down Expand Up @@ -109,6 +122,35 @@ def testGetFeatureInfoFilterPg(self):
self._baseFilterTest('application/json')
self._baseFilterTest('application/vnd.ogc.gml')

def testMultiplePks(self):
"""Test issue GH #41786"""

vl = QgsVectorLayer(self.vlconn_multiplepks, 'someData', 'postgres')
self.assertTrue(vl.isValid())
p = QgsProject()
p.addMapLayers([vl])

json_features_url = ('http://qgis/?SERVICE=WMS&REQUEST=GetFeatureInfo&' +
'LAYERS=someData&STYLES=&' +
'INFO_FORMAT=application/json&' +
'SRS=EPSG%3A4326&' +
'QUERY_LAYERS=someData&X=-1&Y=-1&' +
'FEATURE_COUNT=100&')

req = QgsBufferServerRequest(json_features_url)
res = QgsBufferServerResponse()
self.server.handleRequest(req, res, p)
j = json.loads(bytes(res.body()).decode('utf8'))
self.assertEqual(j, {'features': [{'geometry': None,
'id': 'someData.1@@1',
'properties': {'name': '1-1', 'pk1': 1, 'pk2': 1},
'type': 'Feature'},
{'geometry': None,
'id': 'someData.1@@2',
'properties': {'name': '1-2', 'pk1': 1, 'pk2': 2},
'type': 'Feature'}],
'type': 'FeatureCollection'})


if __name__ == '__main__':
unittest.main()

0 comments on commit 450f5c9

Please sign in to comment.