Skip to content

Commit fcf608b

Browse files
authored
Merge pull request allixender#36 from crim-ca/forward-densification-params
ensure that cell edge densification parameters are forwarded
2 parents 43eaef2 + fdff195 commit fcf608b

File tree

2 files changed

+101
-3
lines changed

2 files changed

+101
-3
lines changed

dggrid4py/dggrid_runner.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ def get_geo_out(legacy=True, has_gdal=True):
299299
"clip_cell_res": int,
300300
"clip_cell_addresses": str,
301301
"clip_region_files": str,
302+
"clip_cell_densification": int,
302303
"clipper_scale_factor": float,
303304
"input_address_type": DggsInputAddressTypeT,
304305
"input_hier_ndx_form": DggsInputHierNdxFormT,
@@ -555,8 +556,14 @@ class Dggs:
555556
# Add more aliases as needed
556557
}, init=False, repr=False)
557558

558-
def _resolve_key(self, key: str) -> str:
559+
def _resolve_key(self, key: str, strict: bool = False) -> str | None:
559560
# If key is an alias, return canonical; else if it's a canonical, return as is
561+
if strict:
562+
if key in self._aliases:
563+
return self._aliases[key]
564+
elif key in self.__dataclass_fields__:
565+
return key
566+
return None
560567
return self._aliases.get(key, key)
561568

562569
def set_par(self, par_key: str, par_value: DggridMetaConfigParameterT):
@@ -589,12 +596,16 @@ def to_dict(self) -> dict[str, DggridMetaConfigParameterT]:
589596
def metafile(self) -> DggridMetafileT:
590597
return copy.copy(dg_grid_meta(self))
591598

592-
def update(self, **kwargs):
599+
def update(self, strict=False, **kwargs):
593600
"""
594601
Back propagate keyword parameters if they can be mapped to an attribute handled by this class.
602+
603+
:param strict:
604+
If True, only keys that match existing attributes will be set.
605+
If False, unknown keys will be set with provided name.
595606
"""
596607
for key, value in kwargs.items():
597-
found = self._resolve_key(key)
608+
found = self._resolve_key(key, strict=strict)
598609
if found:
599610
self.set_par(found, value)
600611

@@ -1275,6 +1286,7 @@ def grid_cell_polygons_for_extent(
12751286
tmp_id = uuid.uuid4()
12761287
tmp_dir = self.working_dir
12771288
dggs = dgselect(dggs_type = dggs_type, res= resolution, mixed_aperture_level=mixed_aperture_level)
1289+
dggs.update(**conf_extra, strict=True)
12781290

12791291
subset_conf: DggridMetaConfigT = { 'update_frequency': 100000, 'clip_subset_type': 'WHOLE_EARTH' }
12801292

@@ -1372,6 +1384,7 @@ def grid_cell_centroids_for_extent(
13721384
tmp_id = uuid.uuid4()
13731385
tmp_dir = self.working_dir
13741386
dggs = dgselect(dggs_type = dggs_type, res= resolution, mixed_aperture_level=mixed_aperture_level)
1387+
dggs.update(**conf_extra, strict=True)
13751388

13761389
subset_conf: DggridMetaConfigT = { 'update_frequency': 100000, 'clip_subset_type': 'WHOLE_EARTH' }
13771390

@@ -1470,6 +1483,7 @@ def grid_cell_polygons_from_cellids(
14701483
tmp_id = uuid.uuid4()
14711484
tmp_dir = self.working_dir
14721485
dggs = dgselect(dggs_type = dggs_type, res= resolution, mixed_aperture_level=mixed_aperture_level)
1486+
dggs.update(**conf_extra, strict=True)
14731487

14741488
subset_conf: DggridMetaConfigT = { 'update_frequency': 100000, 'clip_subset_type': clip_subset_type }
14751489
seq_df = None
@@ -1506,6 +1520,12 @@ def grid_cell_polygons_from_cellids(
15061520
'clip_cell_addresses' : " ".join([str(address) for address in cell_id_list])
15071521
}
15081522
)
1523+
if "clip_cell_densification" in conf_extra:
1524+
subset_conf.update(
1525+
{
1526+
'clip_cell_densification' : conf_extra['clip_cell_densification']
1527+
}
1528+
)
15091529

15101530
subset_conf.update(specify_resolution(**conf_extra))
15111531
subset_conf.update(specify_orient_type_args(**conf_extra))
@@ -1610,6 +1630,7 @@ def grid_cell_centroids_from_cellids(
16101630
tmp_id = uuid.uuid4()
16111631
tmp_dir = self.working_dir
16121632
dggs = dgselect(dggs_type = dggs_type, res= resolution, mixed_aperture_level=mixed_aperture_level)
1633+
dggs.update(**conf_extra, strict=True)
16131634

16141635
subset_conf: DggridMetaConfigT = { 'update_frequency': 100000, 'clip_subset_type': clip_subset_type }
16151636
seq_df = None
@@ -1737,6 +1758,7 @@ def grid_cellids_for_extent(
17371758
tmp_id = uuid.uuid4()
17381759
tmp_dir = self.working_dir
17391760
dggs = dgselect(dggs_type = dggs_type, res= resolution, mixed_aperture_level=mixed_aperture_level)
1761+
dggs.update(**conf_extra, strict=True)
17401762

17411763
subset_conf = { 'update_frequency': 100000, 'clip_subset_type': 'WHOLE_EARTH' }
17421764

@@ -1821,6 +1843,7 @@ def cells_for_geo_points(
18211843
tmp_id = uuid.uuid4()
18221844
tmp_dir = self.working_dir
18231845
dggs = dgselect(dggs_type = dggs_type, res= resolution, mixed_aperture_level=mixed_aperture_level)
1846+
dggs.update(**conf_extra, strict=True)
18241847

18251848
cols = set(geodf_points_wgs84.columns.tolist())
18261849
cols = cols - set('geometry')
@@ -1917,6 +1940,7 @@ def address_transform(
19171940
tmp_id = uuid.uuid4()
19181941
tmp_dir = self.working_dir
19191942
dggs = dgselect(dggs_type = dggs_type, res= resolution, mixed_aperture_level=mixed_aperture_level)
1943+
dggs.update(**conf_extra, strict=True)
19201944

19211945
if cell_id_list is None or len(cell_id_list) <= 0:
19221946
raise ValueError("Expecting cell_id_list to transform.")

tests/test_dggrid.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import shapely
66

77
from dggrid4py import DGGRIDv8, Dggs
8+
from dggrid4py.dggrid_runner import output_hier_ndx_forms
89

910
dggrid = DGGRIDv8()
1011

@@ -149,6 +150,8 @@ def mock_dggrid_grid_gen_run(__metafile):
149150
dggrid.grid_cell_polygons_for_extent(
150151
dggs_type="IGEO7",
151152
resolution=3,
153+
densification=5,
154+
geodetic_densify=0.01,
152155
clip_geom=clip_bound,
153156
# use string to preserve precision and training zeros explicitly
154157
dggs_vert0_azimuth=0.0,
@@ -173,6 +176,8 @@ def mock_dggrid_grid_gen_run(__metafile):
173176
# NOTE: 'dggs_res_spec' to be inferred from Dggs() attribute, not passing it explicitly to 'specify_resolution'
174177
"dggs_res_spec 3",
175178
"precision 7",
179+
"densification 5",
180+
"geodetic_densify 0.01",
176181
"clip_subset_type GDAL",
177182
# "clip_region_files /tmp/dggrid/...",
178183
# "cell_output_file_name /tmp/dggrid/...",
@@ -190,3 +195,72 @@ def mock_dggrid_grid_gen_run(__metafile):
190195
# "output_hier_ndx_form DIGIT_STRING",
191196
"point_output_type NONE"
192197
}
198+
199+
200+
def test_grid_cell_polygons_from_cellids(monkeypatch):
201+
metafile = []
202+
203+
def mock_dggrid_grid_gen_run(__metafile):
204+
metafile[:] = __metafile
205+
return -1 # cause grid_gen to early-exit in error
206+
207+
monkeypatch.setattr(dggrid, "run", mock_dggrid_grid_gen_run)
208+
209+
with pytest.raises(ValueError): # catch and ignore (early-abort "run error")
210+
dggrid.grid_cell_polygons_from_cellids(
211+
dggs_type="IGEO7",
212+
resolution=3,
213+
cell_id_list=["023"],
214+
clip_cell_densification=5,
215+
clip_subset_type="COARSE_CELLS", # required for densification to take effect
216+
input_address_type="HIERNDX",
217+
input_hier_ndx_forms="DIGIT_STRING",
218+
input_hier_ndx_systems="Z7",
219+
output_cell_label_type="OUTPUT_ADDRESS_TYPE",
220+
output_address_type="HIERNDX",
221+
output_hier_ndx_forms="DIGIT_STRING",
222+
output_hier_ndx_systems="Z7",
223+
# use string to preserve precision and training zeros explicitly
224+
dggs_vert0_azimuth=0.0,
225+
dggs_vert0_lat="58.282525588538994675786", # default: 58.28252559
226+
dggs_vert0_lon="11.20", # default: 11.25
227+
)
228+
229+
# pre-check temp file paths to ignore in check of specific values
230+
meta_args = dict([line.split(" ") for line in metafile])
231+
assert meta_args["clip_region_files"].startswith("/tmp/dggrid")
232+
assert meta_args["cell_output_file_name"].startswith("/tmp/dggrid")
233+
meta_args.pop("clip_region_files")
234+
meta_args.pop("cell_output_file_name")
235+
metafile_patched = [f"{key} {val}" for key, val in meta_args.items()]
236+
237+
assert set(metafile_patched) == {
238+
"dggrid_operation GENERATE_GRID",
239+
"dggs_type IGEO7",
240+
"dggs_proj ISEA",
241+
"dggs_aperture 7",
242+
"dggs_topology HEXAGON",
243+
# NOTE: 'dggs_res_spec' to be inferred from Dggs() attribute, not passing it explicitly to 'specify_resolution'
244+
"dggs_res_spec 3",
245+
"precision 7",
246+
# "clip_region_files /tmp/dggrid/...",
247+
# "cell_output_file_name /tmp/dggrid/...",
248+
"cell_output_type GDAL",
249+
"cell_output_gdal_format FlatGeobuf",
250+
"clip_cell_addresses 023",
251+
"clip_cell_densification 5",
252+
"clip_subset_type COARSE_CELLS",
253+
"clip_cell_res 1",
254+
# following set explicitly by input parameters
255+
"dggs_orient_specify_type SPECIFIED",
256+
"dggs_vert0_azimuth 0.0",
257+
"dggs_vert0_lat 58.282525588538994675786",
258+
"dggs_vert0_lon 11.20",
259+
"input_address_type HIERNDX",
260+
"output_cell_label_type OUTPUT_ADDRESS_TYPE",
261+
"output_address_type HIERNDX",
262+
# WARNING: following technically not set by Dggs(), though it probably should for 'IGEO7' ?
263+
# "output_hier_ndx_system Z7",
264+
# "output_hier_ndx_form DIGIT_STRING",
265+
"point_output_type NONE"
266+
}

0 commit comments

Comments
 (0)