Tuesday, February 4, 2020

Working with GeoJSON and GeoPandas

GeoJSON is an extension of regular JSON data structure that supports geographic/geometry types, such as: Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection.

GeoPandas is an open source project to make working with geospatial data in python easier. GeoPandas extends the datatypes used by pandas to allow spatial operations on geometric types, (geopandas.org, 2020).

Typically, GeoPandas is used to read GeoJSON data into a DataFrame as seen below. This is same process you will read regular JSON into Pandas dataframe.

import geopandas as gpd
nigerian_states = gpd.read_file(r"C:\Users\Yusuf_08039508010\Desktop\ng_State.geojson")

Because geopandas works with geospatial data, you can easily plot the data which generates a plot of the GeoDataFrame with matplotlib.  If a column is specified, the plot coloring will be based on values in that column.

%matplotlib inline
nigerian_states.plot(column='state_name', legend=True, figsize=(15, 15))
%matplotlib inline: makes sure that the plot displays in jupyter notebook
column='state_name': color is achieved by specifying the column name
legend=True: legend is set to true to display it since default setting is false
figsize=(15, 15): figure can be altered.

The full map is as seen below...

Label the polygons with the state's name column.

%matplotlib inline
ax = nigerian_states.plot(column='state_name', legend=True, figsize=(15, 15))
map = nigerian_states.apply(lambda x: ax.annotate(s=x.state_name, xy=x.geometry.centroid.coords[0], ha='center'),axis=1)

Geopandas can read almost any vector-based spatial data format including ESRI shapefile, GeoJSON, KML, AutoCAD DXF/DWG, SpatialLite, GML, Geopackage files and more.

No comments:

Post a Comment