Another Example of GeoPandas and Python Spatial Joins

Another Example of GeoPandas and Python Spatial Joins πŸ—Ί

Yesterday, I posted a much more complicated piece of code that pulled addresses from the SAM (State Address Management) database and did a spatial join to add a column to the file with Assembly District and Municipality. This was a bit too complex, so I made a simpler one for other purposes that doesn’t require the coordinates to be obtained from SAM.

This python script takes two parameters:

  • Path to a CSV file that contains an X and Y coordinate
  • Path to a Shapefile or Geopackage to Join Against

Then the code will create a new CSV file with the spatially joined attributes pulled from the Shapefile. I have only run it on a few large data sets, but I found it took roughly 1 second to join 1,000 records from call to end of end of script.

01#!/usr/bin/python
02 
03import requests,sys,json,os,csv
04 
05import pandas as pd
06import geopandas as gpd
07 
08lines=[]
09 
10# read list of addresses from parameter 1
11with open(sys.argv[-2], newline='') as csvfile:
12    for line in csv.DictReader(csvfile):
13        lines.append(line)
14 
15# convert to pandas
16locPd = pd.DataFrame(lines,columns=lines[0].keys())
17locPd.convert_dtypes()
18 
19locPd = gpd.GeoDataFrame(locPd,  geometry=gpd.points_from_xy(locPd.x.astype('float32'), locPd.y.astype('float32')))
20 
21# run spatial joins against parameter 2
22ad = gpd.read_file(sys.argv[-1])
23locPd = gpd.sjoin(locPd, ad, op="within")
24 
25# remove added geometery and index columns
26del locPd['geometry']
27del locPd['index_right']
28 
29# write pandas back to out csv
30locPd.to_csv (os.path.splitext(sys.argv[-2])[0]+'-output.csv', index = False, header=True)

1 Trackback or Pingback

Leave a Reply

Your email address will not be published. Required fields are marked *