xarray.Dataset.xvec.to_crs#
- Dataset.xvec.to_crs(variable_crs=None, **variable_crs_kwargs)#
Transform
shapely.Geometry
objects of a variable to a new coordinate reference system.Returns a new object with all the original data in addition to the transformed variable. The CRS the current array must be set using
GeometryIndex
.This method will transform all points in all objects. It has no notion or projecting entire geometries. All segments joining points are assumed to be lines in the current projection, not geodesics. Objects crossing the dateline (or other projection boundary) will have undesirable behavior.
- Parameters
- variable_crsdict-like or None, optional
A dict where the keys are the names of the coordinates and values target CRS in any format accepted by
pyproj.CRS.from_user_input()
such as an authority string (e.g."EPSG:4326"
), EPSG code (e.g.4326
) or a WKT string.- **variable_crs_kwargsoptional
The keyword arguments form of
variable_crs
. One ofvariable_crs
orvariable_crs_kwargs
must be provided.
- Returns
- assignedsame type as caller
A new object with the variables transformed to target CRSs.
See also
Notes
Currently supports only
xarray.Variable
objects that are set as coordinates withGeometryIndex
assigned. The implementation currently wrapsDataset.assign_coords
orDataArray.assign_coords
.Examples
Transform coordinates backed by
GeometryIndex
from EPSG:4326 to ESPG:3857.>>> da = ( ... xr.DataArray( ... np.random.rand(2), ... coords={"geom": [shapely.Point(1, 2), shapely.Point(3, 4)]}, ... dims="geom", ... ) ... .xvec.set_geom_indexes("geom", crs=4326) ... ) >>> da <xarray.DataArray (geom: 2)> array([0.47575118, 0.09271935]) Coordinates: * geom (geom) object POINT (1 2) POINT (3 4) Indexes: geom GeometryIndex (crs=EPSG:4326) >>> da.xvec.to_crs(geom=3857) <xarray.DataArray (geom: 2)> array([0.47575118, 0.09271935]) Coordinates: * geom (geom) object POINT (111319.49079327357 222684.20850554405) POIN... Indexes: geom GeometryIndex (crs=EPSG:3857)
The same can be done using dictionary arguments.
>>> da.xvec.to_crs({"geom": 3857}) <xarray.DataArray (geom: 2)> array([0.47575118, 0.09271935]) Coordinates: * geom (geom) object POINT (111319.49079327357 222684.20850554405) POIN... Indexes: geom GeometryIndex (crs=EPSG:3857)
The same applies to a
xarray.Dataset
.>>> ds = ( ... xr.Dataset(coords={"geom": [shapely.Point(1, 2), shapely.Point(3, 4)]}) ... .xvec.set_geom_indexes("geom", crs=4326) ... ) >>> ds <xarray.Dataset> Dimensions: (geom: 2) Coordinates: * geom (geom) object POINT (1 2) POINT (3 4) Data variables: *empty* Indexes: geom GeometryIndex (crs=EPSG:4326) >>> ds.xvec.to_crs(geom=3857) <xarray.Dataset> Dimensions: (geom: 2) Coordinates: * geom (geom) object POINT (111319.49079327357 222684.20850554405) POIN... Data variables: *empty* Indexes: geom GeometryIndex (crs=EPSG:3857)