Visualizing GeoJSON data using GeoPandas and Python

Every second, a huge amount of information appears in the world, which in most cases is impossible for a person to process and understand. Thanks to data visualization (drawing graphs, charts and mapping data), people can analyze information, draw conclusions and make forecasts.

The author of this article is Dr. Saumen Atta, postdoctoral fellow at the Laboratory of Mathematical Sciences at the University of Nantes, France. You can check out his blog at link. You can find the original article here.

Geo JSON is an open format for storing geographic data structures based on JSON. (Wikipedia)

In this tutorial, we will learn how to visualize GeoJSON data using the Python GeoPandas library using the data available from this link. The provided GeoJSON file corresponds to the Calcutta Municipal Corporation map data with information about each area.

Importing the GeoPandas Package

We can import the GeoPandas package using the following command:

import geopandas as gpd

If you don’t have the GeoPandas package installed on your system, you can use the following pip command:

pip install geopandas

Reading and processing a GeoJSON file

We are now ready to read the data from the GeoJSON file. This can be done with the following command:

The data provided contains information about the districts of the Calcutta Municipal Corporation in West Bengal, India.

In the current guide, the data is used for educational purposes only. The author of the tutorial does not check the authenticity and correctness of the information in the GeoJSON file.

Building a map using GeoJSON data

It is very easy to build a GeoJSON file using the GeoPandas library. The command is shown below:

df.plot()

The above command will show the following figure:

We can also pass a few parameters to the plot() function to change how the map looks like this:

df.plot(figsize=(10,10), edgecolor=’purple’, facecolor=’green’)

So the map will look like this:

Parsing a GeoJSON file

We can also parse the GeoJSON file by defining the shape of the input as follows:

df.shape

The output will be a tuple shown below:

(141, 2)

Therefore, the kolkata.geojson file has 141 rows and 2 columns (i.e. attributes). This also means that the municipality of Kolkata has 141 districts according to the input data file.

We can also view the first five lines of the input as follows:

The result is shown below:

We see that the attribute names are “WARD” and “geometry”.

More options for building visualizations

It is possible that we want to draw only the boundaries of the districts. This is easy to do with the following command:

df.boundary.plot(figsize=(10,10))

The result is shown below:

If we want to draw the boundary of a certain area, say district number 23 in the municipality of Kolkata, we can use the following commands:

df_23 = df.loc[df[‘WARD’] == ’23’]

df_23.boundary.plot(figsize=(10,10))

Also, we can also use alternative commands like this:

df_23 = df[df.WARD==‘23’]

df_23.boundary.plot(figsize=(10,10))

The resulting map is shown below:

So, we learned how to extract data from a GeoJSON file using the GeoPandas Python library, and we also learned how to display GeoJSON data in various ways.

Note

In addition to translating this article, I was puzzled to provide a fully working code. But the following difficulties arose:

1. The geopandas module was not installed. The command “pip install geopandas” gave an error. Having studied the information, I found out that the installation of this library requires the preliminary installation of the following modules:

• pip install wheel

• pip install pipwin

• pipwin install numpy

• pipwin install pandas

• pipwin install shapely

• pipwin install gdal

• pipwin install fiona

• pipwin install pyproj

• pipwin install rtree

• pip install geopandas

and only after that the code earned.

1. Despite the fact that the code worked, the picture with the image of the card was not displayed. To do this, enter at the end of the code: “plt.show()”. And before that, you need to install the matplotlib module using the “pip install matplotlib” command, and then import it.

2. To output data from df.head(), I used the print() function: “print(df.head())”.

I hope these comments will help you get better acquainted with the geopandas library and its benefits. And also I provide a link to github.