Skip to content

Commit

Permalink
Add xarray_to_raster function (#2072)
Browse files Browse the repository at this point in the history
* Add xarray_to_raster function

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Use numpy 1.26.4 for testing

* Skip py 3.8 testing

* Remove datapane

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
giswqs and pre-commit-ci[bot] authored Jul 12, 2024
1 parent d58a242 commit afaf27c
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/workflows/docs-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
python -m pip install --upgrade pip
pip install --user --no-cache-dir Cython
pip install .[all]
pip install numpy==1.26.4
- name: LOAD EE CREDENTIALS
run: python ./.github/ee_token.py
env:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
python -m pip install --upgrade pip
pip install --user --no-cache-dir Cython
pip install .[all]
pip install numpy==1.26.4
- name: LOAD EE CREDENTIALS
run: python ./.github/ee_token.py
env:
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v4
- name: Set up Python
Expand All @@ -32,6 +32,7 @@ jobs:
- name: Install dependencies
run: |
pip install .[all]
pip install numpy==1.26.4
- name: PKG-TEST
run: |
python -m unittest discover tests/
Expand Down
39 changes: 30 additions & 9 deletions examples/notebooks/geemap_colab.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install geemap"
"%pip install -U geemap"
]
},
{
Expand All @@ -56,9 +56,9 @@
"id": "5",
"metadata": {},
"source": [
"### Create an interactive map\n",
"## Authenticate and initialize Earth Engine API\n",
"\n",
"Running the following cell will start the Earth Engine authentication. Follow the instructions [here](https://book.geemap.org/chapters/01_introduction.html#earth-engine-authentication) to authenticate Earth Engine."
"Replace `YOUR_PROJECT_ID` with your own project ID."
]
},
{
Expand All @@ -68,15 +68,18 @@
"metadata": {},
"outputs": [],
"source": [
"Map = geemap.Map()"
"ee.Authenticate()\n",
"ee.Initialize(project=\"YOUR_PROJECT_ID\")"
]
},
{
"cell_type": "markdown",
"id": "7",
"metadata": {},
"source": [
"### Add Earth Engine data"
"### Create an interactive map\n",
"\n",
"Running the following cell will start the Earth Engine authentication. Follow the instructions [here](https://book.geemap.org/chapters/01_introduction.html#earth-engine-authentication) to authenticate Earth Engine."
]
},
{
Expand All @@ -85,6 +88,24 @@
"id": "8",
"metadata": {},
"outputs": [],
"source": [
"m = geemap.Map()"
]
},
{
"cell_type": "markdown",
"id": "9",
"metadata": {},
"source": [
"### Add Earth Engine data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "10",
"metadata": {},
"outputs": [],
"source": [
"# Add Earth Engine dataset\n",
"image = ee.Image(\"USGS/SRTMGL1_003\")\n",
Expand All @@ -97,16 +118,16 @@
"}\n",
"\n",
"# Add Earth Engine layers to Map\n",
"Map.addLayer(image, vis_params, \"DEM\")\n",
"m.add_layer(image, vis_params, \"DEM\")\n",
"\n",
"# Center the map based on an Earth Engine object or coordinates (longitude, latitude)\n",
"Map.setCenter(86.9250, 27.9881, 4)\n",
"Map"
"m.set_center(86.9250, 27.9881, 4)\n",
"m"
]
},
{
"cell_type": "markdown",
"id": "9",
"id": "11",
"metadata": {},
"source": [
"If you can see the map above, you have successfully authenticated the Earth Engine Python API. Otherwise, you might need to follow the instructions [here](https://docs.google.com/document/d/1ZGSmrNm6_baqd8CHt33kIBWOlvkh-HLr46bODgJN1h0/edit?usp=sharing) to create a Cloud Project, which is required to use the Earth Engine Python API."
Expand Down
38 changes: 38 additions & 0 deletions geemap/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16246,3 +16246,41 @@ def get_google_maps_api_key(key: str = "GOOGLE_MAPS_API_KEY") -> Optional[str]:
if api_key := _get_colab_secret(key):
return api_key
return os.environ.get(key, None)


def xarray_to_raster(dataset, filename: str, **kwargs: Dict[str, Any]):
"""Convert an xarray Dataset to a raster file.

Args:
dataset (xr.Dataset): The input xarray Dataset to be converted.
filename (str): The output filename for the raster file.
**kwargs (Dict[str, Any]): Additional keyword arguments passed to the `rio.to_raster()` method.
See https://corteva.github.io/rioxarray/stable/examples/convert_to_raster.html for more info.

Returns:
None
"""
import rioxarray

dims = list(dataset.dims)

new_names = {}

if "lat" in dims:
new_names["lat"] = "y"
dims.remove("lat")
if "lon" in dims:
new_names["lon"] = "x"
dims.remove("lon")
if "lng" in dims:
new_names["lng"] = "x"
dims.remove("lng")
if "latitude" in dims:
new_names["latitude"] = "y"
dims.remove("latitude")
if "longitude" in dims:
new_names["longitude"] = "x"
dims.remove("longitude")

dataset = dataset.rename(new_names)
dataset.transpose(..., "y", "x").rio.to_raster(filename, **kwargs)
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ backends = [
"plotly",
]
extra = [
"datapane",
"ee_extra>=0.0.15",
"ffmpeg-python",
"gdown",
Expand Down

0 comments on commit afaf27c

Please sign in to comment.