@@ -1492,10 +1492,10 @@ def regions(
14921492 Parameters
14931493 ----------
14941494 raster : xr.DataArray
1495- connections : int, default=4
1495+ neighborhood : int, default=4
14961496 4 or 8 pixel-based connectivity.
1497- name: str, default='regions'
1498- output xr.DataArray.name property.
1497+ name : str, default='regions'
1498+ Output xr.DataArray.name property.
14991499
15001500 Returns
15011501 -------
@@ -1507,90 +1507,56 @@ def regions(
15071507
15081508 Examples
15091509 --------
1510- .. plot::
1511- :include-source:
1512-
1513- import matplotlib.pyplot as plt
1514- import numpy as np
1515- import xarray as xr
1516-
1517- from xrspatial import generate_terrain
1518- from xrspatial.zonal import regions
1519-
1520-
1521- # Generate Example Terrain
1522- W = 500
1523- H = 300
1524-
1525- template_terrain = xr.DataArray(np.zeros((H, W)))
1526- x_range=(-20e6, 20e6)
1527- y_range=(-20e6, 20e6)
1528-
1529- terrain_agg = generate_terrain(
1530- template_terrain, x_range=x_range, y_range=y_range
1531- )
1532-
1533- # Edit Attributes
1534- terrain_agg = terrain_agg.assign_attrs(
1535- {
1536- 'Description': 'Example Terrain',
1537- 'units': 'km',
1538- 'Max Elevation': '4000',
1539- }
1540- )
1541-
1542- terrain_agg = terrain_agg.rename({'x': 'lon', 'y': 'lat'})
1543- terrain_agg = terrain_agg.rename('Elevation')
1544-
1545- # Create Regions
1546- regions_agg = regions(terrain_agg)
1547-
1548- # Edit Attributes
1549- regions_agg = regions_agg.assign_attrs({'Description': 'Example Regions',
1550- 'units': ''})
1551- regions_agg = regions_agg.rename('Region Value')
1552-
1553- # Plot Terrain (Values)
1554- terrain_agg.plot(cmap = 'terrain', aspect = 2, size = 4)
1555- plt.title("Terrain (Values)")
1556- plt.ylabel("latitude")
1557- plt.xlabel("longitude")
1558-
1559- # Plot Regions
1560- regions_agg.plot(cmap = 'terrain', aspect = 2, size = 4)
1561- plt.title("Regions")
1562- plt.ylabel("latitude")
1563- plt.xlabel("longitude")
1564-
15651510 .. sourcecode:: python
15661511
1567- >>> print(terrain_agg[200:203, 200:202])
1568- <xarray.DataArray 'Elevation' (lat: 3, lon: 2)>
1569- array([[1264.02296597, 1261.947921 ],
1570- [1285.37105519, 1282.48079719],
1571- [1306.02339636, 1303.4069579 ]])
1572- Coordinates:
1573- * lon (lon) float64 -3.96e+06 -3.88e+06
1574- * lat (lat) float64 6.733e+06 6.867e+06 7e+06
1575- Attributes:
1576- res: (80000.0, 133333.3333333333)
1577- Description: Example Terrain
1578- units: km
1579- Max Elevation: 4000
1580-
1581- >>> print(regions_agg[200:203, 200:202])
1582- <xarray.DataArray 'Region Value' (lat: 3, lon: 2)>
1583- array([[39557., 39558.],
1584- [39943., 39944.],
1585- [40327., 40328.]])
1586- Coordinates:
1587- * lon (lon) float64 -3.96e+06 -3.88e+06
1588- * lat (lat) float64 6.733e+06 6.867e+06 7e+06
1589- Attributes:
1590- res: (80000.0, 133333.3333333333)
1591- Description: Example Regions
1592- units:
1593- Max Elevation: 4000
1512+ >>> import numpy as np
1513+ >>> import xarray as xr
1514+ >>> from xrspatial.zonal import regions
1515+
1516+ >>> # Create a raster with distinct value regions
1517+ >>> arr = np.array([[1, 1, 0, 2, 2],
1518+ ... [1, 1, 0, 2, 2],
1519+ ... [0, 0, 0, 0, 0],
1520+ ... [3, 3, 0, 3, 3],
1521+ ... [3, 3, 0, 3, 3]], dtype=np.float64)
1522+ >>> raster = xr.DataArray(arr, dims=['y', 'x'])
1523+ >>> print(raster.values)
1524+ [[1. 1. 0. 2. 2.]
1525+ [1. 1. 0. 2. 2.]
1526+ [0. 0. 0. 0. 0.]
1527+ [3. 3. 0. 3. 3.]
1528+ [3. 3. 0. 3. 3.]]
1529+
1530+ >>> # With 4-connectivity, each group of connected same-value
1531+ >>> # pixels becomes a unique region
1532+ >>> result = regions(raster, neighborhood=4)
1533+ >>> print(result.values)
1534+ [[1. 1. 2. 3. 3.]
1535+ [1. 1. 2. 3. 3.]
1536+ [2. 2. 2. 2. 2.]
1537+ [5. 5. 2. 6. 6.]
1538+ [5. 5. 2. 6. 6.]]
1539+
1540+ >>> # Note: The two bottom-corner 3-regions are separate because
1541+ >>> # they are not connected (the zero-valued cross separates them)
1542+ >>> print(f"Number of unique regions: {len(np.unique(result.values))}")
1543+ Number of unique regions: 5
1544+
1545+ >>> # With 8-connectivity, diagonal neighbors also connect regions
1546+ >>> diagonal = np.array([[1, 0, 1],
1547+ ... [0, 1, 0],
1548+ ... [1, 0, 1]], dtype=np.float64)
1549+ >>> raster_diag = xr.DataArray(diagonal, dims=['y', 'x'])
1550+ >>> result_8 = regions(raster_diag, neighborhood=8)
1551+ >>> print(result_8.values)
1552+ [[1. 2. 1.]
1553+ [2. 1. 2.]
1554+ [1. 2. 1.]]
1555+
1556+ >>> # All 1s are connected diagonally into one region,
1557+ >>> # all 0s form another region
1558+ >>> print(f"Number of unique regions: {len(np.unique(result_8.values))}")
1559+ Number of unique regions: 2
15941560 """
15951561 if neighborhood not in (4 , 8 ):
15961562 raise ValueError ("`neighborhood` value must be either 4 or 8)" )
0 commit comments