forked from developmentseed/ml-hv-grid-pub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_pred_map.py
More file actions
64 lines (50 loc) · 2.11 KB
/
Copy pathgen_pred_map.py
File metadata and controls
64 lines (50 loc) · 2.11 KB
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
"""
gen_geojson.py
@author: developmentseed
Script to load predictions for a list of tiles and produce a geojson map
"""
from os import path as op
import json
from utils import make_geo_feature
from config import ckpt_dir, preds_dir, gen_geojson_params as geo_p
import matplotlib
import matplotlib.cm as cm
###########################
# Load predictions
###########################
for pred_fname, out_geoj_fname in zip(geo_p['pred_fnames'], geo_p['geojson_out_fnames']):
pred_fpath = op.join(preds_dir, pred_fname)
print('\nGenerating geojson prediction map for: {}'.format(pred_fname))
with open(pred_fpath, 'r') as pred_f:
pred_dict = json.load(pred_f)
###########################
# Create geojson file
###########################
feature_list = []
norm = matplotlib.colors.Normalize(vmin=geo_p['upper_thresh_lims'][0], vmax=1, clip=True)
mapper = cm.ScalarMappable(norm=norm, cmap=cm.inferno)
for tile_ind, tile_pred in pred_dict.items():
# Get tile index
if tile_ind == 'page_ind':
continue
x, y, z = tile_ind.split('-')
tile_dict = dict(x=int(x), y=int(y), z=int(z))
feature = make_geo_feature(tile_dict, float(tile_pred),
geo_p['upper_thresh_lims'],
geo_p['thresh_cols'],
geo_p['deci_prec'])
#mapper)
if not geo_p['exclude_subthresh']:
feature_list.append(feature)
elif (geo_p['exclude_subthresh'] and
feature['properties']['pred'] > geo_p['upper_thresh_lims'][0]):
feature_list.append(feature)
#############################################
# Assemble features into one geojson and save
#############################################
json_dict = {'type': 'FeatureCollection',
'features': feature_list}
geojson_fpath = op.join(preds_dir, out_geoj_fname)
print('Saved geojson map as {}'.format(geojson_fpath))
with open(geojson_fpath, 'w') as geojson_file:
json.dump(json_dict, geojson_file)