geopandas / contextily

Context geo-tiles in Python
https://contextily.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
493 stars 81 forks source link

Wrong lat and lon from contextily bbox #207

Closed automataIA closed 1 year ago

automataIA commented 1 year ago

I write this code:

sind = Place("Sindian")
print(sind.bbox_map)

and the output is: (13516512.585724287, 13540972.434775542, 2856910.369186746, 2876478.248427753)

But the coordinate for example are, circa: (25.014 , 121.473)

Why?

darribas commented 1 year ago

Return is always in Web Mercator, which is expressed in metres, not in lon/lat as the underlying tiles are always on that projection. Does that make sense?

From the documentation:

https://github.com/geopandas/contextily/blob/46b37b6dbd5c407e776f8f3a4b0125711f585766/contextily/place.py#L46-L47

darribas commented 1 year ago

Closing as I think this is answered, but happy to engage further if there's any additional question.

automataIA commented 1 year ago

For other people:

import pyproj

def web_mercator_to_lonlat(x, y):
    # Define the Web Mercator and WGS 84 (longitude/latitude) projections
    web_mercator = pyproj.Proj(proj='utm', zone=33, datum='WGS84')
    wgs84 = pyproj.Proj(proj='longlat', datum='WGS84')

    # Convert the Web Mercator coordinates to longitude/latitude
    lon, lat = pyproj.transform(web_mercator, wgs84, x, y)

    return lon, lat

def lonlat_to_web_mercator(lon, lat):
    # Define the Web Mercator and WGS 84 (longitude/latitude) projections
    web_mercator = pyproj.Proj(proj='utm', zone=33, datum='WGS84')
    wgs84 = pyproj.Proj(proj='longlat', datum='WGS84')

    # Convert the longitude/latitude coordinates to Web Mercator
    x, y = pyproj.transform(wgs84, web_mercator, lon, lat)

    return x, y