Skip to content

Commit 0148cbe

Browse files
committed
Made sure the parent resultion is of type int consistently during processing
1 parent f861c59 commit 0148cbe

File tree

7 files changed

+40
-21
lines changed

7 files changed

+40
-21
lines changed

raster2dggs/common.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def assemble_kwargs(
107107

108108
return kwargs
109109

110+
110111
def zero_padding(dggs: str) -> int:
111112
max_res_lookup = {
112113
"h3": const.MAX_H3,
@@ -120,6 +121,7 @@ def zero_padding(dggs: str) -> int:
120121
raise ValueError(f"Unknown DGGS type: {dggs}")
121122
return len(str(max_res))
122123

124+
123125
def get_parent_res(dggs: str, parent_res: Union[None, int], resolution: int) -> int:
124126
"""
125127
Uses a parent resolution,
@@ -128,20 +130,17 @@ def get_parent_res(dggs: str, parent_res: Union[None, int], resolution: int) ->
128130
129131
Used for intermediate re-partioning.
130132
"""
131-
default_dggs_parent_res = {
132-
"h3": max(const.MIN_H3, (resolution - const.DEFAULT_PARENT_OFFSET)),
133-
"rhp": max(const.MIN_RHP, (resolution - const.DEFAULT_PARENT_OFFSET)),
134-
"geohash": max(const.MIN_GEOHASH, (resolution - const.DEFAULT_PARENT_OFFSET)),
135-
"maidenhead": const.MIN_MAIDENHEAD,
136-
"s2": max(const.MIN_S2, (resolution - const.DEFAULT_PARENT_OFFSET)),
137-
}
138-
if not dggs in default_dggs_parent_res:
133+
if not dggs in const.DEFAULT_DGGS_PARENT_RES.keys():
139134
raise RuntimeError(
140135
"Unknown dggs {dggs}) - must be one of [ {options} ]".format(
141-
dggs=dggs, options=", ".join(default_dggs_parent_res.keys())
136+
dggs=dggs, options=", ".join(const.DEFAULT_DGGS_PARENT_RES.keys())
142137
)
143138
)
144-
return parent_res if parent_res is not None else default_dggs_parent_res[dggs]
139+
return (
140+
int(parent_res)
141+
if parent_res is not None
142+
else const.DEFAULT_DGGS_PARENT_RES[dggs]
143+
)
145144

146145

147146
def address_boundary_issues(
@@ -166,8 +165,6 @@ def address_boundary_issues(
166165
of the original (i.e. window-based) partitioning. Using the nested structure of the DGGS is an useful property
167166
to address this problem.
168167
"""
169-
parent_res = get_parent_res(dggs, parent_res, resolution)
170-
171168
LOGGER.debug(
172169
f"Reading Stage 1 output ({pq_input}) and setting index for parent-based partitioning"
173170
)

raster2dggs/constants.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,13 @@
2121
}
2222

2323
DEFAULT_PARENT_OFFSET = 6
24+
25+
DEFAULT_DGGS_PARENT_RES = {
26+
"h3": lambda resolution: max(MIN_H3, (resolution - DEFAULT_PARENT_OFFSET)),
27+
"rhp": lambda resolution: max(MIN_RHP, (resolution - DEFAULT_PARENT_OFFSET)),
28+
"geohash": lambda resolution: max(
29+
MIN_GEOHASH, (resolution - DEFAULT_PARENT_OFFSET)
30+
),
31+
"maidenhead": lambda resolution: MIN_MAIDENHEAD,
32+
"s2": lambda resolution: max(MIN_S2, (resolution - DEFAULT_PARENT_OFFSET)),
33+
}

raster2dggs/geohash.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
from raster2dggs import __version__
1818

1919

20-
PAD_WIDTH = common.zero_padding('geohash')
20+
PAD_WIDTH = common.zero_padding("geohash")
21+
2122

2223
def _geohashfunc(
2324
sdf: xr.DataArray,
@@ -46,7 +47,9 @@ def _geohashfunc(
4647
# Secondary (parent) Geohash index, used later for partitioning
4748
geohash_parent = [gh[:parent_precision] for gh in geohash]
4849
subset = subset.drop(columns=["x", "y"])
49-
subset[f"geohash_{precision:0{PAD_WIDTH}d}"] = pd.Series(geohash, index=subset.index)
50+
subset[f"geohash_{precision:0{PAD_WIDTH}d}"] = pd.Series(
51+
geohash, index=subset.index
52+
)
5053
subset[f"geohash_{parent_precision:0{PAD_WIDTH}d}"] = pd.Series(
5154
geohash_parent, index=subset.index
5255
)
@@ -71,7 +74,11 @@ def _geohash_parent_groupby(
7174
high resolution raster at a coarse Geohash precision.
7275
"""
7376
if decimals > 0:
74-
return df.groupby(f"geohash_{precision:0{PAD_WIDTH}d}").agg(aggfunc).round(decimals)
77+
return (
78+
df.groupby(f"geohash_{precision:0{PAD_WIDTH}d}")
79+
.agg(aggfunc)
80+
.round(decimals)
81+
)
7582
else:
7683
return (
7784
df.groupby(f"geohash_{precision:0{PAD_WIDTH}d}")
@@ -201,7 +208,7 @@ def geohash(
201208
raster_input,
202209
output_directory,
203210
int(resolution),
204-
int(parent_res),
211+
parent_res,
205212
warp_args,
206213
**kwargs,
207214
)

raster2dggs/h3.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
PAD_WIDTH = common.zero_padding("h3")
2020

21+
2122
def _h3func(
2223
sdf: xr.DataArray,
2324
resolution: int,
@@ -194,7 +195,7 @@ def h3(
194195
raster_input,
195196
output_directory,
196197
int(resolution),
197-
int(parent_res),
198+
parent_res,
198199
warp_args,
199200
**kwargs,
200201
)

raster2dggs/maidenhead.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def maidenhead(
201201
raster_input,
202202
output_directory,
203203
int(resolution),
204-
int(parent_res),
204+
parent_res,
205205
warp_args,
206206
**kwargs,
207207
)

raster2dggs/rHP.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
PAD_WIDTH = common.zero_padding("h3")
2020

21+
2122
def _rhpfunc(
2223
sdf: xr.DataArray,
2324
resolution: int,
@@ -64,7 +65,9 @@ def _rhp_parent_groupby(
6465
high resolution raster at a coarser resolution.
6566
"""
6667
if decimals > 0:
67-
return df.groupby(f"rhp_{resolution:0{PAD_WIDTH}d}").agg(aggfunc).round(decimals)
68+
return (
69+
df.groupby(f"rhp_{resolution:0{PAD_WIDTH}d}").agg(aggfunc).round(decimals)
70+
)
6871
else:
6972
return (
7073
df.groupby(f"rhp_{resolution:0{PAD_WIDTH}d}")
@@ -194,7 +197,7 @@ def rhp(
194197
raster_input,
195198
output_directory,
196199
int(resolution),
197-
int(parent_res),
200+
parent_res,
198201
warp_args,
199202
**kwargs,
200203
)

raster2dggs/s2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
PAD_WIDTH = common.zero_padding("s2")
2020

21+
2122
def _s2func(
2223
sdf: xr.DataArray,
2324
resolution: int,
@@ -198,7 +199,7 @@ def s2(
198199
raster_input,
199200
output_directory,
200201
int(resolution),
201-
int(parent_res),
202+
parent_res,
202203
warp_args,
203204
**kwargs,
204205
)

0 commit comments

Comments
 (0)