TransWikia.com

GeoServer and Leaflet GetFeature problem with bounding boxes on click location

Geographic Information Systems Asked by Victor Marotta on May 30, 2021

I have a Leaflet map with WMS layers from GeoServer. I want to click on a determinate location and make a GetFeature request to GeoServer so that I can show a GeoJSON on Leaflet of the features clicked.

I’ve been able to make it request the data and show it in Leaflet, however there’s been a problem with the bounding box of the request and how GeoServer is dealing with it.

This is my map with a loaded WMS layer, I want to select the polygon represented by the blue bounding box. When I click on location 2 I get my desired polygon, however, when I click on location 1 I get both my desired polygon and the polygon with the red bounding box.

enter image description here

If I click on a polygon that does not share bounding boxes with other features everything works ok.

This is my request, made by my application server to GeoServer:

myserver.com.br/geoserver/wms?
service=WFS&
version=2.0.0&
request=GetFeature&
typeNames=gianetti:CAD_Lote&
outputformat=application/json&
srsName=EPSG:4326&
bbox=-20.75447104737565,-42.87179304096168,-20.754466022047144,-42.87178801563317,urn:ogc:def:crs:EPSG:4326&
count=1

I added count = 1 to limit results to only one feature, if I remove this parameter my results are both polygons or even more polygons, which I do not want.

I generated my bounding box dynamically on Leaflet, using the click event latlng object plus or minus the pixel size. I’ve tried to make a bounding box with minY=maxY and minX=maxX, but the error persists.

Below is the code that makes the request in Leaflet:

map.addEventListener('click',getFeature);

selectedLayers = L.geoJSON().addTo(map);

function getFeature(e) {

    if (select) { // Switcher to check if the selection tool is enabled

        selectedLayers.clearLayers() // Clear previous selection
        // Backend address to request a WFS geojson: /select/:layer/:lat1/:lng1/:lat2/:lng2/:srs
    
        pixelSize = (40075016.686 * Math.abs(Math.cos(map.getCenter().lat / 180 * Math.PI)) / Math.pow(2, map.getZoom()+8))/111120 // Calculates pixelSize in GMS
    
        for (i=0;i<activeLayers.length;i++){
            params = {  
                layers: activeLayers[i],       
                lat1: e.latlng.lat-0.5*pixelSize,
                lng1: e.latlng.lng-0.5*pixelSize,
                lat2: e.latlng.lat+0.5*pixelSize,
                lng2: e.latlng.lng+0.5*pixelSize, 
                srs: 'urn:ogc:def:crs:EPSG:4326'
            }
            url = '/select/' + Object.values(params).join('/');
            console.log(params)
            $.ajax({ // Making request to backend server
                url: url,
                success: function (data, status, xhr) {
                    loadFeatureAsLayer(data)
                },
                error: function (xhr, status, error) {
                    console.log(error)
                }
            }); 
        }      
    } 
}

Then, on my backend server, built with node.js, I make the request using some custom authentication headers:

app.get('/select/:layer/:lat1/:lng1/:lat2/:lng2/:srs',(req,res) => {

    params = {  
        service: 'WFS',
        version: '2.0.0',
        request: 'GetFeature',
        typeNames: req.params.layer,         
        outputformat: 'application/json',
        srsName:'EPSG:4326',
        bbox: req.params.lat1+','+req.params.lng1+','+req.params.lat2+','+req.params.lng2+','+req.params.srs,
        count: 1
    }

    url = 'myserver.com.br/geoserver/wms?' + Object.entries(params).map(e => e.join('=')).join('&');

    fetch(url, {method : 'GET', headers: headers})
    .then(res => res.text())
    .then(data => {
        console.log('WFS requisition sent: ' + url)
        res.send(data)
    })
    .catch(error => res.send(error))
})

I searched though SE for an answer and found something similar here, in this problem @user30184 elucidated that GeoServer makes the search for features in a determinate location by comparing ir with envelopes of searched features, which might explain why I’m having this issue. Is there any way to disable this behavior in GeoServer or is there any way around this problem to get what I want?

EDIT

Below is my Geoserver URL when I click location 2:

    https://mapas.genteufv.com.br/geoserver/gianetti/wms?
service=WFS&
version=2.0.0&
request=GetFeature&
typeNames=gianetti:CAD_Lote&
outputformat=application/json&
srsName=EPSG:4326&
bbox=-20.75447104737565,-42.87179304096168,-20.754466022047144,-42.87178801563317,urn:ogc:def:crs:EPSG:4326&
count=1

And then my response with the polygon from the red bounding box:

{"type":"FeatureCollection","features":[{"type":"Feature","id":"CAD_Lote.fid--570a0915_178376a904f_1ea0","geometry":{"type":"Polygon","coordinates":[[[-42.87537308,-20.75358136],[-42.87534321,-20.75341135],[-42.87402867,-20.75354788],[-42.87378353,-20.75355822],[-42.87359681,-20.75357227],[-42.8732492,-20.75356351],[-42.87300616,-20.75353671],[-42.87272261,-20.75346778],[-42.87237809,-20.75335052],[-42.87193077,-20.75312494],[-42.87168515,-20.75300035],[-42.87155135,-20.75290428],[-42.87148458,-20.75286198],[-42.87147999,-20.75285923],[-42.87147534,-20.75285656],[-42.87147064,-20.75285397],[-42.8714659,-20.75285145],[-42.87146111,-20.75284901],[-42.87145628,-20.75284665],[-42.87145141,-20.75284437],[-42.87144649,-20.75284216],[-42.87144153,-20.75284004],[-42.87143654,-20.752838],[-42.87143151,-20.75283604],[-42.87142644,-20.75283417],[-42.87142134,-20.75283237],[-42.87141621,-20.75283066],[-42.87141104,-20.75282904],[-42.87140585,-20.7528275],[-42.87140063,-20.75282604],[-42.87139538,-20.75282467],[-42.87139011,-20.75282339],[-42.87138482,-20.75282219],[-42.8713795,-20.75282107],[-42.87137417,-20.75282005],[-42.87136881,-20.75281911],[-42.87136344,-20.75281826],[-42.87135806,-20.7528175],[-42.87135266,-20.75281682],[-42.87134725,-20.75281624],[-42.87134183,-20.75281574],[-42.8713364,-20.75281533],[-42.87133097,-20.75281501],[-42.87132553,-20.75281477],[-42.87132009,-20.75281463],[-42.87131464,-20.75281457],[-42.8713092,-20.75281461],[-42.87130375,-20.75281473],[-42.87129831,-20.75281494],[-42.87129288,-20.75281524],[-42.87128745,-20.75281563],[-42.87128202,-20.75281611],[-42.87127661,-20.75281667],[-42.87127121,-20.75281733],[-42.87126582,-20.75281807],[-42.87126045,-20.7528189],[-42.87125138,-20.75282047],[-42.87124234,-20.75282219],[-42.87123333,-20.75282405],[-42.87122436,-20.75282606],[-42.87121543,-20.75282822],[-42.87120653,-20.75283052],[-42.87119768,-20.75283296],[-42.87118888,-20.75283555],[-42.87118012,-20.75283827],[-42.87117142,-20.75284114],[-42.87116277,-20.75284415],[-42.87115418,-20.7528473],[-42.87114564,-20.75285059],[-42.87113717,-20.75285402],[-42.87112876,-20.75285758],[-42.87112042,-20.75286128],[-42.87111214,-20.75286511],[-42.87110394,-20.75286908],[-42.87109581,-20.75287317],[-42.87108776,-20.7528774],[-42.87107978,-20.75288176],[-42.87107189,-20.75288625],[-42.87106408,-20.75289087],[-42.87105636,-20.75289561],[-42.87104872,-20.75290047],[-42.87104117,-20.75290546],[-42.87103372,-20.75291057],[-42.87102636,-20.7529158],[-42.8710191,-20.75292115],[-42.87101194,-20.75292661],[-42.87100487,-20.75293219],[-42.87099792,-20.75293788],[-42.87099106,-20.75294369],[-42.87098432,-20.7529496],[-42.87097768,-20.75295563],[-42.87097093,-20.75296111],[-42.87096429,-20.7529667],[-42.87095775,-20.7529724],[-42.87095131,-20.7529782],[-42.87094498,-20.75298411],[-42.87093876,-20.75299012],[-42.87093266,-20.75299623],[-42.87092666,-20.75300243],[-42.87092078,-20.75300874],[-42.87091502,-20.75301514],[-42.87090937,-20.75302163],[-42.87090385,-20.75302821],[-42.87089845,-20.75303488],[-42.87089317,-20.75304164],[-42.87088801,-20.75304848],[-42.87088298,-20.7530554],[-42.87087808,-20.75306241],[-42.87087331,-20.75306949],[-42.87086867,-20.75307665],[-42.87086416,-20.75308389],[-42.87085979,-20.7530912],[-42.87085555,-20.75309857],[-42.87085145,-20.75310602],[-42.87084748,-20.75311353],[-42.87084365,-20.7531211],[-42.87083996,-20.75312874],[-42.87083642,-20.75313643],[-42.87083301,-20.75314418],[-42.87082975,-20.75315198],[-42.87082663,-20.75315984],[-42.87082365,-20.75316774],[-42.87082082,-20.7531757],[-42.87081814,-20.75318369],[-42.87081474,-20.75319318],[-42.87081152,-20.75320271],[-42.87080846,-20.7532123],[-42.87080558,-20.75322193],[-42.87080287,-20.75323161],[-42.87080033,-20.75324133],[-42.87079797,-20.75325109],[-42.87079579,-20.75326088],[-42.87079378,-20.75327071],[-42.87079195,-20.75328057],[-42.8707903,-20.75329045],[-42.87078882,-20.75330036],[-42.87078753,-20.7533103],[-42.87078641,-20.75332025],[-42.87078547,-20.75333022],[-42.87078471,-20.7533402],[-42.87078413,-20.75335019],[-42.87078373,-20.75336019],[-42.87078351,-20.7533702],[-42.87078347,-20.7533802],[-42.87078361,-20.75339021],[-42.87078393,-20.75340021],[-42.87078443,-20.75341021],[-42.87078511,-20.7534202],[-42.87078597,-20.75343017],[-42.87078701,-20.75344013],[-42.87078822,-20.75345007],[-42.87078962,-20.75345999],[-42.87079119,-20.75346989],[-42.87079294,-20.75347976],[-42.87079487,-20.7534896],[-42.87079613,-20.75349737],[-42.87079753,-20.75350512],[-42.87079908,-20.75351284],[-42.87080077,-20.75352054],[-42.8708026,-20.7535282],[-42.87080457,-20.75353584],[-42.87080668,-20.75354344],[-42.87080893,-20.75355101],[-42.87081133,-20.75355854],[-42.87081386,-20.75356603],[-42.87081653,-20.75357347],[-42.87081933,-20.75358087],[-42.87082228,-20.75358823],[-42.87082535,-20.75359553],[-42.87082857,-20.75360279],[-42.87083191,-20.75360999],[-42.87083539,-20.75361713],[-42.870839,-20.75362421],[-42.87084275,-20.75363124],[-42.87084662,-20.7536382],[-42.87085062,-20.7536451],[-42.87085474,-20.75365193],[-42.87085899,-20.7536587],[-42.87086337,-20.75366539],[-42.87086787,-20.75367201],[-42.87087249,-20.75367856],[-42.87087723,-20.75368503],[-42.87088209,-20.75369142],[-42.87088707,-20.75369773],[-42.87089217,-20.75370396],[-42.87089737,-20.7537101],[-42.8709027,-20.75371616],[-42.87090813,-20.75372213],[-42.87091367,-20.75372801],[-42.87091932,-20.75373379],[-42.87095694,-20.75376386],[-42.8709652,-20.75377104],[-42.87097359,-20.75377809],[-42.87098211,-20.753785],[-42.87099075,-20.75379177],[-42.87099952,-20.7537984],[-42.8710084,-20.75380489],[-42.8710174,-20.75381124],[-42.87102651,-20.75381744],[-42.87103574,-20.75382349],[-42.87104507,-20.75382939],[-42.87105451,-20.75383515],[-42.87106406,-20.75384074],[-42.8710737,-20.75384619],[-42.87108344,-20.75385148],[-42.87109328,-20.75385661],[-42.87110321,-20.75386158],[-42.87110664,-20.75386322],[-42.87110781,-20.75385989],[-42.87119382,-20.75361446],[-42.87126258,-20.75341826],[-42.87128221,-20.75334683],[-42.87127725,-20.75327046],[-42.87122446,-20.75320432],[-42.87123759,-20.75316736],[-42.87136624,-20.75321845],[-42.87204214,-20.75349082],[-42.87198494,-20.75365912],[-42.87218148,-20.75371838],[-42.87235979,-20.753773],[-42.87253426,-20.75381298],[-42.87277251,-20.75386646],[-42.87291634,-20.75390804],[-42.87311537,-20.75396558],[-42.87330441,-20.7540102],[-42.8731459,-20.75447844],[-42.87322647,-20.75449866],[-42.87322802,-20.7544969],[-42.87323116,-20.75449344],[-42.87323437,-20.75449003],[-42.87323764,-20.75448668],[-42.87324098,-20.75448337],[-42.87324437,-20.75448013],[-42.87324782,-20.75447694],[-42.87325133,-20.7544738],[-42.8732549,-20.75447073],[-42.87325852,-20.75446771],[-42.8732622,-20.75446475],[-42.87326594,-20.75446185],[-42.87326972,-20.75445902],[-42.87327356,-20.75445624],[-42.87327745,-20.75445353],[-42.87328139,-20.75445089],[-42.87328538,-20.75444831],[-42.87328941,-20.75444579],[-42.87329349,-20.75444334],[-42.87329761,-20.75444095],[-42.87330178,-20.75443864],[-42.87330599,-20.75443639],[-42.87331024,-20.75443421],[-42.87331453,-20.75443211],[-42.87331886,-20.75443007],[-42.87332322,-20.7544281],[-42.87332763,-20.7544262],[-42.87333206,-20.75442438],[-42.87333653,-20.75442263],[-42.87334103,-20.75442095],[-42.87334556,-20.75441935],[-42.87335012,-20.75441782],[-42.8733547,-20.75441636],[-42.87335932,-20.75441498],[-42.87336395,-20.75441368],[-42.87336861,-20.75441245],[-42.8733733,-20.7544113],[-42.873378,-20.75441022],[-42.87338272,-20.75440922],[-42.87338746,-20.7544083],[-42.87339222,-20.75440746],[-42.87339699,-20.75440669],[-42.87340177,-20.754406],[-42.87340657,-20.75440539],[-42.87341138,-20.75440486],[-42.87341619,-20.75440441],[-42.87342102,-20.75440404],[-42.87342585,-20.75440374],[-42.87343068,-20.75440352],[-42.87343552,-20.75440339],[-42.87344036,-20.75440333],[-42.8734452,-20.75440335],[-42.87345004,-20.75440345],[-42.87345488,-20.75440363],[-42.87345971,-20.75440388],[-42.87346454,-20.75440422],[-42.87346936,-20.75440463],[-42.87347417,-20.75440513],[-42.87347897,-20.7544057],[-42.87348376,-20.75440635],[-42.87348854,-20.75440708],[-42.8734933,-20.75440788],[-42.87349805,-20.75440877],[-42.87350278,-20.75440973],[-42.87350749,-20.75441077],[-42.87351219,-20.75441188],[-42.87351686,-20.75441308],[-42.87352151,-20.75441434],[-42.87352613,-20.75441569],[-42.87353073,-20.75441711],[-42.8735353,-20.7544186],[-42.87353985,-20.75442017],[-42.87354436,-20.75442181],[-42.87354885,-20.75442352],[-42.8735533,-20.75442531],[-42.87355771,-20.75442717],[-42.8735621,-20.75442911],[-42.87356644,-20.75443111],[-42.87357075,-20.75443318],[-42.87357502,-20.75443533],[-42.87357925,-20.75443754],[-42.87358344,-20.75443983],[-42.87358758,-20.75444218],[-42.87358774,-20.75444227],[-42.87346292,-20.75418652],[-42.87381933,-20.75408469],[-42.87399927,-20.75402744],[-42.87417392,-20.75397137],[-42.87435558,-20.75391764],[-42.87454629,-20.75385827],[-42.87472511,-20.75381062],[-42.87495,-20.75374154],[-42.87494181,-20.7537148],[-42.87504912,-20.7536816],[-42.87537308,-20.75358136]]]},"geometry_name":"geom","properties":{"id":21,"distrito":1,"setor":1,"quadra":4,"lote":282,"inscricao_lote":"01.01.0004.0282"}}],"totalFeatures":3,"numberMatched":3,"numberReturned":1,"timeStamp":"2021-03-18T17:27:00.453Z","crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:EPSG::4326"}}}

EDIT 2

In this case scenario I won’t have overlapping features in the same layer, this is a constraint on the conceptual model of the application. I might have overlapping features from different layers which is not a problem, since I make an individual requisition for each layer displayed on the map.

I believe it is important to clarify that I won’t be querying only polygons, but also points and lines. When querying lines by this method I am getting the same problem of the bounding box queried inside the line envelop. Points are working fine.

That said, and as the application aims to be an all purpose framework for spatial data sharing I believe that using some attribute of the layer (like the id) to select instead of only a location based method would be a problem since: 1 – ID’s might not be available, since data comes from Geoserver and the users might hide the ID’s as they are primary keys; 2 – users might use funny names for ID’s, like ‘id_0’ or ‘id_1’ which are common since in my area people usually uploads tables with existing ID’s to PostGIS, which is used as my database.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP