xrspatial.convolution.calc_cellsize

xrspatial.convolution.calc_cellsize(raster)[source]

Calculates cell size of an array based on its attributes. Default = meters. If lat-lon, units are converted to meters.

Parameters

raster (xarray.DataArray) – 2D array of input values.

Returns

  • cellsize (tuple) – Tuple of (cellsize_x, cellsize_y).

  • cellsize_x (float) – Size of cells in x-direction.

  • cellsize_y (float) – Size of cells in y-direction.

Examples

import matplotlib.pyplot as plt
import numpy as np
import xarray as xr

from xrspatial import generate_terrain
from xrspatial.convolution import calc_cellsize

# Generate Example Terrain
W = 500
H = 300

template_terrain = xr.DataArray(np.zeros((H, W)))
x_range=(-20e6, 20e6)
y_range=(-20e6, 20e6)

terrain_agg = generate_terrain(
    template_terrain, x_range=x_range, y_range=y_range
)

# Edit Attributes
terrain_agg = terrain_agg.assign_attrs(
    {
        'Description': 'Example Terrain',
        'units': 'km',
        'Max Elevation': '4000',
    }
)

terrain_agg = terrain_agg.rename({'x': 'lon', 'y': 'lat'})
terrain_agg = terrain_agg.rename('Elevation')
>>> print(terrain_agg[200:203, 200:202])
<xarray.DataArray 'Elevation' (lat: 3, lon: 2)>
array([[1264.02296597, 1261.947921  ],
       [1285.37105519, 1282.48079719],
       [1306.02339636, 1303.4069579 ]])
Coordinates:
* lon      (lon) float64 -3.96e+06 -3.88e+06
* lat      (lat) float64 6.733e+06 6.867e+06 7e+06
Attributes:
    res:            (80000.0, 133333.3333333333)
    Description:    Example Terrain
    units:          km
    Max Elevation:  4000
>>> # Calculate Cellsize
>>> cellsize = calc_cellsize(terrain_agg)
>>> print(cellsize)
(80000.0, 133333.3333333333)