Skip to content

Instantly share code, notes, and snippets.

@fitnr
Last active March 12, 2016 04:55
Show Gist options
  • Select an option

  • Save fitnr/3bf44634b3af7d6fcad6 to your computer and use it in GitHub Desktop.

Select an option

Save fitnr/3bf44634b3af7d6fcad6 to your computer and use it in GitHub Desktop.

First, install svgis and breaks. They require some hefty prerequisites. sorry. The last step requires ImageMagick.

brew install gdal numpy scipy # os x
apt-get install python-numpy python-scipy gdal-bin # linux
pip install svgis breaks

Download get-tiger. Optional: edit it to include the Census data fields you desire.

# this will download a counties SHP file and add in some default data fields
make COUNTY YEAR=2014

We can find some metadata what we just downloaded in get-tiger/data.json. Here's something cool: B25035 MEDIAN YEAR STRUCTURE BUILT.

Let's make a chloropleth of that!

# Create a new SHP file with data bins for Year Built
breaks 2014/county/tl_2014_us_county.shp B25035_001 county_yearbuilt.shp

OK, great, now let's turn that into an SVG.

First we'll need some CSS. Let's turn to colorbrewer and grab a scheme with five classes:

# save as style.css
.bin_0{fill:rgb(255,255,212)}
.bin_1{fill:rgb(254,217,142)}
.bin_2{fill:rgb(254,153,41)}
.bin_3{fill:rgb(217,95,14)}
.bin_4{fill:rgb(153,52,4)}
.bin_None{fill:rgb(225,225,225)}

(The None value will show up if any of the rows have missing data)

And let's pick a map projection. This one looks good: USA Contiguous Albers Equal Area Conic

Now draw a map!

svgis draw county_yearbuilt.shp --style style.css --crs epsg:102003 --scale 8000 --bounds -125 24 -66 50 -o county_yearbuilt.svg

Taking that step-by-step:

--style style.css # use the CSS file with the bins
--crs epsg:102003 # project in USA Contiguous Albers Equal Area Conic
--scale 8000 # 8 KM to one svg unit. An svg unit might be represented as a single pixel.
--bounds -125 24 -66 50 # The bounds for our map, minx miny maxy maxy (in other words West, South, East, North)
-o county_yearbuilt.svg # save the file

Now you can open up the svg in an editor and play with it!

Or, convert to a png:

convert -density 1800 county_yearbuilt.svg -trim +repage county_yearbuilt.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment