-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseGeocoder
More file actions
45 lines (37 loc) · 1.75 KB
/
Copy pathReverseGeocoder
File metadata and controls
45 lines (37 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
import pandas as pd
import reverse_geocoder as rg
# --- 1. Load your CSV data ---
input_file_path = "../content/gdrive/My Drive/Colab_Notebooks/5G_coverage/Simplified_Rand_Real.csv"
df = pd.read_csv(input_file_path)
#Get output directory and create if not exist
output_dir = "../content/gdrive/My Drive/Colab_Notebooks/5GCoverageResults/"
os.makedirs(output_dir, exist_ok=True) # Create the output directory if it doesn't exist
# --- 2. Prepare coordinates for batch geocoding ---
coordinates = list(zip(df['SiteLatitude_decDeg'], df['SiteLongitude_decDeg']))
# --- 3. Perform batch reverse geocoding ---
results = rg.search(coordinates)
# --- 4. Extract state information and add to DataFrame ---
states = []
for result in results:
state = result.get('admin1') # First try admin1
if state is None or state == "":
state = result.get('admin2') # If admin1 is missing, try admin2
if state is None or state == "":
state = result.get('name', 'Not Found') # If both are missing, use 'name' or 'Not Found'
states.append(state)
df['State'] = states
# --- 5. Separate into files by state ---
for state in df['State'].unique():
if state != "Not Found": # Skip 'Not Found' entries
state_df = df[df['State'] == state]
output_file_path = os.path.join(output_dir, f"{state}_sites.csv")
state_df.to_csv(output_file_path, index=False)
print(f"Created {output_file_path}")
else:
not_found_df = df[df['State'] == "Not Found"]
output_file_path = os.path.join(output_dir, "sites_not_found.csv")
not_found_df.to_csv(output_file_path, index=False)
print(f"Created {output_file_path}")
# --- 6. (Optional) Display the first few rows with the new State column ---
print(df.head())