forked from millerjes37/CSV---GeoJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGISer.py
90 lines (79 loc) · 3.14 KB
/
GISer.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import pandas as pd
import googlemaps
import json
# Function to load data
def load_data(file_path):
try:
return pd.read_csv(file_path)
except Exception as e:
print(f"Error loading the data: {e}")
return None
# Function to consolidate address
def consolidate_address(row):
try:
return f"{row['ADDRESS']}, {row['CITY']}, {row['STATE']} {row['ZIP']}"
except KeyError as e:
print(f"Missing column in data: {e}")
return None
# Initialize the Google Maps client with your API key
gmaps = googlemaps.Client(key='AIzaSyCRfdzXBrBWPuFIpXmj_jTCepp6b3oJiGg')
# Function to geocode an address using Google Maps
def geocode_address(address):
if not address:
return None, None
try:
geocode_result = gmaps.geocode(address)
if geocode_result:
location = geocode_result[0]['geometry']['location']
return location['lat'], location['lng']
else:
print(f"No results found for {address}")
return None, None
except Exception as e:
print(f"Error geocoding {address}: {e}")
return None, None
# Load the dataset
df = load_data('/Users/jacksonmiller/Desktop/School Mapping/GISMAP Scripts/SCHL_dataset.csv')
if df is None:
raise Exception("Failed to load data.")
# Consolidate address components into a single column
df['Full_Address'] = df.apply(consolidate_address, axis=1)
# Apply geocoding
df['coords'] = df['Full_Address'].apply(geocode_address)
# Filter out rows where geocoding was not successful
df = df[df['coords'].apply(lambda x: x is not None and x != (None, None))]
# Check if the DataFrame is empty after filtering
if not df.empty:
# Split the coordinates into latitude and longitude for easier access
df[['Latitude', 'Longitude']] = pd.DataFrame(df['coords'].tolist(), index=df.index)
# Create the GeoJSON structure
geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [row['Longitude'], row['Latitude']],
},
"properties": {
"school_id": row.get('IDOE_SCHOOL_ID', ''),
"school_name": row.get('SCHOOL_NAME', ''),
"principal": f"{row.get('PRINCIPAL_FIRST_NAME', '')} {row.get('PRINCIPAL_LAST_NAME', '')}",
"email": row.get('PRINCIPAL_EMAIL', ''),
"phone": row.get('PHONE', ''),
"address": row.get('ADDRESS', ''),
"city": row.get('CITY', ''),
"state": row.get('STATE', ''),
"zip": row.get('ZIP', ''),
"homepage": row.get('SCHOOL_HOMEPAGE', ''),
"choice_flag": row.get('CHOICE_FLAG', False)
},
} for idx, row in df.iterrows()
],
}
# Save the GeoJSON to a file
with open('/Users/jacksonmiller/Desktop/School Mapping/GISMAP Scripts/SCHL_schools_geojson.geojson', 'w') as f:
json.dump(geojson, f)
else:
print("No valid geocoded results to create GeoJSON.")