Last active
March 16, 2016 02:40
-
-
Save brett-miller/2fc1e9d7ae16ac871dc0 to your computer and use it in GitHub Desktop.
list of dbf files to geojson
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 characters
Convert folder of dbf files to geojson | |
'''sh | |
ls " | awk '{ system("ogr2ogr -f GeoJSON " $1 "/.geojson" $1) }' | |
''' | |
FARS user data manual: ftp://ftp.nhtsa.dot.gov/FARS/FARS-DOC/USERGUIDE-2014.pdf | |
Washington State Code = 53 | |
GEO Codes: http://www.gsa.gov/portal/getMediaData?mediaId=214171 | |
Lake Forest Park City Code = 1107 | |
Filter accidents file for Washington State | |
'''python | |
import json | |
accidents=json.loads(open('accident.dbf.geojson').read())['features'] | |
waStateAccidents=[accident for accident in accidents if accident["properties"]["STATE"]==53] | |
lfpStateAccidents=[accident for accident in accidents if accident["properties"]["CITY"]==1107] | |
'''python | |
from sets import Set | |
import collections | |
import json | |
import codecs | |
def flatten(d, parent_key='', sep='_'): | |
items = [] | |
for k, v in d.items(): | |
new_key = parent_key + sep + k if parent_key else k | |
if isinstance(v, collections.MutableMapping): | |
items.extend(flatten(v, new_key, sep=sep).items()) | |
else: | |
items.append((new_key, v)) | |
return dict(items) | |
accidents=json.loads(open('accident.dbf.geojson').read())['features'] | |
keys=Set() | |
accidentsFlattened=[flatten(accident) for accident in accidents] | |
for accident in accidentsFlattened: | |
keys=keys.union(accident.keys()) | |
fieldnames = list(keys) | |
with codecs.open("accidents.tsv", "w", "utf-8") as f: | |
f.write('\t'.join(fieldnames) + '\n') | |
for row in accidentsFlattened: | |
for field in fieldnames: | |
try: | |
f.write(unicode(row[field]) + '\t') | |
except: | |
f.write('\t') | |
f.write('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment