Skip to content

Commit

Permalink
Update regions_dict (#27)
Browse files Browse the repository at this point in the history
* Update regions_dict

Convert to a class that can be accessed like a dict.

* Fix not_australia NameError
  • Loading branch information
stellema authored Aug 27, 2024
1 parent 43e704d commit 3839ea7
Showing 1 changed file with 91 additions and 50 deletions.
141 changes: 91 additions & 50 deletions acs_plotting_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,58 +161,99 @@
"aridity_index_labels": ["Hyper-arid", "Arid", "Semi-arid", "Dry sub-humid"],
}

# # Load the State and Region shape files
# write a dictionary of the shapefile geopandas dataframes.
# hese will be used for state boundaries, LGAs, NRM, etc
regions_dict = {}

shape_files = [
# "aus_local_gov",
# "aus_states_territories",
class RegionShapefiles:
"""Load and return a shapefile based on its name."""

def __init__(self, path, shapefiles):
"""Create an instance of the RegionShapefiles class.
Parameters
----------
path : str
The path to the shapefiles directory.
shapefiles : list
A list of shapefile names to load.
"""
self.path = path
self.shapefiles = shapefiles
self._regions_dict = {}

def __call__(self, name):
"""Retrieve the shapefile for the given region name.
Parameters
----------
name : str
The name of the region to retrieve.
Returns
-------
GeoDataFrame or GeoSeries
The shapefile data for the specified region.
"""
if name not in self._regions_dict:
if name in self.shapefiles:
self._regions_dict[name] = gpd.read_file(
f"{self.path}/{name}/{name}.shp"
)
elif name == "not_australia":
# Define a white mask for the area outside of Australian land
# We will use this to hide data outside of the Australian land borders.
# note that this is not a data mask,
# the data under the masked area is still loaded and computed, but not visualised
base_name = name[4:] # Remove 'not_' prefix
base_region = self(base_name).copy()
base_region.crs = crs
# This mask is a rectangular box around the maximum land extent of Australia
# with a buffer of 10 degrees on every side,
# with the Australian land area cut out so only the ocean is hidden.
not_region = gpd.GeoSeries(
data=[
box(*box(*base_region.total_bounds).buffer(20).bounds
).difference(base_region["geometry"].values[0])
],
crs=ccrs.PlateCarree(),
)
self._regions_dict[name] = not_region
else:
raise ValueError(f"Shapefile for region '{name}' not found.")
return self._regions_dict[name]

def __getitem__(self, name):
if name not in self._regions_dict:
self(name)
return self._regions_dict[name]

def __setitem__(self, name, value):
self._regions_dict[name] = value

def keys(self):
return self._regions_dict.keys()

def __len__(self):
return len(self._regions_dict)

def __repr__(self):
return repr(self._regions_dict)

def update(self, *args, **kwargs):
self._regions_dict.update(*args, **kwargs)


# Define the path and shapefiles
# These will be used for state boundaries, LGAs, NRM, etc
shapefiles_path = "/g/data/ia39/aus-ref-clim-data-nci/shapefiles/data"
shapefiles = [
"aus_local_gov",
"aus_states_territories",
"australia",
# "nrm_regions",
# "river_regions",
"nrm_regions",
"river_regions",
"ncra_regions",
]
PATH = "/g/data/ia39/aus-ref-clim-data-nci/shapefiles/data"
for name in shape_files:
regions_dict.update(
{
name: gpd.read_file(
f"{PATH}/{name}/{name}.shp"
)
}
)
# regions_dict.update(
# {
# "broadacre_regions": gpd.read_file(
# f"{PATH}/broadacre_regions/aagis_asgs16v1_g5a.shp"
# )
# }
# )

# define a white mask for the area outside of Australian land
# We will use this to hide data outside of the Australian land borders.
# note that this is not a data mask,
# the data under the masked area is still loaded and computed, but not visualised

australia = regions_dict["australia"].copy()

# Define the CRS of the shapefile manually
australia.crs = crs

# This mask is a rectangular box around the maximum land extent of Australia
# with a buffer of 10 degrees on every side,
# with the Australian land area cut out so only the ocean is hidden.
not_australia = gpd.GeoSeries(
data=[
box(*box(*australia.total_bounds).buffer(20).bounds).difference(
australia["geometry"].values[0]
)
],
crs=ccrs.PlateCarree(),
)

# Create an instance of the RegionShapefiles class
regions_dict = RegionShapefiles(shapefiles_path, shapefiles)

# # Define a function for plotting maps
# This is the function you call to plot all the graphs
Expand Down Expand Up @@ -590,8 +631,8 @@ def plot_acs_hazard(
if mask_not_australia:
# outside the shape, fill white
ax.add_geometries(
not_australia,
crs=not_australia.crs,
regions_dict["not_australia"],
crs=regions_dict["not_australia"].crs,
facecolor="white",
linewidth=0,
zorder=5,
Expand Down

0 comments on commit 3839ea7

Please sign in to comment.