Search code examples
pythonpython-requestsbounding-box

Python wfs request with bbox results in undesired characters in the request url


I make a WFS request with the python library "request". One of the parameters is a bounding box whose coordinates are given with 4 variables. The problem is that the final url has some additional characters around the bbox coordinates resulting in a wrong request. Any idea on how these characters appear and how to get rid of them?

Example coordinates:

(136226.446, 456092.217, 136726.446, 456592.217)

My code:

coords= (bbx_min_x, bbx_min_y, bbx_max_x, bbx_max_y)
url = 'https://geodata.nationaalgeoregister.nl/bag/wfs/v1_1?'
params = {'bbox' : '{}'.format(coords), 'service' : 'WFS' , 'typeName' : 'bag:pand' , 'version' : '2.0.0' , 'startIndex' : 0, 'request' : 'GetFeature', 'outputFormat' : 'json'}
exp = Request('GET', url, params=params).prepare().url

The resulted url (from print(exp)):

https://geodata.nationaalgeoregister.nl/bag/wfs/v1_1?bbox=%28136226.446%2C+456092.217%2C+136726.446%2C+456592.217%29&service=WFS&typeName=bag%3Apand&version=2.0.0&startIndex=0&request=GetFeature&outputFormat=json

Solution

  • I found the solution. To understand the problem I needed to find out the URL encoding (see here). In the code that I had, the bbox coordinates were parsed as a tuple.

    In url encoding though the open parenthesis is encoded with the character %28 changing the lower X coordinate of my bounding box from 136226.446 to 28136226.446. This resulted in a wrong bounding box.

    To fix this I give each coordinate separately:

    'bbox' : "{0},{1},{2},{3}".format(bbx_min_x,bbx_min_y, bbx_max_x,bbx_max_y)