11import click
22import numpy as np
3+ import rasterra as rt
34from rra_tools import jobmon
45
56from rra_building_density import cli_options as clio
67from rra_building_density import constants as bdc
7- from rra_building_density import utils
88from rra_building_density .data import BuildingDensityData
9+ from rra_building_density .process import utils
910
1011
1112def format_ghsl_main (
1213 block_key : str ,
13- measure : str ,
1414 time_point : str ,
1515 resolution : int | str ,
1616 output_dir : str ,
1717) -> None :
18- ghsl_version = bdc .GHSL_VERSIONS ["r2023a" ]
19- ghsl_measure = ghsl_version .prefix_and_measure (measure )[1 ]
18+ version = bdc .GHSL_VERSIONS ["r2023a" ]
2019 crs = bdc .CRSES ["wgs84" ].to_pyproj ()
21-
2220 bd_data = BuildingDensityData (output_dir )
21+
22+ print ("Loading the reference block" )
23+ mask_version = bdc .LATEST_MICROSOFT_VERSION
24+ reference_block = bd_data .load_tile (
25+ resolution ,
26+ provider = mask_version .name ,
27+ block_key = block_key ,
28+ # This will be the transition time point
29+ time_point = mask_version .time_points [0 ],
30+ measure = "density" ,
31+ )
32+
2333 print ("Loading the tile index" )
2434 tile_index = bd_data .load_tile_index (resolution )
25- tile_index_info = bd_data .load_tile_index_info (resolution )
26-
27- print ("Building template" )
2835 block_index = tile_index [tile_index .block_key == block_key ]
36+
2937 block_poly_series = block_index .dissolve ("block_key" ).geometry
30- block_poly = block_poly_series .iloc [0 ]
3138 block_poly_ghsl = (
3239 utils .bbox_safe_buffer (block_poly_series , 5000 ).to_crs (crs ).iloc [0 ]
3340 )
3441
35- block_template = utils .make_raster_template (
36- block_poly ,
37- resolution = tile_index_info .tile_resolution ,
38- crs = bdc .CRSES ["equal_area" ],
42+ print ("Loading the GHSL data" )
43+ density_arr , raw_volume_arr , raw_nonresidential_density_arr = (
44+ utils .load_and_resample_ghsl_data ( # noqa: SLF001
45+ measure = measure ,
46+ time_point = time_point ,
47+ ghsl_version = version ,
48+ bounds = block_poly_ghsl ,
49+ reference_block = reference_block ,
50+ bd_data = bd_data ,
51+ )._ndarray
52+ for measure in ["density" , "volume" , "nonresidential_density" ]
3953 )
40-
41- print ("Loading GHSL data" )
42- raw_tile = bd_data .load_provider_tile (
43- ghsl_version ,
44- bounds = block_poly_ghsl ,
45- measure = ghsl_measure ,
46- time_point = time_point ,
47- year = time_point [:4 ],
54+ print ("Generating height and proportion residential arrays" )
55+ height_arr = utils .generate_height_array (density_arr , raw_volume_arr ) # type: ignore[arg-type]
56+ proportion_residential_arr = utils .generate_proportion_residential_array (
57+ density_arr , # type: ignore[arg-type]
58+ raw_nonresidential_density_arr , # type: ignore[arg-type]
4859 )
49- raw_tile = raw_tile .astype (np .float32 ) / 10000.0
5060
51- print ("Resampling" )
52- tile = raw_tile .set_no_data_value (np .nan ).resample_to (block_template , "average" )
53- tile = utils .suppress_noise (tile )
54- print ("Saving" )
55- bd_data .save_tile (
56- tile ,
57- resolution ,
58- provider = "ghsl_r2023a" ,
59- block_key = block_key ,
60- time_point = time_point ,
61- measure = measure ,
62- )
61+ print ("Generating and saving output arrays" )
62+ out_ops = {
63+ "height" : lambda _ , h , __ : h ,
64+ "proportion_residential" : lambda _ , __ , p : p ,
65+ "density" : lambda d , _ , __ : d ,
66+ "residential_density" : lambda d , _ , p : d * p ,
67+ "nonresidential_density" : lambda d , _ , p : d * (1 - p ),
68+ "volume" : lambda d , h , _ : h * d ,
69+ "residential_volume" : lambda d , h , p : h * d * p ,
70+ "nonresidential_volume" : lambda d , h , p : h * d * (1 - p ),
71+ }
72+ for measure , op in out_ops .items ():
73+ out = rt .RasterArray (
74+ data = op (density_arr , height_arr , proportion_residential_arr ), # type: ignore[no-untyped-call]
75+ transform = reference_block .transform ,
76+ crs = reference_block .crs ,
77+ no_data_value = np .nan ,
78+ )
79+ bd_data .save_tile (
80+ out ,
81+ resolution ,
82+ provider = "ghsl_r2023a" ,
83+ block_key = block_key ,
84+ time_point = time_point ,
85+ measure = measure ,
86+ )
6387
6488
6589@click .command ()
66- @clio .with_measure (bdc .GHSLVersion .measure_map )
6790@clio .with_block_key ()
6891@clio .with_time_point ()
6992@clio .with_resolution (bdc .RESOLUTIONS )
7093@clio .with_output_directory (bdc .MODEL_ROOT )
7194def format_ghsl_task (
7295 block_key : str ,
73- measure : str ,
7496 time_point : str ,
7597 resolution : str ,
7698 output_dir : str ,
7799) -> None :
78100 """Build predictors for a given tile and time point."""
79- format_ghsl_main (block_key , measure , time_point , resolution , output_dir )
101+ format_ghsl_main (block_key , time_point , resolution , output_dir )
80102
81103
82104@click .command ()
83- @clio .with_measure (bdc .GHSLVersion .measure_map , allow_all = True )
84105@clio .with_time_point (allow_all = True )
85106@clio .with_resolution (bdc .RESOLUTIONS )
86107@clio .with_output_directory (bdc .MODEL_ROOT )
87108@clio .with_queue ()
88109def format_ghsl (
89- measure : list [str ],
90110 time_point : str ,
91111 resolution : str ,
92112 output_dir : str ,
@@ -100,7 +120,7 @@ def format_ghsl(
100120 print ("Loading the tile index" )
101121 tile_index = bd_data .load_tile_index (resolution )
102122 block_keys = tile_index .block_key .unique ().tolist ()
103- njobs = len (block_keys ) * len (time_point ) * len ( measure )
123+ njobs = len (block_keys ) * len (time_points )
104124 print (f"Formating building density for { njobs } block-times" )
105125
106126 memory , runtime = ghsl_version .process_resources (resolution )
@@ -114,7 +134,6 @@ def format_ghsl(
114134 },
115135 node_args = {
116136 "block-key" : block_keys ,
117- "measure" : measure ,
118137 "time-point" : time_points ,
119138 },
120139 task_resources = {
0 commit comments