含小数坐标的WMS GetFeatureInfo请求失效问题求助
问题描述
使用betterWms插件实现地图点击获取点位功能,当点击点位对应的像素坐标不含小数时,生成的WMS请求URL有效;但像素坐标含小数时请求失效。已尝试将URL中的小数点替换为%2E、逗号替换为%2C等编码方式,均无效果,仅移除小数可使URL正常工作。请求发送至GeoServer,数据源为Shapefile,图层坐标系为EPSG:4326(与代码设置一致),图层可正常显示。曾尝试修改图层WFS部分的小数位数至50位,但因是WMS请求,不确定是否有效。
相关代码
getFeatureInfoUrl: function (latlng) { // Construct a GetFeatureInfo request URL given a point var point = this._map.latLngToContainerPoint(latlng, this._map.getZoom()), size = this._map.getSize(), params = { request: 'GetFeatureInfo', service: 'WMS', srs: 'EPSG:4326', styles: this.wmsParams.styles, transparent: this.wmsParams.transparent, version: this.wmsParams.version, format: this.wmsParams.format, bbox: this._map.getBounds().toBBoxString(), height: size.y, width: size.x, layers: this.wmsParams.layers, query_layers: this.wmsParams.layers, info_format: 'application/json' }; params[params.version === '1.3.0' ? 'i' : 'x'] = point.x; params[params.version === '1.3.0' ? 'j' : 'y'] = point.y; console.log(point); var url = this._url + L.Util.getParamString(params, this._url, true); }
解决方案
- 强制像素坐标为整数:WMS的GetFeatureInfo请求中,
x(或1.3.0版本的i)和y(或1.3.0版本的j)参数代表地图图片上的像素位置,必须是整数。修改代码中设置这两个参数的部分,对point.x和point.y取整:params[params.version === '1.3.0' ? 'i' : 'x'] = Math.round(point.x); params[params.version === '1.3.0' ? 'j' : 'y'] = Math.round(point.y); - 验证bbox参数顺序:如果使用WMS 1.3.0版本,bbox的顺序应为
miny,minx,maxy,maxx,而1.1.1版本是minx,miny,maxx,maxy。确认toBBoxString()的输出符合对应WMS版本的要求,避免因坐标系顺序错误导致请求失效。 - 查看GeoServer日志:登录GeoServer后台,查看请求日志,获取带小数坐标请求的具体错误信息,进一步定位问题根源。
内容的提问来源于stack exchange,提问作者adrian
相关产品推荐
相关产品推荐

