-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
55 lines (46 loc) · 1.45 KB
/
utils.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
import os.path
import psycopg2
import pickle
import json
import copy
import matplotlib.pyplot as plt
class FeatureCollection:
def __init__(self):
self.features = []
def __str__(self):
json.dumps(self.export())
def export(self):
return {'type': 'FeatureCollection',
'features': self.features}
def dump(self, file_name):
json.dump(self.export(), open(file_name, 'wb'))
def add(self, geom, props):
try:
geom = json.loads(geom)
except:
pass
self.features.append({'type': 'Feature',
'geometry': geom,
'properties': props})
def deepcopy(self):
fc = FeatureCollection()
fc.features = copy.deepcopy(self.features)
return fc
def gyr_cmap(N):
cmap = plt.get_cmap('RdYlGn')
return lambda x: '#{:02x}{:02x}{:02x}'.format(*cmap(((N-x) * 256)/N, bytes=True))
def cache(cache_file):
def wrap(f):
def cached(*args, **kwargs):
if not os.path.isfile(cache_file) or kwargs.get('refresh'):
res = f(*args)
pickle.dump(res, open(cache_file, 'wb'))
else:
res = pickle.load(open(cache_file))
return res
return cached
return wrap
def get_conn():
return psycopg2.connect(database='geodjango', user='megacell')
def get_scale(max_val):
return lambda x: float(x) / max_val