saurabhharak commited on
Commit
e433a21
·
1 Parent(s): a599346

Upload 7 files

Browse files
Files changed (8) hide show
  1. .gitattributes +1 -0
  2. app.py +612 -0
  3. databasefn.py +277 -0
  4. lgd_database.db +3 -0
  5. mapping.py +89 -0
  6. pg_utils_fn.py +735 -0
  7. requirements.txt +4 -0
  8. utils.py +546 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ lgd_database.db filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,612 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import streamlit as st
3
+ import pandas as pd
4
+ from pg_utils_fn import create_entity_name_list, fetch_block_mapping, fetch_gp_mapping
5
+ from pg_utils_fn import fetch_district_mapping
6
+ from pg_utils_fn import populate_entity_mapping
7
+ from pg_utils_fn import load_file
8
+ from utils import update_all_data
9
+ from pg_utils_fn import create_village_mapped_dataset, populate_village_mapping, process_file
10
+ from pg_utils_fn import create_sub_district_mapped_dataset, populate_sub_district_mapping
11
+ from pg_utils_fn import create_gp_mapped_dataset, populate_gp_mapping
12
+ from pg_utils_fn import create_block_mapped_dataset, populate_block_mapping
13
+ from pg_utils_fn import generate_download_link
14
+ from pg_utils_fn import update_variations
15
+ from pg_utils_fn import create_district_mapped_dataset
16
+ from pg_utils_fn import create_mapped_dataset, get_state_mappings
17
+ from databasefn import insert_record
18
+
19
+
20
+ # Set Streamlit configuration
21
+ st.set_option('deprecation.showfileUploaderEncoding', False)
22
+ def home_page():
23
+
24
+ # Add an attractive header
25
+ st.title('CodeYatra')
26
+ #st.image('mandala.jpg') # Replace 'your_logo.png' with the path to your logo image
27
+
28
+ # Write a brief and engaging introduction
29
+ st.write(
30
+ "Welcome to the CodeYatra LGD Mapping App! 🌟 This app makes mapping a breeze, allowing "
31
+ "you to explore states, districts, sub-districts, blocks, gram panchayats, and villages LGD Mapping with ease. "
32
+ "Whether you're a researcher 👩‍🔬, a data analyst 📊, or simply curious about India's geography, this app is "
33
+ "the perfect resource for you. Say goodbye to tedious manual searches and say hello to accurate and "
34
+ "up-to-date mapping at your fingertips. Let's embark on an exciting journey of exploration and "
35
+ "discovery with the CodeYatra LGD Code Mapping App! 🗺️🚀"
36
+ )
37
+
38
+
39
+ st.warning("⚠️ **Important Notice:** ")
40
+ st.write(" To ensure smooth operation of the application, please update the column names as follows:")
41
+ st.write("- State: Change the state column name to 'state_name'")
42
+ st.write("- District: Change the district column name to 'district_name'")
43
+ st.write("- Sub District: Change the sub district column name to 'sub_district_name'")
44
+ st.write("- Block: Change the block column name to 'block_name'")
45
+ st.write("- Gram Panchayat (GP): Change the gp column name to 'gp_name'")
46
+ st.write("- Village: Change the village column name to 'village_name'")
47
+
48
+
49
+
50
+ # Add visually appealing images or illustrations to showcase the app's features
51
+ #st.image('your_app_features.png', use_column_width=True) # Replace 'your_app_features.png' with the path to your app features image
52
+
53
+ st.subheader('Start Mapping Now')
54
+ st.write('Ready to explore and map LGD codes? Click the button below to get started.')
55
+
56
+
57
+ if st.button('Start Mapping', key='sub_mapping_button'):
58
+ #redirect_to_subset_dataset_page()
59
+ redirect_to_state_mapping_page()
60
+
61
+
62
+ def redirect_to_update_dataset_page():
63
+ # Modify the URL parameters to navigate to the state mapping page
64
+ state_mapping_url = st.experimental_set_query_params(page='update')
65
+ page_route()
66
+ st.experimental_rerun()
67
+ def redirect_to_subset_dataset_page():
68
+ # Modify the URL parameters to navigate to the state mapping page
69
+ state_mapping_url = st.experimental_set_query_params(page='subset')
70
+ page_route()
71
+ st.experimental_rerun()
72
+ def redirect_to_state_mapping_page():
73
+ # Modify the URL parameters to navigate to the state mapping page
74
+ state_mapping_url = st.experimental_set_query_params(page='state')
75
+ page_route()
76
+ st.experimental_rerun()
77
+
78
+ def redirect_to_district_page():
79
+ # Modify the URL parameters to navigate to the district page
80
+ district_url = st.experimental_set_query_params(page='district')
81
+ page_route()
82
+ st.experimental_rerun()
83
+ def redirect_to_block_page():
84
+ # Modify the URL parameters to navigate to the block page
85
+ block_url = st.experimental_set_query_params(page='block')
86
+ page_route()
87
+ st.experimental_rerun()
88
+
89
+ def redirect_to_panchayat_page():
90
+ # Modify the URL parameters to navigate to the block page
91
+ panchayat_url = st.experimental_set_query_params(page='panchayat')
92
+ page_route()
93
+ st.experimental_rerun()
94
+
95
+ def redirect_to_village_page():
96
+ # Modify the URL parameters to navigate to the block page
97
+ panchayat_url = st.experimental_set_query_params(page='village')
98
+ page_route()
99
+ st.experimental_rerun()
100
+ def redirect_to_sub_district_page():
101
+ # Modify the URL parameters to navigate to the district page
102
+ subdistrict_url = st.experimental_set_query_params(page='subdistrict')
103
+ page_route()
104
+ st.experimental_rerun()
105
+
106
+
107
+ def page_route():
108
+ query_params = st.experimental_get_query_params()
109
+ if "page" in query_params and query_params["page"][0] == "state":
110
+ state_mapping_page()
111
+ elif "page" in query_params and query_params["page"][0] == "subset":
112
+ subset_page()
113
+ elif "page" in query_params and query_params["page"][0] == "update":
114
+ update_data()
115
+ elif "page" in query_params and query_params["page"][0] == "district":
116
+ district_page()
117
+ elif "page" in query_params and query_params["page"][0] == "block":
118
+ block_page()
119
+ elif "page" in query_params and query_params["page"][0] == "panchayat":
120
+ gp_page()
121
+ elif "page" in query_params and query_params["page"][0] == "subdistrict":
122
+ sub_district_page()
123
+ elif "page" in query_params and query_params["page"][0] == "village":
124
+ village_page()
125
+ elif "page" in query_params and query_params["page"][0] == "insertRecord":
126
+ pass
127
+ #insert_record()
128
+ else:
129
+ home_page()
130
+
131
+ # Main app logic
132
+ def main():
133
+ st.set_page_config(
134
+ page_title="CodeYatra",
135
+ page_icon="🌟",
136
+ layout="centered",
137
+ initial_sidebar_state="expanded",
138
+ )
139
+ page_route()
140
+
141
+ def update_data():
142
+ st.title("Update the Corpus of LGD codes")
143
+ if st.button('Update Corpus', key='corpus_button'):
144
+ update_all_data()
145
+
146
+
147
+ #sub set
148
+ def subset_page():
149
+ st.title("Select Specific Columns from CSV/Excel File")
150
+
151
+ # File uploader
152
+ file = st.file_uploader("Upload a CSV or Excel file", type=["csv", "xlsx"])
153
+
154
+ if file is not None:
155
+ # Process the file
156
+
157
+ df = process_file(file)
158
+ if df is not None:
159
+ # Display the processed DataFrame
160
+ st.write('Subsetted Columns')
161
+ st.write(df.head())
162
+ st.info("Important: To perform LGD mapping accurately, please download the subsetted file. It contains the selected columns required for mapping. Without the subsetted file, LGD mapping cannot be done effectively.")
163
+ st.warning("⚠️ **Important Notice:** Please upload the subsetted file on the next page for LGD mapping. It contains the required columns for accurate mapping.")
164
+
165
+ # Download button
166
+ csv = df.to_csv(index=False)
167
+ b64 = base64.b64encode(csv.encode()).decode()
168
+ href = f'<a href="data:file/csv;base64,{b64}" download="processed_data.csv">Download CSV</a>'
169
+ st.markdown(href, unsafe_allow_html=True)
170
+
171
+ if st.button('Start Mapping', key='mapping_button'):
172
+ redirect_to_state_mapping_page()
173
+ # State Mapping Code
174
+ def state_mapping_page(dataset_selected=False):
175
+ """
176
+ Generates the state mapping page which allows users to upload a CSV or Excel file containing a 'state_name' column.
177
+ It then creates a mapped dataset which contains a state_code column based on the state_name column in the uploaded dataset.
178
+ It also allows users to update the state name variations and download the mapped dataset.
179
+ If the district_mapping_button is clicked, it redirects to the district mapping page.
180
+
181
+ Parameters:
182
+ ----------
183
+ dataset_selected: bool, optional
184
+ Default value is False. If True, the 'dataset_file' is obtained from the session state. If False, it prompts the user to upload a dataset file.
185
+
186
+ Returns:
187
+ ----------
188
+ None
189
+ """
190
+
191
+ st.title('State LGD Mapping')
192
+
193
+
194
+ if not dataset_selected:
195
+ dataset_file = st.file_uploader('Upload dataset', type=['csv', 'xlsx'])
196
+ if dataset_file is None:
197
+ st.warning("Please upload a dataset file.")
198
+ return
199
+ else:
200
+ dataset_file = st.session_state['dataset_file']
201
+
202
+ dataset = pd.read_csv(dataset_file) if dataset_file.name.endswith('.csv') else pd.read_excel(dataset_file)
203
+ if 'state_name' not in dataset.columns:
204
+ st.error("Error: The dataset does not contain the 'state_name' column.")
205
+ return
206
+ st.subheader("Before State LGD Mapping")
207
+ st.write(dataset.head())
208
+ with st.spinner("Processing..."):
209
+ state_mapping = get_state_mappings()
210
+ mapped_dataset = create_mapped_dataset(dataset, state_mapping)
211
+ unmatched_names = mapped_dataset[mapped_dataset['state_code'] == -2]['state_name']
212
+
213
+ if unmatched_names.empty:
214
+ st.success('No Unmatched State Names')
215
+ mapped_dataset.to_csv('data.csv', index=False)
216
+
217
+ st.subheader("After State LGD Mapping")
218
+ with st.spinner("Processing..."):
219
+ st.write(mapped_dataset.head())
220
+ generate_download_link(mapped_dataset)
221
+
222
+ if st.button('Start District Mapping', key='district_mapping_button'):
223
+ if 'district_name' not in mapped_dataset.columns:
224
+ st.error("Error: The dataset does not contain the 'district_name' column.")
225
+ return
226
+ redirect_to_district_page()
227
+ else:
228
+ st.subheader('Unmatched State Names')
229
+ st.write(unmatched_names.unique())
230
+ note = "Please provide the state name variations separated by commas or a single state name."
231
+ st.info(note)
232
+ st.subheader('Update State Name Variations')
233
+ state_mapping = get_state_mappings()
234
+ mapped_dataset = create_mapped_dataset(dataset, state_mapping)
235
+ unmatched_names = mapped_dataset[mapped_dataset['state_code'] == -2]['state_name']
236
+ entity_table_name = "states"
237
+ update_variations(unmatched_names.unique(), state_mapping, entity_table_name)
238
+ unmatched_names = mapped_dataset[mapped_dataset['state_code'] == -2]['state_name']
239
+
240
+ if unmatched_names.empty:
241
+ st.write(mapped_dataset.head())
242
+ mapped_dataset.to_csv('data.csv', index=False)
243
+ generate_download_link(mapped_dataset)
244
+ if st.button('Start District Mapping', key='district_mapping_button'):
245
+ if 'district_name' not in mapped_dataset.columns:
246
+ st.error("Error: The dataset does not contain the 'district_name' column.")
247
+ return
248
+ redirect_to_district_page()
249
+
250
+ def district_page():
251
+ """
252
+ This function is responsible for displaying the District LGD mapping page. It fetches the district mapping dataset,
253
+ maps the dataset, and downloads the mapped dataset. If there are any unmatched district names, it prompts the user to
254
+ provide the district name variations. Once the district name variations are provided, it updates the entityNameVariants
255
+ column in the SQLite table and generates a new mapped dataset for download. If the 'Start Sub-District/Block Mapping'
256
+ button is clicked, it does nothing.
257
+ """
258
+ st.title('District LGD Mapping')
259
+ st.subheader("Before District LGD Mapping")
260
+ state_dataset = load_file()
261
+ st.write(state_dataset.head())
262
+ # Apply district mapping and create a new dataset
263
+ data = fetch_district_mapping()
264
+ district_mapping = populate_entity_mapping(data,'district_name','state_code')
265
+ mapped_dataset = create_district_mapped_dataset(state_dataset, district_mapping)
266
+ # Check if there are any unmatched names
267
+ unmatched_names = mapped_dataset[mapped_dataset['district_code'] == -2]['district_name']
268
+
269
+ if unmatched_names.empty:
270
+ # Display a message if there are no unmatched names
271
+ st.success('No Unmatched district Names')
272
+ mapped_dataset.to_csv("data.csv",index=False)
273
+ # Create a CSV file in memory
274
+ st.subheader("After District LGD Mapping")
275
+ st.write(state_dataset.head())
276
+ generate_download_link(mapped_dataset)
277
+ condition = True
278
+ if 'sub_district_name' not in mapped_dataset.columns:
279
+ condition = False
280
+
281
+ if st.button('Start Block Mapping', key='block_mapping_button'):
282
+ if 'block_name' not in mapped_dataset.columns:
283
+ st.error("Error: The dataset does not contain the 'block_name' column.")
284
+ return
285
+ redirect_to_block_page()
286
+
287
+ if condition:
288
+ if st.button('Start Sub-District Mapping', key='sub-district_mapping_button'):
289
+ if 'sub_district_name' not in mapped_dataset.columns:
290
+ st.error("Error: The dataset does not contain the 'sub_district_name' column.")
291
+ return
292
+ redirect_to_sub_district_page()
293
+ else:
294
+ # Display the dataset with unmatched names
295
+ st.subheader('Unmatched District Names')
296
+ st.write(f'Unmatched District Count: '+str(len(unmatched_names.unique())))
297
+ st.write(unmatched_names.unique())
298
+ # Display the note
299
+ note = "Please provide the district name variations separated by commas or a single district name."
300
+ st.info(note)
301
+ # Accept comma-separated values or single value only
302
+ st.subheader('Update District Name Variations')
303
+ #district_mapping = populate_entity_mapping(data,'district_name','state_code')
304
+ #mapped_dataset = create_district_mapped_dataset(state_dataset, district_mapping)
305
+ # Check if there are any unmatched names
306
+ #unmatched_names = mapped_dataset[mapped_dataset['district_code'] == -2]['district_name']
307
+ district_names = [row[0] for row in data]
308
+ entity_table_name = "district"
309
+ update_variations(unmatched_names.unique(), district_names, entity_table_name)
310
+ #unmatched_names = mapped_dataset[mapped_dataset['district_code'] == -2]['district_name']
311
+ if unmatched_names.empty:
312
+ st.success('District Name Variations Updated Successfully.')
313
+ # Create a CSV file in memory
314
+ mapped_dataset.to_csv("data.csv",index=False)
315
+ st.subheader("After District LGD Mapping")
316
+ st.write(mapped_dataset.head())
317
+ generate_download_link(mapped_dataset)
318
+
319
+ if 'block_name' in mapped_dataset.columns:
320
+ if st.button('Start Block Mapping', key='block_mapping_button'):
321
+ if 'block_name' not in mapped_dataset.columns:
322
+ st.error("Error: The dataset does not contain the 'block_name' column.")
323
+ return
324
+ redirect_to_block_page()
325
+
326
+ if 'sub_district_name' in mapped_dataset.columns:
327
+ if st.button('Start Sub-District Mapping', key='sub-district_mapping_button'):
328
+ if 'sub_district_name' not in mapped_dataset.columns:
329
+ st.error("Error: The dataset does not contain the 'sub_district_name' column.")
330
+ return
331
+ redirect_to_sub_district_page()
332
+
333
+ # Block mapping
334
+ def block_page():
335
+ """
336
+ This function is responsible for displaying the block LGD mapping page. It fetches the block mapping dataset,
337
+ maps the dataset, and downloads the mapped dataset. If there are any unmatched block names, it prompts the user to
338
+ provide the block name variations. Once the block name variations are provided, it updates the entityNameVariants
339
+ column in the SQLite table and generates a new mapped dataset for download. If the 'Start Sub-block/Block Mapping'
340
+ button is clicked, it does nothing.
341
+ """
342
+ st.title('Block LGD Mapping')
343
+ st.subheader("Before Block LGD Mapping")
344
+ block_dataset = pd.read_csv('data.csv')
345
+ st.write(block_dataset.head())
346
+ data = fetch_block_mapping()
347
+ # Apply block mapping and create a new dataset
348
+ block_mapping = populate_entity_mapping(data,'block_name','district_code')
349
+ mapped_dataset = create_block_mapped_dataset(block_dataset, block_mapping)
350
+ # Check if there are any unmatched names
351
+ unmatched_names = mapped_dataset[mapped_dataset['block_code'] == -2]['block_name']
352
+
353
+ if unmatched_names.empty:
354
+ # Display a message if there are no unmatched names
355
+ st.success('No Unmatched Block Names')
356
+
357
+ # Create a CSV file in memory
358
+
359
+ st.subheader("After Block LGD Mapping")
360
+ mapped_dataset.to_csv("data.csv",index=False)
361
+ st.write(mapped_dataset.head())
362
+ generate_download_link(mapped_dataset)
363
+ if st.button('Start Panchayat Mapping', key='Panchayat_mapping_button'):
364
+ if 'gp_name' not in mapped_dataset.columns:
365
+ st.error("Error: The dataset does not contain the 'gp_name' column.")
366
+ return
367
+ redirect_to_panchayat_page()
368
+
369
+ else:
370
+ # Display the dataset with unmatched names
371
+ st.subheader('Unmatched block Names')
372
+ st.write(f'Unmatched block Count: '+str(len(unmatched_names.unique())))
373
+ st.write(unmatched_names.unique())
374
+
375
+ # Display the note
376
+ note = "Please provide the block name variations separated by commas or a single block name."
377
+ st.warning(note)
378
+
379
+ # Accept comma-separated values or single value only
380
+ st.subheader('Update Block Name Variations')
381
+
382
+ entity_table_name = "block"
383
+ block_mapping = populate_entity_mapping(data,'block_name','district_code')
384
+ mapped_dataset = create_block_mapped_dataset(block_dataset, block_mapping)
385
+ block_names = [row[0] for row in data]
386
+ unmatched_names = mapped_dataset[mapped_dataset['block_code'] == -2]['block_name']
387
+ unmatched_names = unmatched_names.unique()
388
+ update_variations(unmatched_names, block_names, entity_table_name)
389
+ # Display a success message
390
+ unmatched_names = mapped_dataset[mapped_dataset['block_code'] == -2]['block_name']
391
+ if unmatched_names.empty:
392
+ st.success('Block Name Variations Updated Successfully.')
393
+ # Create a CSV file in memory
394
+ st.subheader("After block LGD Mapping")
395
+ mapped_dataset.to_csv("data.csv",index=False)
396
+ st.write(mapped_dataset.head())
397
+ generate_download_link(mapped_dataset)
398
+
399
+ if st.button('Start Panchayat Mapping', key='Panchayat_mapping_button'):
400
+ if 'gp_name' not in mapped_dataset.columns:
401
+ st.error("Error: The dataset does not contain the 'gp_name' column.")
402
+ return
403
+ redirect_to_panchayat_page()
404
+
405
+ #GP Mapping
406
+
407
+
408
+
409
+ def gp_page():
410
+ """
411
+ This function is responsible for displaying the gp LGD mapping page. It fetches the gp mapping dataset,
412
+ maps the dataset, and downloads the mapped dataset. If there are any unmatched gp names, it prompts the user to
413
+ provide the gp name variations. Once the gp name variations are provided, it updates the entityNameVariants
414
+ column in the SQLite table and generates a new mapped dataset for download. If the 'Start Sub-gp/gp Mapping'
415
+ button is clicked, it does nothing.
416
+ """
417
+ st.title('GP LGD Mapping')
418
+ st.subheader("Before GP LGD Mapping")
419
+ gp_dataset = pd.read_csv('data.csv')
420
+ data= fetch_gp_mapping()
421
+ st.write(gp_dataset.head())
422
+ unmatched_names = None
423
+ # Apply gp mapping and create a new dataset
424
+
425
+ #gp_mapping = populate_gp_mapping()
426
+ gp_mapping = populate_entity_mapping(data,'gp_name','block_code')
427
+ mapped_dataset = create_gp_mapped_dataset(gp_dataset, gp_mapping)
428
+
429
+ # Check if there are any unmatched names
430
+ unmatched_names = mapped_dataset[mapped_dataset['gp_code'] == -2]['gp_name']
431
+ if unmatched_names.empty:
432
+ # Display a message if there are no unmatched names
433
+ st.success('No Unmatched GP Names')
434
+
435
+ # Create a CSV file in memory
436
+ csv_file = mapped_dataset.to_csv(index=False)
437
+ st.subheader("After GP LGD Mapping")
438
+ st.write(mapped_dataset.head())
439
+
440
+ generate_download_link(mapped_dataset)
441
+
442
+ if st.button('Start Village Mapping', key='village_mapping_button'):
443
+ if 'village_name' not in mapped_dataset.columns:
444
+ st.error("Error: The dataset does not contain the 'village_name' column.")
445
+ return
446
+ redirect_to_panchayat_page()
447
+
448
+ else:
449
+ # Display the dataset with unmatched names
450
+ st.subheader('Unmatched GP Names')
451
+ st.write(f'Unmatched GP Count: '+str(len(unmatched_names.unique())))
452
+ st.write(unmatched_names.unique())
453
+
454
+ # Display the note
455
+ note = "Please provide the GP name variations separated by commas or a single GP name."
456
+ st.info(note)
457
+
458
+ # Accept comma-separated values or single value only
459
+ st.subheader('Update GP Name Variations')
460
+ gp_mapping = populate_gp_mapping()
461
+ mapped_dataset = create_gp_mapped_dataset(gp_dataset, gp_mapping)
462
+ entity_name = create_entity_name_list()
463
+ # Check if there are any unmatched names
464
+ unmatched_names = mapped_dataset[mapped_dataset['gp_code'] == -2]['gp_name']
465
+ update_variations(unmatched_names.unique(), entity_name, "gp")
466
+ unmatched_names = mapped_dataset[mapped_dataset['gp_code'] == -2]['gp_name']
467
+ if unmatched_names.empty:
468
+ st.success('GP Name Variations Updated Successfully.')
469
+ # Create a CSV file in memory
470
+ mapped_dataset.to_csv("data.csv",index=False)
471
+ st.subheader("After GP LGD Mapping")
472
+ st.write(mapped_dataset.head())
473
+ # Generate download link for the CSV file
474
+ generate_download_link(mapped_dataset)
475
+ if st.button('Start Village Mapping', key='village_mapping_button'):
476
+ if 'village_name' not in mapped_dataset.columns:
477
+ st.error("Error: The dataset does not contain the 'village_name' column.")
478
+ return
479
+ redirect_to_panchayat_page()
480
+
481
+
482
+ #village page
483
+
484
+ def village_page():
485
+ """
486
+ This function is responsible for displaying the village LGD mapping page. It fetches the village mapping dataset,
487
+ maps the dataset, and downloads the mapped dataset. If there are any unmatched village names, it prompts the user to
488
+ provide the village name variations. Once the village name variations are provided, it updates the entityNameVariants
489
+ column in the SQLite table and generates a new mapped dataset for download. If the 'Start Sub-village/village Mapping'
490
+ button is clicked, it does nothing.
491
+ """
492
+ st.title('village LGD Mapping')
493
+ st.subheader("Before village LGD Mapping")
494
+ village_dataset = pd.read_csv('data.csv')
495
+ st.write(village_dataset.head())
496
+ # Apply village mapping and create a new dataset
497
+ village_mapping = populate_village_mapping()
498
+
499
+ mapped_dataset = create_village_mapped_dataset(village_dataset, village_mapping)
500
+
501
+ # Check if there are any unmatched names
502
+ unmatched_names = mapped_dataset[mapped_dataset['village_code'] == -2]['village_name']
503
+
504
+ if unmatched_names.empty:
505
+ # Display a message if there are no unmatched names
506
+ st.success('No Unmatched Village Names')
507
+ # Create a CSV file in memory
508
+ st.subheader("After village LGD Mapping")
509
+ st.write(mapped_dataset.head())
510
+ generate_download_link(mapped_dataset)
511
+
512
+ else:
513
+ # Display the dataset with unmatched names
514
+ st.subheader('Unmatched village Names')
515
+ st.write(f'Unmatched villages Count: '+str(len(unmatched_names.unique())))
516
+ st.write(unmatched_names.unique())
517
+ # Display the note
518
+ note = "Please provide the village name variations separated by commas or a single village name."
519
+ st.info(note)
520
+ # Accept comma-separated values or single value only
521
+ st.subheader('Update village Name Variations')
522
+ village_mapping = populate_village_mapping()
523
+ mapped_dataset = create_village_mapped_dataset(village_dataset, village_mapping)
524
+ # Check if there are any unmatched names
525
+ unmatched_names = mapped_dataset[mapped_dataset['village_code'] == -2]['panchayat_name']
526
+ update_variations(unmatched_names.unique(), village_mapping, "villages")
527
+ unmatched_names = mapped_dataset[mapped_dataset['village_code'] == -2]['panchayat_name']
528
+ if unmatched_names.empty:
529
+ st.success('village Name Variations Updated Successfully.')
530
+ # Create a CSV file in memory
531
+ mapped_dataset.to_csv("data.csv",index=False)
532
+ st.subheader("After village LGD Mapping")
533
+ st.write(mapped_dataset.head())
534
+ # Generate download link for the CSV file
535
+ generate_download_link(mapped_dataset)
536
+
537
+
538
+
539
+ #sub-district mapping
540
+ def sub_district_page():
541
+ """
542
+ This function is responsible for displaying the Sub-District LGD mapping page. It fetches the Sub-District mapping dataset,
543
+ maps the dataset, and downloads the mapped dataset. If there are any unmatched Sub-District names, it prompts the user to
544
+ provide the Sub-District name variations. Once the Sub-District name variations are provided, it updates the entityNameVariants
545
+ column in the SQLite table and generates a new mapped dataset for download. If the 'Start Sub-Sub-District/Sub-District Mapping'
546
+ button is clicked, it does nothing.
547
+ """
548
+ st.title('Sub-District LGD Mapping')
549
+ st.subheader("Before Sub-District LGD Mapping")
550
+ sub_district_dataset = pd.read_csv('data.csv')
551
+ st.write(sub_district_dataset.head())
552
+ # Apply Sub-District mapping and create a new dataset
553
+ sub_district_mapping = populate_sub_district_mapping()
554
+
555
+ mapped_dataset = create_sub_district_mapped_dataset(sub_district_dataset, sub_district_mapping)
556
+
557
+ # Check if there are any unmatched names
558
+ unmatched_names = mapped_dataset[mapped_dataset['sub_district_code'] == -2]['sub_district_name']
559
+
560
+ if unmatched_names.empty:
561
+ # Display a message if there are no unmatched names
562
+ st.success('No Unmatched Sub-District Names')
563
+ mapped_dataset.to_csv("data.csv",index=False)
564
+ # Create a CSV file in memory
565
+ generate_download_link(mapped_dataset)
566
+ if st.button('Start Panchayat Mapping', key='Panchayat_mapping_button'):
567
+ if 'panchayat_name' not in mapped_dataset.columns:
568
+ st.error("Error: The dataset does not contain the 'panchayat_name' column.")
569
+ return
570
+ redirect_to_panchayat_page()
571
+
572
+ else:
573
+ # Display the dataset with unmatched names
574
+ st.subheader('Unmatched Sub-District Names')
575
+ st.write(f'Unmatched Sub-District Count: '+str(len(unmatched_names.unique())))
576
+ st.write(unmatched_names)
577
+
578
+ # Display the note
579
+ note = "Please provide the Sub-District name variations separated by commas or a single Sub-District name."
580
+ st.info(note)
581
+
582
+ # Accept comma-separated values or single value only
583
+ st.subheader('Update Sub-District Name Variations')
584
+ sub_district_mapping = populate_sub_district_mapping()
585
+ mapped_dataset = create_sub_district_mapped_dataset(sub_district_dataset, sub_district_mapping)
586
+ unmatched_names = mapped_dataset[mapped_dataset['sub_district_code'] == -2]['sub_district_name']
587
+ update_variations(unmatched_names.unique(), sub_district_mapping, "sub_district")
588
+ unmatched_names = mapped_dataset[mapped_dataset['sub_district_code'] == -2]['sub_district_name']
589
+ if unmatched_names.empty:
590
+ # Display a success message
591
+ st.success('Sub-District Name Variations Updated Successfully.')
592
+ # Create a CSV file in memory
593
+ csv_file = mapped_dataset.to_csv(index=False)
594
+ st.subheader("After Sub-District LGD Mapping")
595
+ st.write(mapped_dataset.head())
596
+ mapped_dataset.to_csv("data.csv",index=False)
597
+ st.write(mapped_dataset.head())
598
+ # Generate download link for the CSV file
599
+ generate_download_link(mapped_dataset)
600
+
601
+
602
+ if st.button('Start Panchayat Mapping', key='Panchayat_mapping_button'):
603
+ if 'panchayat_name' not in mapped_dataset.columns:
604
+ st.error("Error: The dataset does not contain the 'panchayat_name' column.")
605
+ return
606
+ redirect_to_panchayat_page()
607
+
608
+
609
+
610
+
611
+ if __name__ == "__main__":
612
+ main()
databasefn.py ADDED
@@ -0,0 +1,277 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import sqlite3
3
+ import pandas as pd
4
+ def insert_record(table_name):
5
+ """
6
+ Inserts a new record into the specified table in the LGD database.
7
+
8
+ Parameters:
9
+ - table_name (str): The name of the table to insert the record into.
10
+
11
+ Returns:
12
+ - None
13
+ """
14
+ st.header("Insert Record")
15
+ # Connect to the SQLite database
16
+ conn = sqlite3.connect("lgd_database.db")
17
+ cursor = conn.cursor()
18
+
19
+ # Retrieve the first 5 rows from the table
20
+ select_query = f"SELECT * FROM {table_name}"
21
+ cursor.execute(select_query)
22
+ rows = cursor.fetchall()
23
+
24
+ # Convert the rows to a DataFrame
25
+ columns = [description[0] for description in cursor.description]
26
+ df = pd.DataFrame(rows, columns=columns)
27
+ df = df.iloc[:, :-1]
28
+ # Display the retrieved rows in a table
29
+ st.subheader("Existing Records")
30
+ st.write(df)
31
+
32
+ # Set default values for level_name and level_code based on table_name
33
+ if table_name == "states":
34
+ level_name = "State"
35
+ level_code = 1
36
+ elif table_name == "district":
37
+ level_name = "District"
38
+ level_code = 2
39
+ elif table_name == "sub_district":
40
+ level_name = "Sub-district"
41
+ level_code = 3
42
+ elif table_name == "block":
43
+ level_name = "Block"
44
+ level_code = 4
45
+ elif table_name == "gp":
46
+ level_name = "Gram Panchayats"
47
+ level_code = 5
48
+ else:
49
+ level_name = ""
50
+ level_code = 0
51
+
52
+ # Input fields for entityLGDCode and levelCode
53
+ entity_lgd_code = st.number_input("Entity LGD Code", min_value=0, step=1)
54
+
55
+ # Input field for entityName
56
+ entity_name = st.text_input("Entity Name")
57
+
58
+ # Input field for entityNameVariants
59
+ entity_name_variants = st.text_input("Entity Name Variants")
60
+
61
+ # Input field for entityParent
62
+ entity_parent = st.number_input("Entity Parent", min_value=0, step=1)
63
+
64
+ if level_name and level_code:
65
+ st.text(f"Level Name: {level_name}")
66
+ st.text(f"Level Code: {level_code}")
67
+ else:
68
+ # Input fields for levelName and levelCode
69
+ level_name = st.text_input("Level Name")
70
+ level_code = st.number_input("Level Code", min_value=0, step=1)
71
+
72
+ # Insert button
73
+ if st.button("Insert"):
74
+ # Perform validation checks before inserting the record
75
+ errors = []
76
+
77
+ if entity_lgd_code == 0:
78
+ errors.append("Entity LGD Code cannot be zero.")
79
+
80
+ if level_code == 0:
81
+ errors.append("Level Code cannot be zero.")
82
+
83
+ if not entity_name or not isinstance(entity_name, str):
84
+ errors.append("Entity Name is required and must be a text.")
85
+
86
+ if not level_name or not isinstance(level_name, str):
87
+ errors.append("Level Name is required and must be a text.")
88
+
89
+ if not entity_name_variants or not isinstance(entity_name_variants, str):
90
+ errors.append("Entity Name Variants is required and must be a text.")
91
+
92
+ if not entity_parent or not isinstance(entity_parent, int):
93
+ errors.append("Entity Parent is required and must be an integer.")
94
+
95
+ if errors:
96
+ st.error("\n".join(errors))
97
+ else:
98
+ # Connect to the SQLite database
99
+ conn = sqlite3.connect("lgd_database.db")
100
+ cursor = conn.cursor()
101
+
102
+ # Check if the entityLGDCode already exists in the table
103
+ select_query = f"SELECT entityLGDCode FROM {table_name} WHERE entityLGDCode = ?"
104
+ cursor.execute(select_query, (entity_lgd_code,))
105
+ existing_code = cursor.fetchone()
106
+
107
+ if existing_code:
108
+ st.error("Entity LGD Code already exists in the table.")
109
+ else:
110
+ # Prepare the SQL query for creating the table if it doesn't exist
111
+ create_table_query = f"""
112
+ CREATE TABLE IF NOT EXISTS {table_name} (
113
+ entityLGDCode INTEGER PRIMARY KEY,
114
+ entityName TEXT,
115
+ levelCode INTEGER,
116
+ levelName TEXT,
117
+ entityNameVariants TEXT,
118
+ entityParent INTEGER
119
+ )
120
+ """
121
+ cursor.execute(create_table_query)
122
+
123
+ # Prepare the SQL query for inserting the record
124
+ insert_query = f"""
125
+ INSERT INTO {table_name} (entityLGDCode, entityName, levelCode, levelName, entityNameVariants, entityParent)
126
+ VALUES (?, ?, ?, ?, ?, ?)
127
+ """
128
+ values = (entity_lgd_code, entity_name, level_code, level_name, entity_name_variants, entity_parent)
129
+
130
+ try:
131
+ # Execute the SQL query
132
+ cursor.execute(insert_query, values)
133
+ conn.commit()
134
+ st.success("Record inserted successfully!")
135
+ except sqlite3.Error as e:
136
+ st.error("An error occurred while inserting the record: {}".format(e))
137
+ conn.rollback()
138
+ finally:
139
+ # Close the database connection
140
+ cursor.close()
141
+ conn.close()
142
+
143
+ # Reset the input fields
144
+ entity_lgd_code = 0
145
+ level_code = 0
146
+ entity_name = ""
147
+ level_name = ""
148
+ entity_name_variants = ""
149
+ entity_parent = 0
150
+
151
+
152
+ def update_record(table_name):
153
+ """
154
+ Update a record in a SQLite database table.
155
+
156
+ :param table_name: The name of the table to update the record in.
157
+ :type table_name: str
158
+
159
+ :return: None
160
+ :rtype: None
161
+ """
162
+ st.header("Update Record")
163
+
164
+ # Input field for entityLGDCode
165
+ entity_lgd_code = st.number_input("Entity LGD Code", min_value=0, step=1)
166
+
167
+ # Input field for entityName
168
+ entity_name = st.text_input("Entity Name")
169
+
170
+ # Input field for entityNameVariants
171
+ entity_name_variants = st.text_input("Entity Name Variants")
172
+
173
+ # Input field for entityParent
174
+ entity_parent = st.number_input("Entity Parent", min_value=0, step=1)
175
+
176
+ # Update button
177
+ if st.button("Update"):
178
+ # Perform validation checks before updating the record
179
+ errors = []
180
+
181
+ if entity_lgd_code == 0:
182
+ errors.append("Entity LGD Code cannot be zero.")
183
+
184
+ if not entity_name or not isinstance(entity_name, str):
185
+ errors.append("Entity Name is required and must be a text.")
186
+
187
+ if not entity_name_variants or not isinstance(entity_name_variants, str):
188
+ errors.append("Entity Name Variants is required and must be a text.")
189
+
190
+ if not entity_parent or not isinstance(entity_parent, int):
191
+ errors.append("Entity Parent is required and must be an integer.")
192
+
193
+ if errors:
194
+ st.error("\n".join(errors))
195
+ else:
196
+ # Connect to the SQLite database
197
+ conn = sqlite3.connect("lgd_database.db")
198
+ cursor = conn.cursor()
199
+
200
+ # Prepare the SQL query for updating the record
201
+ update_query = f"""
202
+ UPDATE {table_name}
203
+ SET entityName = ?, entityNameVariants = ?, entityParent = ?
204
+ WHERE entityLGDCode = ?
205
+ """
206
+ values = (entity_name, entity_name_variants, entity_parent, entity_lgd_code)
207
+
208
+ try:
209
+ # Execute the SQL query
210
+ cursor.execute(update_query, values)
211
+ conn.commit()
212
+ st.success("Record updated successfully!")
213
+ except sqlite3.Error as e:
214
+ st.error("An error occurred while updating the record: {}".format(e))
215
+ conn.rollback()
216
+ finally:
217
+ # Close the database connection
218
+ cursor.close()
219
+ conn.close()
220
+
221
+ # Reset the input fields
222
+ entity_lgd_code = 0
223
+ entity_name = ""
224
+ entity_name_variants = ""
225
+ entity_parent = 0
226
+
227
+ def delete_record(table_name):
228
+ """
229
+ Deletes a record from the specified table in an SQLite database based on the entityLGDCode.
230
+
231
+ :param table_name: Name of the table to delete the record from.
232
+ :type table_name: str
233
+
234
+ :return: None
235
+ :rtype: None
236
+ """
237
+ st.header("Delete Record")
238
+
239
+ # Input field for entityLGDCode
240
+ entity_lgd_code = st.number_input("Entity LGD Code", min_value=0, step=1)
241
+
242
+ # Delete button
243
+ if st.button("Delete"):
244
+ # Connect to the SQLite database
245
+ conn = sqlite3.connect("lgd_database.db")
246
+ cursor = conn.cursor()
247
+
248
+ # Prepare the SQL query for deleting the record
249
+ delete_query = f"""
250
+ DELETE FROM {table_name}
251
+ WHERE entityLGDCode = ?
252
+ """
253
+ values = (entity_lgd_code,)
254
+
255
+ try:
256
+ # Execute the SQL query
257
+ cursor.execute(delete_query, values)
258
+ conn.commit()
259
+ st.success("Record deleted successfully!")
260
+ except sqlite3.Error as e:
261
+ st.error("An error occurred while deleting the record: {}".format(e))
262
+ conn.rollback()
263
+ finally:
264
+ # Close the database connection
265
+ cursor.close()
266
+ conn.close()
267
+
268
+ # Reset the input fields
269
+ entity_lgd_code = 0
270
+
271
+ """ if __name__ == "__main__":
272
+ table_name = "block" # Provide the table name here
273
+
274
+ # Usage examples
275
+ update_record(table_name)
276
+ delete_record(table) """
277
+
lgd_database.db ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:94221cb8148c0db3ba3b4f238293c015df16813a73e4fd60ba75371a3ddac21c
3
+ size 306753536
mapping.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+ import streamlit as st
3
+ import pandas as pd
4
+ from collections import defaultdict
5
+ import base64
6
+ import multiprocessing
7
+
8
+ from pg_utils_fn import create_mapped_dataset, get_state_mappings
9
+
10
+
11
+
12
+ def create_gp_mapped_dataset(dataset, mapping):
13
+ """
14
+ Create a mapped dataset by associating gp codes with gp names in the dataset.
15
+ """
16
+ dataset['panchayat_name'] = dataset['panchayat_name'].str.strip()
17
+ dataset['gp_code'] = dataset['panchayat_name'].str.lower().map(mapping)
18
+ dataset.loc[dataset['gp_code'].isnull(), 'gp_code'] = -2
19
+ return dataset
20
+
21
+ def fetch_gp_mapping():
22
+ """
23
+ Fetch the gp mapping from the SQLite database.
24
+
25
+ Returns:
26
+ - A list of tuples containing the gp entity name, LGD code, name variants, and parent entity.
27
+ """
28
+ # Connect to the SQLite database
29
+ conn = sqlite3.connect('lgd_database.db')
30
+ cursor = conn.cursor()
31
+
32
+ # Retrieve gp data from the 'gps' table
33
+ cursor.execute("SELECT entityName, entityLGDCode, entityNameVariants, entityParent FROM gp")
34
+ data = cursor.fetchall()
35
+
36
+ # Close the database connection
37
+ conn.close()
38
+
39
+ return data
40
+
41
+ def populate_gp_mapping():
42
+ """
43
+ Populates a gp mapping dictionary using data from a database and a local file.
44
+
45
+ Returns:
46
+ A defaultdict containing the mapping of gp names to their respective codes.
47
+ """
48
+ state_dataset = pd.read_csv('data.csv')
49
+ data = fetch_gp_mapping()
50
+ unique_rows = state_dataset.drop_duplicates(subset=['panchayat_name'])
51
+ unique_rows_lower = unique_rows.apply(lambda x: (x['panchayat_name'].strip().lower(), x['block_code']), axis=1).tolist()
52
+
53
+ entity_mapping = {}
54
+ edname = "Not Available"
55
+ for entity_name, entity_code, entity_variants, parent_code in data:
56
+ for row in unique_rows_lower:
57
+ entity_name_lower = row[0]
58
+ state_code = row[1]
59
+ if entity_name_lower == entity_name.lower() :
60
+ if int(parent_code) == int(state_code) :
61
+ entity_mapping[entity_name_lower.lower()] = entity_code
62
+ if entity_variants:
63
+ for variant in entity_variants.split(','):
64
+ entity_mapping[variant.strip().lower()] = entity_code
65
+
66
+ return entity_mapping
67
+
68
+
69
+
70
+ import pandas as pd
71
+
72
+
73
+ def main_gp():
74
+ gp_dataset = pd.read_csv('data.csv')
75
+ gp_mapping = populate_gp_mapping()
76
+
77
+ mapped_dataset = create_gp_mapped_dataset(gp_dataset, gp_mapping)
78
+
79
+ unmatched_names = mapped_dataset[mapped_dataset['gp_code'] == -2]['panchayat_name']
80
+ return unmatched_names,mapped_dataset
81
+
82
+
83
+
84
+ def main_state(dataset):
85
+ state_mapping = get_state_mappings()
86
+ mapped_dataset = create_mapped_dataset(dataset, state_mapping)
87
+ unmatched_names = mapped_dataset[mapped_dataset['state_code'] == -2]['state_name']
88
+
89
+ return unmatched_names,mapped_dataset
pg_utils_fn.py ADDED
@@ -0,0 +1,735 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+ import streamlit as st
3
+ import pandas as pd
4
+ from collections import defaultdict
5
+ import base64
6
+ import multiprocessing
7
+
8
+
9
+
10
+ def load_file():
11
+ data = pd.read_csv('data.csv')
12
+ return data
13
+
14
+ def process_file(file):
15
+ try:
16
+ if file.type == 'text/csv':
17
+ df = pd.read_csv(file)
18
+ elif file.type == 'application/vnd.ms-excel':
19
+ df = pd.read_excel(file)
20
+ else:
21
+ raise ValueError("Invalid file format. Only CSV and Excel files are supported.")
22
+
23
+ # Get the present columns based on the intersection of specified columns and available columns
24
+ specified_columns = ["state_name", "district_name", "sub_district_name", "block_name", "gp_name", "village_name"]
25
+ present_columns = list(set(specified_columns).intersection(df.columns))
26
+
27
+ if not present_columns:
28
+ raise ValueError("None of the specified columns are present in the file.")
29
+
30
+ # Select only the present columns
31
+ st.write("Dataset Information:")
32
+ st.write(df.head())
33
+ df = df[present_columns]
34
+
35
+ # Perform operations on the selected columns or use them as needed
36
+ # ...
37
+
38
+ return df
39
+
40
+ except Exception as e:
41
+ st.error("An error occurred during file processing: " + str(e))
42
+ return None
43
+
44
+ # Streamlit app
45
+
46
+ def get_state_mappings():
47
+ """
48
+ This function retrieves state names, codes, and variants from an SQLite database and returns a dictionary
49
+ containing the state mappings. No parameters are required. The keys of the dictionary are the state names and
50
+ variants in lowercase, and the values are the corresponding state codes. Returns a dictionary.
51
+ """
52
+ with sqlite3.connect('lgd_database.db') as conn:
53
+ cursor = conn.cursor()
54
+ cursor.execute("SELECT entityName, entityLGDCode, entityNameVariants FROM states")
55
+ data = cursor.fetchall()
56
+
57
+ mapping_dict = {}
58
+ for state_name, state_code, state_variants in data:
59
+ mapping_dict[state_name.lower()] = state_code
60
+ if state_variants:
61
+ for variant in state_variants.split(','):
62
+ mapping_dict[variant.strip().lower()] = state_code
63
+
64
+ return mapping_dict
65
+
66
+ def create_mapped_dataset(dataset, mapping):
67
+
68
+ dataset['state_name'] = dataset['state_name'].str.strip()
69
+ dataset['state_code'] = dataset['state_name'].str.lower().map(mapping)
70
+ dataset.loc[dataset['state_code'].isnull(), 'state_code'] = -2
71
+ return dataset
72
+
73
+
74
+
75
+ def create_selectbox_widget(name, values):
76
+ """
77
+ Creates a select box widget using the Streamlit library. The select box widget allows the user to choose a value from a list of values.
78
+
79
+ :param name: The name of the select box widget.
80
+ :type name: str
81
+ :param values: A list of values to be displayed in the select box.
82
+ :type values: list
83
+ :return: The selected value from the select box.
84
+ :rtype: any
85
+ """
86
+ return st.selectbox(f'{name}', values, key=name)
87
+
88
+
89
+ def query_state_data():
90
+ """
91
+ Connects to an SQLite database and retrieves all the entity names and their variants from the 'states' table.
92
+
93
+ :return: A list of tuples containing the entity names and their variants.
94
+ """
95
+ conn = sqlite3.connect('lgd_database.db')
96
+ cursor = conn.cursor()
97
+ cursor.execute("SELECT entityName, entityNameVariants FROM states")
98
+ state_data = cursor.fetchall()
99
+ conn.close()
100
+ return state_data
101
+
102
+ def update_state_variants(state_name, name_variant, state_data):
103
+ """
104
+ Updates the variants of a state name in the state_data dictionary.
105
+
106
+ Parameters:
107
+ state_name (str): The name of the state to update the variants for.
108
+ name_variant (str): The new name variant to add.
109
+ state_data (dict): A dictionary containing state names and their variants.
110
+
111
+ Returns:
112
+ tuple or None: A tuple containing the updated state name and its variants if the state name is found and updated in the dictionary. Otherwise, returns None.
113
+ """
114
+ for state_name_db, entityNameVariants in state_data:
115
+ if state_name.lower() == state_name_db.lower():
116
+ new_variants = f"{entityNameVariants}, {name_variant}" if entityNameVariants else name_variant
117
+ conn = sqlite3.connect('lgd_database.db')
118
+ cursor = conn.cursor()
119
+ cursor.execute("UPDATE states SET entityNameVariants = ? WHERE entityName = ?", (new_variants, state_name_db))
120
+ conn.commit()
121
+ conn.close()
122
+ return state_name_db, new_variants
123
+ return None, None
124
+
125
+ def process_unmatched_names(unmatched_names, state_mapping):
126
+ """
127
+ Process unmatched names and update state data.
128
+
129
+ Parameters:
130
+ - unmatched_names (list): A list of unmatched names.
131
+ - state_mapping (dict): A dictionary mapping state names to their data.
132
+
133
+ Returns:
134
+ None
135
+ """
136
+ state_data = query_state_data()
137
+ for unmatched_name_index, unmatched_name in enumerate(unmatched_names):
138
+ st.write(f'Unmatched Name: {unmatched_name}')
139
+ state_name = create_selectbox_widget(f'Enter state name {unmatched_name_index}:', list(state_mapping.keys()))
140
+ state_exists = state_name.lower() in state_mapping.keys()
141
+
142
+ if state_name and not state_exists:
143
+ st.error('State name not found in the table. Please enter a valid state name.')
144
+
145
+ name_variant = st.text_input(f'Enter name variant {unmatched_name_index}:')
146
+
147
+ if state_name and name_variant and state_exists:
148
+ state_name_db, new_variants = update_state_variants(state_name, name_variant, state_data)
149
+ if state_name_db and new_variants:
150
+ st.success(f'State Name: {state_name_db} Variations: {new_variants} Updated Successfully.')
151
+
152
+ st.write('---')
153
+
154
+
155
+ def fetch_district_mapping():
156
+ """
157
+ Fetch the district mapping from the SQLite database.
158
+
159
+ Returns:
160
+ - A list of tuples containing the district entity name, LGD code, name variants, and parent entity.
161
+ """
162
+ # Connect to the SQLite database
163
+ conn = sqlite3.connect('lgd_database.db')
164
+ cursor = conn.cursor()
165
+
166
+ # Retrieve district data from the 'districts' table
167
+ cursor.execute("SELECT entityName, entityLGDCode, entityNameVariants, entityParent FROM district")
168
+ data = cursor.fetchall()
169
+
170
+ # Close the database connection
171
+ conn.close()
172
+
173
+ return data
174
+
175
+ def populate_entity_mapping(data,column_name,parent_column_name):
176
+ """
177
+ Populates a entity mapping dictionary using data from a database and a local file.
178
+
179
+ Returns:
180
+ A defaultdict containing the mapping of entity names to their respective codes.
181
+ """
182
+ # Load unique entity data
183
+ state_dataset = pd.read_csv('data.csv')
184
+ unique_rows = state_dataset.drop_duplicates(subset=[column_name])
185
+ unique_rows_lower = unique_rows.apply(lambda x: (x[column_name].strip().lower(), x[parent_column_name]), axis=1).tolist()
186
+
187
+ entity_mapping = {}
188
+ for entity_name, entity_code, entity_variants, parent_code in data:
189
+ for row in unique_rows_lower:
190
+ entity_name_lower = row[0]
191
+ state_code = row[1]
192
+ if int(parent_code) == int(state_code):
193
+ if entity_name_lower.strip() == entity_name.strip().lower():
194
+ entity_mapping[entity_name_lower] = entity_code
195
+ #print(entity_name_lower)
196
+ else:
197
+ if entity_variants:
198
+ for variant in entity_variants.split(','):
199
+ if variant.strip().lower() == entity_name_lower.strip():
200
+ entity_mapping[variant.strip().lower()] = entity_code
201
+ print(variant.strip().lower())
202
+
203
+ return entity_mapping
204
+
205
+
206
+
207
+ """
208
+ def create_district_mapped_dataset(dataset, mapping):
209
+
210
+ Create a mapped dataset by associating state codes with district names in the dataset.
211
+
212
+ dataset['district_name'] = dataset['district_name'].str.strip().str.lower()
213
+ dataset['district_code'] = dataset['district_name'].str.lower().map(mapping)
214
+ dataset.loc[dataset['district_code'].isnull(), 'district_code'] = -1
215
+ return dataset
216
+ """
217
+
218
+
219
+
220
+ def process_district_name(district_name, mapping):
221
+ """
222
+ Process a district name by stripping, converting to lowercase, and mapping to a code.
223
+ """
224
+ district_name = district_name.strip().lower()
225
+ return district_name, mapping.get(district_name, -2)
226
+
227
+ def create_district_mapped_dataset(dataset, mapping):
228
+ """
229
+ Create a mapped dataset by associating state codes with district names in the dataset.
230
+ """
231
+ pool = multiprocessing.Pool()
232
+ results = pool.starmap(process_district_name, zip(dataset['district_name'], [mapping]*len(dataset)))
233
+ dataset['district_name'], dataset['district_code'] = zip(*results)
234
+ return dataset
235
+
236
+
237
+ import sqlite3
238
+
239
+ def update_variations(unmatched_names, mapping, entity_table_name, chunk_size=1):
240
+ """
241
+ Updates the variations of unmatched names in the given mapping dictionary for a specific entity table.
242
+
243
+ Parameters:
244
+ - unmatched_names (list): A list of unmatched names to update the variations for.
245
+ - mapping (dict): A dictionary mapping entity names to their variations.
246
+ - entity_table_name (str): The name of the entity table to update the variations in.
247
+ - chunk_size (int): The size of each processing chunk.
248
+
249
+ Returns:
250
+ - str: The message "Done" indicating that the variations have been updated successfully.
251
+ """
252
+ try:
253
+ conn = sqlite3.connect('lgd_database.db')
254
+ cursor = conn.cursor()
255
+
256
+ num_unmatched = len(unmatched_names)
257
+ num_chunks = (num_unmatched + chunk_size - 1) // chunk_size
258
+
259
+ chunk_index = 0 # Initialize chunk index
260
+
261
+ while chunk_index < num_chunks:
262
+ start_idx = chunk_index * chunk_size
263
+ end_idx = min((chunk_index + 1) * chunk_size, num_unmatched)
264
+ current_chunk = unmatched_names[start_idx:end_idx]
265
+
266
+ for index, unmatched_name in enumerate(current_chunk):
267
+ entity_name = create_selectbox_widget(f'Select {entity_table_name} name {start_idx + index + 1}:', list(mapping))
268
+ if not entity_name:
269
+ continue
270
+
271
+ entity_exists = entity_name in mapping
272
+ if not entity_exists:
273
+ st.error(f'{entity_table_name} name not found in the table. Please enter a valid {entity_table_name} name.')
274
+ continue
275
+
276
+ name_variant = st.text_input(f'Select an appropriate value for the following variant {unmatched_name}:')
277
+ if not name_variant:
278
+ continue
279
+
280
+ cursor.execute(f"SELECT entityName, entityNameVariants, entityLGDCode FROM {entity_table_name}")
281
+ entity_data = cursor.fetchall()
282
+
283
+ for entity_name_db, entityNameVariants, entity_LGD_Code in entity_data:
284
+ if entity_name.lower() == entity_name_db.lower():
285
+ new_variants = f"{entityNameVariants.strip()}, {name_variant.strip()}" if entityNameVariants else name_variant
286
+ cursor.execute(f"UPDATE {entity_table_name} SET entityNameVariants = ? WHERE entityLGDCode = ?", (new_variants.strip(), int(entity_LGD_Code)))
287
+ st.success(f'{entity_name_db} Variation Updated Successfully.')
288
+ break
289
+
290
+ conn.commit()
291
+ st.write('---')
292
+
293
+ chunk_index += 1 # Increment chunk index
294
+
295
+ if chunk_index < num_chunks:
296
+ unique_button_key = f"load_next_button_{chunk_index}"
297
+ load_next_button = st.button("Load Next Chunk", key=unique_button_key)
298
+ if not load_next_button:
299
+ break # Break the loop if button is not clicked
300
+
301
+ conn.close()
302
+
303
+ except Exception as e:
304
+ st.error(f"An error occurred: {str(e)}")
305
+ return "Done"
306
+
307
+
308
+
309
+
310
+
311
+
312
+
313
+
314
+
315
+ def update_variationsold(unmatched_names, mapping, entity_table_name):
316
+ """
317
+ Updates the variations of unmatched names in the given mapping dictionary for a specific entity table.
318
+
319
+ Parameters:
320
+ - unmatched_names (list): A list of unmatched names to update the variations for.
321
+ - mapping (dict): A dictionary mapping entity names to their variations.
322
+ - entity_table_name (str): The name of the entity table to update the variations in.
323
+
324
+ Returns:
325
+ - str: The message "Done" indicating that the variations have been updated successfully.
326
+ """
327
+ try:
328
+ conn = sqlite3.connect('lgd_database.db')
329
+ cursor = conn.cursor()
330
+
331
+ for index, unmatched_name in enumerate(unmatched_names):
332
+ entity_name = create_selectbox_widget(f'Select {entity_table_name} name {index+1}:', list(mapping))
333
+ if not entity_name:
334
+ continue
335
+
336
+ entity_exists = entity_name in mapping
337
+ if not entity_exists:
338
+ st.error(f'{entity_table_name} name not found in the table. Please enter a valid {entity_table_name} name.')
339
+ continue
340
+
341
+ name_variant = st.text_input(f'Select an appropriate value for the following variant {unmatched_name}:')
342
+ if not name_variant:
343
+ continue
344
+
345
+ cursor.execute(f"SELECT entityName, entityNameVariants, entityLGDCode FROM {entity_table_name}")
346
+ entity_data = cursor.fetchall()
347
+
348
+ for entity_name_db, entityNameVariants, entity_LGD_Code in entity_data:
349
+ if entity_name.lower() == entity_name_db.lower():
350
+ new_variants = f"{entityNameVariants.strip()}, {name_variant.strip()}" if entityNameVariants else name_variant
351
+ cursor.execute(f"UPDATE {entity_table_name} SET entityNameVariants = ? WHERE entityLGDCode = ?", (new_variants.strip(), int(entity_LGD_Code)))
352
+ st.success(f'{entity_name_db} Variation Updated Successfully.')
353
+ break
354
+
355
+ conn.commit()
356
+ st.write('---')
357
+
358
+ conn.close()
359
+
360
+ except Exception as e:
361
+ st.error(f"An error occurred: {str(e)}")
362
+ return "Done"
363
+
364
+
365
+ def update_variationso(unmatched_names, mapping, entity_table_name):
366
+ """
367
+ Updates the variations of a given entity in the database.
368
+
369
+ Parameters:
370
+ - unmatched_names (list): A list of unmatched names.
371
+ - mapping (dict): A dictionary mapping entity names to their corresponding values.
372
+ - entity_table_name (str): The name of the entity table.
373
+
374
+ Returns:
375
+ - str: The status message indicating the success of the function.
376
+ """
377
+ try:
378
+ entity_name = create_selectbox_widget(f'Select {entity_table_name} name :', list(mapping))
379
+ if not entity_name:
380
+ return
381
+
382
+ entity_exists = entity_name in mapping
383
+ if not entity_exists:
384
+ st.error(f'{entity_table_name} name not found in the table. Please enter a valid {entity_table_name} name.')
385
+ return
386
+
387
+ name_variant = st.text_input(f'Enter name variant {unmatched_names[0]}:')
388
+ if not name_variant:
389
+ return
390
+
391
+ conn = sqlite3.connect('lgd_database.db')
392
+ cursor = conn.cursor()
393
+
394
+ cursor.execute(f"SELECT entityName, entityNameVariants, entityLGDCode FROM {entity_table_name}")
395
+ entity_data = cursor.fetchall()
396
+
397
+ for entity_name_db, entityNameVariants, entity_LGD_Code in entity_data:
398
+ if entity_name.lower() == entity_name_db.lower():
399
+ new_variants = f"{entityNameVariants.strip()}, {name_variant.strip()}" if entityNameVariants else name_variant
400
+ cursor.execute(f"UPDATE {entity_table_name} SET entityNameVariants = ? WHERE entityLGDCode = ?", (new_variants.strip(), int(entity_LGD_Code)))
401
+ st.success(f'{entity_name_db} Variation Updated Successfully.')
402
+ break
403
+
404
+ conn.commit()
405
+ conn.close()
406
+ st.write('---')
407
+
408
+ except Exception as e:
409
+ st.error(f"An error occurred: {str(e)}")
410
+ return "Done"
411
+
412
+
413
+ def update_variations_without_parent(unmatched_names, mapping, entity_table_name):
414
+
415
+ try:
416
+
417
+
418
+ entity_name = create_selectbox_widget(f'Select {entity_table_name} name :', list(mapping))
419
+
420
+
421
+
422
+
423
+ entity_exists = entity_name in mapping
424
+
425
+
426
+
427
+ if entity_name and not entity_exists:
428
+
429
+ st.error(f'{entity_table_name} name not found in the table. Please enter a valid {entity_table_name} name.')
430
+
431
+
432
+
433
+ name_variant = st.text_input(f'Enter name variant {unmatched_names[0]}:')
434
+
435
+ if entity_name and name_variant and entity_exists:
436
+
437
+ conn = sqlite3.connect('lgd_database.db')
438
+
439
+ cursor = conn.cursor()
440
+
441
+ cursor.execute(f"SELECT entityName, entityNameVariants, entityLGDCode FROM {entity_table_name}")
442
+
443
+ entity_data = cursor.fetchall()
444
+
445
+ for entity_name_db, entityNameVariants, entity_LGD_Code in entity_data:
446
+
447
+ if entity_name.lower() == entity_name_db.lower():
448
+
449
+ new_variants = f"{entityNameVariants.strip()}, {name_variant.strip()}" if entityNameVariants else name_variant
450
+
451
+ cursor.execute(f"UPDATE {entity_table_name} SET entityNameVariants = ? WHERE entityLGDCode = ?", (new_variants.strip(), int(entity_LGD_Code)))
452
+
453
+ st.success(f'{entity_name_db} Variation Updated Successfully.')
454
+
455
+
456
+
457
+
458
+
459
+ conn.commit()
460
+
461
+ conn.close()
462
+
463
+ st.write('---')
464
+
465
+
466
+
467
+ except Exception as e:
468
+
469
+ st.error(f"An error occurred: {str(e)}")
470
+
471
+ return "Done"
472
+
473
+
474
+
475
+
476
+ import concurrent.futures
477
+
478
+
479
+
480
+ def generate_download_link(mapped_dataset):
481
+
482
+ csv_file = mapped_dataset.to_csv(index=False)
483
+ b64 = base64.b64encode(csv_file.encode()).decode()
484
+ href = f'<a href="data:file/csv;base64,{b64}" download="mapped_dataset.csv">Download</a>'
485
+ st.success('Download Mapped Dataset')
486
+ st.markdown(href, unsafe_allow_html=True)
487
+
488
+
489
+ def fetch_block_mapping():
490
+ """
491
+ Fetch the block mapping from the SQLite database.
492
+
493
+ Returns:
494
+ - A list of tuples containing the block entity name, LGD code, name variants, and parent entity.
495
+ """
496
+ # Connect to the SQLite database
497
+ conn = sqlite3.connect('lgd_database.db')
498
+ cursor = conn.cursor()
499
+
500
+ # Retrieve block data from the 'blocks' table
501
+ cursor.execute("SELECT entityName, entityLGDCode, entityNameVariants, entityParent FROM block")
502
+ data = cursor.fetchall()
503
+
504
+ # Close the database connection
505
+ conn.close()
506
+
507
+ return data
508
+
509
+ def populate_block_mapping():
510
+
511
+ state_dataset = pd.read_csv('data.csv')
512
+
513
+ unique_rows = state_dataset.drop_duplicates(subset=['block_name'])
514
+ unique_rows_lower = unique_rows.apply(lambda x: (x['block_name'].strip().lower(), x['district_code']), axis=1).tolist()
515
+
516
+ district_mapping = {}
517
+ edname = "Not Available"
518
+
519
+ for district_name, district_code, district_variants, parent_code in data:
520
+ for row in unique_rows_lower:
521
+ district_name_lower = row[0]
522
+ state_code = row[1]
523
+ if district_name_lower == district_name.lower():
524
+ if int(parent_code) == int(state_code):
525
+ district_mapping[district_name.lower()] = district_code
526
+ if district_variants:
527
+ for variant in district_variants.split(','):
528
+ district_mapping[variant.strip().lower()] = district_code
529
+
530
+
531
+ for district_name, district_code, district_variants, parent_code in data:
532
+ if edname.lower() == district_name.lower():
533
+ if int(parent_code) == int(0):
534
+ district_mapping[district_name.lower()] = district_code
535
+ if district_variants:
536
+ for variant in district_variants.split(','):
537
+ district_mapping[variant.strip().lower()] = district_code
538
+
539
+
540
+ return district_mapping
541
+
542
+
543
+
544
+ def create_block_mapped_dataset(dataset, mapping):
545
+ """
546
+ Create a mapped dataset by associating block codes with block names in the dataset.
547
+ """
548
+ dataset['block_name'] = dataset['block_name'].str.strip()
549
+ dataset['block_code'] = dataset['block_name'].str.lower().map(mapping)
550
+ dataset.loc[dataset['block_code'].isnull(), 'block_code'] = -2
551
+ return dataset
552
+
553
+
554
+ def fetch_gp_mapping():
555
+ """
556
+ Fetch the gp mapping from the SQLite database.
557
+
558
+ Returns:
559
+ - A list of tuples containing the gp entity name, LGD code, name variants, and parent entity.
560
+ """
561
+ # Connect to the SQLite database
562
+ conn = sqlite3.connect('lgd_database.db')
563
+ cursor = conn.cursor()
564
+
565
+ # Retrieve gp data from the 'gps' table
566
+ cursor.execute("SELECT entityName, entityLGDCode, entityNameVariants, entityParent FROM gp")
567
+ data = cursor.fetchall()
568
+
569
+ # Close the database connection
570
+ conn.close()
571
+
572
+ return data
573
+
574
+ def fetch_village_mapping():
575
+ """
576
+ Fetch the gp mapping from the SQLite database.
577
+
578
+ Returns:
579
+ - A list of tuples containing the gp entity name, LGD code, name variants, and parent entity.
580
+ """
581
+ # Connect to the SQLite database
582
+ conn = sqlite3.connect('lgd_database.db')
583
+ cursor = conn.cursor()
584
+
585
+ # Retrieve gp data from the 'gps' table
586
+ cursor.execute("SELECT villageNameEnglish, villageCode, entityNameVariants FROM villages")
587
+ data = cursor.fetchall()
588
+
589
+ # Close the database connection
590
+ conn.close()
591
+
592
+ return data
593
+
594
+ def create_entity_name_list():
595
+ data = fetch_gp_mapping()
596
+ entity_name_list = [entity_name for entity_name, _, _, _ in data]
597
+ return entity_name_list
598
+ def populate_gp_mapping():
599
+ """
600
+ Populates a gp mapping dictionary using data from a database and a local file.
601
+
602
+ Returns:
603
+ A dictionary containing the mapping of gp names to their respective codes.
604
+ """
605
+ state_dataset = pd.read_csv('data.csv')
606
+ data = fetch_gp_mapping()
607
+ unique_rows = state_dataset.drop_duplicates(subset=['gp_name'])
608
+ unique_rows_lower = unique_rows.apply(lambda x: (str(x['gp_name']).strip().lower(), x['block_code']), axis=1).tolist()
609
+
610
+ entity_mapping = {}
611
+
612
+ # Populate mapping for entity name and variants
613
+ for entity_name, entity_code, entity_variants, parent_code in data:
614
+ for row in unique_rows_lower:
615
+ entity_name_lower = row[0]
616
+ state_code = row[1]
617
+ if entity_name_lower == entity_name.lower() and int(parent_code) == int(state_code):
618
+ entity_mapping[entity_name_lower] = entity_code
619
+ if entity_variants:
620
+ for variant in entity_variants.split(','):
621
+ entity_mapping[variant.strip().lower()] = entity_code
622
+
623
+ # Populate mapping for special case entity name
624
+ edname = "Not Available"
625
+ for entity_name, entity_code, entity_variants, parent_code in data:
626
+ if edname.lower() == entity_name.lower() and str(parent_code) == str(0):
627
+ entity_mapping[entity_name.lower()] = entity_code
628
+ if entity_variants:
629
+ for variant in entity_variants.split(','):
630
+ entity_mapping[variant.strip().lower()] = entity_code
631
+
632
+ return entity_mapping
633
+
634
+
635
+ def populate_village_mapping():
636
+ """
637
+ Populates a gp mapping dictionary using data from a database and a local file.
638
+
639
+ Returns:
640
+ A defaultdict containing the mapping of gp names to their respective codes.
641
+ """
642
+ state_dataset = pd.read_csv('data.csv')
643
+ data = fetch_village_mapping()
644
+ unique_rows = state_dataset.drop_duplicates(subset=['village_name'])
645
+ unique_rows_lower = unique_rows.apply(lambda x: (x['village_name'].strip().lower(), x['panchayat_name']), axis=1).tolist()
646
+
647
+ district_mapping = {}
648
+
649
+ for district_name, district_code, district_variants, parent_code in data:
650
+ for row in unique_rows_lower:
651
+ district_name_lower = row[0]
652
+ state_code = row[1]
653
+ if district_name_lower == district_name.lower():
654
+ if int(parent_code) == int(state_code):
655
+ district_mapping[district_name_lower] = district_code
656
+ if district_variants:
657
+ for variant in district_variants.split(','):
658
+ district_mapping[variant.strip().lower()] = district_code
659
+
660
+ return district_mapping
661
+
662
+
663
+
664
+ def create_gp_mapped_dataset(dataset, mapping):
665
+ """
666
+ Create a mapped dataset by associating gp codes with gp names in the dataset.
667
+ """
668
+ dataset['gp_name'] = dataset['gp_name'].str.strip()
669
+ dataset['gp_code'] = dataset['gp_name'].str.lower().map(mapping)
670
+ dataset.loc[dataset['gp_code'].isnull(), 'gp_code'] = -2
671
+ return dataset
672
+
673
+ def create_village_mapped_dataset(dataset, mapping):
674
+ """
675
+ Create a mapped dataset by associating gp codes with gp names in the dataset.
676
+ """
677
+ dataset['village_name'] = dataset['village_name'].str.strip()
678
+ dataset['village_code'] = dataset['village_name'].str.lower().map(mapping)
679
+ dataset.loc[dataset['village_code'].isnull(), 'village_code'] = -2
680
+ return dataset
681
+
682
+ def fetch_sub_district_mapping():
683
+ """
684
+ Fetch the Sub-District mapping from the SQLite database.
685
+
686
+ Returns:
687
+ - A list of tuples containing the Sub-District entity name, LGD code, name variants, and parent entity.
688
+ """
689
+ # Connect to the SQLite database
690
+ conn = sqlite3.connect('lgd_database.db')
691
+ cursor = conn.cursor()
692
+
693
+ # Retrieve Sub-District data from the 'Sub-Districts' table
694
+ cursor.execute("SELECT entityName, entityLGDCode, entityNameVariants, entityParent FROM sub_district")
695
+ data = cursor.fetchall()
696
+
697
+ # Close the database connection
698
+ conn.close()
699
+ return data
700
+
701
+ def populate_sub_district_mapping():
702
+ """
703
+ Populates a sub_district mapping dictionary using data from a database and a local file.
704
+
705
+ Returns:
706
+ A defaultdict containing the mapping of sub_district names to their respective codes.
707
+ """
708
+ state_dataset = pd.read_csv('data.csv')
709
+ data = fetch_sub_district_mapping()
710
+ unique_rows = state_dataset.drop_duplicates(subset=['sub_district_name'])
711
+ unique_rows_lower = unique_rows.apply(lambda x: (x['sub_district_name'].strip().lower(), x['district_code']), axis=1).tolist()
712
+ district_mapping = {}
713
+ for district_name, district_code, district_variants, parent_code in data:
714
+ for row in unique_rows_lower:
715
+ district_name_lower = row[0]
716
+ state_code = row[1]
717
+ if district_name_lower == district_name.lower():
718
+ if int(parent_code) == int(state_code):
719
+ district_mapping[district_name_lower] = district_code
720
+ if district_variants:
721
+ for variant in district_variants.split(','):
722
+ district_mapping[variant.strip().lower()] = district_code
723
+
724
+ return district_mapping
725
+
726
+
727
+
728
+ def create_sub_district_mapped_dataset(dataset, mapping):
729
+ """
730
+ Create a mapped dataset by associating Sub-District codes with Sub-District names in the dataset.
731
+ """
732
+ dataset['sub_district_name'] = dataset['sub_district_name'].str.strip()
733
+ dataset['sub_district_code'] = dataset['sub_district_name'].str.lower().map(mapping)
734
+ dataset.loc[dataset['sub_district_code'].isnull(), 'sub_district_code'] = -2
735
+ return dataset
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ sqlite3
2
+ streamlit
3
+ pandas
4
+ numpy
utils.py ADDED
@@ -0,0 +1,546 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+ import requests
3
+ import sqlite3
4
+ import hashlib
5
+
6
+ def fetch_data_from_api(code,local_body_type_code):
7
+ url = f'https://lgdirectory.gov.in/webservices/lgdws/localBodyList?stateCode={code}&localbodyTypeCode={local_body_type_code}'
8
+ payload = {}
9
+ headers = {}
10
+ response = requests.request("POST", url, headers=headers, data=payload)
11
+ if response.status_code == 200:
12
+ data = response.json()
13
+ return data
14
+ else:
15
+ print("Failed to fetch data from the API.")
16
+ return None
17
+
18
+ def calculate_hash(data):
19
+ hash_object = hashlib.sha256(str(data).encode())
20
+ return hash_object.hexdigest()
21
+
22
+
23
+ def get_level_code(tablename):
24
+ conn = sqlite3.connect('lgd_database.db')
25
+ cursor = conn.cursor()
26
+ # Execute the SQL query
27
+ query = f"SELECT entityLGDCode FROM {tablename}"
28
+ cursor.execute(query)
29
+
30
+ # Fetch all the rows from the query result
31
+ rows = cursor.fetchall()
32
+
33
+ # Extract the entityLGDCode values into a Python list
34
+ result = [row[0] for row in rows]
35
+
36
+ # Close the cursor and connection
37
+ cursor.close()
38
+
39
+ # Print the final result
40
+ return result
41
+
42
+
43
+
44
+ def check_and_update_data():
45
+ #url = 'https://lgdirectory.gov.in/webservices/lgdws/villageListWithHierarchy?subDistrictCode='
46
+ #url = 'https://lgdirectory.gov.in/webservices/lgdws/subdistrictList?districtCode='
47
+ #url = 'https://lgdirectory.gov.in/webservices/lgdws/districtList?stateCode='
48
+ #url = 'https://lgdirectory.gov.in/webservices/lgdws/blockList?districtCode='
49
+ #url = 'https://lgdirectory.gov.in/webservices/lgdws/getBlockwiseMappedGP?blockCode='
50
+
51
+ try:
52
+ result = get_level_code("states")
53
+ for code in result:
54
+ print(code)
55
+ for local_body_type_code in range(30):
56
+ try:
57
+ data = fetch_data_from_api(str(code), local_body_type_code)
58
+ if data:
59
+ data_hash = calculate_hash(data)
60
+ insert_local_body_data_in_database(data, data_hash, code)
61
+ else:
62
+ print("Failed to fetch data from the API.")
63
+ except Exception as e:
64
+ print("An error occurred:", str(e))
65
+ except Exception as e:
66
+ print("An error occurred:", str(e))
67
+
68
+ def insert_district_data_in_database(data,data_hash, code):
69
+ conn = sqlite3.connect('lgd_database.db')
70
+ cursor = conn.cursor()
71
+
72
+ # Create a table if it doesn't exist
73
+ cursor.execute('''CREATE TABLE IF NOT EXISTS district (
74
+ entityLGDCode INTEGER PRIMARY KEY,
75
+ census2001Code TEXT,
76
+ census2011Code TEXT,
77
+ entityName TEXT,
78
+ levelCode INTEGER,
79
+ levelName TEXT,
80
+ entityNameVariants TEXT,
81
+ entityParent TEXT,
82
+ dataHash TEXT
83
+ )''')
84
+
85
+ try:
86
+ # Start a transaction
87
+ conn.execute("BEGIN TRANSACTION")
88
+
89
+ # Insert the data into the table
90
+ for item in data:
91
+ entity_lgd_code = item['districtCode']
92
+ census_2001_code = item['census2001Code']
93
+ census_2011_code = item['census2011Code']
94
+ entity_name = item['districtNameEnglish']
95
+ level_code = 2
96
+ level_name = "District"
97
+ entity_name_variants = item['districtNameLocal']
98
+ entity_parent = code
99
+
100
+ cursor.execute('''INSERT INTO district (
101
+ entityLGDCode, census2001Code, census2011Code, entityName,
102
+ levelCode, levelName, entityNameVariants, entityParent,dataHash
103
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?,?)''',
104
+ (
105
+ entity_lgd_code, census_2001_code, census_2011_code, entity_name,
106
+ level_code, level_name, entity_name_variants, entity_parent,data_hash
107
+ ))
108
+ print(f"Data for districtCode: {entity_lgd_code} inserted successfully.")
109
+
110
+ # Commit the changes
111
+ conn.execute("COMMIT")
112
+ print("All data inserted successfully.")
113
+
114
+ except Exception as e:
115
+ # Rollback the transaction in case of any error
116
+ conn.execute("ROLLBACK")
117
+ print(f"Error occurred: {str(e)}")
118
+
119
+ finally:
120
+ # Close the connection
121
+ conn.close()
122
+
123
+ import sqlite3
124
+
125
+ def insert_sub_district_data_in_database(data,datahash ,code):
126
+ conn = sqlite3.connect('lgd_database.db')
127
+ cursor = conn.cursor()
128
+
129
+ # Create a table if it doesn't exist
130
+ cursor.execute('''CREATE TABLE IF NOT EXISTS sub_district (
131
+ entityLGDCode INTEGER PRIMARY KEY,
132
+ census2001Code TEXT,
133
+ census2011Code TEXT,
134
+ entityName TEXT,
135
+ levelCode INTEGER,
136
+ levelName TEXT,
137
+ entityNameVariants TEXT,
138
+ entityParent TEXT,
139
+ dataHash TEXT
140
+ )''')
141
+
142
+ try:
143
+ # Start a transaction
144
+ conn.execute("BEGIN TRANSACTION")
145
+
146
+ # Insert the data into the table
147
+ for item in data:
148
+ entity_lgd_code = item['subdistrictCode']
149
+ census_2001_code = item['census2001Code']
150
+ census_2011_code = item['census2011Code']
151
+ entity_name = item['subdistrictNameEnglish']
152
+ level_code = 3
153
+ level_name = "sub_district"
154
+ entity_name_variants = item['subdistrictNameLocal']
155
+ entity_parent = code
156
+
157
+ cursor.execute('''INSERT INTO sub_district (
158
+ entityLGDCode, census2001Code, census2011Code, entityName,
159
+ levelCode, levelName, entityNameVariants, entityParent, dataHash
160
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?,?)''',
161
+ (
162
+ entity_lgd_code, census_2001_code, census_2011_code, entity_name,
163
+ level_code, level_name, entity_name_variants, entity_parent,datahash
164
+ ))
165
+ print(f"Data for subdistrictCode: {entity_lgd_code} inserted successfully.")
166
+
167
+ # Commit the changes
168
+ conn.execute("COMMIT")
169
+ print("All data inserted successfully.")
170
+
171
+ except Exception as e:
172
+ # Rollback the transaction in case of any error
173
+ conn.execute("ROLLBACK")
174
+ print(f"Error occurred: {str(e)}")
175
+
176
+ finally:
177
+ # Close the connection
178
+ conn.close()
179
+
180
+ import sqlite3
181
+
182
+ def insert_block_data_in_database(data,datahash ,code):
183
+ conn = sqlite3.connect('lgd_database.db')
184
+ cursor = conn.cursor()
185
+
186
+ # Create a table if it doesn't exist
187
+ cursor.execute('''CREATE TABLE IF NOT EXISTS block (
188
+ entityLGDCode INTEGER PRIMARY KEY,
189
+ entityName TEXT,
190
+ levelCode INTEGER,
191
+ levelName TEXT,
192
+ entityNameVariants TEXT,
193
+ entityParent TEXT,
194
+ dataHash TEXT
195
+ )''')
196
+
197
+ try:
198
+ # Start a transaction
199
+ conn.execute("BEGIN TRANSACTION")
200
+
201
+ # Insert the data into the table
202
+ for item in data:
203
+ entity_lgd_code = item['blockCode']
204
+ entity_name = item['blockNameEnglish']
205
+ level_code = 4
206
+ level_name = "block"
207
+ entity_name_variants = item['blockNameLocal']
208
+ entity_parent = code
209
+
210
+ cursor.execute('''INSERT INTO block (
211
+ entityLGDCode, entityName,
212
+ levelCode, levelName, entityNameVariants, entityParent, dataHash
213
+ ) VALUES (?, ?, ?, ?, ?, ?, ?)''',
214
+ (
215
+ entity_lgd_code, entity_name,
216
+ level_code, level_name, entity_name_variants, entity_parent,datahash
217
+ ))
218
+ print(f"Data for block: {entity_lgd_code} inserted successfully.")
219
+
220
+ # Commit the changes
221
+ conn.execute("COMMIT")
222
+ print("All data inserted successfully.")
223
+
224
+ except Exception as e:
225
+ # Rollback the transaction in case of any error
226
+ conn.execute("ROLLBACK")
227
+ print(f"Error occurred: {str(e)}")
228
+
229
+ finally:
230
+ # Close the connection
231
+ conn.close()
232
+ import sqlite3
233
+ import urllib3
234
+
235
+ def insert_gp_data_in_database(data, datahash, code):
236
+ conn = sqlite3.connect('lgd_database.db')
237
+ cursor = conn.cursor()
238
+
239
+ # Create a table if it doesn't exist
240
+ cursor.execute('''CREATE TABLE IF NOT EXISTS gp (
241
+ entityLGDCode INTEGER PRIMARY KEY,
242
+ entityName TEXT,
243
+ levelCode INTEGER,
244
+ levelName TEXT,
245
+ entityNameVariants TEXT,
246
+ entityParent TEXT,
247
+ entityParentName TEXT,
248
+ dataHash TEXT
249
+ )''')
250
+
251
+ try:
252
+ # Start a transaction
253
+ conn.execute("BEGIN TRANSACTION")
254
+
255
+ # Insert the data into the table
256
+ for item in data:
257
+ entity_lgd_code = item['localBodyCode']
258
+ entity_name = item['localBodyNameEnglish']
259
+ level_code = 5
260
+ level_name = "Gram Panchayats"
261
+ entity_name_variants = item['localBodyNameLocal']
262
+ entity_parent = code
263
+ entity_parent_name = "block"
264
+
265
+ cursor.execute('''INSERT INTO gp (
266
+ entityLGDCode, entityName,
267
+ levelCode, levelName, entityNameVariants, entityParent,entityParentName, dataHash
268
+ ) VALUES (?, ?, ?, ?, ?, ?, ?,?)''',
269
+ (
270
+ entity_lgd_code, entity_name,
271
+ level_code, level_name, entity_name_variants, entity_parent,entity_parent_name,datahash
272
+ ))
273
+ print(f"Data for gp: {entity_lgd_code} inserted successfully.")
274
+
275
+ # Commit the changes
276
+ conn.execute("COMMIT")
277
+ print("All data inserted successfully.")
278
+
279
+ except urllib3.exceptions.TimeoutError as te:
280
+ print("Timeout error occurred while making the HTTP request.")
281
+ # Handle the timeout error here, e.g., retry the request or log the error.
282
+
283
+ except Exception as e:
284
+ # Rollback the transaction in case of any other error
285
+ conn.execute("ROLLBACK")
286
+ print(f"Error occurred: {str(e)}")
287
+
288
+ finally:
289
+ # Close the connection
290
+ conn.close()
291
+
292
+ import sqlite3
293
+ import urllib3
294
+
295
+ def insert_local_body_data_in_database(data, datahash, code):
296
+ conn = sqlite3.connect('lgd_database.db')
297
+ cursor = conn.cursor()
298
+
299
+ # Create a table if it doesn't exist
300
+ cursor.execute('''CREATE TABLE IF NOT EXISTS local_body (
301
+ entityLGDCode INTEGER PRIMARY KEY,
302
+ entityName TEXT,
303
+ entitylocalBodyTypeName TEXT,
304
+ levelCode INTEGER,
305
+ levelName TEXT,
306
+ entityNameVariants TEXT,
307
+ entityParent TEXT,
308
+ entityParentName TEXT,
309
+ dataHash TEXT
310
+ )''')
311
+
312
+ try:
313
+ # Start a transaction
314
+ conn.execute("BEGIN TRANSACTION")
315
+
316
+ # Insert the data into the table
317
+ for item in data:
318
+ entity_lgd_code = item['localBodyCode']
319
+ entity_name = item['localBodyNameEnglish']
320
+ entity_local_body_type_name = item['localBodyTypeName']
321
+ level_code = -1
322
+ level_name = "Local Body Type Name"
323
+ entity_name_variants = item['localBodyNameLocal']
324
+ entity_parent = code
325
+ entity_parent_name = "state"
326
+
327
+ cursor.execute('''INSERT INTO local_body (
328
+ entityLGDCode, entityName,entitylocalBodyTypeName,
329
+ levelCode, levelName, entityNameVariants, entityParent,entityParentName, dataHash
330
+ ) VALUES (?, ?, ?, ?, ?, ?, ?,?,?)''',
331
+ (
332
+ entity_lgd_code, entity_name,entity_local_body_type_name,
333
+ level_code, level_name, entity_name_variants, entity_parent,entity_parent_name,datahash
334
+ ))
335
+ print(f"Data for gp: {entity_lgd_code} inserted successfully.")
336
+
337
+ # Commit the changes
338
+ conn.execute("COMMIT")
339
+ print("All data inserted successfully.")
340
+
341
+ except urllib3.exceptions.TimeoutError as te:
342
+ print("Timeout error occurred while making the HTTP request.")
343
+ # Handle the timeout error here, e.g., retry the request or log the error.
344
+
345
+ except Exception as e:
346
+ # Rollback the transaction in case of any other error
347
+ conn.execute("ROLLBACK")
348
+ print(f"Error occurred: {str(e)}")
349
+
350
+ finally:
351
+ # Close the connection
352
+ conn.close()
353
+
354
+
355
+ import sqlite3
356
+
357
+ def store_village_data_in_database(data, data_hash):
358
+ conn = sqlite3.connect('lgd_database.db')
359
+ cursor = conn.cursor()
360
+
361
+ # Create a table if it doesn't exist
362
+ cursor.execute('''CREATE TABLE IF NOT EXISTS villages (
363
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
364
+ stateCode INTEGER,
365
+ stateNameEnglish TEXT,
366
+ districtCode INTEGER,
367
+ districtNameEnglish TEXT,
368
+ subDistrictCode INTEGER,
369
+ subDistrictNameEnglish TEXT,
370
+ blockCode INTEGER,
371
+ blockNameEnglish TEXT,
372
+ localBodyCode INTEGER,
373
+ localBodyTypeCode INTEGER,
374
+ localBodyNameEnglish TEXT,
375
+ villageCode INTEGER,
376
+ villageNameEnglish TEXT,
377
+ villageStatus TEXT,
378
+ dataHash TEXT
379
+ )''')
380
+
381
+ try:
382
+ # Start a transaction
383
+ conn.execute("BEGIN TRANSACTION")
384
+
385
+ # Insert the data
386
+ for item in data:
387
+ state_code = item['stateCode']
388
+ state_name = item['stateNameEnglish']
389
+ district_code = item['districtCode']
390
+ district_name = item['districtNameEnglish']
391
+ subdistrict_code = item['subDistrictCode']
392
+ subdistrict_name = item['subDistrictNameEnglish']
393
+ block_code = item['blockCode']
394
+ block_name = item['blockNameEnglish']
395
+ local_body_code = item['localBodyCode']
396
+ local_body_type_code = item['localBodyTypeCode']
397
+ local_body_name = item['localBodyNameEnglish']
398
+ village_code = item['villageCode']
399
+ village_name = item['villageNameEnglish']
400
+ village_status = item['villageStatus']
401
+
402
+ # Insert a new row
403
+ cursor.execute('''INSERT INTO villages (
404
+ stateCode, stateNameEnglish, districtCode, districtNameEnglish,
405
+ subDistrictCode, subDistrictNameEnglish, blockCode, blockNameEnglish,
406
+ localBodyCode, localBodyTypeCode, localBodyNameEnglish,
407
+ villageCode, villageNameEnglish, villageStatus, dataHash
408
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''',
409
+ (
410
+ state_code, state_name, district_code, district_name,
411
+ subdistrict_code, subdistrict_name, block_code, block_name,
412
+ local_body_code, local_body_type_code, local_body_name,
413
+ village_code, village_name, village_status, data_hash
414
+ ))
415
+ print(f"Data for stateCode: {state_code}, villageCode: {village_code} inserted successfully.")
416
+
417
+ # Commit the changes
418
+ conn.execute("COMMIT")
419
+ print("All data inserted successfully.")
420
+
421
+ except Exception as e:
422
+ # Rollback the transaction in case of any error
423
+ conn.execute("ROLLBACK")
424
+ print(f"Error occurred: {str(e)}")
425
+
426
+ finally:
427
+ # Close the connection
428
+ conn.close()
429
+
430
+
431
+
432
+ def check_write_and_update_data(data,table_name):
433
+ try:
434
+ data = data
435
+ if data:
436
+ conn = sqlite3.connect('lgd_database.db')
437
+ cursor = conn.cursor()
438
+
439
+ # Retrieve the stored data from the database
440
+ cursor.execute(f'SELECT entityLGDCode, entityName FROM {table_name}')
441
+ rows = cursor.fetchall()
442
+
443
+ changed_rows = set()
444
+
445
+ for item in data:
446
+ if table_name == 'states':
447
+ entity_lgd_code = item['stateCode']
448
+ entity_name = item['stateNameEnglish']
449
+ elif table_name == 'district':
450
+ entity_lgd_code = item['districtCode']
451
+ entity_name = item['districtNameEnglish']
452
+ elif table_name == 'sub_district':
453
+ entity_lgd_code = item['subdistrictCode']
454
+ entity_name = item['subdistrictNameEnglish']
455
+ elif table_name == 'block':
456
+ entity_lgd_code = item['blockCode']
457
+ entity_name = item['blockNameEnglish']
458
+ elif table_name == 'gp':
459
+ entity_lgd_code = item['localBodyCode']
460
+ entity_name = item['localBodyNameEnglish']
461
+ """ elif table_name == 'villages':
462
+ entity_lgd_code = item['localBodyCode']
463
+ entity_name = item['localBodyNameEnglish'] """
464
+
465
+
466
+ # Find the matching row in the database
467
+ matching_rows = [row for row in rows if row[0] == entity_lgd_code]
468
+
469
+ if matching_rows:
470
+ # Check if entityLGDCode and entityName have changed
471
+ row = matching_rows[0]
472
+ if row[0] != entity_lgd_code or row[1] != entity_name:
473
+ changed_rows.add((entity_lgd_code, entity_name))
474
+
475
+ if changed_rows:
476
+ print("The following rows have changed:")
477
+ for row in changed_rows:
478
+ entity_lgd_code, entity_name = row
479
+ print("entityLGDCode:", entity_lgd_code)
480
+ print("entityName:", entity_name)
481
+ print()
482
+ # Update the values in the database
483
+ cursor.execute(f"UPDATE {table_name} SET entityName = ? WHERE entityLGDCode = ?", (entity_name, entity_lgd_code))
484
+ conn.commit()
485
+ print("Data updated successfully!")
486
+
487
+ else:
488
+ print("Data has not changed.")
489
+
490
+ # Update the data hash in the database
491
+
492
+ conn.close()
493
+
494
+ else:
495
+ print("Failed to fetch data from the API.")
496
+
497
+ except Exception as e:
498
+ print("An error occurred:", str(e))
499
+
500
+
501
+ def fetch_data_from_api_update(url):
502
+ payload = {}
503
+ headers = {}
504
+ response = requests.request("POST", url, headers=headers, data=payload)
505
+ if response.status_code == 200:
506
+ data = response.json()
507
+ return data
508
+ else:
509
+ print("Failed to fetch data from the API.")
510
+ return None
511
+
512
+
513
+ def update_all_data():
514
+ #url = 'https://lgdirectory.gov.in/webservices/lgdws/villageListWithHierarchy?subDistrictCode='
515
+ #url = 'https://lgdirectory.gov.in/webservices/lgdws/subdistrictList?districtCode='
516
+ #url = 'https://lgdirectory.gov.in/webservices/lgdws/districtList?stateCode='
517
+ #url = 'https://lgdirectory.gov.in/webservices/lgdws/blockList?districtCode='
518
+ #url = 'https://lgdirectory.gov.in/webservices/lgdws/getBlockwiseMappedGP?blockCode='
519
+ name_pair = {'district':'states','sub_district':'district','block':'district','gp':'block'}
520
+ for key, value in name_pair.items():
521
+ table_name = key
522
+ print('table name:', table_name)
523
+ try:
524
+ result = get_level_code(value)
525
+ for code in result:
526
+ try:
527
+ if table_name == 'states':
528
+ url = f'https://lgdirectory.gov.in/webservices/lgdws/stateList'
529
+ elif table_name == 'district':
530
+ url = f'https://lgdirectory.gov.in/webservices/lgdws/districtList?stateCode={code}'
531
+ elif table_name == 'sub_district':
532
+ url = f'https://lgdirectory.gov.in/webservices/lgdws/subdistrictList?districtCode={code}'
533
+ elif table_name == 'block':
534
+ url = f'https://lgdirectory.gov.in/webservices/lgdws/blockList?districtCode={code}'
535
+ elif table_name == 'gp':
536
+ url = f'https://lgdirectory.gov.in/webservices/lgdws/getBlockwiseMappedGP?blockCode={code}'
537
+ data = fetch_data_from_api_update(url)
538
+
539
+ if data:
540
+ check_write_and_update_data(data,table_name)
541
+ else:
542
+ print("Failed to fetch data from the API.")
543
+ except Exception as e:
544
+ print("An error occurred:", str(e))
545
+ except Exception as e:
546
+ print("An error occurred:", str(e))