-
Notifications
You must be signed in to change notification settings - Fork 1
/
wildfires.py
104 lines (88 loc) · 3.98 KB
/
wildfires.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os, psycopg2, json
from datetime import datetime
from . import sources
urls = {
'GeoMAC Large Fire Points': 'https://wildfire.cr.usgs.gov/arcgis/rest/services/geomac_fires/MapServer/1',
'GeoMAC Fire Perimeters': 'https://wildfire.cr.usgs.gov/arcgis/rest/services/geomac_fires/MapServer/2',
}
class FirePoint:
def __init__(self, location, internal_id, usgs_id, name, contained, discovered, cause, acres):
assert location['type'] and location['coordinates']
self.location = location
self.internal_id = internal_id
self.usgs_id = usgs_id
self.name = name
self.contained = contained
self.discovered = discovered
self.cause = cause
self.acres = acres
@property
def title(self):
x, y = self.location['coordinates']
return '{name} fire near {lat:.2f}, {lon:.2f}'.format(lon=x, lat=y, **self.__dict__)
@property
def description(self):
x, y = self.location['coordinates']
return '{acres} acre {name} fire'.format(lon=x, lat=y, **self.__dict__)
@property
def type(self):
return 'fire'
@property
def id(self):
return self.usgs_id
def store_fire_point(db, fire_dict):
''' Add fire point to the db if the fire point does not already exist in the db
'''
db.execute('SELECT * FROM fire_points WHERE usgs_id = %s', (fire_dict['usgs_id'],))
point_exists = db.rowcount
if not point_exists:
# Create point with location coordinates for insert
fire_coordinates = fire_dict['location']['coordinates']
coordinates = 'POINT(%s %s)' % (fire_coordinates[0], fire_coordinates[1])
db.execute('INSERT INTO fire_points (location, usgs_id, name, contained, discovered, cause, acres) VALUES (%s, %s, %s, %s, %s, %s, %s)',
(coordinates, fire_dict['usgs_id'], fire_dict['name'], fire_dict['contained'], fire_dict['discovered'], fire_dict['cause'], fire_dict['acres']))
def convert_fire_point(feature):
'''
'''
properties = feature['properties']
usgs_id = properties['uniquefireidentifier']
name = properties['incidentname']
contained = properties['percentcontained']
discovered = datetime.utcfromtimestamp(properties['firediscoverydatetime'] / 1000)
cause = properties['firecause']
acres = properties['acres']
return dict(location=feature['geometry'], usgs_id=usgs_id, name=name, contained=contained,
discovered=discovered, cause=cause, acres=acres)
def get_one_fire(db, usgs_id):
'''
'''
db.execute('''SELECT ST_AsGeoJSON(location) as coordinates_json, *
FROM fire_points WHERE usgs_id = %s''',
(usgs_id, ))
fire_row = db.fetchone()
location = json.loads(fire_row['coordinates_json'])
return FirePoint(location, fire_row['id'], fire_row['usgs_id'], fire_row['name'],
fire_row['contained'], fire_row['discovered'],
fire_row['cause'], fire_row['acres'])
def get_current_fires(db):
''' Return all fires that users should be notified of.
'''
# Need to not get all fires, need to get all for today or something?
db.execute('SELECT ST_AsGeoJSON(location) as coordinates_json, * FROM fire_points')
fire_rows = db.fetchall()
fires = []
for fire_row in fire_rows:
fire_location = json.loads(fire_row['coordinates_json'])
fire_point = FirePoint(fire_location, fire_row['id'], fire_row['usgs_id'], fire_row['name'],
fire_row['contained'], fire_row['discovered'], fire_row['cause'],
fire_row['acres'])
fires.append(fire_point)
return fires
def main():
with psycopg2.connect(os.environ['DATABASE_URL']) as conn:
with conn.cursor() as db:
for feature in sources.load_esri_source(urls['GeoMAC Large Fire Points']):
fire = convert_fire_point(feature)
store_fire_point(db, fire)
if __name__ == '__main__':
exit(main())