{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Extract Wake County blocks from to a new shapefile\n", "This notebook queries the NC statewide block file [link](https://www2.census.gov/geo/tiger/TIGER2010BLKPOPHU/tabblock2010_37_pophu.zip) for those in Wake County (CoFIPS = 183) and saves to a new shapefile. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "#Package imports\n", "import geopandas as gpd\n", "from datetime import datetime\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "#User inputs\n", "stateFIPS = '37'\n", "countyFIPS = '183'\n", "outFilename = './data/spatial/tabblock2010_{}{}_pophu.shp'.format(stateFIPS,countyFIPS)\n", "outMetaFilename = outFilename[:-4] + \"_README.txt\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Downloading blocks for 37; this take a few minutes...\n" ] } ], "source": [ "#Pull the state block data for the supplied FIPS code\n", "print(\"Downloading blocks for {}; this take a few minutes...\".format(stateFIPS))\n", "dataURL = 'https://www2.census.gov/geo/tiger/TIGER2010BLKPOPHU/tabblock2010_{}_pophu.zip'.format(stateFIPS)\n", "fcBlocks = gpd.read_file(dataURL)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Subsetting data for County FIPS \n" ] } ], "source": [ "#Subset county blocks\n", "print(\"Subsetting data for County FIPS \".format(countyFIPS))\n", "fcWake_blocks = fcBlocks[fcBlocks.COUNTYFP10 == countyFIPS]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Saving to ./data/spatial/tabblock2010_37183_pophu.shp\n" ] } ], "source": [ "#Save to a file\n", "print(\"Saving to {}\".format(outFilename))\n", "fcWake_blocks.to_file(outFilename,filetype='Shapefile')\n", "#Save projection to .prj file\n", "with open(outFilename[:-3]+'prj','w') as outPrj:\n", " outPrj.write('GEOGCS[\"GCS_North_American_1983\",DATUM[\"D_North_American_1983\",SPHEROID[\"GRS_1980\",6378137.0,298.257222101]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "#Write metadata file\n", "timeStamp = datetime.now().strftime(\"%Y-%m-%d\")\n", "with open(outMetaFilename,'w') as outFN:\n", " outFN.write('''Census block data for FIPS {0}{1} extracted from\n", "{2} on {3}\n", "'''.format(stateFIPS,countyFIPS,dataURL,timeStamp))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }