Skip to content

Commit

Permalink
Feature/ctx sources deprecation (#517)
Browse files Browse the repository at this point in the history
* contextily update: replace sources with providers
- OSM_C -> OpenStreetMap.Mapnik
- ST_TERRAIN -> Stamen.Terrain

* contextily update: replace sources with providers

* eca_san_salvador: replace sources with providers

* contextily.sources has been purged from the code base
  • Loading branch information
emanuel-schmid authored Jul 20, 2022
1 parent e766242 commit 373ecae
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 39 deletions.
9 changes: 5 additions & 4 deletions climada/engine/impact.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import warnings
import datetime as dt
from itertools import zip_longest
import contextily as ctx
import numpy as np
from scipy import sparse
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -586,7 +587,7 @@ def plot_raster_eai_exposure(self, res=None, raster_res=None, save_tiff=None,

def plot_basemap_eai_exposure(self, mask=None, ignore_zero=False, pop_name=True,
buffer=0.0, extend='neither', zoom=10,
url='http://tile.stamen.com/terrain/tileZ/tileX/tileY.png',
url=ctx.providers.Stamen.Terrain,
axis=None, **kwargs):
"""Plot basemap expected annual impact of each exposure.
Expand All @@ -607,7 +608,7 @@ def plot_basemap_eai_exposure(self, mask=None, ignore_zero=False, pop_name=True,
zoom : int, optional
zoom coefficient used in the satellite image
url : str, optional
image source, e.g. ctx.sources.OSM_C
image source, e.g. ctx.providers.OpenStreetMap.Mapnik
axis : matplotlib.axes.Axes, optional
axis to use
kwargs : dict, optional
Expand Down Expand Up @@ -677,7 +678,7 @@ def plot_hexbin_impact_exposure(self, event_id=1, mask=None, ignore_zero=False,

def plot_basemap_impact_exposure(self, event_id=1, mask=None, ignore_zero=False,
pop_name=True, buffer=0.0, extend='neither', zoom=10,
url='http://tile.stamen.com/terrain/tileZ/tileX/tileY.png',
url=ctx.providers.Stamen.Terrain,
axis=None, **kwargs):
"""Plot basemap impact of an event at each exposure.
Requires attribute imp_mat.
Expand All @@ -702,7 +703,7 @@ def plot_basemap_impact_exposure(self, event_id=1, mask=None, ignore_zero=False,
zoom : int, optional
zoom coefficient used in the satellite image
url : str, optional
image source, e.g. ctx.sources.OSM_C
image source, e.g. ctx.providers.OpenStreetMap.Mapnik
axis : matplotlib.axes.Axes, optional
axis to use
kwargs : dict, optional
Expand Down
4 changes: 2 additions & 2 deletions climada/entity/exposures/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ def plot_raster(self, res=None, raster_res=None, save_tiff=None,

def plot_basemap(self, mask=None, ignore_zero=False, pop_name=True,
buffer=0.0, extend='neither', zoom=10,
url='http://tile.stamen.com/terrain/{z}/{x}/{y}.png',
url=ctx.providers.Stamen.Terrain,
axis=None, **kwargs):
"""Scatter points over satellite image using contextily
Expand All @@ -749,7 +749,7 @@ def plot_basemap(self, mask=None, ignore_zero=False, pop_name=True,
zoom : int, optional
zoom coefficient used in the satellite image
url : str, optional
image source, e.g. ctx.sources.OSM_C
image source, e.g. ctx.providers.OpenStreetMap.Mapnik
axis : matplotlib.axes._subplots.AxesSubplot, optional
axis to use
kwargs : optional
Expand Down
2 changes: 1 addition & 1 deletion climada/test/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_ctx_osm_pass(self):
myexp.check()

try:
myexp.plot_basemap(url=ctx.sources.OSM_A)
myexp.plot_basemap(url=ctx.providers.OpenStreetMap.Mapnik)
except urllib.error.HTTPError:
self.assertEqual(1, 0)

Expand Down
20 changes: 3 additions & 17 deletions doc/tutorial/climada_entity_Exposures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1556,21 +1556,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"2021-06-04 17:07:51,154 - climada.entity.exposures.base - INFO - Setting latitude and longitude attributes.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/zeliestalhanske/miniconda3/envs/climada_env/lib/python3.8/site-packages/contextily/tile.py:265: FutureWarning: The url format using 'tileX', 'tileY', 'tileZ' as placeholders is deprecated. Please use '{x}', '{y}', '{z}' instead.\n",
" warnings.warn(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"2021-06-04 17:07:51,154 - climada.entity.exposures.base - INFO - Setting latitude and longitude attributes.\n",
"2021-06-04 17:07:55,046 - climada.entity.exposures.base - INFO - Setting latitude and longitude attributes.\n",
"2021-06-04 17:07:55,124 - climada.entity.exposures.base - INFO - Setting latitude and longitude attributes.\n",
"2021-06-04 17:07:58,604 - climada.entity.exposures.base - INFO - Setting latitude and longitude attributes.\n"
Expand Down Expand Up @@ -1604,9 +1590,9 @@
"source": [
"# Example 4: plot_basemap method\n",
"import contextily as ctx\n",
"# select the background image from the available ctx.sources\n",
"# select the background image from the available ctx.providers\n",
"ax = exp_templ.plot_basemap(buffer=30000, cmap='brg'); # using open street map\n",
"ax = exp_templ.plot_basemap(buffer=30000, url=ctx.sources._T_WATERCOLOR, cmap='brg', zoom=9); # set image zoom"
"ax = exp_templ.plot_basemap(buffer=30000, url=ctx.providers.Stamen.Watercolor, cmap='brg', zoom=9); # set image zoom"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion requirements/env_climada.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dependencies:
- bottleneck>=1.3
- cartopy>=0.20.0
- cfgrib>=0.9.7,<0.9.10 # 0.9.10 cannot read the icon_grib files from https://opendata.dwd.de
- contextily>=1.0,<1.2 # 1.2 has removed the deprecated contextily.sources
- contextily>=1.0
- dask>=2.25
- gdal!=3.4.1 # has an issue with fiona on Windows (#403)
- geopandas>=0.8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"output_type": "stream",
"text": [
"/tmp/ipykernel_6587/3558672532.py:11: FutureWarning: The \"contextily.tile_providers\" module is deprecated and will be removed in contextily v1.1. Please use \"contextily.providers\" instead.\n",
" ax = ent_2015.exposures.plot_basemap(s=1, zoom=15, url=ctx.sources.OSM_A, vmax=60000, cmap='autumn')\n"
" ax = ent_2015.exposures.plot_basemap(s=1, zoom=15, url=ctx.providers.OpenStreetMap.Mapnik, vmax=60000, cmap='autumn')\n"
]
},
{
Expand Down Expand Up @@ -134,7 +134,7 @@
"\n",
"# Exposures (bienes): los utilizados en el script San Salvador Risk\n",
"print('Total value in 2015: {:.3e}'.format(ent_2015.exposures.gdf.value.sum()))\n",
"ax = ent_2015.exposures.plot_basemap(s=1, zoom=15, url=ctx.sources.OSM_A, vmax=60000, cmap='autumn')\n",
"ax = ent_2015.exposures.plot_basemap(s=1, zoom=15, url=ctx.providers.OpenStreetMap.Mapnik, vmax=60000, cmap='autumn')\n",
"ax.set_title('Exposure 2015')\n",
"\n",
"# Impact Functions (funciones de impacto): los utilizados en el script San Salvador Risk\n",
Expand Down Expand Up @@ -183,7 +183,7 @@
"output_type": "stream",
"text": [
"/tmp/ipykernel_6587/2982039758.py:13: FutureWarning: The \"contextily.tile_providers\" module is deprecated and will be removed in contextily v1.1. Please use \"contextily.providers\" instead.\n",
" ax = ent_2040.exposures.plot_basemap(s=1, zoom=15, url=ctx.sources.OSM_A, vmax=60000, cmap='autumn')\n"
" ax = ent_2040.exposures.plot_basemap(s=1, zoom=15, url=ctx.providers.OpenStreetMap.Mapnik, vmax=60000, cmap='autumn')\n"
]
},
{
Expand Down Expand Up @@ -234,7 +234,7 @@
"ent_2040.check() # check values are well set and assignes default values\n",
"\n",
"print('Valor total en 2040: {:.3e}'.format(ent_2040.exposures.gdf.value.sum()))\n",
"ax = ent_2040.exposures.plot_basemap(s=1, zoom=15, url=ctx.sources.OSM_A, vmax=60000, cmap='autumn')\n",
"ax = ent_2040.exposures.plot_basemap(s=1, zoom=15, url=ctx.providers.OpenStreetMap.Mapnik, vmax=60000, cmap='autumn')\n",
"ax.set_title('Exposure 2040');"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"\n",
"# Exposures: the ones we used in San Salvador Risk script\n",
"print('Total value in 2015: {:.3e}'.format(ent_2015.exposures.gdf.value.sum()))\n",
"ax = ent_2015.exposures.plot_basemap(s=1, zoom=15, url=ctx.sources.OSM_A, vmax=60000, cmap='autumn')\n",
"ax = ent_2015.exposures.plot_basemap(s=1, zoom=15, url=ctx.providers.OpenStreetMap.Mapnik, vmax=60000, cmap='autumn')\n",
"ax.set_title('Exposure 2015')\n",
"\n",
"# Impact Functions: the ones we used in San Salvador Risk script\n",
Expand Down Expand Up @@ -169,7 +169,7 @@
"ent_2040.check() # check values are well set and assignes default values\n",
"\n",
"print('Total value in 2040: {:.3e}'.format(ent_2040.exposures.gdf.value.sum()))\n",
"ax = ent_2040.exposures.plot_basemap(s=1, zoom=15, url=ctx.sources.OSM_A, vmax=60000, cmap='autumn')\n",
"ax = ent_2040.exposures.plot_basemap(s=1, zoom=15, url=ctx.providers.OpenStreetMap.Mapnik, vmax=60000, cmap='autumn')\n",
"ax.set_title('Exposure 2040');"
]
},
Expand Down
12 changes: 6 additions & 6 deletions script/applications/eca_san_salvador/San_Salvador_Risk.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@
"output_type": "stream",
"text": [
"<ipython-input-17-bac194a48607>:2: FutureWarning: The \"contextily.tile_providers\" module is deprecated and will be removed in contextily v1.1. Please use \"contextily.providers\" instead.\n",
" imp_acel.plot_basemap_eai_exposure(url=ctx.sources.OSM_C, zoom=15, s=2, cmap='gnuplot')\n"
" imp_acel.plot_basemap_eai_exposure(url=ctx.providers.OpenStreetMap.Mapnik, zoom=15, s=2, cmap='gnuplot')\n"
]
},
{
Expand Down Expand Up @@ -796,7 +796,7 @@
],
"source": [
"import contextily as ctx\n",
"imp_acel.plot_basemap_eai_exposure(url=ctx.sources.OSM_C, zoom=15, s=2, cmap='gnuplot');"
"imp_acel.plot_basemap_eai_exposure(url=ctx.providers.OpenStreetMap.Mapnik, zoom=15, s=2, cmap='gnuplot');"
]
},
{
Expand All @@ -809,7 +809,7 @@
"output_type": "stream",
"text": [
"<ipython-input-18-bd8773f4f87c>:2: FutureWarning: The \"contextily.tile_providers\" module is deprecated and will be removed in contextily v1.1. Please use \"contextily.providers\" instead.\n",
" ax = imp_acel.plot_basemap_eai_exposure(mask=np.argwhere((exp_acel.gdf.category==2).to_numpy()).reshape(-1), url=ctx.sources.OSM_C, zoom=15, s=2, cmap='gnuplot')\n"
" ax = imp_acel.plot_basemap_eai_exposure(mask=np.argwhere((exp_acel.gdf.category==2).to_numpy()).reshape(-1), url=ctx.providers.OpenStreetMap.Mapnik, zoom=15, s=2, cmap='gnuplot')\n"
]
},
{
Expand Down Expand Up @@ -837,7 +837,7 @@
],
"source": [
"import numpy as np\n",
"ax = imp_acel.plot_basemap_eai_exposure(mask=np.argwhere((exp_acel.gdf.category==2).to_numpy()).reshape(-1), url=ctx.sources.OSM_C, zoom=15, s=2, cmap='gnuplot')\n",
"ax = imp_acel.plot_basemap_eai_exposure(mask=np.argwhere((exp_acel.gdf.category==2).to_numpy()).reshape(-1), url=ctx.providers.OpenStreetMap.Mapnik, zoom=15, s=2, cmap='gnuplot')\n",
"ax.set_title('Expected Annual Impact - no AUP');"
]
},
Expand All @@ -851,7 +851,7 @@
"output_type": "stream",
"text": [
"<ipython-input-19-c305943236c7>:1: FutureWarning: The \"contextily.tile_providers\" module is deprecated and will be removed in contextily v1.1. Please use \"contextily.providers\" instead.\n",
" ax = imp_acel.plot_basemap_eai_exposure(mask=np.argwhere((exp_acel.gdf.category==1).to_numpy()).reshape(-1), url=ctx.sources.OSM_C, zoom=15, s=2, cmap='gnuplot')\n"
" ax = imp_acel.plot_basemap_eai_exposure(mask=np.argwhere((exp_acel.gdf.category==1).to_numpy()).reshape(-1), url=ctx.providers.OpenStreetMap.Mapnik, zoom=15, s=2, cmap='gnuplot')\n"
]
},
{
Expand All @@ -878,7 +878,7 @@
}
],
"source": [
"ax = imp_acel.plot_basemap_eai_exposure(mask=np.argwhere((exp_acel.gdf.category==1).to_numpy()).reshape(-1), url=ctx.sources.OSM_C, zoom=15, s=2, cmap='gnuplot')\n",
"ax = imp_acel.plot_basemap_eai_exposure(mask=np.argwhere((exp_acel.gdf.category==1).to_numpy()).reshape(-1), url=ctx.providers.OpenStreetMap.Mapnik, zoom=15, s=2, cmap='gnuplot')\n",
"ax.set_title('Expected Annual Impact - AUP');"
]
},
Expand Down
4 changes: 2 additions & 2 deletions script/applications/eca_san_salvador/functions_ss.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def plot_salvador_ma():
ax = shape.plot(figsize=(10, 10), alpha=0.5)
ax.set_xlim(-9943223.896891385, -9911000.065720687)
ax.set_ylim(1530712.637786494, 1555600.2891258441)
ctx.add_basemap(ax, zoom=12, url=ctx.sources.ST_TERRAIN)
ctx.add_basemap(ax, zoom=12, url=ctx.providers.Stamen.Terrain)
rect = patches.Rectangle((-9931038.907412536, 1536570.51725147), 4354.653554389253,
2941.9125608841423, linewidth=1, edgecolor='r', facecolor='none')
ax.add_patch(rect)
Expand Down Expand Up @@ -148,7 +148,7 @@ def plot_exposure_ss(exposures, point=None):
plt.legend(lines_legend, text_legend, numpoints=1, loc=3, title='AUP housing')
plt.gca().add_artist(legend1)

ctx.add_basemap(ax, zoom=15, url=ctx.sources.OSM_C, origin='upper')
ctx.add_basemap(ax, zoom=15, url=ctx.providers.OpenStreetMap.Mapnik, origin='upper')
scale_bar(ax, 0.5, location=(0.93, 0.4), linewidth=2)
rect = patches.Rectangle((-9931033.307412536, 1536686.51725147), 4345.053554389253,
2934.0125608841423, linewidth=2, edgecolor='r', facecolor='none', zorder=200)
Expand Down

0 comments on commit 373ecae

Please sign in to comment.