-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspections.py
49 lines (43 loc) · 1.66 KB
/
inspections.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
__author__ = 'don'
import xmltodict
import geojson
import json
from datetime import datetime
from collections import OrderedDict
from operator import attrgetter
with open('inspections.xml') as fd:
obj = xmltodict.parse(fd.read())
features = []
for business in obj['Business_Inspection_Violation']['Business']:
try:
lat = float(business['Latitude'])
lng = float(business['Longitude'])
name = business['Name']
address = business['Address']
description = business['Description']
except TypeError:
pass
else:
try:
inspections = business['Inspection']
latestDate = datetime.min
result = ""
for inspection in inspections:
inspDate = datetime.strptime(inspection["Inspection_Date"],"%m/%d/%Y")
if (inspDate > latestDate):
latestDate = inspDate
result = inspection["Inspection_Result"]
except (TypeError, KeyError):
pass
else:
score = inspection["Inspection_Score"]
theDate = latestDate.strftime("%m/%d/%Y")
if (result == "Satisfactory"):
color = "#008000"
elif (result == "Unsatisfactory"):
color = "#FF0000"
else:
color = "#808080"
features.append(geojson.Feature(geometry=geojson.Point((lng, lat)), id=name, properties={"Name":name, "Address":address, "Description":description, "Inspected":theDate, "Result":result, "Score":score, "marker-size": "small", "marker-color": color}))
fc = geojson.FeatureCollection(features)
print geojson.dumps(fc, indent=2)