Size: 32B\n",
+ "Dimensions: (space: 3)\n",
"Coordinates:\n",
- " * points (space) object 400B POINT (72.14265652432513 -68.90724731796287)...\n",
+ " * points (space) object 24B POINT (171.61191601133118 -10.61889803036...\n",
+ " * spatial_ref int64 8B 0\n",
"Dimensions without coordinates: space\n",
"Data variables:\n",
" *empty*\n",
"Indexes:\n",
- " points GeometryIndex (crs=None)"
+ " points GeometryIndex (crs=None)\n",
+ " spatial_ref CRSIndex (crs=EPSG:4326)"
]
},
"execution_count": 8,
@@ -1568,23 +1504,21 @@
}
],
"source": [
- "ds_no_crs = ds_wgs84.drop_vars(\"spatial_ref\")\n",
- "\n",
- "ds_no_crs"
+ "xr.concat([ds_wgs84.isel(space=[0, 1]), ds_wgs84.isel(space=[-1])], \"space\")"
]
},
{
"cell_type": "markdown",
- "id": "fff6acec-9d29-443e-a29c-802833b4388d",
+ "id": "cb9a1bc2-2052-4b62-a486-551a2dc4ef37",
"metadata": {},
"source": [
- "It is possible to combine datasets with and without a defined CRS. The resulting dataset will have the common CRS found among all datasets. "
+ "It is possible to combine heterogeneous geospatial Datasets (e.g., raster, vector, grid, mesh) as long as they all have a spatial reference coordinate with the same name and with a `CRSIndex`."
]
},
{
"cell_type": "code",
"execution_count": 9,
- "id": "d142186b-9f5a-4809-b775-66e35e7af175",
+ "id": "aaf7ed29-66c5-42f6-872f-12ea0792c63d",
"metadata": {},
"outputs": [
{
@@ -1960,51 +1894,36 @@
" stroke: currentColor;\n",
" fill: currentColor;\n",
"}\n",
- "<xarray.Dataset> Size: 408B\n",
- "Dimensions: (space: 50)\n",
+ "<xarray.Dataset> Size: 328B\n",
+ "Dimensions: (space: 10, lat: 10, lon: 20)\n",
"Coordinates:\n",
- " * points (space) object 400B POINT (72.14265652432513 -68.90724731796...\n",
+ " * points (space) object 80B POINT (171.61191601133118 -10.61889803036...\n",
" * spatial_ref int64 8B 0\n",
+ " * lat (lat) float64 80B -90.0 -70.0 -50.0 -30.0 ... 50.0 70.0 90.0\n",
+ " * lon (lon) float64 160B -180.0 -161.1 -142.1 ... 142.1 161.1 180.0\n",
"Dimensions without coordinates: space\n",
"Data variables:\n",
" *empty*\n",
"Indexes:\n",
" points GeometryIndex (crs=None)\n",
- " spatial_ref CRSIndex (crs=EPSG:4326) Dimensions:
Coordinates: (2)
Data variables: (0)
Indexes: (2)
GeometryIndex (crs=None)
GeometryIndex(\n",
- " [<POINT (72.143 -68.907)>\n",
- " <POINT (-71.612 1.312)>\n",
- " <POINT (-13.051 81.693)>\n",
- " <POINT (-109.915 20.408)>\n",
+ " spatial_ref CRSIndex (crs=EPSG:4326) Dimensions:
Coordinates: (4)
points
(space)
object
POINT (171.61191601133118 -10.61...
array([<POINT (171.612 -10.619)>, <POINT (-43.13 19.777)>,\n",
+ " <POINT (152.369 65.452)>, <POINT (-85.791 65.476)>,\n",
+ " <POINT (-65.125 31.479)>, <POINT (-137.487 28.777)>,\n",
+ " <POINT (-92.964 42.436)>, <POINT (-65.328 -49.904)>,\n",
+ " <POINT (167.069 -59.028)>, <POINT (-85.086 66.675)>], dtype=object) spatial_ref
()
int64
0
lat
(lat)
float64
-90.0 -70.0 -50.0 ... 70.0 90.0
array([-90., -70., -50., -30., -10., 10., 30., 50., 70., 90.]) lon
(lon)
float64
-180.0 -161.1 ... 161.1 180.0
array([-180. , -161.052632, -142.105263, -123.157895, -104.210526,\n",
+ " -85.263158, -66.315789, -47.368421, -28.421053, -9.473684,\n",
+ " 9.473684, 28.421053, 47.368421, 66.315789, 85.263158,\n",
+ " 104.210526, 123.157895, 142.105263, 161.052632, 180. ]) Data variables: (0)
Indexes: (4)
GeometryIndex (crs=None)
GeometryIndex(\n",
+ " [<POINT (171.612 -10.619)>\n",
+ " <POINT (-43.13 19.777)>\n",
+ " <POINT (152.369 65.452)>\n",
+ " <POINT (-85.791 65.476)>\n",
" ...\n",
- " <POINT (145.205 71.243)>\n",
- " <POINT (-0.4 -39.431)>\n",
- " <POINT (-6.234 89.254)>\n",
- " <POINT (59.03 -12.747)>],\n",
- " crs=None) CRSIndex (crs=EPSG:4326)
CRSIndex\n",
+ " <POINT (-92.964 42.436)>\n",
+ " <POINT (-65.328 -49.904)>\n",
+ " <POINT (167.069 -59.028)>\n",
+ " <POINT (-85.086 66.675)>],\n",
+ " crs=None) CRSIndex (crs=EPSG:4326)
CRSIndex\n",
"<Geographic 2D CRS: EPSG:4326>\n",
"Name: WGS 84\n",
"Axis Info [ellipsoidal]:\n",
@@ -2016,14 +1935,23 @@
"Datum: World Geodetic System 1984 ensemble\n",
"- Ellipsoid: WGS 84\n",
"- Prime Meridian: Greenwich\n",
- " Attributes: (0)
"
+ "PandasIndex
PandasIndex(Index([-90.0, -70.0, -50.0, -30.0, -10.0, 10.0, 30.0, 50.0, 70.0, 90.0], dtype='float64', name='lat')) PandasIndex
PandasIndex(Index([ -180.0, -161.05263157894737, -142.10526315789474,\n",
+ " -123.15789473684211, -104.21052631578948, -85.26315789473685,\n",
+ " -66.31578947368422, -47.36842105263159, -28.42105263157896,\n",
+ " -9.47368421052633, 9.4736842105263, 28.42105263157893,\n",
+ " 47.36842105263156, 66.31578947368419, 85.26315789473682,\n",
+ " 104.21052631578948, 123.15789473684208, 142.10526315789468,\n",
+ " 161.05263157894734, 180.0],\n",
+ " dtype='float64', name='lon')) Attributes: (0)
"
],
"text/plain": [
- " Size: 408B\n",
- "Dimensions: (space: 50)\n",
+ " Size: 328B\n",
+ "Dimensions: (space: 10, lat: 10, lon: 20)\n",
"Coordinates:\n",
- " * points (space) object 400B POINT (72.14265652432513 -68.90724731796...\n",
+ " * points (space) object 80B POINT (171.61191601133118 -10.61889803036...\n",
" * spatial_ref int64 8B 0\n",
+ " * lat (lat) float64 80B -90.0 -70.0 -50.0 -30.0 ... 50.0 70.0 90.0\n",
+ " * lon (lon) float64 160B -180.0 -161.1 -142.1 ... 142.1 161.1 180.0\n",
"Dimensions without coordinates: space\n",
"Data variables:\n",
" *empty*\n",
@@ -2038,16 +1966,898 @@
}
],
"source": [
- "ds_wgs84 + ds_no_crs"
+ "# lat-lon rectilinear grid\n",
+ "\n",
+ "ds_grid = xr.Dataset(coords={\"lat\": np.linspace(-90, 90, 10), \"lon\": np.linspace(-180, 180, 20)})\n",
+ "ds_grid_wgs84 = ds_grid.proj.assign_crs(spatial_ref=\"epsg:4326\")\n",
+ "\n",
+ "# merge the lat-lon grid with the vector data cube\n",
+ "xr.merge([ds_wgs84, ds_grid_wgs84])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "5f4fc990-ab39-443f-8f9b-a0b5130706c2",
+ "metadata": {},
+ "source": [
+ "## Unset the CRS\n",
+ "\n",
+ "Just drop the CRS index and/or coordinate"
]
},
{
"cell_type": "code",
- "execution_count": null,
- "id": "f51552bb-654d-4187-bf30-67c3fe96a118",
+ "execution_count": 10,
+ "id": "83f828d1-bdac-42d3-949f-95c21830a21a",
"metadata": {},
- "outputs": [],
- "source": []
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ "
<xarray.Dataset> Size: 80B\n",
+ "Dimensions: (space: 10)\n",
+ "Coordinates:\n",
+ " * points (space) object 80B POINT (171.61191601133118 -10.618898030367944...\n",
+ "Dimensions without coordinates: space\n",
+ "Data variables:\n",
+ " *empty*\n",
+ "Indexes:\n",
+ " points GeometryIndex (crs=None) Dimensions:
Coordinates: (1)
Data variables: (0)
Indexes: (1)
GeometryIndex (crs=None)
GeometryIndex(\n",
+ " [<POINT (171.612 -10.619)>\n",
+ " <POINT (-43.13 19.777)>\n",
+ " <POINT (152.369 65.452)>\n",
+ " <POINT (-85.791 65.476)>\n",
+ " ...\n",
+ " <POINT (-92.964 42.436)>\n",
+ " <POINT (-65.328 -49.904)>\n",
+ " <POINT (167.069 -59.028)>\n",
+ " <POINT (-85.086 66.675)>],\n",
+ " crs=None) Attributes: (0)
"
+ ],
+ "text/plain": [
+ " Size: 80B\n",
+ "Dimensions: (space: 10)\n",
+ "Coordinates:\n",
+ " * points (space) object 80B POINT (171.61191601133118 -10.618898030367944...\n",
+ "Dimensions without coordinates: space\n",
+ "Data variables:\n",
+ " *empty*\n",
+ "Indexes:\n",
+ " points GeometryIndex (crs=None)"
+ ]
+ },
+ "execution_count": 10,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "ds_no_crs = ds_wgs84.drop_vars(\"spatial_ref\")\n",
+ "\n",
+ "ds_no_crs"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "fff6acec-9d29-443e-a29c-802833b4388d",
+ "metadata": {},
+ "source": [
+ "It is possible to combine datasets with and without a defined CRS. The resulting dataset will have the common CRS found among all datasets. "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "id": "d142186b-9f5a-4809-b775-66e35e7af175",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ "
<xarray.Dataset> Size: 88B\n",
+ "Dimensions: (space: 10)\n",
+ "Coordinates:\n",
+ " * points (space) object 80B POINT (171.61191601133118 -10.61889803036...\n",
+ " * spatial_ref int64 8B 0\n",
+ "Dimensions without coordinates: space\n",
+ "Data variables:\n",
+ " *empty*\n",
+ "Indexes:\n",
+ " points GeometryIndex (crs=None)\n",
+ " spatial_ref CRSIndex (crs=EPSG:4326) Dimensions:
Coordinates: (2)
Data variables: (0)
Indexes: (2)
GeometryIndex (crs=None)
GeometryIndex(\n",
+ " [<POINT (171.612 -10.619)>\n",
+ " <POINT (-43.13 19.777)>\n",
+ " <POINT (152.369 65.452)>\n",
+ " <POINT (-85.791 65.476)>\n",
+ " ...\n",
+ " <POINT (-92.964 42.436)>\n",
+ " <POINT (-65.328 -49.904)>\n",
+ " <POINT (167.069 -59.028)>\n",
+ " <POINT (-85.086 66.675)>],\n",
+ " crs=None) CRSIndex (crs=EPSG:4326)
CRSIndex\n",
+ "<Geographic 2D CRS: EPSG:4326>\n",
+ "Name: WGS 84\n",
+ "Axis Info [ellipsoidal]:\n",
+ "- Lat[north]: Geodetic latitude (degree)\n",
+ "- Lon[east]: Geodetic longitude (degree)\n",
+ "Area of Use:\n",
+ "- name: World.\n",
+ "- bounds: (-180.0, -90.0, 180.0, 90.0)\n",
+ "Datum: World Geodetic System 1984 ensemble\n",
+ "- Ellipsoid: WGS 84\n",
+ "- Prime Meridian: Greenwich\n",
+ " Attributes: (0)
"
+ ],
+ "text/plain": [
+ " Size: 88B\n",
+ "Dimensions: (space: 10)\n",
+ "Coordinates:\n",
+ " * points (space) object 80B POINT (171.61191601133118 -10.61889803036...\n",
+ " * spatial_ref int64 8B 0\n",
+ "Dimensions without coordinates: space\n",
+ "Data variables:\n",
+ " *empty*\n",
+ "Indexes:\n",
+ " points GeometryIndex (crs=None)\n",
+ " spatial_ref CRSIndex (crs=EPSG:4326)"
+ ]
+ },
+ "execution_count": 11,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "ds_wgs84 + ds_no_crs"
+ ]
}
],
"metadata": {
diff --git a/xproj/accessor.py b/xproj/accessor.py
index eac36b9..9ba220d 100644
--- a/xproj/accessor.py
+++ b/xproj/accessor.py
@@ -234,7 +234,7 @@ def crs(self) -> pyproj.CRS | None:
else:
return crs_index.crs
- def set_crs(
+ def assign_crs(
self,
coord_name_crs: Mapping[Hashable, Any] | None = None,
allow_override: bool = False,
@@ -253,7 +253,7 @@ def set_crs(
:meth:`pyproj.CRS.from_user_input() ` such
as an authority string (e.g. ``"EPSG:4326"``), EPSG code (e.g. ``4326``) or
a WKT string.
- If the coordinate(s) doesn't exist they will be created.
+ If the coordinate(s) doesn't exist it will be created.
Only one item is currently allowed.
allow_override : bool, default False
Allow to replace the index if the coordinates already have an index.