Plot cartopy img_tiles¶
InĀ [1]:
Copied!
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.io import img_tiles
extent = [172,179,-42,-34] # North island
print(f'width, height = ({extent[1]-extent[0]}, {extent[3]-extent[2]}) deg')
zoom = 7
extent = [-80,-50,-50,-10] # South America
print(f'width, height = ({extent[1]-extent[0]}, {extent[3]-extent[2]}) deg')
zoom = 4
# NOTE: zoom specifications should be selected based on extent:
# -- 2 = coarse image, select for worldwide or continental scales
# -- 4-6 = medium coarseness, select for countries and larger states
# -- 6-10 = medium fineness, select for smaller states, regions, and cities
# -- 10-12 = fine image, select for city boundaries and zip codes
# -- 14+ = extremely fine image, select for roads, blocks, buildings
# max 19
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.io import img_tiles
extent = [172,179,-42,-34] # North island
print(f'width, height = ({extent[1]-extent[0]}, {extent[3]-extent[2]}) deg')
zoom = 7
extent = [-80,-50,-50,-10] # South America
print(f'width, height = ({extent[1]-extent[0]}, {extent[3]-extent[2]}) deg')
zoom = 4
# NOTE: zoom specifications should be selected based on extent:
# -- 2 = coarse image, select for worldwide or continental scales
# -- 4-6 = medium coarseness, select for countries and larger states
# -- 6-10 = medium fineness, select for smaller states, regions, and cities
# -- 10-12 = fine image, select for city boundaries and zip codes
# -- 14+ = extremely fine image, select for roads, blocks, buildings
# max 19
width, height = (7, 8) deg width, height = (30, 40) deg
Simple¶
no img_tiles
InĀ [2]:
Copied!
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
stock_img¶
InĀ [3]:
Copied!
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.stock_img()
ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('stock_img')
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.stock_img()
ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('stock_img')
Out[3]:
Text(0.5, 1.0, 'stock_img')
InĀ [4]:
Copied!
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.add_image(img_tiles.GoogleTiles(style='street'),zoom)
# ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('GoogleTiles - street')
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.add_image(img_tiles.GoogleTiles(style='street'),zoom)
# ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('GoogleTiles - street')
Out[4]:
Text(0.5, 1.0, 'GoogleTiles - street')
satellite¶
InĀ [5]:
Copied!
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.add_image(img_tiles.GoogleTiles(style='satellite'),zoom)
# ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('GoogleTiles - satellite')
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.add_image(img_tiles.GoogleTiles(style='satellite'),zoom)
# ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('GoogleTiles - satellite')
Out[5]:
Text(0.5, 1.0, 'GoogleTiles - satellite')
terrain¶
all black..
InĀ [6]:
Copied!
# GoogleTiles terrain - all black!
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.add_image(img_tiles.GoogleTiles(style='terrain'),zoom)
# ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('GoogleTiles - terrain')
# GoogleTiles terrain - all black!
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.add_image(img_tiles.GoogleTiles(style='terrain'),zoom)
# ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('GoogleTiles - terrain')
Out[6]:
Text(0.5, 1.0, 'GoogleTiles - terrain')
OSM¶
InĀ [7]:
Copied!
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.add_image(img_tiles.OSM(),zoom)
# ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('OSM')
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.add_image(img_tiles.OSM(),zoom)
# ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('OSM')
Out[7]:
Text(0.5, 1.0, 'OSM')
spoofed OSM¶
InĀ [8]:
Copied!
#https://stackoverflow.com/questions/77248120/stamen-terrain-map-not-working-in-cartopy
def image_spoof(self, tile):
import io
from urllib.request import urlopen, Request
from PIL import Image
url = self._image_url(tile) # get the url of the street map API
req = Request(url) # start request
req.add_header('User-agent','Anaconda 3')
fh = urlopen(req)
im_data = io.BytesIO(fh.read())
fh.close()
img = Image.open(im_data)
img = img.convert(self.desired_tile_form) # set image format
return img, self.tileextent(tile), 'lower' # reformat for cartopy
img_tiles.OSM.get_image = image_spoof # reformat web request for street map spoofing
tile_image = img_tiles.OSM() # spoofed, downloaded street map
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.add_image(tile_image,zoom)
# ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('spoofed OSM')
#https://stackoverflow.com/questions/77248120/stamen-terrain-map-not-working-in-cartopy
def image_spoof(self, tile):
import io
from urllib.request import urlopen, Request
from PIL import Image
url = self._image_url(tile) # get the url of the street map API
req = Request(url) # start request
req.add_header('User-agent','Anaconda 3')
fh = urlopen(req)
im_data = io.BytesIO(fh.read())
fh.close()
img = Image.open(im_data)
img = img.convert(self.desired_tile_form) # set image format
return img, self.tileextent(tile), 'lower' # reformat for cartopy
img_tiles.OSM.get_image = image_spoof # reformat web request for street map spoofing
tile_image = img_tiles.OSM() # spoofed, downloaded street map
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.add_image(tile_image,zoom)
# ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('spoofed OSM')
Out[8]:
Text(0.5, 1.0, 'spoofed OSM')
spoofed QuadtreeTiles¶
InĀ [9]:
Copied!
img_tiles.QuadtreeTiles.get_image = image_spoof # reformat web request for street map spoofing
tile_image = img_tiles.QuadtreeTiles() # spoofed, downloaded street map
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.add_image(tile_image,zoom)
# ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('spoofed QuadtreeTiles')
img_tiles.QuadtreeTiles.get_image = image_spoof # reformat web request for street map spoofing
tile_image = img_tiles.QuadtreeTiles() # spoofed, downloaded street map
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.add_image(tile_image,zoom)
# ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('spoofed QuadtreeTiles')
Out[9]:
Text(0.5, 1.0, 'spoofed QuadtreeTiles')
InĀ [10]:
Copied!
img_tiles.StadiaMapsTiles.get_image = image_spoof # reformat web request for street map spoofing
tile_image = img_tiles.StadiaMapsTiles(style='alidade_smooth', apikey='3f2c1505-df0e-4ac6-940c-83febd5cd7c5') # spoofed, downloaded street map
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.add_image(tile_image,zoom)
# ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('spoofed StadiaMapsTiles - alidade_smooth')
img_tiles.StadiaMapsTiles.get_image = image_spoof # reformat web request for street map spoofing
tile_image = img_tiles.StadiaMapsTiles(style='alidade_smooth', apikey='3f2c1505-df0e-4ac6-940c-83febd5cd7c5') # spoofed, downloaded street map
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.add_image(tile_image,zoom)
# ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('spoofed StadiaMapsTiles - alidade_smooth')
Out[10]:
Text(0.5, 1.0, 'spoofed StadiaMapsTiles - alidade_smooth')
stamen_terrain¶
InĀ [11]:
Copied!
img_tiles.StadiaMapsTiles.get_image = image_spoof #
tile_image = img_tiles.StadiaMapsTiles(style='stamen_terrain', apikey='3f2c1505-df0e-4ac6-940c-83febd5cd7c5') # spoofed, downloaded street map
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.add_image(tile_image,zoom)
# ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('spoofed StadiaMapsTiles - stamen_terrain')
img_tiles.StadiaMapsTiles.get_image = image_spoof #
tile_image = img_tiles.StadiaMapsTiles(style='stamen_terrain', apikey='3f2c1505-df0e-4ac6-940c-83febd5cd7c5') # spoofed, downloaded street map
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.add_image(tile_image,zoom)
# ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('spoofed StadiaMapsTiles - stamen_terrain')
Out[11]:
Text(0.5, 1.0, 'spoofed StadiaMapsTiles - stamen_terrain')
osm_bright¶
InĀ [12]:
Copied!
img_tiles.StadiaMapsTiles.get_image = image_spoof #
tile_image = img_tiles.StadiaMapsTiles(style='osm_bright', apikey='3f2c1505-df0e-4ac6-940c-83febd5cd7c5') # spoofed, downloaded street map
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.add_image(tile_image,zoom)
# ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('spoofed StadiaMapsTiles - osm_bright')
img_tiles.StadiaMapsTiles.get_image = image_spoof #
tile_image = img_tiles.StadiaMapsTiles(style='osm_bright', apikey='3f2c1505-df0e-4ac6-940c-83febd5cd7c5') # spoofed, downloaded street map
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": ccrs.PlateCarree()})
ax.set_extent(extent)
ax.add_image(tile_image,zoom)
# ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.set_title('spoofed StadiaMapsTiles - osm_bright')
Out[12]:
Text(0.5, 1.0, 'spoofed StadiaMapsTiles - osm_bright')
InĀ [13]:
Copied!
# or a full reference on the styles available please see
# https://docs.stadiamaps.com/themes/.
# or a full reference on the styles available please see
# https://docs.stadiamaps.com/themes/.