I've got a list of voting precinct geospatial polygons that need to join to a different table with just lat/long points for each zip code. How can I join each of the precinct polygons to its nearest zip code lat/long point? Polygons are not overlapping and many points will be joined to multiple polygons. Many polygons' nearest point are outside of the Polygon coordinates.
This article has helped me to build this script which quickly maps each of my points to the coordinate polygon it fits inside of:
import pandas as pd import geopandas as gpd df_zip = pd.read_csv('zipdata.csv') gdf_pts = gpd.GeoDataFrame(df_zip, geometry=gpd.points_from_xy(df_zip.Longitude, df_zip.Latitude)) precinct_file = 'precinctdata.geojson' gdf_coord = gpd.read_file(precinct_file) sjoined_listings = gpd.sjoin(gdf_pts, gdf_coord, op=”within”) # where zipdata.csv includes a latitude and longitude column along w/ other zip code data # and precinctdata.geojson is a geojson file that includes polygons for over 100k voting precincts
Now instead of mapping each zip point to the precinct polygon it belongs to, I want to map each polygon to its nearest point so that ALL precinct polygons will have a corresponding zip code point that is the nearest point to it. Many points will map up to multiple polygons as there's over 140k precincts and less than 42k zip codes in the dataset.