geopandas / contextily

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

TMS tile providers: negative {-y} in urls #250

Open abubelinha opened 3 weeks ago

abubelinha commented 3 weeks ago

Basically all this is already described in https://github.com/geopandas/xyzservices/issues/177

I found some tile servers in these pages and I was trying to use them in contextily:

https://www.ign.es/web/ide-area-nodo-ide-ign https://idee.es/en/servicios-teselas

I am guessing there is a problem with xyz syntax since their urls end in {z}/{x}/{-y}.extension Not sure how to implement that in my code:

import contextily as cx
# gdf is a simple geodataframe of a 10 x 10 Km square polygon inside Spain
ax = gdf.plot(figsize=(10, 10), alpha=0.05, edgecolor="k")
# this works well for my area:
tile_source = cx.providers.OpenStreetMap.Mapnik  
tile_attribution = 'OSM contributors' 
# but this fails:
tile_source='https://tms-pnoa-ma.idee.es/1.0.0/pnoa-ma/{z}/{x}/-{y}.jpeg'  
tile_attribution = 'idee.es pnoa-ma'
cx.add_basemap(ax=ax, crs='epsg:4326', source=tile_source, attribution=tile_attribution)
plt.savefig('contextily_test.png', bbox_inches='tight')

Thanks for any help @abubelinha

EDIT: some related links?

abubelinha commented 3 weeks ago

I was able to got this to work touching some tile.py lines in my installed package.

I basically look for a '{-y}' string in source url. If that substring exists, I reconstruct the tiles array in both bounds2img() and _merge_tiles() in order to correct y values using the formula provided by @tmcw here: https://gist.github.com/tmcw/4954720

This is probably not the best way to do it but I am happy to share my code so an expert can tell if there is something reusable in it or this should be implemented in a very different way. Sorry I am not a git user.
Would it be enough if I clone the repo, edit the file and share a link here, so someone else can test it?

martinfleis commented 3 weeks ago

Would it be enough if I clone the repo, edit the file and share a link here, so someone else can test it?

Yeah, that would help!

abubelinha commented 2 weeks ago

I uploaed my modified tile.py here: https://github.com/abubelinha/contextily/commits/inverted_y_sources/contextily/tile.py

But when I compared lines changed, I realized I was not using the last version published in github: I edited the pip-installed version (1.5.2) instead.

I later tried to install development version from github, but I couldn't:

C:\Python38\scripts\pip install https://github.com/geopandas/contextily/archive/master.zip
Collecting https://github.com/geopandas/contextily/archive/master.zip
  Using cached https://github.com/geopandas/contextily/archive/master.zip
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error

  × Getting requirements to build wheel did not run successfully.
  ¦ exit code: 1
      LookupError: setuptools-scm was unable to detect version for C:\Users\abu\AppData\Local\Temp\pip-req-build-mexwnes2.

      Make sure you're either building from a fully intact git repository or PyPI tarballs. Most other sources (such as GitHub's tarballs, a git checkout without the .git folder) don't contain the necessary metadata and will not work.

      For example, if you're using pip, instead of https://github.com/user/proj/archive/master.zip use git+https://github.com/user/proj.git#egg=proj
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.

I hope differences are not important and you can test my changes (I only touched a couple of functions and added a new one).

In my commit I also added (as a commit-comment) a contextily_tms_test() function to test my changes using a given server, zoom and bounding box coordinates to generate a image.

I couldn't find yet any global coverage mapping service which also uses flipped '{-y}' tiles and reaches high zoom levels (i.e. 14 or higher). So my examples are only from the Spanish services I posted above and I guess they will fail to serve tiles outside Spain unless you use big bounding boxes to force small zoom levels.

Please test it using other flipped-Y servers that you know

martinfleis commented 1 week ago

This is all eventually related to #120 which was never finished.

abubelinha commented 1 week ago

No idea, but IMHO my issue is much simpler.

Just a matter of correcting y values when servers use an inverted tile numbering system for y axis