Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jterry64 committed Feb 21, 2024
1 parent 586f68c commit 049a934
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions city_metrix/layers/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,17 @@ def _zonal_stats(self, stats_func):
stats = self._zonal_stats_fishnet(stats_func)

if self.layer is not None:
# decode zone and layer value using bit operations
stats["layer"] = stats["zone"].astype("uint32").values >> 16
stats["zone"] = stats["zone"].astype("uint32").values & 65535

stats = stats.groupby("zone").apply(
lambda df: df.drop(columns="zone").groupby("layer").sum().to_dict()[stats_func]
)
# group layer values together into a dictionary per zone
def group_layer_values(df):
layer_values = df.drop(columns="zone").groupby("layer").sum()
layer_dicts = layer_values.to_dict()
return layer_dicts[stats_func]

stats = stats.groupby("zone").apply(group_layer_values)

return stats

Expand Down Expand Up @@ -136,6 +141,7 @@ def _zonal_stats_tile(self, tile_gdf, stats_func):
zones = self._rasterize(tile_gdf, align_to)

if self.layer is not None:
# encode layer into zones by bitshifting
zones = zones + (layer_data.astype("uint32") << 16)

stats = zonal_stats(zones, aggregate_data, stats_funcs=stats_func)
Expand Down

0 comments on commit 049a934

Please sign in to comment.