@@ -155,70 +155,119 @@ def _run_dask_numpy(data: da.Array) -> da.Array:
155155def aspect (agg : xr .DataArray ,
156156 name : Optional [str ] = 'aspect' ) -> xr .DataArray :
157157 """
158- Calculates, for all cells in the array,
159- the downward slope direction of each cell
160- based on the elevation of its neighbors in a 3x3 grid.
161- The value is measured clockwise in degrees with 0 and 360 at due north.
162- Flat areas are given a value of -1.
163- Values along the edges are not calculated.
164-
165- Parameters:
158+ Calculates the aspect value of an elevation aggregate.
159+
160+ Calculates, for all cells in the array, the downward slope direction
161+ of each cell based on the elevation of its neighbors in a 3x3 grid.
162+ The value is measured clockwise in degrees with 0 and 360 at due
163+ north. Flat areas are given a value of -1. Values along the edges
164+ are not calculated.
165+
166+ Parameters
166167 ----------
167- agg: xarray.DataArray
168- 2D array of elevation values. NumPy, CuPy, NumPy-backed Dask,
169- or Cupy-backed Dask array .
170- name: str, optional ( default = " aspect")
168+ agg : xarray.DataArray
169+ 2D NumPy, CuPy, NumPy-backed Dask, or Cupy-backed Dask array
170+ of elevation values .
171+ name : str, default=' aspect'
171172 Name of ouput DataArray.
172173
173- Returns:
174- ----------
175- xarray.DataArray
176- 2D array, of the same type as the input, of calculated aspect values.
174+ Returns
175+ -------
176+ aspect_agg : xarray.DataArray of the same type as `agg`
177+ 2D aggregate array of calculated aspect values.
177178 All other input attributes are preserved.
178179
179- Notes:
180+ References
180181 ----------
181- Algorithm References:
182- - esri, How Aspect Works, http://desktop.arcgis.com/en/arcmap/10.3/tools/spatial-analyst-toolbox/how-aspect-works.htm#ESRI_SECTION1_4198691F8852475A9F4BC71246579FAA, Accessed Apr. 21, 2021. # noqa
183- - Burrough, P. A., McDonnell, R., McDonnell, R. A., & Lloyd, C. D. (2015). Principles of geographical information systems. Oxford university press. pp 406. # noqa
184-
185- Examples:
186- ----------
187- Imports
188- >>> import numpy as np
189- >>> import xarray as xr
190- >>> import xrspatial
191-
192- Create Elevation DataArray
193- >>> agg = xr.DataArray(np.array([[0, 1, 0, 0],
194- >>> [1, 1, 0, 0],
195- >>> [0, 1, 2, 2],
196- >>> [1, 0, 2, 0],
197- >>> [0, 2, 2, 2]]),
198- >>> dims = ["lat", "lon"])
199- >>> height, width = agg.shape
200- >>> _lon = np.linspace(0, width - 1, width)
201- >>> _lat = np.linspace(0, height - 1, height)
202- >>> agg["lon"] = _lon
203- >>> agg["lat"] = _lat
204-
205- Create Aspect DataArray
206- >>> aspect = xrspatial.aspect(agg)
207- >>> print(aspect)
208- <xarray.DataArray 'aspect' (lat: 5, lon: 4)>
209- array([[nan, nan, nan, nan],
210- [nan, 0., 18.43494882, nan],
211- [nan, 270., 341.56505118, nan],
212- [nan, 288.43494882, 315., nan],
213- [nan, nan, nan, nan]])
214- Coordinates:
215- * lon (lon) float64 0.0 1.0 2.0 3.0
216- * lat (lat) float64 0.0 1.0 2.0 3.0 4.0
217-
218- Terrain Example:
219- - makepath, User Guide, https://makepath.github.io/xarray-spatial/assets/examples/user-guide.html, Accessed Apr. 21, 2021 # noqa
182+ - arcgis: http://desktop.arcgis.com/en/arcmap/10.3/tools/spatial-analyst-toolbox/how-aspect-works.htm#ESRI_SECTION1_4198691F8852475A9F4BC71246579FAA # noqa
183+
184+ Examples
185+ --------
186+ .. plot::
187+ :include-source:
188+
189+ import datashader as ds
190+ import matplotlib.pyplot as plt
191+ from xrspatial import generate_terrain, aspect
192+
193+ # Create Canvas
194+ W = 500
195+ H = 300
196+ cvs = ds.Canvas(plot_width = W,
197+ plot_height = H,
198+ x_range = (-20e6, 20e6),
199+ y_range = (-20e6, 20e6))
200+
201+ # Generate Example Terrain
202+ terrain_agg = generate_terrain(canvas = cvs)
203+
204+ # Edit Attributes
205+ terrain_agg = terrain_agg.assign_attrs(
206+ {
207+ 'Description': 'Example Terrain',
208+ 'units': 'km',
209+ 'Max Elevation': '4000',
210+ }
211+ )
212+
213+ terrain_agg = terrain_agg.rename({'x': 'lon', 'y': 'lat'})
214+ terrain_agg = terrain_agg.rename('Elevation')
215+
216+ # Create Aspect Aggregate Array
217+ aspect_agg = aspect(agg = terrain_agg, name = 'Aspect')
218+
219+ # Edit Attributes
220+ aspect_agg = aspect_agg.assign_attrs(
221+ {
222+ 'Description': 'Example Aspect',
223+ 'units': 'deg',
224+ }
225+ )
226+
227+ # Plot Terrain
228+ terrain_agg.plot(cmap = 'terrain', aspect = 2, size = 4)
229+ plt.title("Terrain")
230+ plt.ylabel("latitude")
231+ plt.xlabel("longitude")
232+
233+ # Plot Aspect
234+ aspect_agg.plot(aspect = 2, size = 4)
235+ plt.title("Aspect")
236+ plt.ylabel("latitude")
237+ plt.xlabel("longitude")
238+
239+ .. sourcecode:: python
240+
241+ >>> print(terrain_agg[200:203, 200:202])
242+ <xarray.DataArray 'Elevation' (lat: 3, lon: 2)>
243+ array([[1264.02249454, 1261.94748873],
244+ [1285.37061171, 1282.48046696],
245+ [1306.02305679, 1303.40657515]])
246+ Coordinates:
247+ * lon (lon) float64 -3.96e+06 -3.88e+06
248+ * lat (lat) float64 6.733e+06 6.867e+06 7e+06
249+ Attributes:
250+ res: 1
251+ Description: Example Terrain
252+ units: km
253+ Max Elevation: 4000
254+
255+ .. sourcecode:: python
256+
257+ >>> print(aspect_agg[200:203, 200:202])
258+ <xarray.DataArray 'Aspect' (lat: 3, lon: 2)>
259+ array([[ 8.18582638, 8.04675084],
260+ [ 5.49302641, 9.86625477],
261+ [12.04270534, 16.87079619]])
262+ Coordinates:
263+ * lon (lon) float64 -3.96e+06 -3.88e+06
264+ * lat (lat) float64 6.733e+06 6.867e+06 7e+06
265+ Attributes:
266+ res: 1
267+ Description: Example Aspect
268+ units: deg
269+ Max Elevation: 4000
220270 """
221-
222271 # numpy case
223272 if isinstance (agg .data , np .ndarray ):
224273 out = _run_numpy (agg .data )
0 commit comments