Beware of decimal pixel values in OpenLayers.

Posted by – Wednesday 2012-12-19

So you are programming an OpenLayers based map viewer and attempt to make a WMS GetFeatureInfo request. The response will later be pretty-formatted and displayed to the user. In order to to accomplish this you create JavaScript code like the shown below:

var parameters = {
    srs: map.getProjection(),
    bbox: map.getExtent().toBBOX(6),
    width: map.getSize().w,
    height: map.getSize().h,
    x: evt.xy.x,
    y: evt.xy.y,
    layers: 'municipalities',
    query_layers: 'municipalities'
};

OpenLayers.Request.issue({
    method: 'get',
    url: this.getFeatureInfoUrl,
    async: false,
    params: parameters,
    success: function(data) {
       var responseText = data.responseText;
       // ...
    }
});

where evt (lines 6 and 7) represents the mouse click event that triggered the GetFeatureInfo request.

if using GeoServer, you might get an error message that will look like this:

<ServiceExceptionReport version="1.1.1">
    <ServiceException code="InvalidPoint">
        X and Y incorrectly specified
    </ServiceException>
</ServiceExceptionReport>

Where is the problem? The sent values x and y – that represent the pixel coordinates in the map viewport – were decimal numbers. The WMS GetFeatureInfo request expects that both numbers are positive integer values.

This problem can be fixed by using the JavaScript function parseInt(), which will strip the decimal part and return an integer number (lines 6 and 7).

var parameters = {
    srs: map.getProjection(),
    bbox: map.getExtent().toBBOX(6),
    width: map.getSize().w,
    height: map.getSize().h,
    x: parseInt(evt.xy.x),
    y: parseInt(evt.xy.y),
    layers: 'municipalities',
    query_layers: 'municipalities'
};

0 Comments on Beware of decimal pixel values in OpenLayers.

Respond

Respond

Comments

Comments