Map Visualization


Installation


pip install geopandas
sudo apt-get install libproj-dev proj-data proj-bin  
sudo apt-get install libgeos-dev
pip install cython cartopy
pip install Cartopy Cython
pip install geoplot
    

Basic Code


import geopandas as gpd
import geoplot as gpt

ePATH = './Electoral_Divisions_-_OSi_National_Statutory_Boundaries.geojson'
df_places = gpd.read_file(ePATH)
df_places.plot() # plot the whole map defined by geojson
df_places['geometry'][0] # only plot one polygon
df_places['Count_Placeholder'] = np.random.randint(10,size=df_places.shape[0])

gpt.choropleth(df_places,hue=df_places['Count_Placeholder'],cmap='Greens') # plot each polygon with a color under colormap
    

Read GEOJSON with json

To only read coordinates from geojson,


import json
with open('a.geojson') as f:
    data = json.load(f)
coors = data['features'][0]['geometry']['coordinates']
    

Plot polygons with matplotlib

This method works with provided vertices' coordinates, in a format of lists [[[x1,y1],[x1,y2],[x2,y2],[x2,y1],...]],


import geojson
import matplotlib.pyplot as plt
from descartes import PolygonPatch

BLUE = '#6699cc'
fig = plt.figure() 
ax = fig.gca() 
ax.add_patch(PolygonPatch(geojson.Polygon(coors)
    ,fc=BLUE, ec=BLUE,alpha=0.5,zorder=2 ))
ax.add_patch(PolygonPatch(\
    geojson.Polygon([[[xmin,ymin],[xmin,ymax],
                    [xmax,ymax],[xmax,ymin]]])
    ,fc=BLUE, ec=BLUE,alpha=0.5,zorder=2 ))
ax.axis('scaled')
plt.show()
    

References


  1. FreeMapTools
  2. Mapping in Python with geopandas
  3. Geographic Data with Basemap
  4. Mapping with Matplotlib, Pandas, Geopandas and Basemap in Python
  5. Mapping Geograph Data in Python
  6. Leaflet an open-source Javascript library for mobile-friendly interactive maps
  7. Folium Marker Clusters
  8. Leaflet Plugins
  9. Plotting with Geoplot and GeoPandas
  10. How to plot geo-data using matplotlib/python