Last active
May 29, 2018 11:29
-
-
Save manassra/d6aebeb804c09601ff98b497a3d7397f to your computer and use it in GitHub Desktop.
Revisions
-
manassra revised this gist
May 29, 2018 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,6 @@ with open('labor-force.csv', 'r') as f: reader = csv.DictReader(f); for row in reader: statefip = int(row['state_fips']) countyfip = int(row['county_fips']) labor_force = int(row['labor_force']) -
manassra revised this gist
May 29, 2018 . 1 changed file with 39 additions and 30 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,36 +1,45 @@ import sys import json import csv data = {} geojson = {} ## Step 1: Load the labor force data set, and store into a dictionary ## based on the state and county codes with open('labor-force.csv', 'r') as f: reader = csv.DictReader(f); for row in reader: print row statefip = int(row['state_fips']) countyfip = int(row['county_fips']) labor_force = int(row['labor_force']) employed = int(row['employed']) unemployment_level = int(row['unemployment_level']) unemployment_rate = float(row['unemployment_rate']) if statefip not in data: data[statefip] = {} data[statefip][countyfip] = { 'labor_force': labor_force, 'employed': employed, 'unemployment_level': unemployment_level, 'unemployment_rate': unemployment_rate } ## Step 2: Load the shapes for the counties (features), lookup the labor force ## values based on the state/county codes, and set it as part of the feature ## properties with open('counties.geojson', 'r') as f: geojson = json.load(f) for feature in geojson['features']: featureProperties = feature['properties'] statefp = int(featureProperties['STATEFP']) countyfp = int(featureProperties['COUNTYFP']) featureData = data.get(statefp).get(countyfp, {}) for key in featureData.keys(): featureProperties[key] = featureData[key] ## Step 3: Save the augmented shapefile with open('counties-unemployment.geojson', 'w') as f: json.dump(geojson, f) -
manassra renamed this gist
May 29, 2018 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
manassra created this gist
May 29, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ import sys import json import csv data = {} with open('labor-force.csv', 'r') as f: reader = csv.DictReader(f); for row in reader: print row statefip = int(row['state_fips']) countyfip = int(row['county_fips']) labor_force = 0 if (row['labor_force']): labor_force = int(row['labor_force']) employed = int(row['employed']) unemployment_level = int(row['unemployment_level']) unemployment_rate = float(row['unemployment_rate']) if statefip not in data: data[statefip] = {} data[statefip][countyfip] = { 'labor_force': labor_force, 'employed': employed, 'unemployment_level': unemployment_level, 'unemployment_rate': unemployment_rate } geojson = None with open('counties.geojson', 'r') as f: geojson = json.load(f) for feature in geojson['features']: featureProperties = feature['properties'] statefp = int(featureProperties['STATEFP']) countyfp = int(featureProperties['COUNTYFP']) featureData = data.get(statefp).get(countyfp, {}) for key in featureData.keys(): featureProperties[key] = featureData[key] with open('counties-unemployment.geojson', 'w') as f: json.dump(geojson, f)