Spaces:
No application file
No application file
initial commit
Browse files- compliance.py +175 -0
- requirements.txt +10 -0
compliance.py
ADDED
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import geopandas as gpd
|
4 |
+
import numpy as np
|
5 |
+
import itertools
|
6 |
+
from scipy.spatial import cKDTree
|
7 |
+
import geopy.distance
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
+
from operator import itemgetter
|
10 |
+
|
11 |
+
# Title of the app
|
12 |
+
st.title("Automatic Compliance Monitoring for Brick Kilns")
|
13 |
+
|
14 |
+
# Dropdown for selecting the state
|
15 |
+
state = st.selectbox("Select State", ["Punjab", "Haryana", "Bihar"]) # Update the list as needed
|
16 |
+
|
17 |
+
# "Uttar_pradesh"
|
18 |
+
|
19 |
+
# Checkboxes for different compliance criteria
|
20 |
+
distance_kilns = st.checkbox("Inter-brick kiln distance < 1km")
|
21 |
+
distance_hospitals = st.checkbox("Distance to Hospitals < 800m")
|
22 |
+
distance_water_bodies = st.checkbox("Distance to Water bodies < 500m")
|
23 |
+
fp2 = "/home/shataxi.dubey/shataxi_work/India_State_Shapefile/India_State_Shapefile/India_State_Boundary.shp"
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
# Read file using gpd.read_file()
|
29 |
+
data2 = gpd.read_file(fp2)
|
30 |
+
# Function to calculate the nearest distances to water bodies
|
31 |
+
def ckdnearest(brick_kilns, rivers, gdfB_cols=['geometry']):
|
32 |
+
A = np.vstack([np.array(geom) for geom in brick_kilns[['lon','lat']].values])
|
33 |
+
B = [np.array(geom.coords) for geom in rivers.geometry.to_list()]
|
34 |
+
B_ix = tuple(itertools.chain.from_iterable(
|
35 |
+
[itertools.repeat(i, x) for i, x in enumerate(list(map(len, B)))]))
|
36 |
+
B = np.concatenate(B)
|
37 |
+
ckd_tree = cKDTree(B)
|
38 |
+
dist, river_point_idx = ckd_tree.query(A, k=1)
|
39 |
+
closest_river_point = B[river_point_idx]
|
40 |
+
river_origin_idx = itemgetter(*river_point_idx)(B_ix)
|
41 |
+
gdf = pd.concat(
|
42 |
+
[brick_kilns, rivers.loc[river_origin_idx, gdfB_cols].reset_index(drop=True),
|
43 |
+
pd.DataFrame(closest_river_point, columns = ['closest_river_point_long','closest_river_point_lat']),
|
44 |
+
pd.Series(dist, name='dist')], axis=1)
|
45 |
+
return gdf
|
46 |
+
# Function to calculate the nearest distances to hospitals
|
47 |
+
|
48 |
+
def ckdnearest_hospital(brick_kilns, hospital_df):
|
49 |
+
A = np.vstack([np.array(geom) for geom in brick_kilns[['lon','lat']].values])
|
50 |
+
B = np.vstack([np.array(geom) for geom in hospital_df[['Longitude','Latitude']].values])
|
51 |
+
ckd_tree = cKDTree(B)
|
52 |
+
dist, hospital_idx = ckd_tree.query(A, k=1)
|
53 |
+
closest_hospital_point = B[hospital_idx]
|
54 |
+
gdf = pd.concat(
|
55 |
+
[brick_kilns,
|
56 |
+
pd.DataFrame(closest_hospital_point, columns=['closest_hospital_long', 'closest_hospital_lat']),
|
57 |
+
pd.Series(dist, name='dist')], axis=1)
|
58 |
+
return gdf
|
59 |
+
# Function to calculate distances between brick kilns and nearest hospitals
|
60 |
+
def cal_bk_hosp_dist(path, hospital_df):
|
61 |
+
state_bk = pd.read_csv(path)
|
62 |
+
bk_hospital_mapping = ckdnearest_hospital(state_bk, hospital_df)
|
63 |
+
bk_hospital_mapping['distance_km'] = 0
|
64 |
+
for i in range(len(bk_hospital_mapping)):
|
65 |
+
bk_hospital_mapping['distance_km'][i] = geopy.distance.distance(
|
66 |
+
(bk_hospital_mapping['lat'][i], bk_hospital_mapping['lon'][i]),
|
67 |
+
(bk_hospital_mapping['closest_hospital_lat'][i], bk_hospital_mapping['closest_hospital_long'][i])
|
68 |
+
).km
|
69 |
+
return bk_hospital_mapping
|
70 |
+
|
71 |
+
# Load hospitals data
|
72 |
+
hospital_df = pd.read_csv('/home/rishabh.mondal/bkdb/India_Hospital_Data.csv')
|
73 |
+
hospital_df = hospital_df.rename(columns = {'lon' : 'Longitude', 'lat' : 'Latitude'})
|
74 |
+
|
75 |
+
# Function to calculate distances between brick kilns and nearest rivers
|
76 |
+
def cal_bk_river_dist(path, waterways):
|
77 |
+
state_bk = pd.read_csv(path)
|
78 |
+
bk_river_mapping = ckdnearest(state_bk, waterways)
|
79 |
+
bk_river_mapping['distance'] = 0
|
80 |
+
for i in range(len(state_bk)):
|
81 |
+
bk_river_mapping['distance'][i] = geopy.distance.distance(
|
82 |
+
(bk_river_mapping['lat'][i], bk_river_mapping['lon'][i]),
|
83 |
+
(bk_river_mapping['closest_river_point_lat'][i], bk_river_mapping['closest_river_point_long'][i])
|
84 |
+
).km
|
85 |
+
return bk_river_mapping
|
86 |
+
|
87 |
+
# Calculate inter-brick kiln distances
|
88 |
+
|
89 |
+
def ckdnearest_brick_kilns(brick_kilns):
|
90 |
+
A = np.vstack([np.array(geom) for geom in brick_kilns[['lon','lat']].values])
|
91 |
+
ckd_tree = cKDTree(A)
|
92 |
+
dist, idx = ckd_tree.query(A, k=2) # k=2 because the closest point will be itself
|
93 |
+
closest_kiln_point = A[idx[:, 1]] # idx[:, 1] to get the second closest point
|
94 |
+
gdf = pd.concat(
|
95 |
+
[brick_kilns,
|
96 |
+
pd.DataFrame(closest_kiln_point, columns=['closest_kiln_long', 'closest_kiln_lat']),
|
97 |
+
pd.Series(dist[:, 1], name='dist')], axis=1)
|
98 |
+
return gdf
|
99 |
+
# Load waterways shapefile
|
100 |
+
waterways_path = '/home/shataxi.dubey/shataxi_work/compliance_analysis/waterways/waterways.shp'
|
101 |
+
waterways = gpd.read_file(waterways_path)
|
102 |
+
|
103 |
+
# Load brick kilns data (this should be the path to your brick kilns CSV file)
|
104 |
+
brick_kilns_path = '/home/patel_zeel/compass24/exact_latlon/haryana.csv'
|
105 |
+
brick_kilns_paths = {
|
106 |
+
"Punjab": '/home/patel_zeel/compass24/exact_latlon/punjab.csv',
|
107 |
+
"Haryana": '/home/patel_zeel/compass24/exact_latlon/haryana.csv',
|
108 |
+
# "Uttar Pradesh": '/home/patel_zeel/kilns_neurips24/exact_latlon/uttar_pradesh.csv',
|
109 |
+
"Bihar": '/home/patel_zeel/compass24/exact_latlon/bihar.csv',
|
110 |
+
}
|
111 |
+
|
112 |
+
# Load brick kilns data for the selected state
|
113 |
+
brick_kilns_path = brick_kilns_paths[state]
|
114 |
+
brick_kilns = pd.read_csv(brick_kilns_path)
|
115 |
+
|
116 |
+
bk_river_mapping = cal_bk_river_dist(brick_kilns_path, waterways)
|
117 |
+
bk_hospital_mapping = cal_bk_hosp_dist(brick_kilns_path, hospital_df)
|
118 |
+
bk_kiln_mapping = ckdnearest_brick_kilns(pd.read_csv(brick_kilns_path))
|
119 |
+
|
120 |
+
|
121 |
+
brick_kilns['compliant'] = True
|
122 |
+
if distance_kilns:
|
123 |
+
brick_kilns['compliant'] &= bk_kiln_mapping['dist'] >= 1
|
124 |
+
if distance_hospitals:
|
125 |
+
brick_kilns['compliant'] &= bk_hospital_mapping['distance_km'] >= 0.8
|
126 |
+
if distance_water_bodies:
|
127 |
+
brick_kilns['compliant'] &= bk_river_mapping['distance'] >= 0.5
|
128 |
+
|
129 |
+
# Plotting the results
|
130 |
+
fig, ax = plt.subplots(figsize=(8, 6))
|
131 |
+
# data2 = gpd.read_file(waterways_path) # Replace this with the appropriate shapefile for the state map
|
132 |
+
data2.plot(ax=ax, cmap='Pastel2', edgecolor='black', linewidth=0.1) # State map
|
133 |
+
waterways.plot(ax=ax, color='blue', linewidth=0.2) # Water bodies
|
134 |
+
# Plot all brick kilns in green
|
135 |
+
brick_kilns_compliant = brick_kilns[brick_kilns['compliant']]
|
136 |
+
ax.scatter(brick_kilns_compliant['lon'], brick_kilns_compliant['lat'], color='green', s=10, marker='o', label='Compliant Brick Kilns')
|
137 |
+
|
138 |
+
# Plot non-compliant brick kilns in red
|
139 |
+
brick_kilns_non_compliant = brick_kilns[~brick_kilns['compliant']]
|
140 |
+
ax.scatter(brick_kilns_non_compliant['lon'], brick_kilns_non_compliant['lat'], color='red', s=10, marker='o', label='Non-compliant Brick Kilns')
|
141 |
+
if state == 'Bihar':
|
142 |
+
ax.text(83, 25.8, 'Uttar\n Pradesh')
|
143 |
+
ax.text(85.5, 25.5, 'Bihar')
|
144 |
+
ax.text(87.9, 25.3, 'West\n Bengal')
|
145 |
+
ax.set_xlim(83,89)
|
146 |
+
ax.set_ylim(24.25,27)
|
147 |
+
|
148 |
+
elif state == 'Haryana':
|
149 |
+
ax.text(77.3, 29.5, 'Uttar \nPradesh')
|
150 |
+
ax.text(74.5, 28.5, 'Rajasthan')
|
151 |
+
ax.text(75.5, 30.5, 'Punjab')
|
152 |
+
ax.text(76, 29, 'Haryana')
|
153 |
+
ax.text(77, 28.6, 'New Delhi')
|
154 |
+
ax.set_xlim(74,78)
|
155 |
+
ax.set_ylim(27.5,31)
|
156 |
+
elif state == 'Punjab':
|
157 |
+
ax.text(76, 32, 'Himachal\n Pradesh')
|
158 |
+
ax.text(75.5, 31, 'Punjab')
|
159 |
+
ax.text(74, 29.6, 'Rajasthan')
|
160 |
+
ax.text(76, 29.6, 'Haryana')
|
161 |
+
ax.set_xlim(73.5,77)
|
162 |
+
ax.set_ylim(29.5,32.5)
|
163 |
+
|
164 |
+
|
165 |
+
|
166 |
+
plt.legend(loc='upper left')
|
167 |
+
ax.set_axis_off()
|
168 |
+
plt.tight_layout(pad=0)
|
169 |
+
|
170 |
+
st.pyplot(fig)
|
171 |
+
|
172 |
+
# Display the number of non-compliant kilns
|
173 |
+
num_non_compliant = len(brick_kilns_non_compliant)
|
174 |
+
st.write(f"Number of non-compliant brick kilns: {num_non_compliant}")
|
175 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
folium
|
2 |
+
streamlit==1.33.0
|
3 |
+
pandas
|
4 |
+
numpy
|
5 |
+
scipy
|
6 |
+
matplotlib
|
7 |
+
geopandas==0.10.2
|
8 |
+
itertools
|
9 |
+
operator
|
10 |
+
huggingface-hub
|