OSM Tile Loader¶
In [1]:
Copied!
from srai.loaders.osm_loaders import OSMTileLoader
from srai.regionalizers import geocode_to_region_gdf
ZOOM = 9
from srai.loaders.osm_loaders import OSMTileLoader
from srai.regionalizers import geocode_to_region_gdf
ZOOM = 9
Get tiles from tile server¶
In order to get tiles use load method. Method load
returns GeoDataFrame
object with images or path to them, as chosen data_collector. Returned frame contains also geographical coordinates of tile and x, y, z coordinates used by tile servers.
In [2]:
Copied!
loader = OSMTileLoader("https://tile.openstreetmap.de", zoom=ZOOM, verbose=True)
gdf = geocode_to_region_gdf("Wroclaw, Poland")
tiles = loader.load(gdf)
tiles
loader = OSMTileLoader("https://tile.openstreetmap.de", zoom=ZOOM, verbose=True)
gdf = geocode_to_region_gdf("Wroclaw, Poland")
tiles = loader.load(gdf)
tiles
Getting tile from url: https://tile.openstreetmap.de/9/279/170.png Getting tile from url: https://tile.openstreetmap.de/9/279/171.png Getting tile from url: https://tile.openstreetmap.de/9/280/170.png Getting tile from url: https://tile.openstreetmap.de/9/280/171.png
Out[2]:
x | y | geometry | z | tile | |
---|---|---|---|---|---|
region_id | |||||
279_170_9 | 279 | 170 | POLYGON ((16.87500 51.61802, 16.87500 51.17934... | 9 | <PIL.PngImagePlugin.PngImageFile image mode=P ... |
279_171_9 | 279 | 171 | POLYGON ((16.87500 51.17934, 16.87500 50.73646... | 9 | <PIL.PngImagePlugin.PngImageFile image mode=P ... |
280_170_9 | 280 | 170 | POLYGON ((17.57812 51.61802, 17.57812 51.17934... | 9 | <PIL.PngImagePlugin.PngImageFile image mode=P ... |
280_171_9 | 280 | 171 | POLYGON ((17.57812 51.17934, 17.57812 50.73646... | 9 | <PIL.PngImagePlugin.PngImageFile image mode=P ... |
To get records conveniently use SlippyMapId
In [3]:
Copied!
tiles.loc[f"279_170_{ZOOM}"]["tile"]
tiles.loc[f"279_170_{ZOOM}"]["tile"]
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) File /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/PIL/JpegImagePlugin.py:639, in _save(im, fp, filename) 638 try: --> 639 rawmode = RAWMODE[im.mode] 640 except KeyError as e: KeyError: 'P' The above exception was the direct cause of the following exception: OSError Traceback (most recent call last) File /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/PIL/Image.py:643, in Image._repr_image(self, image_format, **kwargs) 642 try: --> 643 self.save(b, image_format, **kwargs) 644 except Exception as e: File /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/PIL/Image.py:2413, in Image.save(self, fp, format, **params) 2412 try: -> 2413 save_handler(self, fp, filename) 2414 except Exception: File /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/PIL/JpegImagePlugin.py:642, in _save(im, fp, filename) 641 msg = f"cannot write mode {im.mode} as JPEG" --> 642 raise OSError(msg) from e 644 info = im.encoderinfo OSError: cannot write mode P as JPEG The above exception was the direct cause of the following exception: ValueError Traceback (most recent call last) File /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/IPython/core/formatters.py:344, in BaseFormatter.__call__(self, obj) 342 method = get_real_method(obj, self.print_method) 343 if method is not None: --> 344 return method() 345 return None 346 else: File /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/PIL/Image.py:661, in Image._repr_jpeg_(self) 656 def _repr_jpeg_(self): 657 """iPython display hook support for JPEG format. 658 659 :returns: JPEG version of the image as bytes 660 """ --> 661 return self._repr_image("JPEG") File /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/PIL/Image.py:646, in Image._repr_image(self, image_format, **kwargs) 644 except Exception as e: 645 msg = f"Could not save to {image_format} for display" --> 646 raise ValueError(msg) from e 647 return b.getvalue() ValueError: Could not save to JPEG for display
Out[3]:
Get one tile¶
In [4]:
Copied!
loader = OSMTileLoader("https://tile.openstreetmap.de", zoom=2)
tile = loader.get_tile_by_x_y(2, 1)
tile
loader = OSMTileLoader("https://tile.openstreetmap.de", zoom=2)
tile = loader.get_tile_by_x_y(2, 1)
tile
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) File /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/PIL/JpegImagePlugin.py:639, in _save(im, fp, filename) 638 try: --> 639 rawmode = RAWMODE[im.mode] 640 except KeyError as e: KeyError: 'P' The above exception was the direct cause of the following exception: OSError Traceback (most recent call last) File /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/PIL/Image.py:643, in Image._repr_image(self, image_format, **kwargs) 642 try: --> 643 self.save(b, image_format, **kwargs) 644 except Exception as e: File /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/PIL/Image.py:2413, in Image.save(self, fp, format, **params) 2412 try: -> 2413 save_handler(self, fp, filename) 2414 except Exception: File /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/PIL/JpegImagePlugin.py:642, in _save(im, fp, filename) 641 msg = f"cannot write mode {im.mode} as JPEG" --> 642 raise OSError(msg) from e 644 info = im.encoderinfo OSError: cannot write mode P as JPEG The above exception was the direct cause of the following exception: ValueError Traceback (most recent call last) File /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/IPython/core/formatters.py:344, in BaseFormatter.__call__(self, obj) 342 method = get_real_method(obj, self.print_method) 343 if method is not None: --> 344 return method() 345 return None 346 else: File /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/PIL/Image.py:661, in Image._repr_jpeg_(self) 656 def _repr_jpeg_(self): 657 """iPython display hook support for JPEG format. 658 659 :returns: JPEG version of the image as bytes 660 """ --> 661 return self._repr_image("JPEG") File /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/PIL/Image.py:646, in Image._repr_image(self, image_format, **kwargs) 644 except Exception as e: 645 msg = f"Could not save to {image_format} for display" --> 646 raise ValueError(msg) from e 647 return b.getvalue() ValueError: Could not save to JPEG for display
Out[4]: