#!/usr/bin/env python # coding: utf-8 # # Geographic projections # # Here, we'll explore what Python libraries give us map projections. Most libraries that include some form of mapping rely on Google Maps or Open Street Map as a backend that supplies the map tiles, which are displayed using the Mercator projection only. But the world isn't flat, and so when zoomed out enough this massively distorts land area. A projection is a way to take the spherical geometry of the Earth and project it onto a flat surface, i.e. our screens. # # Our adventure pretty much has to begin with Basemap. # In[1]: import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap from IPython.display import set_matplotlib_formats # In[2]: from mpl_toolkits import basemap print(basemap.supported_projections) # In[3]: # create a Robinson projection that's centered around # lon=0, the prime meridian m = Basemap(projection='robin', lon_0=0.) fig = plt.figure(figsize=(20,10)) # background color of grey fig.patch.set_facecolor('#e6e8ec') # draw white with no boundary line m.drawmapboundary(color='none', fill_color='white') # draw continents and countries, with the coastline adding extra definition #m.drawcoastlines(color='white') m.fillcontinents(color='black', lake_color='white') m.drawcountries(linewidth=1, color='white') # In[4]: plt.show() # In[15]: import geopandas world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres')) world.head() # In[6]: # world = world[(world.name!="Antarctica")] mercator = '+proj=merc' robinson = '+proj=robin' wgs84 = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs' # In[16]: # robinson projection worldRobinson = world.to_crs(robinson) worldRobinson.plot(figsize=(20,20), color="black", linewidth=1, edgecolor="white",) plt.axis('off') plt.show() # ### References # Chris Roach's talk about Basemap: https://www.youtube.com/watch?v=ZIEyHdvF474 # # ...and his GitHub repo of the same: https://github.com/croach/oreilly-matplotlib-course/blob/master/07%20-%20Mapping%20in%20matplotlib/0702%20-%20Working%20With%20Maps.ipynb #