mirror of
https://github.com/google/earthengine-api.git
synced 2026-02-01 16:00:51 +00:00
1 line
8.3 KiB
Plaintext
1 line
8.3 KiB
Plaintext
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Earth_Engine_asset_from_cloud_geotiff.ipynb","provenance":[{"file_id":"https://github.com/google/earthengine-api/blob/master/python/examples/ipynb/Earth_Engine_asset_from_cloud_geotiff.ipynb","timestamp":1655816119626},{"file_id":"1f_rRBTQVKbPVhaoRRsSWUlBtBgnZkiTz","timestamp":1590793341638}],"private_outputs":true,"collapsed_sections":[],"toc_visible":true},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"code","metadata":{"id":"fSIfBsgi8dNK"},"source":["#@title Copyright 2022 Google LLC. { display-mode: \"form\" }\n","# Licensed under the Apache License, Version 2.0 (the \"License\");\n","# you may not use this file except in compliance with the License.\n","# You may obtain a copy of the License at\n","#\n","# https://www.apache.org/licenses/LICENSE-2.0\n","#\n","# Unless required by applicable law or agreed to in writing, software\n","# distributed under the License is distributed on an \"AS IS\" BASIS,\n","# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n","# See the License for the specific language governing permissions and\n","# limitations under the License."],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"aV1xZ1CPi3Nw"},"source":["<table class=\"ee-notebook-buttons\" align=\"left\"><td>\n","<a target=\"_blank\" href=\"http://colab.research.google.com/github/google/earthengine-api/blob/master/python/examples/ipynb/Earth_Engine_asset_from_cloud_geotiff.ipynb\">\n"," <img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /> Run in Google Colab</a>\n","</td><td>\n","<a target=\"_blank\" href=\"https://github.com/google/earthengine-api/blob/master/python/examples/ipynb/Earth_Engine_asset_from_cloud_geotiff.ipynb\"><img width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /> View source on GitHub</a></td></table>"]},{"cell_type":"markdown","metadata":{"id":"CrEM35gqHouU"},"source":["# Cloud GeoTiff Backed Earth Engine Assets\n","\n","***Note:*** *The REST API contains new and advanced features that may not be suitable for all users. If you are new to Earth Engine, please get started with the [JavaScript guide](https://developers.google.com/earth-engine/guides/getstarted).*\n","\n","Earth Engine can load images from Cloud Optimized GeoTiffs (COGs) in Google Cloud Storage ([learn more](https://developers.google.com/earth-engine/guides/image_overview#images-from-cloud-geotiffs)). This notebook demonstrates how to create Earth Engine assets backed by COGs. An advantage of COG-backed assets is that the spatial and metadata fields of the image will be indexed at asset creation time, making the image more performant in collections. (In contrast, an image created through `ee.Image.loadGeoTIFF` and put into a collection will require a read of the GeoTiff for filtering operations on the collection.) A disadvantage of COG-backed assets is that they may be several times slower than standard assets when used in computations.\n","\n","To create a COG-backed asset, make a `POST` request to the Earth Engine [`CreateAsset` endpoint](https://developers.google.com/earth-engine/reference/rest/v1alpha/projects.assets/create). As shown in the following, this request must be authorized to create an asset in your user folder."]},{"cell_type":"markdown","metadata":{"id":"fmxat3ujhwGx"},"source":["## Start an authorized session\n","\n","To be able to make an Earth Engine asset in your user folder, you need to be able to authenticate as yourself when you make the request. You can use credentials from the Earth Engine authenticator to start an [`AuthorizedSession`](https://google-auth.readthedocs.io/en/master/reference/google.auth.transport.requests.html#google.auth.transport.requests.AuthorizedSession). You can then use the `AuthorizedSession` to send requests to Earth Engine."]},{"cell_type":"code","metadata":{"id":"qVu8GhINwYfO"},"source":["import ee\n","from google.auth.transport.requests import AuthorizedSession\n","\n","ee.Authenticate() # or !earthengine authenticate --auth_mode=gcloud\n","session = AuthorizedSession(ee.data.get_persistent_credentials())"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"jz8e263wvbTN"},"source":["## Request body\n","\n","The request body is an instance of an [EarthEngineAsset](https://developers.google.com/earth-engine/reference/rest/v1alpha/projects.assets#EarthEngineAsset). This is where the path to the COG is specified, along with other useful properties. Note that the image is a small area exported from the composite made in [this example script](https://code.earthengine.google.com/?scriptPath=Examples%3ACloud%20Masking%2FSentinel2). See [this doc](https://developers.google.com/earth-engine/exporting#configuration-parameters) for details on exporting a COG.\n","\n","Earth Engine will determine the bands, geometry, and other relevant information from the metadata of the TIFF. The only other fields that are accepted when creating a COG-backed asset are `properties`, `start_time`, and `end_time`."]},{"cell_type":"code","metadata":{"id":"OGESPnfEvqVq"},"source":["import json\n","from pprint import pprint\n","\n","# Request body as a dictionary.\n","request = {\n"," 'type': 'IMAGE',\n"," 'gcs_location': {\n"," 'uris': ['gs://ee-docs-demos/COG_demo.tif']\n"," },\n"," 'properties': {\n"," 'source': 'https://code.earthengine.google.com/d541cf8b268b2f9d8f834c255698201d'\n"," },\n"," 'startTime': '2016-01-01T00:00:00.000000000Z',\n"," 'endTime': '2016-12-31T15:01:23.000000000Z',\n","}\n","\n","pprint(json.dumps(request))"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"9_MfryWIpyhS"},"source":["## Send the request\n","\n","Make the POST request to the Earth Engine [`projects.assets.create`](https://developers.google.com/earth-engine/reference/rest/v1alpha/projects.assets/create) endpoint."]},{"cell_type":"code","metadata":{"id":"NhmNrvS2p4qQ"},"source":["# Earth Engine enabled Cloud Project.\n","project_folder = 'your-project'\n","# A folder (or ImageCollection) name and the new asset name.\n","asset_id = 'cog-collection/your-cog-asset'\n","\n","url = 'https://earthengine.googleapis.com/v1alpha/projects/{}/assets?assetId={}'\n","\n","response = session.post(\n"," url = url.format(project_folder, asset_id),\n"," data = json.dumps(request)\n",")\n","\n","pprint(json.loads(response.content))"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"mK5lCJY0CDfK"},"source":["## Details on COG-backed assets\n","\n","### Permissions\n","The ACLs of COG-backed Earth Engine assets and the underlying data are managed separately. If a COG-backed asset is shared in Earth Engine, it is the owner's responsibility to ensure that the data in GCS is shared with the same parties. If the data is not visible, Earth Engine will return an error of the form \"Failed to load the GeoTIFF at `gs://my-bucket/my-object#123456`\" (123456 is the generation of the object).\n","\n","### Generations\n","When a COG-backed asset is created, Earth Engine reads the metadata of the TIFF in Cloud Storage and creates asset store entry. The URI associated with that entry must have a generation. See the [object versioning docs](https://cloud.google.com/storage/docs/object-versioning) for details on generations. If a generation is specified (e.g., `gs://foo/bar#123`), Earth Engine will use it. If a generation is not specified, Earth Engine will use the latest generation of the object. \n","\n","That means that if the object in GCS is updated, Earth Engine will return a \"Failed to load the GeoTIFF at `gs://my-bucket/my-object#123456`\" error because the expected object no longer exists (unless the bucket enables multiple object versions). This policy is designed to keep metadata of the asset in sync with the metadata of the object. \n","\n","### Configuration\n","In terms of how a COG should be configured, the TIFF MUST be:\n","\n","- Tiled, where the tile dimensions are either:\n"," - 16x16\n"," - 32x32\n"," - 64x64\n"," - 128x128\n"," - 256x256\n"," - 512x512\n"," - 1024x1024\n","\n","- Arranged so that all IFDs are at the beginning.\n","\n","For best performance:\n","\n","- Use tile dimensions of 128x128 or 256x256.\n","- Include power of 2 overviews.\n","\n","See [this page](https://cogeotiff.github.io/rio-cogeo/Advanced/#web-optimized-cog) for more details on an optimized configuration."]}]} |