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 | class InspireGridGeneration(Component):
"""
This class is responsible for generating the INSPIRE grid for given extent or polygon
and enrich it with elevation and landuse data.
"""
COMPONENT_ID = "InspireGridGeneration"
def __init__(self, general_config_path: str, component_config_path: str) -> None:
super().__init__(general_config_path, component_config_path)
self.grid_partition_size = self.config.get(InspireGridGeneration.COMPONENT_ID, "grid_generation_partition_size")
self.grid_quadkey_level = self.config.getint(
InspireGridGeneration.COMPONENT_ID,
"grid_processing_partition_quadkey_level",
)
self.reference_country = self.config.get(InspireGridGeneration.COMPONENT_ID, "reference_country")
self.country_buffer = self.config.get(InspireGridGeneration.COMPONENT_ID, "country_buffer")
self.grid_generator = InspireGridGenerator(
self.spark,
ColNames.geometry,
ColNames.grid_id,
self.grid_partition_size,
)
# Attributes that will hold the shared common origin if creating INSPIRE grid using polygon(s) instead of
# an extent
self.n_origin: float = None
self.e_origin: float = None
def initalize_data_objects(self):
self.clear_destination_directory = self.config.getboolean(
InspireGridGeneration.COMPONENT_ID, "clear_destination_directory"
)
self.grid_mask = self.config.get(InspireGridGeneration.COMPONENT_ID, "grid_mask")
if self.grid_mask == "polygon":
# inputs
self.input_data_objects = {}
self.input_data_objects[BronzeCountriesDataObject.ID] = BronzeCountriesDataObject(
self.spark,
self.config.get(CONFIG_BRONZE_PATHS_KEY, "countries_data_bronze"),
)
# outputs
grid_do_path = self.config.get(CONFIG_SILVER_PATHS_KEY, "grid_data_silver")
if self.clear_destination_directory:
delete_file_or_folder(self.spark, grid_do_path)
self.output_data_objects = {}
self.output_data_objects[SilverGridDataObject.ID] = SilverGridDataObject(
self.spark, grid_do_path, [ColNames.quadkey]
)
@get_execution_stats
def execute(self):
self.logger.info(f"Starting {self.COMPONENT_ID}...")
if self.grid_mask == "polygon":
self.read()
countries, country_extent = self.get_country_mask(self.reference_country, self.country_buffer)
proj_extent, _ = self.grid_generator.process_latlon_extent(country_extent)
self.n_origin = proj_extent[0]
self.e_origin = proj_extent[1]
ids = [row["temp_id"] for row in countries.select("temp_id").collect()]
self.logger.info(f"Processing {len(ids)} parts of the country")
processed_parts = 0
for id in ids:
self.current_country_part = countries.filter(F.col("temp_id") == id)
self.transform()
self.write()
processed_parts += 1
self.logger.info(f"Finished processing {processed_parts} parts")
else:
self.transform()
self.write()
self.logger.info(f"Finished {self.COMPONENT_ID}")
def transform(self):
self.logger.info(f"Transform method {self.COMPONENT_ID}...")
if self.grid_mask == "polygon":
grid_sdf = self.grid_generator.cover_polygon_with_grid_centroids(
self.current_country_part, n_origin=self.n_origin, e_origin=self.e_origin
)
else:
grid_extent = self.config.geteval(InspireGridGeneration.COMPONENT_ID, "extent")
grid_sdf = self.grid_generator.cover_extent_with_grid_centroids(grid_extent)
grid_sdf = quadkey_utils.assign_quadkey(grid_sdf, 3035, self.grid_quadkey_level)
grid_sdf = grid_sdf.orderBy(ColNames.quadkey)
grid_sdf = grid_sdf.repartition(ColNames.quadkey)
grid_sdf = utils.apply_schema_casting(grid_sdf, SilverGridDataObject.SCHEMA)
self.output_data_objects[SilverGridDataObject.ID].df = grid_sdf
def get_country_mask(self, reference_country, country_buffer):
countries = self.input_data_objects[BronzeCountriesDataObject.ID].df
countries = (
countries.filter(F.col(ColNames.iso2) == reference_country)
.withColumn(
ColNames.geometry,
STF.ST_Buffer(ColNames.geometry, F.lit(country_buffer)),
)
.groupBy()
.agg(STA.ST_Union_Aggr(ColNames.geometry).alias(ColNames.geometry))
)
countries = utils.project_to_crs(countries, 3035, 4326)
country_extent = countries.select(
STF.ST_XMin("geometry").alias("longitude_min"),
STF.ST_YMin("geometry").alias("latitude_min"),
STF.ST_XMax("geometry").alias("longitude_max"),
STF.ST_YMax("geometry").alias("latitude_max"),
).collect()
if len(country_extent) == 0:
raise ValueError(f"BronzeCountriesDataObject for {reference_country} seems to have no geometries")
country_extent = list(country_extent[0][:])
countries = countries.withColumn(ColNames.geometry, F.explode(STF.ST_Dump(ColNames.geometry))).withColumn(
"temp_id", F.monotonically_increasing_id()
)
return countries, country_extent
|