import geopandas, pandas
import rioxarray, xarray
import contextily
import datashader as ds
import matplotlib.pyplot as plt
contextily.__version__
'1.1.0'
dem_url = (
"https://geographicdata.science/book/_downloads/"\
"9d58bbebb7e01b211e0c32c32735aaa1/nasadem_sd.tif"
)
dem_url
'https://geographicdata.science/book/_downloads/9d58bbebb7e01b211e0c32c32735aaa1/nasadem_sd.tif'
dem = xarray.open_rasterio(dem_url)\
.rio.reproject("EPSG:3310")
w = 1280
h = w * dem.shape[1] // dem.shape[2]
agg = ds.Canvas(
plot_height=h, plot_width=w
).raster(dem.where(dem>0).sel(band=1))
/opt/conda/lib/python3.8/site-packages/pyproj/crs/crs.py:280: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6 projstring = _prepare_from_string(projparams)
f, ax = plt.subplots(1, figsize=(16, 8))
agg.plot(ax = ax)
# Add Basemap
contextily.add_basemap(
ax,
crs = dem.rio.crs,
source = contextily.providers.Stamen.TerrainBackground
)
# Add labels
contextily.add_basemap(
ax,
crs = dem.rio.crs,
source = contextily.providers.CartoDB.VoyagerOnlyLabels
)
ax.set_title("Elevation in San Diego (CA) in metres");
plt.savefig("dem.png", dpi=300)
db_url = (
"https://geographicdata.science/book/_downloads/"\
"7fb86b605af15b3c9cbd9bfcbead23e9/tokyo_clean.csv"
)
db_url
'https://geographicdata.science/book/_downloads/7fb86b605af15b3c9cbd9bfcbead23e9/tokyo_clean.csv'
db = pandas.read_csv(db_url)
ax = db.plot.scatter(
"longitude", "latitude", s=0.5, color="k", figsize=(12, 12)
)
contextily.add_basemap(
ax,
crs = "EPSG:4326",
source = contextily.providers.Stamen.TonerLabels,
zoom = 11
)
ax.set_axis_off()
plt.savefig("points.png", dpi=300)
nl = contextily.Place(
"Korea",
source=contextily.providers.NASAGIBS.ViirsEarthAtNight2012
)
nl_labels = contextily.Place(
"Korea",
source=contextily.providers.CartoDB.DarkMatterOnlyLabels
)
ax = nl.plot()
nl_labels.plot(ax=ax);
plt.savefig("lights.png", dpi=300)