Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved _feature function to top level #51

Merged
merged 5 commits into from
Jul 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions virtual_rainforest/core/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@dataclass
class CoreGridConfig:
"""Configure the `core.grid` module
"""Configure the `core.grid` module.
This data class is used to setup the arrangement of grid cells to be used in
running a `virtual_rainforest` simulation.
Expand All @@ -39,7 +39,7 @@ class CoreGridConfig:


class CoreGrid:
"""Define the grid of cells used in a virtual rainforest simulation
"""Define the grid of cells used in a virtual rainforest simulation.
Args:
config: A CoreGridConfig instance used to setup the grid
Expand Down Expand Up @@ -80,7 +80,7 @@ def __repr__(self) -> str:

@staticmethod
def _make_square_grid(cell_area: float, cell_nx: int, cell_ny: int) -> dict:
"""Create a square grid
"""Create a square grid.
Args:
cell_area:
Expand Down Expand Up @@ -111,7 +111,7 @@ def _make_square_grid(cell_area: float, cell_nx: int, cell_ny: int) -> dict:

@staticmethod
def _make_hex_grid(cell_area: float, cell_nx: int, cell_ny: int) -> dict:
"""Create a hexagon grid
"""Create a hexagon grid.
Args:
cell_area:
Expand Down Expand Up @@ -154,6 +154,27 @@ def _make_hex_grid(cell_area: float, cell_nx: int, cell_ny: int) -> dict:

return cell_dict

@staticmethod
def cell_to_feature(cell_tuple: tuple) -> dict:
"""Convert a cell_dict entry to a GeoJSON feature.
The cell_dict keys cell IDs to a dictionary of the cell polygon and centroid.
This function takes a (key, val) tuple from iterating over the cell_dict and
returns a dictionary in the stucture needed for output to GeoJSON.
"""

cell_id, cell_dict = cell_tuple

poly = [tuple(x) for x in cell_dict["poly"].tolist()]
geom = {"type": "Polygon", "coordinates": [poly]}

props = {
"cell_id": cell_id,
"cell_cx": cell_dict["centroid"][0],
"cell_cy": cell_dict["centroid"][1],
}
return {"type": "Feature", "geometry": geom, "properties": props}

def export_geojson(self, outfile: str):
"""Export grid system as geojson file.
Expand All @@ -179,21 +200,5 @@ def export_geojson(self, outfile: str):
},
) as output:

def _feature(cell_tuple):
"""Convert a (key, val) tuple from iterating over the items in
self.cell_dict into a fiona feature"""

cell_id, cell_dict = cell_tuple

poly = [tuple(x) for x in cell_dict["poly"].tolist()]
geom = {"type": "Polygon", "coordinates": [poly]}

props = {
"cell_id": cell_id,
"cell_cx": cell_dict["centroid"][0],
"cell_cy": cell_dict["centroid"][1],
}
return {"type": "Feature", "geometry": geom, "properties": props}

# Write all of self.cell_dict to file
output.writerecords(map(_feature, self.cell_dict.items()))
output.writerecords(map(self.cell_to_feature, self.cell_dict.items()))