Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in the implementation of tile coordinates of babel('bing') #13

Open
ysig opened this issue Mar 12, 2023 · 3 comments
Open

Error in the implementation of tile coordinates of babel('bing') #13

ysig opened this issue Mar 12, 2023 · 3 comments

Comments

@ysig
Copy link

ysig commented Mar 12, 2023

Hi,

thank you for this really nice wrapper library.

I have a problem that seems to be related with an implementation error for bing tiles. Given:

from babelgrid import Babel
babel = Babel('bing')
lat, lon = 14.223633, 40.948055

the predicted tile box boundaries:

tile = babel.geo_to_tile(lat, lon, resolution=res)
print(tile.geometry.geojson['coordinates'][0])

are:

((0.0, 40.979898069620155), (0.0, 66.51326044311185), (45.00000000000001, 66.51326044311185), (45.00000000000001, 40.979898069620155))

which clearly doesn't contain the lat, lon point (the mismatch is in the second dimension 40.938055 < 40.979898069620155)

So either I'm not using the correct way for extracting the boundaries or there is a problem in the implementation.

Thanks,

@JoaoCarabetta
Copy link
Collaborator

Which resolution are you using?

@JoaoCarabetta
Copy link
Collaborator

I ran some tests and everything seems to be looking good.

This map show that the point is inside the polygon for res = 1

from babelgrid import Babel
import folium

res = 1
babel = Babel('bing')
lat, lon = 14.223633, 40.948055

tile = babel.geo_to_tile(lat, lon, resolution=1)

m = folium.Map()
folium.GeoJson(tile.geometry.geojson).add_to(m)
folium.Circle([lat, lon]).add_to(m)
m

image

And I get that the tile contains the point for all resolutions from 1 to 15

from shapely.geometry import Point

def tile_contains_point(res):
    tile = babel.geo_to_tile(lat, lon, resolution=1).geometry.shapely
    return tile.contains(Point([lat, lon]))

all([tile_contains_point(res) for i in range(1, 15)]) == True

@ysig
Copy link
Author

ysig commented Mar 22, 2023

Your code has an error:

def tile_contains_point(res):
    tile = babel.geo_to_tile(lat, lon, resolution=1).geometry.shapely

->

def tile_contains_point(res):
    tile = babel.geo_to_tile(lat, lon, resolution=res).geometry.shapely

The correct code and with two different ways of comparison gives the flaw:

from shapely.geometry import Point
from babelgrid import Babel

babel = Babel('bing')
lat, lon = 14.223633, 40.948055

def tile_contains_point(res):
    tile = babel.geo_to_tile(lat, lon, resolution=res).geometry.shapely
    gs = babel.geo_to_tile(lat, lon, resolution=res).geometry.geojson['coordinates'][0]

    return tile.contains(Point([lat, lon])), in_boundaries(lat, lon, gs)

def in_boundaries(lat, lon, bd):
    min_lat, min_lon = min(a for a,_ in bd), min(b for _,b in bd)
    max_lat, max_lon = max(a for a,_ in bd), max(b for _,b in bd)
    return min_lat <= lat <= max_lat and min_lon <= lon <= max_lon

for res in range(1, 15):
    print('res:', res, ' - ', tile_contains_point(res))
assert all([all(tile_contains_point(res)) for res in range(1, 15)])

Output:

res: 1  -  (True, True)
res: 2  -  (True, True)
res: 3  -  (True, True)
res: 4  -  (False, False)
res: 5  -  (False, False)
res: 6  -  (False, False)
res: 7  -  (False, False)
res: 8  -  (False, False)
res: 9  -  (False, False)
res: 10  -  (False, False)
res: 11  -  (False, False)
res: 12  -  (False, False)
res: 13  -  (False, False)
res: 14  -  (False, False)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants