If you are data analyst or data scientist working with python, then probably you are already familiar with pandas, but if you are entering a spatial domain data analytics you should check out GeoPandas. It extends panda’s capabilities by adding spatial operations and geometric types. In this blog I will focus on two aspects of GeoPandas: visualization and simple spatial analytics.
Let’s start from notebook output area setup, so that we can see the entire map, without vertical scrollbars.
%%javascript
IPython.OutputArea.prototype._should_scroll = function(lines) {
return false;
}
import geopandas as gpd
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
Data used in this post is open source and comes from envirosolutions web page. Similar to pandas, geopandas allows you to easily read and write from spatial formats like: Shapefile, GeoJson, postgis. It is mostly achieved thanks to Fiona which is used here, similarly like Shapely (used for geometry format storage and spatial data analytics) or matplotlib (used for data visualization).
#https://www.envirosolutions.pl/otwarte-dane.html
gdf_admin=gpd.read_file('admins/admin_gen.shp')
gdf_roads_main=gpd.read_file('roads/roads_main_gen.shp')
gdf_rivers_main=gpd.read_file('rivers/rivers_main_gen.shp')
First method for geometry display is using matplotlib.pyplot, which was already disscused in this post : Matplotlib: Lines, bars, pie charts and some other stuff
fig, ax = plt.subplots(1, 1,figsize=(10,20))
plt.title('Administrative division of Poland')
gdf_admin.plot(column='JPT_NAZWA_',
ax=ax,
legend=True,
cmap='tab20',
legend_kwds={'bbox_to_anchor':(1.3,1),
'title': 'Woiwodschaps',
'title_fontsize':12,
'frameon': False}
);
Other option for geometry display is interactive map based on folium/leaflet.js, here you can easily display your geometry with underlying layer (i.e map or aerial imagery)
m=gdf_admin.explore(column='JPT_NAZWA_',tooltip=False)
m
gdf_roads_main[gdf_roads_main.fclass=='motorway'].explore(m=m)