libpysal.weights.da2W¶
- libpysal.weights.da2W(da, criterion='queen', z_value=None, coords_labels={}, k=1, include_nodata=False, n_jobs=1, **kwargs)[source]¶
Create a W object from xarray.DataArray with an additional attribute index containing coordinate values of the raster in the form of Pandas.Index/MultiIndex.
- Parameters:
- da
xarray.DataArray
Input 2D or 3D DataArray with shape=(z, y, x)
- criterion{“rook”, “queen”}
Type of contiguity. Default is queen.
- z_valueint/string/float
Select the z_value of 3D DataArray with multiple layers.
- coords_labels
dictionary
Pass dimension labels for coordinates and layers if they do not belong to default dimensions, which are (band/time, y/lat, x/lon) e.g. coords_labels = {“y_label”: “latitude”, “x_label”: “longitude”, “z_label”: “year”} Default is {} empty dictionary.
- k
int
Order of contiguity, this will select all neighbors upto kth order. Default is 1.
- include_nodatabool
If True, missing values will be assumed as non-missing when selecting higher_order neighbors, Default is False
- n_jobs
int
Number of cores to be used in the sparse weight construction. If -1, all available cores are used. Default is 1.
- **kwargs
keyword
arguments
Optional arguments for
libpysal.weights.W
- da
- Returns:
- w
libpysal.weights.W
instance of spatial weights class W with an index attribute
- w
See also
Notes
Lower order contiguities are also selected.
Returned object contains index attribute that includes a Pandas.MultiIndex object from the DataArray.
Examples
>>> from libpysal.weights.raster import da2W, testDataArray >>> da = testDataArray().rename( {'band': 'layer', 'x': 'longitude', 'y': 'latitude'}) >>> da.dims ('layer', 'latitude', 'longitude') >>> da.shape (3, 4, 4) >>> da.coords Coordinates: * layer (layer) int64 1 2 3 * latitude (latitude) float64 90.0 30.0 -30.0 -90.0 * longitude (longitude) float64 -180.0 -60.0 60.0 180.0 >>> da.attrs {'nodatavals': (-32768.0,)} >>> coords_labels = { "z_label": "layer", "y_label": "latitude", "x_label": "longitude" } >>> w = da2W(da, z_value=2, coords_labels=coords_labels) >>> "%.3f"%w.pct_nonzero '30.000' >>> w[(2, 90.0, 180.0)] == {(2, 90.0, 60.0): 1, (2, 30.0, 180.0): 1} True >>> len(w.index) 10 >>> w.index[:2] MultiIndex([(2, 90.0, 60.0), (2, 90.0, 180.0)], names=['layer', 'latitude', 'longitude'])