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.
#!/usr/bin/python
import requests,sys,json,os,csv
import pandas as pd
import geopandas as gpd
lines=[]
# read list of addresses from parameter 1
with open(sys.argv[-2], newline='') as csvfile:
for line in csv.DictReader(csvfile):
lines.append(line)
# convert to pandas
locPd = pd.DataFrame(lines,columns=lines[0].keys())
locPd.convert_dtypes()
locPd = gpd.GeoDataFrame(locPd, geometry=gpd.points_from_xy(locPd.x.astype('float32'), locPd.y.astype('float32')))
# run spatial joins against parameter 2
ad = gpd.read_file(sys.argv[-1])
locPd = gpd.sjoin(locPd, ad, op="within")
# remove added geometery and index columns
del locPd['geometry']
del locPd['index_right']
# write pandas back to out csv
locPd.to_csv (os.path.splitext(sys.argv[-2])[0]+'-output.csv', index = False, header=True)
You can get anything you want at Alice’s Restaurant, exceptin Alice. Don’t get caught driving garbage around town, but at least have a big bonfire to burn up all those wrappers and stay warm while cooking the turkey.
I am in the process of getting things cleaned up and organized prior to heading up to the wilderness tomorrow. π§Ό Soaking some pinto beans so I can enjoy them at camp. π« I keep watching to see if the forecast won’t be a disaster with a ton of blowing snow, but whatever. βοΈ Honestly, it doesn’t look like there is that much snow in the Eastern Adirondacks and I don’t plan to camp that far from the state highway, and I will bring my snow shovel, road salt and plastic sled π· and should be good. Worse comes to worse, I will have to drag gear back to camp. But I don’t think there is that much snow where I’m heading tomorrow.
Doing a bit more listening to Alice’s Restaurant then heading out for a bike ride, π² probably the one I promised to do to Voorheeesville yesterday but didn’t happen because it was cloudy and gray and the work project took ten times longer then necessary as I had to code it on my phone as I left my laptop charger π»π at work. I want leave early tomorrow, I might do a hike on the way up but I’m a little nervous about snow, so I want to almost get to camp earlier and get set up and then ride and hike from there most likely. Maybe I could do a quick hike on the way up, πΆ I am a bit undecided. π€·ββοΈ I have some nice hikes and bike rides planned once I’m up at camp, especially on Saturday, though Sunday I think with the snow coming I will stay at camp and not venture out until Monday when it should be warm enough that roads are decent by midday. π¬οΈ I am hoping it won’t be windy. The different models have very different ideas on how snowy and cold Sunday will be. I have propane heater and the tank is fairly full, so it should be good.
Then it’s showering and the family Thanksgiving dinner at two o’clock. π¦ First time I’ve been my neice and nephew since July along with my sister and her partner. π΄ π₯§ Should be good to be together, and because it’s mid-day I hopefully won’t have to drive home in the dark, though I don’t mind as my night vision is so much better since LASIK. I am going to donate my unopened boxes contact lens π to the Lions Club box the next time I’m at the library, I feel that’s probably better then just tossing them into the fire π₯ and having to fish out all of those metal backing plates. It will be good to get together with the whole family, π¨βπ¨βπ¦ as who knows how many more chances we’ll all be together, I realize in a few weeks my dad will by 79 and my mom 77. I’m actually impressed they’re doing all the cooking themselves. π§βπ³ Full traditional Thanksgiving dinner, probably not what I would make myself, I’d probably keep it simpler. Maybe raise my own turkey some day. π π₯« Who knows. I’m actually reading another one of Gale Damerow’s book, this one her tome on Raising Backyard Farm Animals. Assuming it actually downloads and loads properly without internet service, that may be a lot of what I read when I’m up camping in the Adirondack Wilderness π² the next few days.
Happy Thanksgiving! The Alice's Restaurant massacree took place a half century ago today, or so the song says. Don't get caught driving garbage around on this Thanksgiving, and certainly not dumping it over a cliff where you might get caught. This song is probably why they have large fences installed in Massachusetts near all of the highway rest areas, to discourage people from dumping garbage.
"Likely a corruption of English/French massacre, possibly from the Missouri French dialect. Originated in the Ozark Mountains, date unknown. Attested no later than 1967 in the song "Alice's Restaurant Massacree" by Arlo Guthrie."