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.

#!/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)

What is an Election District in NY State?

For sometime I’ve puzzled about what an election district is. If you reside in NY State, you probably live in an election district, and if you volunteer for a political campaign, you probably walked an election district. But you probably have no idea what an election district is.

Defining an Election District.

Article 15 of the Election Law, Section 4-100 defines an election district as:

4-100. Election districts; creation and alteration.

1. The State of New York shall be divided into election districts which shall be the basic political subdivision for purposes of registration and voting as provided in this chapter.

2. The creation, consolidation, division or alteration of election districts shall be done by the board of elections.

3. a. Each election district shall be in compact form and may not be partly within and partly without a ward, town, city, a village which has five thousand or more inhabitants and is wholly within one town, or a county legislative, assembly, senatorial or congressional district. Except as provided in paragraph b of this subdivision, election district boundaries, other than those boundaries which are coterminous with the boundaries of those political subdivisions mentioned in this paragraph, must be streets, rivers, railroad lines or other permanent characteristics of the landscape which are clearly visible to any person without the need to use any technical or mechanical device. An election district shall contain not more than nine hundred fifty registrants (excluding registrants in inactive status) or, with the approval of the county board of elections, not more than eleven hundred fifty registrants (excluding registrants in inactive status), but any election district may be divided for the convenience of the voters.

Simply said an election district is:

  • Defined by the County Board of Elections
  • Normally consists of fewer then 950 persons, but not necessarily equal in population
  • Has to be compact and drawn for the “convenience of the voters”
  • Must follow blocks, as defined by Census bureau (see below)
  • Must follow the boundaries of:
    • Congressional Districts
    • State Senate Districts
    • Assembly Districts
    • County Legislature Districts (in “reformed” county charter counties)
    • City Ward Districts (in cities)
    • Municipal Boundaries (city, town, village boundaries)

What is a Census Block?

All political districts in NY State are a series of Census Blocks, the smallest unit of population count by the Federal Government. Census blocks are defined as:

Census Blocks are smallest possible “city” blocks in area, bounded by:

  • Streets, Roads, Highways
  • Railroad lines
  • Water bodies
  • Rivers, Streams, Creeks
  • Municipal Boundaries (city, town, village boundaries)

Except for municipal boundaries, which are usually defined by historical surveys in a variety of measures, all other boundaries are clearly visible to anybody who visit the boundary.

What is a “Legislative District”?

A “legislative district”, e.g.

  • Congressional Districts
  • State Senate Districts
  • Assembly Districts
  • County Legislature Districts (in “reformed” county charter counties)
  • City Ward Districts (in cities)

… is a district that is defined by a grouping of Census Blocks.

Each legislative district under Reynolds v Simms must have an equal population or relatively close to an equal population. State constitutional and statutory restrictions allow for some variance in population, but differences of population greater then 10% in each district are not allowed.

Each legislative district is drawn based on often very detailed constitutional and statutory restrictions. Compact and contiguous restrictions are well known, but state constitutional and statutory restrictions are less known. State legislative districts are constitutionally prohibited from dividing towns but not cities and villages, except were the town is larger then maximum size of the districts.

Under the Civil Rights Act of 1965, it is unlawful to “draw” legislative districts in a way that reduces racial minority voting opportunities. In other words, it’s unlawful to break up groups of census blocks of a certain race. You can not split up a neighborhood that consists of many census blocks of African Americans or Asian into multiple legislative districts. All legislative districts are subject to review by the US Department of Justice to assure there is no dilution of minority votes.

Often legislative districts are drawn by partisan bodies with ideological agendas in mind. Usually when legislative districts are drawn, there is a lot of consideration of past election results and partisan enrollments. While it’s impossible to know how each census block will vote (as voting enrollment is broken down by election district), and census blocks change every ten years prior to reapportionment, one can make an educated guess on how people in a certain neighborhood will vote in the future.

Legislative districts are simply roughly equal population groupings of census blocks, taking in consideration of legal restrictions and political considerations.

How Election Districts Are Drawn.

After reapportionment of congress, the state legislature, county legislatures, and city wards has been approved by each body, Election Districts are re-drawn. All areas in the same congress, state senate, state assembly, county legislature, city ward, and municipality are grouped together.

If there less then 950 active voters in the resulting districts, then the district consists of those grouping of areas. Active voters are those who have voted in one election since the prior two gubernatorial elections. In 2012, a voter would have to vote once between 2006-2011 to be considered active. If a district consists of more then 950 active voters it must be broken up after the lines are drawn. There is no political advantage by breaking up common districts already drawn, so it’s more done as matter of convenience and simplicity.

Having more then 950 active voters one election district would lead to long lines at polling place, and would not provide for particularly useful election results for further analysis. The election law allows an election district to grow to 1,150 active voters between redistricting of legislative districts, however if it grows beyond that number, it must be split into multiple districts.

Election districts aren’t generally drawn for political purposes. All the potentially controversial work was done in drawing the various legislative districts, so drawing election districts really not that important, even if the results of elections are often used to predict future results.

Snow Depth – Sunday November 28

With the light snow locally, plus the decent amount of snow they got in the Berskhires the past few days, there is a noticeable snow pack on the ground. Will be interesting the next good clear day to look at the satellite imagery. You can see some of the snow on the ground yesterday’s Terra satellite but where there was snow, there also was a lot of high clouds.
 

Scotland’s World Leader

#139 Offshore Wind: Scotland’s World Leader

11/25/21 by Reby Media

Web player: https://podcastaddict.com/episode/131791070
Episode: https://media.blubrry.com/engineeringmatters/content.blubrry.com/engineeringmatters/ENG_EP_139_FUGRO_mixdown_mp3_ver_3.mp3

At 4.1GW Berwick Bank is planning to become the largest offshore windfarm in the world. This is enough generating capacity to power Scotland’s homes twice over and it would play a critical role in meeting the UK’s wind energy targets. But in order to move ahead it must overcome a range of challenges from obtaining planning consent to understanding the incredibly complex and variable ground conditions. Project owner SSE Renewables, and its supply chain will be tested like never before. Guests Richard Holland, Geotechnical Package Lead Engineer Berwick Bank Wind Farm, SSE Renewables Alex Meredith, Project Director Berwick Bank Wind Farm, SSE Renewables Pamela Phillips, Project Manager Berwick Bank Wind Farm, SSE Renewables Julia Roope, Global Business Development Manager Offshore Wind, Fugro Mark Schreiber, Project Director, Fugro Partner Fugro is the world’s leading Geo-data specialist, collecting and analysing comprehensive information about the Earth and the structures built upon it. Through integrated data acquisition, analysis and advice, Fugro unlocks insights from Geo-data to help clients design, build and operate their assets in a safe, sustainable and efficient manner.

Rather Just Burn It

Burn Baby Burn

I was thinking the other day, what a pain modern, recycling really is. Having to wash out containers, carefully sort and crush them, put them out on a specific day. It was so much easier living out in the country, when you could just toss them in the trash, take them out to the burn barrel, burn them to a crisp. I will be quite happy when I own my own land, and can burn what I want.