uber / h3-py-notebooks

Jupyter notebooks for h3-py, a hierarchical hexagonal geospatial indexing system
https://h3geo.org/
Apache License 2.0
263 stars 90 forks source link

kring_smoothing is not averaging over its k-ring neighbors #4

Open gpolov-personal opened 3 years ago

gpolov-personal commented 3 years ago

I understand that the intention of the kring_smoothing function is to assign to each cell the "k-ring moving average" of its k-ring neighbors. If this is the idea (maybe the meaning is different), I think the function is not correct, it is quite similar but not the same.

Currently when you are merging:
.merge(df[[hex_col,metric_col]]).fillna(0)

You are assigning to each of the hexk neighbors of a cell the values of the cell value, so each new hexk cell will have the average of all of the cell for which it is a k-ring neighbor. This causes that new cells that were not present appear after the smoothing (so the metric 'mass' is conservative) because even if not present in the original set, the boundary cells will create new neighbors

As far as I understand, the way to smooth would be to assign to each cell the average of all its k-ring neighbors, like a moving mean but with hexagonal rings. Little modifications has to be made in the merge and group_by. Merging against hexk instead of hex_col (but to avoid the mess of columns names suffixes the trick is to temporarily rename the hex_col to hexk in the original data frame and performing a mean in the group by). .merge(df.loc[:,[hex_col,metric_col]].rename(columns={hex_col:'hexk'}), how='left').fillna(0) .groupby([hex_col])[[metric_col]].mean()