-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_maps.py
70 lines (63 loc) · 2.14 KB
/
make_maps.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import json
from cr2 import *
try:
import simplekml
except ImportError, e:
if str(e) == 'No module named simplekml':
print('simplekml not installed, data2kml will not work!')
else:
raise
try:
import geojson
except ImportError, e:
if str(e) == 'No module named geojson':
print('geojson not installed, data2geojson will not work!')
else:
raise
root = 'maps'
def make_folders():
if not os.path.exists(root):
os.mkdir(root)
def data2kml(variable):
filename = os.path.join(root, 'stations-%s.kml'%variable.var)
kml = simplekml.Kml()
variable.meta.T.apply(lambda X: kml.newpoint(
name=X["codigo_estacion"],
description=unicode(X["nombre"].decode('utf8')),
coords=[( X["longitud"], X["latitud"], X["altura"])]) , axis=1)
kml.save(path=filename)
def data2geojson(variable):
filename = os.path.join(root, 'stations-%s.geojson'%variable.var)
features = []
variable.meta.T.apply(lambda X: features.append(
geojson.Feature(geometry=geojson.Point((
float(X["longitud"]),
float(X["latitud"]),
float(X["altura"]))),
properties=dict(name=X["codigo_estacion"],
description=unicode(X["nombre"].decode('utf8')))
)
)
, axis=1)
with open(filename, 'w') as fp:
geojson.dump(geojson.FeatureCollection(features), fp, sort_keys=True)
if __name__ == '__main__':
make_folders()
prec = Cr2('monthly', 'p', 'data.json')
caud = Cr2('monthly', 'q', 'data.json')
temp = Cr2('monthly', 't', 'data.json')
tmin = Cr2('monthly', 'tmin', 'data.json')
tmax = Cr2('monthly', 'tmax', 'data.json')
data2kml(prec)
data2kml(caud)
data2kml(temp)
data2kml(tmin)
data2kml(tmax)
data2geojson(prec)
data2geojson(caud)
data2geojson(temp)
data2geojson(tmin)
data2geojson(tmax)