Code Monkey home page Code Monkey logo

california_2016_election_precinct_data's Introduction

California_2016_Election_precinct_data

California precincts in TopoJSON format

When developing geotuple.org, I was looking for detailed voting district (precinct) boundaries and came across this repository from the Los Angeles Times Data Viz team. They had already done an excellent job of organizing all the information (published here), but I thought I could tidy up the geographic data just a bit for my needs.

Results

The original shapefile (not provided in this repo) is 95M while the resulting TopoJSON comes in at 10M.

$ du -hc merged.*
1.3M	merged.dbf
4.0K	merged.prj
 93M	merged.shp
204K	merged.shx
 95M	total

$ du -h ca*
 10M	ca_precincts_topo.json

However, as the resulting file has been simplified, a more equitable comparison is a reduction of about 50%.

Comparison

Fig 1:

Process

The main objective is to simplify the polygon geometry, by reducing #coordinates, and to take advantage of the topology encoding provided by TopoJSON. Secondly, to load the data into a PostGIS db.

Prerequisites

I used the following tools:

Combining county shapefiles

This inital step combines the original county shapefiles (not provided in this repo) into one:

$ for f in shapefiles/*.shp; do ogr2ogr -update -append merged.shp $f -f "ESRI Shapefile"; done;

Projecting to meters

First of all, we want to project to a coordinate system based on a consistent unit (as opposed to decimal degrees). For California, an appropriate equal-area projection is "California Albers" (EPSG=3310) using meters:

$ ogr2ogr -f "ESRI Shapefile" merged2.shp -t_srs EPSG:3310 merged.shp

Eliminating gaps between county polygons

In order for TopoJSON to do it's "thing", we need to have coincident polygon boundaries. This is not always the case, as seen in this example between the Fresno and Madera counties along the San Joaqin River: Fig 2:

Overcoming this issue involves a fairly length process, centering on the ArcGIS Eliminate command (the specific commands used are detailed in the arcgis/eliminate_process.xml file).

This collapses the gap (sliver) as shown here: Fig 3:

The resulting shapefile can be found in arcgis/arc_elim_shapefile.zip.

Converting to TopoJSON

First convert the output of the ArcGIS process to GeoJSON:

$ ogr2ogr -f "GeoJSON" merged2_geo.json -select "orig_fid pct16" arc_elim.shp

Finally, create the long awaited TopoJSON, while in the process further simplifying the geometry:

$ geo2topo merged2_geo.json > merged2_topo.json
$ toposimplify -p 1 -f merged2_topo.json > merged3_topo.json
$ topoquantize 1e6 merged3_topo.json > ca_precincts_topo.json

For reference, see: Command-Line Cartography, Part 3

Loading data into PostGIS (optional)

The first step is to extract the relevant precinct voting information from the original csv file (not provided in this repo):

$ head -24569 final-results/all_precinct_results.csv > all_precinct_results2.csv

We can then import this file into the table precinct_results by executing the SQL script import_precincts.sql:

# \i postgis/import_precincts.sql
# SELECT COUNT(*) FROM precinct_results;
 count 
-------
 24568
(1 row)

Secondly, we import the precinct boundaries into the table precinct using OGR (optionally projecting back to WGS84):

$ ogr2ogr -f PostgreSQL PG:"dbname='YOUR_NAME' user='YOUR_USER' password='YOUR_PW'" -nln precinct -select "pct16" -lco GEOM_TYPE="geography" -lco GEOMETRY_NAME=geog -s_srs EPSG:3310 -t_srs "EPSG:4326" ca_precincts_topo.json

We can now access the voting data spatially via a join:

# SELECT b.pres_trump_per FROM precinct a INNER JOIN precinct_results b ON a.pct16=b.pct16 WHERE a.pct16 = '037-7300001A';
 pres_trump_per 
----------------
            100
(1 row)

Issues

  • Attempting to convert the unsimplified shapefile to TopoJSON failed:
$ ogr2ogr -f "GeoJSON" merged.geojson -t_srs EPSG:3310 merged.shp
$ geo2topo pct16=merged.geojson > merged.topojson
 buffer.js:495
    throw new Error('"toString()" failed');
    ...

Seemingly because of hitting a limitation in nodejs (presumably due to too many coordinates).

Simplifying the input fixes the problem:

$ ogr2ogr -f "GeoJSON" merged.geojson -simplify 5 -t_srs EPSG:3310 merged2.shp

california_2016_election_precinct_data's People

Contributors

rhansson avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.