From 1ef215294045de7627ac7832acd74b8f9cd81e29 Mon Sep 17 00:00:00 2001 From: Justin Braaten Date: Tue, 20 Aug 2024 11:33:01 -0700 Subject: [PATCH] Docs refactor work: Add Earth Engine Python quickstart PiperOrigin-RevId: 665453734 --- .../linked/generated/quickstart_python.ipynb | 258 ++++++++++++++++++ samples/python/guides/quickstart.py | 86 ++++++ 2 files changed, 344 insertions(+) create mode 100644 guides/linked/generated/quickstart_python.ipynb create mode 100644 samples/python/guides/quickstart.py diff --git a/guides/linked/generated/quickstart_python.ipynb b/guides/linked/generated/quickstart_python.ipynb new file mode 100644 index 000000000..65374cb67 --- /dev/null +++ b/guides/linked/generated/quickstart_python.ipynb @@ -0,0 +1,258 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Get started with Earth Engine for Python\n", + "\n", + "Project: /earth-engine/_project.yaml Book: /earth-engine/_book.yaml" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#@title Copyright 2024 The Earth Engine Community Authors { display-mode: \"form\" }\n", + "#\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." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This quickstart will give you an interactive introduction to visualizing and\n", + "analyzing geospatial data with the Earth Engine Python interface.\n", + "\n", + "## Before you begin\n", + "\n", + "[Register or create](https://code.earthengine.google.com/register) a Google Cloud Project. You'll be prompted to complete the following steps: \n", + "\n", + " * Select the project's purpose: commercial or noncommercial.\n", + " * If the purpose is noncommercial, select a project type.\n", + " * Create a new Google Cloud project or select an existing project.\n", + " * If the purpose is commercial, verify or set up billing for your project.\n", + " * Confirm your project information. \n", + "\n", + "**Note:** If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can [delete the project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects), removing all resources owned by the project. \n", + "\n", + "## Notebook setup\n", + "\n", + "**1.** Import the Earth Engine and geemap libraries." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import ee\n", + "import geemap.core as geemap" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**2.** Authenticate and initialize the Earth Engine service. Follow the\n", + "resulting prompts to complete authentication. Be sure to replace PROJECT_ID\n", + "with the name of the project you set up for this quickstart." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ee.Authenticate()\n", + "ee.Initialize(project='PROJECT_ID')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Add raster data to a map\n", + "\n", + "**1.** Load climate data for a given period and display its metadata." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "jan_2023_climate = (\n", + " ee.ImageCollection('ECMWF/ERA5_LAND/MONTHLY_AGGR')\n", + " .filterDate('2023-01', '2023-02')\n", + " .first()\n", + ")\n", + "jan_2023_climate" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**2.** Instantiate a map object and add the temperature band as a layer with\n", + "specific visualization properties. Display the map." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[30, 0], zoom=2)\n", + "\n", + "vis_params = {\n", + " 'bands': ['temperature_2m'],\n", + " 'min': 229,\n", + " 'max': 304,\n", + " 'palette': 'inferno',\n", + "}\n", + "m.add_layer(jan_2023_climate, vis_params, 'Temperature (K)')\n", + "m" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Add vector data to a map\n", + "\n", + "**1.** Create a vector data object with points for three cities." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "cities = ee.FeatureCollection([\n", + " ee.Feature(ee.Geometry.Point(10.75, 59.91), {'city': 'Oslo'}),\n", + " ee.Feature(ee.Geometry.Point(-118.24, 34.05), {'city': 'Los Angeles'}),\n", + " ee.Feature(ee.Geometry.Point(103.83, 1.33), {'city': 'Singapore'}),\n", + "])\n", + "cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**2.** Add the city locations to the map and redisplay it." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "m.add_layer(cities, name='Cities')\n", + "m" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Extract and chart data\n", + "\n", + "**1.** Import the Altair charting library." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%pip install -q --upgrade altair\n", + "import altair as alt" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**2.** Extract the climate data for the three cities as a pandas DataFrame." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "city_climates = jan_2023_climate.reduceRegions(cities, ee.Reducer.first())\n", + "\n", + "city_climates_dataframe = ee.data.computeFeatures(\n", + " {'expression': city_climates, 'fileFormat': 'PANDAS_DATAFRAME'}\n", + ")\n", + "city_climates_dataframe" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**3.** Plot the temperature for the cities as a bar chart." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "alt.Chart(city_climates_dataframe).mark_bar(size=100).encode(\n", + " alt.X('city:N', sort='y', axis=alt.Axis(labelAngle=0), title='City'),\n", + " alt.Y('temperature_2m:Q', title='Temperature (K)'),\n", + " tooltip=[\n", + " alt.Tooltip('city:N', title='City'),\n", + " alt.Tooltip('temperature_2m:Q', title='Temperature (K)'),\n", + " ],\n", + ").properties(title='January 2023 temperature for selected cities', width=500)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## What's next\n", + "\n", + " * Learn about analyzing data with Earth Engine's [objects and methods](https://developers.google.com/earth-engine/guides/objects_methods_overview).\n", + " * Learn about Earth Engine's [processing environments](https://developers.google.com/earth-engine/guides/processing_environments).\n", + " * Learn about Earth Engine's [machine learning capabilities](https://developers.google.com/earth-engine/guides/machine-learning).\n", + " * Learn how to [export your computation results to BigQuery](https://developers.google.com/earth-engine/guides/exporting_to_bigquery)." + ] + } + ], + "metadata": { + "colab": { + "name": "Get started with Earth Engine for Python" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/samples/python/guides/quickstart.py b/samples/python/guides/quickstart.py new file mode 100644 index 000000000..666a28dba --- /dev/null +++ b/samples/python/guides/quickstart.py @@ -0,0 +1,86 @@ +# Copyright 2024 The Google Earth Engine Community Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Earth Engine Developer's Guide examples for 'Python quickstart'.""" + +# [START earthengine__quickstart__imports] +import ee +import geemap.core as geemap +# [END earthengine__quickstart__imports] + +# [START earthengine__quickstart__auth] +ee.Authenticate() +ee.Initialize(project='PROJECT_ID') +# [END earthengine__quickstart__auth] + +# [START earthengine__quickstart__image_load] +jan_2023_climate = ( + ee.ImageCollection('ECMWF/ERA5_LAND/MONTHLY_AGGR') + .filterDate('2023-01', '2023-02') + .first() +) +jan_2023_climate +# [END earthengine__quickstart__image_load] + +# [START earthengine__quickstart__image_display] +m = geemap.Map(center=[30, 0], zoom=2) + +vis_params = { + 'bands': ['temperature_2m'], + 'min': 229, + 'max': 304, + 'palette': 'inferno', +} +m.add_layer(jan_2023_climate, vis_params, 'Temperature (K)') +m +# [END earthengine__quickstart__image_display] + +# [START earthengine__quickstart__fc_create] +cities = ee.FeatureCollection([ + ee.Feature(ee.Geometry.Point(10.75, 59.91), {'city': 'Oslo'}), + ee.Feature(ee.Geometry.Point(-118.24, 34.05), {'city': 'Los Angeles'}), + ee.Feature(ee.Geometry.Point(103.83, 1.33), {'city': 'Singapore'}), +]) +cities +# [END earthengine__quickstart__fc_create] + +# [START earthengine__quickstart__fc_display] +m.add_layer(cities, name='Cities') +m +# [END earthengine__quickstart__fc_display] + +# [START earthengine__quickstart__import_altair] +%pip install -q --upgrade altair +import altair as alt +# [END earthengine__quickstart__import_altair] + +# [START earthengine__quickstart__extract_data] +city_climates = jan_2023_climate.reduceRegions(cities, ee.Reducer.first()) + +city_climates_dataframe = ee.data.computeFeatures( + {'expression': city_climates, 'fileFormat': 'PANDAS_DATAFRAME'} +) +city_climates_dataframe +# [END earthengine__quickstart__extract_data] + +# [START earthengine__quickstart__plot_data] +alt.Chart(city_climates_dataframe).mark_bar(size=100).encode( + alt.X('city:N', sort='y', axis=alt.Axis(labelAngle=0), title='City'), + alt.Y('temperature_2m:Q', title='Temperature (K)'), + tooltip=[ + alt.Tooltip('city:N', title='City'), + alt.Tooltip('temperature_2m:Q', title='Temperature (K)'), + ], +).properties(title='January 2023 temperature for selected cities', width=500) +# [END earthengine__quickstart__plot_data]