|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 6, |
| 6 | + "metadata": { |
| 7 | + "collapsed": true |
| 8 | + }, |
| 9 | + "outputs": [], |
| 10 | + "source": [ |
| 11 | + "import pandas as pd\n", |
| 12 | + "import geopandas as gpd\n", |
| 13 | + "from shapely.geometry import Point, LineString, Polygon" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "code", |
| 18 | + "execution_count": null, |
| 19 | + "metadata": { |
| 20 | + "collapsed": true |
| 21 | + }, |
| 22 | + "outputs": [], |
| 23 | + "source": [ |
| 24 | + "# Add block-group level housing costs from H+T to 2014 parcel file" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": 3, |
| 30 | + "metadata": {}, |
| 31 | + "outputs": [], |
| 32 | + "source": [ |
| 33 | + "df = pd.read_csv(r'R:\\SoundCast\\Inputs\\2014_esd\\landuse\\parcels_urbansim.txt',\n", |
| 34 | + " delim_whitespace=True)" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": 14, |
| 40 | + "metadata": { |
| 41 | + "collapsed": true |
| 42 | + }, |
| 43 | + "outputs": [], |
| 44 | + "source": [ |
| 45 | + "# Load parcel geography as geoseries\n", |
| 46 | + "geog = df.apply(lambda x: Point((float(x.xcoord_p), float(x.ycoord_p))), axis=1)\n", |
| 47 | + "geog = gpd.GeoSeries(geog)\n", |
| 48 | + "geog.crs = {'init': 'epsg:2285'} # WA State Plane (north)\n", |
| 49 | + "df['lat_lon_geog'] = geog\n", |
| 50 | + "df['geometry'] = geog.to_crs(epsg='2285') # Replace default geometry field with the projected epsg=2285 projection to match shapefiles\n", |
| 51 | + "df.crs = {'init': 'epsg:2285'}" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "code", |
| 56 | + "execution_count": 9, |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [], |
| 59 | + "source": [] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": 20, |
| 64 | + "metadata": {}, |
| 65 | + "outputs": [], |
| 66 | + "source": [ |
| 67 | + "def spatial_join(gdf1, gdf2, keep_field, rename_field, crs):\n", |
| 68 | + " \"\"\"Spatial join two geodataframes, left intersect with base on gdf1\"\"\"\n", |
| 69 | + " df = gpd.sjoin(gdf1, gdf2[['geometry',keep_field]], how=\"left\", op='intersects')\n", |
| 70 | + " df = df.rename(columns={keep_field: rename_field})\n", |
| 71 | + " df.crs = crs\n", |
| 72 | + " \n", |
| 73 | + " return df" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": 21, |
| 79 | + "metadata": {}, |
| 80 | + "outputs": [], |
| 81 | + "source": [ |
| 82 | + "# Join parcels to block groups\n", |
| 83 | + "block_group = gpd.GeoDataFrame.from_file(r'R:\\Brice\\blockgrp2010.shp')\n", |
| 84 | + "block_group.crs = {'init' :'epsg:2285'} # WA State Plane (north)\n", |
| 85 | + "\n", |
| 86 | + "_df = spatial_join(gdf1=df, gdf2=block_group, keep_field='GEOID10', rename_field='final_bg', crs=df.crs)\n", |
| 87 | + "\n", |
| 88 | + "# Attach data from block group level\n", |
| 89 | + "costs = pd.read_csv(r'C:\\Users\\Brice\\travel-modeling\\costs\\htaindex_data_blkgrps_53.csv')\n", |
| 90 | + "\n", |
| 91 | + "_df['final_bg'] = _df['final_bg'].astype('float64')\n", |
| 92 | + "new_parcels = pd.merge(_df,costs,how='left',left_on='final_bg',right_on='blkgrp')\n", |
| 93 | + "\n", |
| 94 | + "new_parcels.to_csv(r'R:\\SoundCastDocuments\\metrics\\housing_transport_costs\\parcel_housing_costs_2014_lodes.csv',\n", |
| 95 | + " index=False)" |
| 96 | + ] |
| 97 | + } |
| 98 | + ], |
| 99 | + "metadata": { |
| 100 | + "kernelspec": { |
| 101 | + "display_name": "Python 2", |
| 102 | + "language": "python", |
| 103 | + "name": "python2" |
| 104 | + }, |
| 105 | + "language_info": { |
| 106 | + "codemirror_mode": { |
| 107 | + "name": "ipython", |
| 108 | + "version": 2 |
| 109 | + }, |
| 110 | + "file_extension": ".py", |
| 111 | + "mimetype": "text/x-python", |
| 112 | + "name": "python", |
| 113 | + "nbconvert_exporter": "python", |
| 114 | + "pygments_lexer": "ipython2", |
| 115 | + "version": "2.7.14" |
| 116 | + } |
| 117 | + }, |
| 118 | + "nbformat": 4, |
| 119 | + "nbformat_minor": 2 |
| 120 | +} |
0 commit comments