Skip to content

Commit

Permalink
Docs refactor work: Add Earth Engine Python quickstart
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 665453734
  • Loading branch information
jdbcode authored and copybara-github committed Aug 20, 2024
1 parent 6e25e22 commit 1ef2152
Show file tree
Hide file tree
Showing 2 changed files with 344 additions and 0 deletions.
258 changes: 258 additions & 0 deletions guides/linked/generated/quickstart_python.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
86 changes: 86 additions & 0 deletions samples/python/guides/quickstart.py
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 1ef2152

Please sign in to comment.