-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate_tiles_cache.py
161 lines (125 loc) · 4.46 KB
/
generate_tiles_cache.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import argparse
import os
from os import listdir
from os.path import isfile
from os.path import join
import gdal
import numpy as np
from joblib import Parallel
from joblib import delayed
from PIL import Image
from skimage import measure
from tqdm import tqdm
from config_loader import *
def open_tif_file(tiff):
# Open tif file
ds = gdal.Open(tiff)
try:
M = np.array(ds.GetRasterBand(1).ReadAsArray()).astype('int16')
except AttributeError:
print("Error while opening file %s." % tiff)
return -1
return M
def get_clusters_area(M):
labels = measure.label(M, connectivity=1)
regions = measure.regionprops(labels, cache=False)
cluster_pop = np.zeros(len(regions), dtype='int32')
for i, x in enumerate(regions):
cluster_pop[i] = x.area
return cluster_pop
def compute_cache_simulation_tiles(input_path, output_path, filename):
save_filename = '{}/{}'.format(output_path, filename)
if os.path.isfile(save_filename):
return True
fload = np.load('{}/{}'.format(input_path, filename))
M = fload['M'].astype('float32')
L = M.shape[0]
nsteps = int(np.max(M) - 2)
# Percentage urbanization
perc_array = np.zeros(nsteps, 'float32')
urb_steps = {}
for s in range(nsteps):
Mtemp = 1.0 * (M > s)
perc_array[s] = np.sum(Mtemp) / (L**2)
urb_steps['s{}'.format(s)] = get_clusters_area(Mtemp)
np.savez_compressed(save_filename, perc=perc_array, M=M, **urb_steps)
return True
def compute_cache_real_tiles(input_path, output_path, filename):
save_filename = '{}/{}'.format(output_path, filename.replace('tif', 'npz'))
if os.path.isfile(save_filename):
return True
M = open_tif_file('{}/{}'.format(input_path, filename))
M = ((M == 0) * 1).astype('uint8')
im = Image.fromarray(M * 255, mode='L').convert('1')
M = np.asarray(im.resize((1000, 1000), Image.NEAREST), dtype='uint8')
L = M.shape[0]
assert L == 1000
perc_urb = np.sum(M) / (L**2)
assert 0 <= perc_urb <= 1
np.savez_compressed(save_filename, purb=perc_urb, M=M)
return True
def make_argument_parser():
"""
Creates an ArgumentParser to read the options for this script from
sys.argv
:return:
"""
parser = argparse.ArgumentParser(
description="Launch cache generation for simulated and real tiles"
)
parser.add_argument('--njobs', '-J', default=10, type=int)
parser.add_argument('--size', '-S', default=1000, type=int)
parser.add_argument('--twosteps', dest='twosteps', action='store_true', help="Two steps model")
parser.add_argument(
'--no-twosteps', dest='twosteps', action='store_false', help="Probabilistic model"
)
parser.add_argument(
'--realtiles', dest='realtiles', action='store_true', help="Real tiles cache generation"
)
parser.add_argument(
'--no-realtiles',
dest='realtiles',
action='store_false',
help="Simulation tiles cache generation",
)
parser.set_defaults(twosteps=True, realtiles=False)
return parser
def main():
configs = load_config()
parser = make_argument_parser()
args = parser.parse_args()
print("PARAMETERS", args)
model_var = args.size
if not args.twosteps:
model_var = 'marco'
if args.realtiles:
input_path = configs['rasterized_tiles_path']
output_path = configs['tiles_cache_path']
onlyfiles = [f for f in listdir(input_path) if isfile(join(input_path, f)) and 'tif' in f]
print("N Real tiles", len(onlyfiles))
_ = [
True
for _ in Parallel(n_jobs=args.njobs)(
delayed(compute_cache_real_tiles)(input_path, output_path, f)
for f in tqdm(onlyfiles)
)
]
input_path = '{}/{}'.format(configs["simulations_path"], model_var)
output_path = '{}/{}'.format(configs["simulations_cache_path"], model_var)
onlyfiles = [f for f in listdir(input_path) if isfile(join(input_path, f)) and 'npz' in f]
print("N Simulations", len(onlyfiles))
# Filter for those files that are not existing
onlyfiles = [
f
for f in onlyfiles
if not os.path.isfile('{}/{}'.format(output_path, f.replace('tif', 'npz')))
]
_ = [
True
for _ in Parallel(n_jobs=args.njobs)(
delayed(compute_cache_simulation_tiles)(input_path, output_path, f)
for f in tqdm(onlyfiles)
)
]
if __name__ == '__main__':
main()