-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_weather.py
130 lines (90 loc) · 3.33 KB
/
update_weather.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from qgis.core import *
import requests
from uuid import uuid4
from sh import unzip, rm
from shapely.wkb import loads as wkb_loads
from shapely.wkt import dumps as wkt_dumps
from cartodb import CartoDBAPIKey, CartoDBException
from config import API_KEY, cartodb_domain
layer_map = {
"TOR": "Tornado Warning",
"SVR": "Severe Thunderstorm Warning",
"FFW": "Flash Flood Warning",
"SMW": "Special Marine Warning",
}
def incr_gen():
i = 1
while True:
yield i
i = i + 1
layer_index = incr_gen()
def get_shapefiles():
print "Getting new NOAA shapefiles..."
r = requests.get("http://www.nws.noaa.gov/regsci/gis/shapefiles/current_warnings.zip")
file_name = str(uuid4())
with open("/tmp/%s.zip" %file_name, "wb") as out_file:
out_file.write(r.content)
return file_name
def cstor_qgis():
print "Initializing QGIS..."
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()
def dstor_qgis():
print "Stopping QGIS..."
QgsApplication.exitQgis()
def extract_shapefiles(in_file):
print "Extracting shapefiles..."
unzip( "-d", "/tmp/%s" % in_file, "/tmp/%s.zip" % in_file)
return "/tmp/%s" % in_file
def import_shapefile(in_dir, file_name):
print "Importing %s (%s)..." % (k, "%s/%s" % (in_dir, file_name))
layer = QgsVectorLayer("%s/%s" % (in_dir, file_name), "layer_%i" % layer_index.next(), "ogr")
if not layer.isValid:
raise Exception("Layer is not valid for file %s" % file_name)
QgsMapLayerRegistry.instance().addMapLayer(layer)
return layer
def remove_temp(in_dir):
print "Deleting temp files.."
rm("-rf", in_dir)
rm("%s.zip" % in_dir)
def flush_and_transmit(features):
print "Connecting to CartoDB..."
cl = CartoDBAPIKey(API_KEY, cartodb_domain)
try:
print "Clearing old feature set..."
cl.sql("TRUNCATE TABLE warning_geom;")
print "Inserting new features..."
for feat in features:
cl.sql("INSERT INTO warning_geom (name, description, the_geom) VALUES ('%s','%s', ST_SetSRID(ST_GeometryFromText('%s'), 4326))" % (feat['key'], feat['desc'], feat['wkt']))
except CartoDBException as e:
print ("some error ocurred", e)
if __name__ == "__main__":
file_id = get_shapefiles()
cstor_qgis()
dir_out = extract_shapefiles(file_id)
layers = []
for k in layer_map.keys():
layers.append(import_shapefile(dir_out, "%s.shp" % k))
features = []
for layer, key in zip(layers, layer_map.keys()):
print " Features: %i" % layer.featureCount()
provider = layer.dataProvider()
allAttrs = provider.attributeIndexes()
# start data retreival: fetch geometry and all attributes for each feature
provider.select(allAttrs)
feat = QgsFeature()
while provider.nextFeature(feat):
# fetch geometry
geom = feat.geometry()
print " Feature ID: %s" % feat.id()
features.append(
dict(
wkt=wkt_dumps(wkb_loads(geom.asWkb())),
key=key,
desc=layer_map[key]
)
)
print "Total features: %i" % len(features)
flush_and_transmit(features)
dstor_qgis()
remove_temp(dir_out)