0

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.

Polygon coordinates Zip Code 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.

I've found some similar questions here and here but weren't able to fit it into my script -- I'm new to GIS but fixed on getting going with it.

3
  • Can you add a screenshot showing the polygons and points? Can a point be inside a polygon? Can multiple points be inside one polygon?
    – BERA
    Jul 4, 2022 at 12:43
  • Added @BERA . Points can be inside the polygon but they don't have to. Broadly, I want to match a voting precinct to the nearest coordinate on the zip code lat/long list - even if the coordinate is not exactly inside the polygon. it's tough to show the entire polygon because its many coordinates
    – TiDrizA
    Jul 4, 2022 at 13:00
  • @BERA hey saw you deleted sorry if I asked something wrong
    – TiDrizA
    Jul 7, 2022 at 14:22

1 Answer 1

-1

You can use NNJoin extension in QGIS3. https://plugins.qgis.org/plugins/NNJoin/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.