{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import plotly.express as px\n", "import json\n", "import ogr\n", "# import geopandas as gpd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get GeoJSON from shapefile" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "#Method 1: Using OGR\n", "\n", "#We used compressed shapefiles obtained from mapshaper.org\n", "driver = ogr.GetDriverByName('ESRI Shapefile')\n", "shp_path = 'shape_files\\\\India_States_2020_compressed\\\\India_states.shp'\n", "data_source = driver.Open(shp_path, 0)\n", "\n", "fc = {\n", " 'type': 'FeatureCollection',\n", " 'features': []\n", " }\n", "\n", "lyr = data_source.GetLayer(0)\n", "for feature in lyr: \n", " fc['features'].append(feature.ExportToJson(as_object=True))\n", "\n", "with open('json_files\\\\India_States_2020_compressed.json', 'w') as f:\n", " json.dump(fc, f)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# #Method 2: Using geopandas\n", "\n", "# # set the filepath and load\n", "# fp = \"shape_files\\\\India_States_2020_compressed\\\\India_States.shp\"\n", "# #reading the file stored in variable fp\n", "# map_df = gpd.read_file(fp)\n", "# #Export it as GeoJSON\n", "# map_df.to_file(\"json_files\\\\India_States_2020_compressed_gpd.json\", driver='GeoJSON')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Open, understand and compress the GeoJSON" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "with open('json_files\\\\India_States_2020_compressed.json') as f:\n", " India_states = json.load(f)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['type', 'geometry', 'properties', 'id'])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Have a look at the features\n", "India_states[\"features\"][0].keys()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'dtname': 'North & Middle Andaman',\n", " 'stcode11': '35',\n", " 'dtcode11': '639',\n", " 'year_stat': '2011_c',\n", " 'Dist_LGD': 632,\n", " 'State_LGD': 35,\n", " 'JID': 178,\n", " 'state_name': 'ANDAMAN & NICOBAR',\n", " 'FID': 0}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#type says that it is a 'Feature'\n", "#geometry contains the coordinates for that feature\n", "#properties contains the state name\n", "#id is the index of the feature\n", "#Let us check the state name for feature 0\n", "India_states[\"features\"][0]['properties']" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[93.7, 7.22]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Let us look at just one location:\n", "India_states[\"features\"][0]['geometry']['coordinates'][0][0][0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load the data and create the visualization" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | index | \n", "st_nm | \n", "count | \n", "
---|---|---|---|
0 | \n", "0 | \n", "ANDAMAN & NICOBAR | \n", "0 | \n", "
1 | \n", "1 | \n", "ARUNACHAL PRADESH | \n", "0 | \n", "
2 | \n", "2 | \n", "ASSAM | \n", "0 | \n", "
3 | \n", "3 | \n", "BIHAR | \n", "3 | \n", "
4 | \n", "4 | \n", "CHANDIGARH | \n", "0 | \n", "