Skip to content

Instantly share code, notes, and snippets.

@brett-miller
Last active March 16, 2016 02:40
Show Gist options
  • Save brett-miller/2fc1e9d7ae16ac871dc0 to your computer and use it in GitHub Desktop.
Save brett-miller/2fc1e9d7ae16ac871dc0 to your computer and use it in GitHub Desktop.
list of dbf files to geojson
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