Locally Reverse Geocode Indian States

A step by step guide of how to reverse geo-code a latitude and longitude pair to get corresponding India State

Probhakar
5 min readAug 17, 2020
Photo by chuttersnap on Unsplash

Hello, folks. Hope everything is going well.

Today I am going to discuss one interesting topic. Suppose you have 40,000 -50,000 co-ordinates you want to know the State. The first thought would be to use APIs provided by Google or Here maps. But unfortunately, the free quota has a very small number of API hits possible.

What should you do in that case? I was in that similar situation where I needed to reverse geo-code half a lakh points every day to get which Indian State it is from. I was searching frantically. Fortunately, I came to know the concept of shapefile. Using shapefile you can locally reverse geo-code easily and an efficient way.

So what is shapefile 🤔?

A shapefile is a set of files in which the geometry of a region is defined in terms of polygon, line, points. The file structure can be like below

|
|--.shp
|--.dbf
|--.shx
|--.prj

Now why the heck there are so many files? Well, let’s go one by one.

.shp: This the file where the vector data is stored in terms of polygon or lines but not mixed.

.dbf: It is the attribute file. For example, you can have one polygon representing the state of West Bengal. Now to know it is west Bengal, you need to store proper attribute regarding that polygon, .dbf file is for that.

.shx: This is the glue between .shp and .dbf file.

.prj: This is a projection file, this projects the vector data to appropriate co-ordinates.

Now that we have an idea, let’s get to the main point. Head on to the below website to download the shapefile for Indian states.

https://www.igismap.com/download-india-boundary-shapefile-free-states-boundary-assembly-constituencies-village-boundaries/#:~:text=country%20boundary%20line.-,Download%20India%20Boundary%20shapefile%20free,CRS%20(Coordinate%20Reference%20System).

Here you can see a plethora of shapefiles like “India Assembly Constituencies Shapefile” and many more. We are going with “Indian states boundary shapefile”. Click on the link and select export and choose SHP from the dropdown. it will send one link to your email id. Download and extract the file in your working directory.

Your working directory should look like this

|
|--main.ipynb
|--Igismap
|--Indian_States.dbf
|--Indian_States.prj
|--Indian_States.prj
|--Indian_States.shx

I am using Jupyter notebook with Python 3 for this project, you can stick to it to avoid any unnecessary error. I am also assuming you have some working knowledge of Python and Jupyter Notebook. Also, pip is configured for your machine. Open the command prompt and insert

pip install pyshp

This is a python library to handle shapefile, let’s play with the shapefile we just downloaded.

import the library and read the shapefile —

import shapefile
shape_file = shapefile.Reader("Igismap/Indian_States.shp")

Now let’s explore what’s there in the shapefile.

print(shape_file)

It prints

Output

Okay, now you are getting to understand. The shapefile consists of 36 polygons. And why 36? Because 28 states and 8 union territories, makes this total of 36 entities. We can view the attributes also —

shape_file.records()

And you will get all the records

[Record #0: ['Andaman & Nicobar Island'],
Record #1: ['Arunanchal Pradesh'],
Record #2: ['Assam'],
Record #3: ['Bihar'],
Record #4: ['Chandigarh'],
Record #5: ['Chhattisgarh'],
Record #6: ['Dadara & Nagar Havelli'],
Record #7: ['Daman & Diu'],
Record #8: ['Goa'],
Record #9: ['Gujarat'],
Record #10: ['Haryana'],
Record #11: ['Himachal Pradesh'],
Record #12: ['Jammu & Kashmir'],
Record #13: ['Jharkhand'],
Record #14: ['Karnataka'],
Record #15: ['Kerala'],
Record #16: ['Lakshadweep'],
Record #17: ['Madhya Pradesh'],
Record #18: ['Maharashtra'],
Record #19: ['Manipur'],
Record #20: ['Meghalaya'],
Record #21: ['Mizoram'],
Record #22: ['Nagaland'],
Record #23: ['NCT of Delhi'],
Record #24: ['Puducherry'],
Record #25: ['Punjab'],
Record #26: ['Rajasthan'],
Record #27: ['Sikkim'],
Record #28: ['Tamil Nadu'],
Record #29: ['Telangana'],
Record #30: ['Tripura'],
Record #31: ['Uttar Pradesh'],
Record #32: ['Uttarakhand'],
Record #33: ['West Bengal'],
Record #34: ['Odisha'],
Record #35: ['Andhra Pradesh']]

How beautiful isn’t it? 😁 Let’s plot the shapefile

%matplotlib inlineimport matplotlib.pyplot as pltplt.figure(figsize=(10,10))
for shape in shape_file.shapeRecords():
x = [i[0] for i in shape.shape.points[:]]
y = [i[1] for i in shape.shape.points[:]]
plt.scatter(x, y, s=1)
plt.show()

And the output will be out beloved motherland 💕

INDIA

Enough playing, now let's come back to reverse-coding. We have all the polygon data if we have any point (Latitude, Longitude) we can search for the polygon in which the point lies, holla…Out job will be done.

Thankfully one guy already did this, you can find the repository at https://github.com/gka. But the issue is that it’s written for Python 2. So I just modified it a little bit to accommodate in Python 3. All credit goes to him and with due respect 🙏😀. It’s up to you.

You can insert in Jupyter notebook

! git clone https://github.com/epsi95/pyshpgeocode

or you can clone via command prompt or you download and extract, the ultimate file structure should be like

|
|--main.ipynb
|--pyshpgeocode
|--README.md
|--setup.py
|--shapegeocode.py
|--Igismap
|--Indian_States.dbf
|--Indian_States.prj
|--Indian_States.prj
|--Indian_States.shx

Now that all setup, import the module as

import pyshpgeocode.shapegeocode as shapegeocode

Read the shapefile —

gc = shapegeocode.geocoder(‘Igismap/Indian_States.shp’)

Yey, all done. Now you just insert the (Latitude, Longitude) and enjoy the reverse geo-coded State 😁

gc.geocode(22.5726, 88.3639)
Reverse Geo-Coded State

I hope this article was helpful. If you like it, well I am happy you like it 😅.

For now, bubye 😊

Peace 😎!

--

--

Responses (1)