1

How does QGIS do the GetFeatureInfo Requests when I select a raster layer (WMS Layer) and use the Identify tool on the map?enter image description here

And how do I implement this in my PyQGIS Plugin, so I can send GetFeatureInfo requests with x and y coordinates? Letting QGIS generate the GetFeatureInfo request with coordinates in PyQGIS would work as well.

If that is not possible, what is the correct way to generate the bbox, width, height, i and j in a GetFeatureInfo request with coordinates only?

ThomasG77
  • 30,725
  • 1
  • 53
  • 93
  • GetFeatureInfo request is the same as a GetMap request, (because the server needs to know which map the pixel coordinate relates too, and requests are stateless), except, request=GetFeatureInfo not GetMap, and you specify pixel coordinates (i,j) for 1.3.0 or x,y for 1.1.1, and format of the info response – nmtoken Jul 23 '21 at 09:46

1 Answers1

2

You don't need to care about doing manually the GetFeatureInfo manually. Instead use the identify method from QgsRasterDataProvider class (https://qgis.org/api/classQgsRasterDataProvider.html#a5b033a4ab93593c8378af8005ed52ccd)

wmsLayer = iface.activeLayer()
# Change with your coordinates
ident = wmsLayer.dataProvider().identify(QgsPointXY(693135,6803672), QgsRaster.IdentifyFormatFeature)

You can look at ident.results() and the features

print(ident.results()[0][0].features())

To see one feature from the getfeatureinfo behind the scene

fields = [f.name() for f in ident.results()[0][0].features()[0].fields()] attributes = ident.results()[0][0].features()[0].attributes() print(dict(zip(fields, attributes)))

Edit:

As you may need to reproject coordinates from canvas to WMS layer projection, you can use the following

canvas_crs = iface.mapCanvas().mapSettings().destinationCrs()
wms_crs = iface.activeLayer().crs()
pointXY = QgsPointXY(4.2745, 47.7904)
tr = QgsCoordinateTransform(canvas_crs, wms_crs, QgsProject.instance())
geom = QgsGeometry.fromPointXY(pointXY)
geom.transform(tr)
print(geom.asPoint())

PS: adapted from more generic answer https://gis.stackexchange.com/a/163689/638

ThomasG77
  • 30,725
  • 1
  • 53
  • 93
  • Thank you for your helpful reply. Unfortunately, I can not find anything in the documentation on how to change the EPSG for the request? Should I just use Proj4 to adjust the coordinates to the EPSG of the Wms layer? – Steven Grether Jul 27 '21 at 15:26
  • Edited my answer – ThomasG77 Jul 28 '21 at 00:36