-
Notifications
You must be signed in to change notification settings - Fork 2
/
auxiliary.py
executable file
·352 lines (245 loc) · 9.65 KB
/
auxiliary.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from tokenize import PlainToken
sys.path.append('scripts/pipeline_components/')
sys.path.append('scripts/src/')
import data_handlers
import helpers
import yaml
import sys
import os
import json
import argparse
import numpy as np
import matplotlib.pyplot as plt
import wget
import glob
"""
Script that generates the auxiliary outputs needed throughout the pipeline.
This script needs to be run first. It initializes the aux/ directory
that contains these outputs.
If the user does not run this script first, a warning is sent in the main script
These auxiliary outputs are :
Extracted from the BDTOPO
- buildings : geographic coordinates of the buidlings in a given departement.
- plants : geographic coordinates of the plants in a given departement.
Extracted from their own folder :
- IRIS : statistical clusters of 2,000 inhabitants each
- Communes : lowest aggregation level in the validation registries
"""
# Arguments
parser = argparse.ArgumentParser(description = 'Auxiliary files for the large scale detection pipeline')
parser.add_argument('--dpt', default = None, help = "Department to proceed")
args = parser.parse_args()
if args.dpt is not None:
dpt = args.dpt
if isinstance(dpt, str):
dpt = int(dpt)
else:
print('Please input a departement number to run the pipeline.')
raise ValueError
# Load the configuration file
config = 'config.yml'
with open(config, 'rb') as f:
configuration = yaml.load(f, Loader=yaml.FullLoader)
# Get the folders from the configuration file
aux_dir = configuration.get('aux_dir')
# source directories for the auxiliary outputs
source_commune_dir = configuration.get('source_commune_dir')
source_topo_dir = configuration.get('source_topo_dir')
source_images_dir = configuration.get('source_images_dir')
# main script
# intialize the aux/ directory
if not os.path.isdir(aux_dir):
os.mkdir(aux_dir)
"""
helper functions for the extraction of the MNS
"""
#Dictionnary of Request Parameters
RP = {
"MNS" : {
"KEY" : "altimetrie/",
"VERSION" : "1.3.0",
"LAYERS" : "ELEVATION.ELEVATIONGRIDCOVERAGE.HIGHRES.MNS",
"FORMAT" : "image/jpeg",
"REQUEST" : "GetMap",
"EXCEPTIONS" : "text/xml",
"STYLES" : "",
"CRS" : "EPSG:2154",
"WIDTH" : "6250",
"HEIGHT" : "6250"
}}
def return_bbox(coord):
left, top = coord
x_shift, y_shift = (6250) * abs(0.2), (6250) * abs(-0.2)
right, bottom = coord[0] + x_shift, coord[1] - y_shift
BB = "{},{},{},{}".format(left, bottom, right, top)
return BB
'''Returns the URL corresponding to the wanted MNS (altitude heatmap) image'''
def get_mns_url(coord):
KEY = RP["MNS"]["KEY"]
VERSION = RP["MNS"]["VERSION"]
FORMAT = RP["MNS"]["FORMAT"]
REQUEST = RP["MNS"]["REQUEST"]
EXCEPTIONS = RP["MNS"]["EXCEPTIONS"]
STYLES = RP["MNS"]["STYLES"]
CRS = RP["MNS"]["CRS"]
LAYERS = RP["MNS"]["LAYERS"]
WIDTH = RP["MNS"]["WIDTH"]
HEIGHT = RP["MNS"]["HEIGHT"]
BB = return_bbox(coord)
URL = 'https://wxs.ign.fr/'+KEY+'geoportail/r/wms/?'\
+'LAYERS='+LAYERS+'&'\
+'EXCEPTIONS='+EXCEPTIONS+'&'\
+'VERSION='+VERSION+'&'\
+'FORMAT='+FORMAT+'&'\
+'REQUEST='+REQUEST+'&'\
+'STYLES='+STYLES+'&'\
+'CRS='+CRS+'&'\
+'BBOX='+BB+'&'\
+'WIDTH='+WIDTH+'&'\
+'HEIGHT='+HEIGHT
return URL
'''Plots the wanted url image.'''
def show_url_img(coord):
# get the URL
URL = get_mns_url(coord)
# prepare and load the image
dnsRDM = 'tmp_IGN_'+str(int((np.random.rand(1)[0])*10**9))+'.jpg'
wget.download(URL,'./'+dnsRDM)
A=plt.imread('./'+dnsRDM)
os.remove('./'+dnsRDM)
return A
"""
function that requests the WMS server to extract the MNS of the tiles
"""
def get_associated_mns(aux_dir, source_images_dir):
"""
retrieves the MNS of the departement.
"""
# load the tile list
# set up the directory
mns_dir = os.path.join(aux_dir, 'mns')
if not os.path.exists(mns_dir):
os.mkdir(mns_dir)
# check that the folder for the current departement exists
dpt_mns_dir = os.path.join(mns_dir, str(dpt))
if not os.path.exists(dpt_mns_dir):
os.mkdir(dpt_mns_dir)
completed_tiles = os.listdir(dpt_mns_dir)
# get the tiles list of the departement
tiles = helpers.compute_tiles_coordinates(source_images_dir)
for tile in tiles.keys():
# for each tile, check whether it has been competed or not
if '{}.png'.format(tile) in completed_tiles:
continue
else:
full_tile = compute_mns(tiles[tile])
tile_name = '{}.png'.format(tile)
target_directory = os.path.join(dpt_mns_dir, tile_name)
plt.imsave(target_directory, full_tile)
def initialize_tiles_list(source_dir, target_tiles):
"""
returns a dictionnary where each key is the tile name
and each value is set to False, meaning that none of the
tiles have yet been proceeded.
"""
tiles_list = {}
if target_tiles is None: # if we do not input a specific list of tiles to proceed, go for all of them.
dnsSHP = glob.glob(source_dir + "/**/dalles.shp", recursive = True)
if not dnsSHP:
print("Error, the shape file could not be found.")
raise ValueError
with collection(dnsSHP[0], 'r') as input: # look for the tile that contains the point
for shapefile_record in input:
name = shapefile_record["properties"]["NOM"][2:-4]
tiles_list[name] = False
else:
for tile in target_tiles:
tiles_list[tile] = False
return tiles_list
# compute the tiles list
# for which we will sort the building locations.
tiles_list = initialize_tiles_list(source_images_dir, configuration.get('tiles_list'))
"""
computes the MNS of a tile
"""
def compute_mns(tile_coords):
"""
computes the MNS of a tile by requesting chunks on the
WMS serveur of the IGN.
args :
- tile_coords : a list coming from the compute_tiles_coordinates function.
"""
# request to the MNS the 16 subparts of the tile
images = {}
row = -1
for i in range(4 * 4):
k = i%4
if i%4 == 0:
row += 1
left, top = tile_coords[0][0] # initialize wrt to the upper left corner of the tile.
x_shift, y_shift = k * (6250) * abs(0.2), row * (6250) * abs(-0.2)
left, top = left + x_shift, top - y_shift
coord = left, top
A = show_url_img(coord)
images[i] = A
# aggregate the parts together into one large tile
full_tile = np.empty((25000,25000))
row = -1
for i in range(4*4):
k = i%4
if i%4 == 0:
row += 1
block = images[i]
y_start, y_end = k * 6250, (k+1) * 6250
x_start, x_end = row * 6250, (row + 1) * 6250
full_tile[x_start:x_end , y_start:y_end] = block
return full_tile
def main():
# buildings
if not os.path.exists(os.path.join(aux_dir, 'sorted_buildings_{}.json'.format(args.dpt))):
print('Computing the location of the buildings...')
buildings_locations = data_handlers.get_buildings_locations(source_topo_dir)
sorted_buildings = data_handlers.assign_building_to_tiles(tiles_list, buildings_locations, source_images_dir)
# sort the buildings by tile
# postprocessing_helpers.assign_building_to_tiles(tiles_list, buildings, self.img_dir, self.temp_dir, self.dpt)
# save the file
print('Computation complete. Saving the file.')
with open(os.path.join(aux_dir, 'sorted_buildings_{}.json'.format(args.dpt)), 'w') as f:
json.dump(sorted_buildings, f, indent=2)
print('Done.')
# power plants
if not os.path.exists(os.path.join(aux_dir, 'plants_locations_{}.json'.format(args.dpt))):
# List of power plants
print('Extracting the localization of the power plants...')
plants_locations = data_handlers.get_power_plants(source_topo_dir)
# Saving the file
with open(os.path.join(aux_dir, 'plants_locations_{}.json'.format(args.dpt)), 'w') as f:
json.dump(plants_locations, f, indent = 2)
# Computation for the IRIS :
# if not os.path.exists(os.path.join(aux_dir, 'iris_{}.json'.format(args.dpt))):
# print('Filtering the IRIS attached to departement {}...'.format(args.dpt))
# iris_location = data_handlers.get_iris(source_iris_dir, args.dpt)
# save the file
# print('Computation complete. Saving the file.')
# with open(os.path.join(aux_dir, 'iris_{}.json'.format(args.dpt)), 'w') as f:
# json.dump(iris_location, f, indent=2)
print('Done.')
# communes
if not os.path.exists(os.path.join(aux_dir, 'communes_{}.json'.format(args.dpt))):
print('Filtering the communes attached to departement {}...'.format(args.dpt))
communes_location = data_handlers.get_communes(source_commune_dir, args.dpt)
# save the file
print('Computation complete. Saving the file.')
with open(os.path.join(aux_dir, 'communes_{}.json'.format(args.dpt)), 'w') as f:
json.dump(communes_location, f, indent=2)
print('Done.')
# MNS of the departement
# print('Extracting the MNS of the departement ...')
# get_associated_mns(aux_dir, source_images_dir)
# print('Done.')
if __name__ == '__main__':
# run the initialization of the auxiliary files.
main()