import numpy as np
import pandas as pd
import plotly.express as px
import json
import ogr
# import geopandas as gpd
#Method 1: Using OGR
#We used compressed shapefiles obtained from mapshaper.org
driver = ogr.GetDriverByName('ESRI Shapefile')
shp_path = 'shape_files\\India_States_2020_compressed\\India_states.shp'
data_source = driver.Open(shp_path, 0)
fc = {
'type': 'FeatureCollection',
'features': []
}
lyr = data_source.GetLayer(0)
for feature in lyr:
fc['features'].append(feature.ExportToJson(as_object=True))
with open('json_files\\India_States_2020_compressed.json', 'w') as f:
json.dump(fc, f)
# #Method 2: Using geopandas
# # set the filepath and load
# fp = "shape_files\\India_States_2020_compressed\\India_States.shp"
# #reading the file stored in variable fp
# map_df = gpd.read_file(fp)
# #Export it as GeoJSON
# map_df.to_file("json_files\\India_States_2020_compressed_gpd.json", driver='GeoJSON')
with open('json_files\\India_States_2020_compressed.json') as f:
India_states = json.load(f)
#Have a look at the features
India_states["features"][0].keys()
dict_keys(['type', 'geometry', 'properties', 'id'])
#type says that it is a 'Feature'
#geometry contains the coordinates for that feature
#properties contains the state name
#id is the index of the feature
#Let us check the state name for feature 0
India_states["features"][0]['properties']
{'dtname': 'North & Middle Andaman', 'stcode11': '35', 'dtcode11': '639', 'year_stat': '2011_c', 'Dist_LGD': 632, 'State_LGD': 35, 'JID': 178, 'state_name': 'ANDAMAN & NICOBAR', 'FID': 0}
#Let us look at just one location:
India_states["features"][0]['geometry']['coordinates'][0][0][0]
[93.7, 7.22]
#Load the csv file and check its contents. Make sure that there is one entry corresponding to each state in the geojson.
df = pd.read_csv('data/state_dummy_data_no_null.csv')
df.head()
index | st_nm | count | |
---|---|---|---|
0 | 0 | ANDAMAN & NICOBAR | 0 |
1 | 1 | ARUNACHAL PRADESH | 0 |
2 | 2 | ASSAM | 0 |
3 | 3 | BIHAR | 3 |
4 | 4 | CHANDIGARH | 0 |
max_value = df['count'].max()
fig = px.choropleth(df, geojson=India_states, locations='st_nm', color='count',
color_continuous_scale="Viridis",
range_color=(0, max_value),
featureidkey="properties.state_name",
projection="mercator"
)
fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
fig.write_image("images\\India_choropleth.png")
fig.write_html('html_files\\plotly_choropleth_demo.html')
#To make sure that you have entries corresponding to all states in geo-json, you can simply print out
#the state names of the geojson
#You many need to rename some states/UT of the df according to the geojson
#For example, the geo-json has New Delhi named as 'DELHI'
for i in range(0, len(India_states["features"])):
print(India_states["features"][i]["properties"]["state_name"])
ANDAMAN & NICOBAR ANDHRA PRADESH ARUNACHAL PRADESH ASSAM BIHAR CHANDIGARH CHHATTISGARH DADRA & NAGAR HAVE DAMAN & DIU DELHI GOA GUJARAT HARYANA HIMACHAL PRADESH JAMMU & KASHMIR JHARKHAND KARNATAKA KERALA LADAKH LAKSHADWEEP MADHYA PRADESH MAHARASHTRA MANIPUR MEGHALAYA MIZORAM NAGALAND ODISHA PUDUCHERRY PUNJAB RAJASTHAN SIKKIM TAMIL NADU TELANGANA TRIPURA UTTAR PRADESH UTTARAKHAND WEST BENGAL