コード例 #1
0
def fraction_overlap_mask(shapes_gp, lons, lats, min_overlap):
    """Create a 3D boolean array for grid cells over the shape overlap threshold.

    Parameters
    ----------
    shapes_gp : geopandas GeoDataFrame
        Shapes/regions
    lons : numpy ndarray
        Grid longitude values
    lats : numpy ndarray
        Grid latitude values
    threshold : float
        Minimum fractional overlap

    Returns
    -------
    mask_3D : xarray DataArray
        Three dimensional (i.e. region/lat/lon) boolean array
    """

    assert min_overlap > 0.0, "Minimum overlap must be fractional value > 0"
    assert min_overlap <= 1.0, "Minimum overlap must be fractional value <= 1.0"
    _check_regular_grid(lons)
    _check_regular_grid(lats)

    shapes_rm = regionmask.from_geopandas(shapes_gp)
    fraction = overlap_fraction(shapes_rm, lons, lats)
    fraction = _squeeze_and_drop_region(fraction)
    mask_3D = fraction > min_overlap

    return mask_3D
コード例 #2
0
 def _open_dataset(self):
     try:
         import regionmask
     except ImportError:
         raise ImportError('please install regionmask')
     super()._open_dataset()
     self._dtypes = self._dataframe.dtypes.to_dict()
     self._dtypes = {n: str(t) for (n, t) in self._dtypes.items()}
     self._dataframe = regionmask.from_geopandas(
         self._dataframe, **self._regionmask_kwargs
     )
コード例 #3
0
def test_from_geopandas_default(geodataframe_clean):

    result = from_geopandas(geodataframe_clean)

    assert isinstance(result, Regions)

    assert result.polygons[0].equals(poly1)
    assert result.polygons[1].equals(poly2)
    assert result.numbers == [0, 1]
    assert result.names == ["Region0", "Region1"]
    assert result.abbrevs == ["r0", "r1"]
    assert result.name == "unnamed"
    assert result.source is None
コード例 #4
0
def test_from_geopandas_default(geodataframe_clean):

    result = from_geopandas(geodataframe_clean)

    assert isinstance(result, Regions)

    assert result.polygons[0].equals(dummy_outlines_poly[0])
    assert result.polygons[1].equals(dummy_outlines_poly[1])
    assert result.polygons[2].equals(dummy_outlines_poly[2])
    assert result.numbers == [0, 1, 2]
    assert result.names == ["Region0", "Region1", "Region2"]
    assert result.abbrevs == ["r0", "r1", "r2"]
    assert result.name == "unnamed"
    assert result.source is None
コード例 #5
0
def test_from_geopandas_use_columns(geodataframe_clean):

    result = from_geopandas(
        geodataframe_clean,
        numbers="numbers",
        names="names",
        abbrevs="abbrevs",
        name="name",
        source="source",
    )

    assert isinstance(result, Regions)

    assert result.polygons[0].equals(poly1)
    assert result.polygons[1].equals(poly2)
    assert result.numbers == [1, 2]
    assert result.names == ["Unit Square1", "Unit Square2"]
    assert result.abbrevs == ["uSq1", "uSq2"]
    assert result.name == "name"
    assert result.source == "source"
コード例 #6
0
def get_regionmask(geodataframe, **kwargs):
    import regionmask

    return regionmask.from_geopandas(geodataframe, **kwargs)
コード例 #7
0
def test_from_geopandas_wrong_input():
    with pytest.raises(
            TypeError,
            match="`geodataframe` must be a geopandas 'GeoDataFrame'"):
        from_geopandas(None)
コード例 #8
0
def test_from_geopandas_column_missing(geodataframe_clean, arg):

    with pytest.raises(KeyError):
        from_geopandas(geodataframe_clean, **{arg: "not_a_column"})
コード例 #9
0
def test_from_geopandas_duplicates_error(geodataframe_duplicates, arg):

    with pytest.raises(ValueError,
                       match="{} cannot contain duplicate values".format(arg)):
        from_geopandas(geodataframe_duplicates, **{arg: arg})
コード例 #10
0
def test_from_geopandas_missing_error(geodataframe_missing, arg):

    with pytest.raises(ValueError,
                       match="{} cannot contain missing values".format(arg)):
        from_geopandas(geodataframe_missing, **{arg: arg})
コード例 #11
0
# Fit for each gridcell with the whole year average.

# %%
ds_yearly_trends = (ds_labourloss.labour.groupby(
    "time.year").mean().pipe(lambda x: fit_parallel_wrapper(
        gsat_change, x.sel(year=gsat_change.year), "year")))

# %% [markdown]
# ## Using the RiceAtlas data
#
# Now we will use the data in the RiceAtlas shapefile to create masks and
# weightings for our results.

# %%
# Use regionmask to start with
regions = regionmask.from_geopandas(ra, names="HASC")
mask = regions.mask(ds.lon, ds.lat)
# Turn regions into dimensions instead
mask_regions = xr.concat([(mask == r.number) for r in regions], dim="region")
mask_regions["region"] = ra.HASC.values

all_masks = []
all_weights = []
peak_months = []
for i_region, region in ra.iterrows():
    months = [month_dict[m] for m in region[[f"HMO_PK{i}" for i in (1, 2, 3)]]]
    weights = region[[f"P_S{i}" for i in (1, 2, 3)]].astype(float).values
    single_mask = mask == regions[region.HASC].number
    if not single_mask.any():
        single_mask = valid_gridcells.sel(
            lon=[region.geometry.centroid.x],