0

My data is in SRS EPSG:4326. My map (view/layers) are in the default EPSG:3857.

I want to just work in the data projection and would like to query a feature on map click with my data projection coordinates, but I only seem to get a value back if I use the view coordinates and projection. I don't want to force the map to be reprojected to EPSG:4326 by OpenLayers because it gets stretched. I am using getGetFeatureInfoUrl to build the URL to call.

An AJAX call to this URL returns/finds a feature:

var viewResolution = map.getView().getResolution();
var coords = [-11131949.079327358,4865942.279503176];
var url = wmsSource.getGetFeatureInfoUrl(coords, viewResolution, 'EPSG:3857', {'INFO_FORMAT': 'application/json'});
/*
URL is now:

http://mymapserver/geoserver/myworkspace/wms?
SERVICE=WMS&
VERSION=1.1.1&
REQUEST=GetFeatureInfo&
FORMAT=image%2Fpng&
TRANSPARENT=true&
QUERY_LAYERS=myworkspace%3Amyfeature&
TILED=true&
STYLES=&
LAYERS=myworkspace%3Amyfeature&
INFO_FORMAT=application%2Fjson&
X=28&
Y=29&
WIDTH=256&
HEIGHT=256&
SRS=EPSG%3A3857&
BBOX=-11271098.442818949%2C3757032.814272983%2C-10018754.171394622%2C5009377.085697311&
*/

The code below does not find a feature, why?

var viewResolution = map.getView().getResolution();
// transform coordinates to EPSG:4326
var coords = ol.proj.transform([-11131949.079327358,4865942.279503176], 'EPSG:3857', 'EPSG:4326'); 
// coords now equals [-100,40]
var url = wmsSource.getGetFeatureInfoUrl(coords, viewResolution, 'EPSG:4326', {'INFO_FORMAT': 'application/json'});
/*
URL is now:

http://mympaserver/geoserver/myworkspace/wms? 
SERVICE=WMS&
VERSION=1.1.1&
REQUEST=GetFeatureInfo&
FORMAT=image%2Fpng&
TRANSPARENT=true&
QUERY_LAYERS=myworkspace%3Amyfeature&
TILED=true&
STYLES=&
LAYERS=myworkspace%3Amyfeature&
INFO_FORMAT=application%2Fjson&
X=56&
Y=291&
WIDTH=256&
HEIGHT=256&
SRS=EPSG%3A4326&
BBOX=-180%2C90%2C180%2C450&
*/

Shouldn't the feature be found?

Do I need to somehow transform the resolution as well?

I am specifying the coordinate system as EPSG:4326. If the URL built by OpenLayers is incorrect, why?

  • 1
    For a GFI request you apply an XY coordinate to a GetMap request, I don't think you can just convert the XY pixel coordinates ~ is that what your coords var is trying to do here? – nmtoken Jun 12 '18 at 23:04
  • 1
    I do not understand how even your first example can find anything. The coordinates in GetFeatureInfo request are pixel coordinates, that means rows and columns of the image that is returned by the corresponding WMS GetMap request. – user30184 Jun 12 '18 at 23:04
  • The method "getGetFeatureInfoUrl" just builds the URL, I'll update the question – user210757 Jun 13 '18 at 13:49
1

A GetFeatureInfo (GFI) request is effectively a GetMap (GM) request with added parameters, so from your original GFI as below

http://mymapserver/geoserver/myworkspace/wms?
SERVICE=WMS&
VERSION=1.1.1&
REQUEST=GetFeatureInfo&
FORMAT=image%2Fpng&
TRANSPARENT=true&
QUERY_LAYERS=myworkspace%3Amyfeature&
TILED=true&
STYLES=&
LAYERS=myworkspace%3Amyfeature&
INFO_FORMAT=application%2Fjson&
X=28&
Y=29&
WIDTH=256&
HEIGHT=256&
SRS=EPSG%3A3857&
BBOX=-11271098.442818949%2C3757032.814272983%2C-10018754.171394622%2C5009377.085697311&

If you remove QUERY_LAYERS=myworkspace%3Amyfeature&INFO_FORMAT=application%2Fjson&X=28&Y=29& and swap REQUEST=GetFeatureInfo& for REQUEST=GetMap& you have the GetMap request that you are querying at pixel coordinate X,Y.

So you have a GetMap/GetFetaureInfo request in one coordinate reference system (EPSG:3857) and you want to convert this to a GetMap/GetFetaureInfo request in another coordinate reference system (EPSG:4326).

You can't just get the coordinate reference for the clicked point and transform this to a coordinate in the new coordinate reference system, because the GFI doesn't take as a parameter a point value expressed in the coordinate reference system, but rather needs a pixel coordinate for the 'new' GetMap request.

As I understand it, you will need to do the following:

  • Transform the bounding box of the first GM request so it represents exactly the same area in the new GM request in the new coordinate reference system
  • Transform the clicked point coordinate reference to the new coordinate reference system
  • Calculate the pixel coordinate of the new 'clicked' point in the 'new/pseudo' GM request, based on the image size and image resolution in that request.

Even then, depending on precision, you may not find the exact same feature identified in the first request.

Looking at your second generated GFI request as below:

http://mympaserver/geoserver/myworkspace/wms? 
SERVICE=WMS&
VERSION=1.1.1&
REQUEST=GetFeatureInfo&
FORMAT=image%2Fpng&
TRANSPARENT=true&
QUERY_LAYERS=myworkspace%3Amyfeature&
TILED=true&
STYLES=&
LAYERS=myworkspace%3Amyfeature&
INFO_FORMAT=application%2Fjson&
X=56&
Y=291&
WIDTH=256&
HEIGHT=256&
SRS=EPSG%3A4326&
BBOX=-180%2C90%2C180%2C450&

The bounding box does not appear to represent the same extent, (it appears to cover most of the globe, which isn't the same as the first GM/GFI).

You ask whether the same feature should be identified, and you can see for yourself, by turning the second GFI into the underlying GM like:

http://mympaserver/geoserver/myworkspace/wms? 
SERVICE=WMS&
VERSION=1.1.1&
REQUEST=GetMap&
FORMAT=image%2Fpng&
TRANSPARENT=true&
TILED=true&
STYLES=&
LAYERS=myworkspace%3Amyfeature&
WIDTH=256&
HEIGHT=256&
SRS=EPSG%3A4326&
BBOX=-180%2C90%2C180%2C450&

Your code does change the pixel coordinate so you can see for yourself whether location X=56&Y=291& is anywhere close geographically to the original clicked point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.