-
Notifications
You must be signed in to change notification settings - Fork 0
/
census_finder.py
34 lines (27 loc) · 1.12 KB
/
census_finder.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
import geopandas as gpd
from shapely.geometry import Point
# Location of the GeoJSON file
geojson_location = "sf-tracts-2020-GEOID.geojson"
# Load the GeoJSON file into a GeoDataFrame
gdf = gpd.read_file(geojson_location)
# Filter out invalid geometries
gdf = gdf[gdf.is_valid]
def get_census_tract_info(latitude, longitude):
# Create a Point object from the provided coordinates
point = Point(longitude, latitude)
# Perform the spatial query to find which polygon contains the point
for index, row in gdf.iterrows():
if row['geometry'].contains(point):
return {
"TRACTCE20": row['TRACTCE20'],
"GEOID": row['GEOID'],
}
return "No census tract found for the given coordinates."
# Example usage
latitude = 37.7749 # Replace with actual latitude
longitude = -122.4194 # Replace with actual longitude
census_tract_info = get_census_tract_info(latitude, longitude)
if census_tract_info:
print(f"Census Tract Info: {census_tract_info}")
else:
print("Could not determine the census tract for the provided coordinates.")