Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
spatialthoughts committed Jan 24, 2024
1 parent 7dca1f1 commit fd51105
Show file tree
Hide file tree
Showing 18 changed files with 2,446 additions and 2,009 deletions.
766 changes: 383 additions & 383 deletions code/end_to_end_gee/01_python_api_syntax.ipynb

Large diffs are not rendered by default.

750 changes: 375 additions & 375 deletions code/end_to_end_gee/02_automatic_conversion_of_scripts.ipynb

Large diffs are not rendered by default.

602 changes: 301 additions & 301 deletions code/end_to_end_gee/03_export_a_collection.ipynb

Large diffs are not rendered by default.

716 changes: 358 additions & 358 deletions code/end_to_end_gee/04_creating_charts.ipynb

Large diffs are not rendered by default.

23 changes: 10 additions & 13 deletions docs/code/end_to_end_gee/01_python_api_syntax.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
"source": [
"#### Initialization\n",
"\n",
"First of all, you need to run the following cells to initialize the API and authorize your account. You will be prompted to sign-in to the account and allow access to *View and Manage your Earth Engine data*. Once you approve, you will get an a verification code that needs to be entered at the prompt. This step needs to be done just once per session."
"First of all, you need to run the following cells to initialize the API and authorize your account. You must have a Google Cloud Project associated with your GEE account. Replace the `cloud_project` with your own project from [Google Cloud Console](https://console.cloud.google.com/).\n",
"\n",
"You will be prompted to allow the notebook to access your Google credentials to sign-in to the account and allow access to *Google Drive and Google Cloud data*. Once you approve, it will proceed to initialize the Earth Engine API. This step needs to be done just once per session."
]
},
{
Expand All @@ -41,18 +43,13 @@
},
"outputs": [],
"source": [
"ee.Authenticate()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "KwN_Ti_YIAJS"
},
"outputs": [],
"source": [
"ee.Initialize()"
"cloud_project = 'spatialthoughts'\n",
"\n",
"try:\n",
" ee.Initialize(project=cloud_project)\n",
"except:\n",
" ee.Authenticate()\n",
" ee.Initialize(project=cloud_project)\n"
]
},
{
Expand Down
184 changes: 166 additions & 18 deletions docs/code/end_to_end_gee/02_automatic_conversion_of_scripts.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,178 @@
"id": "WD-QeVYP2iMT"
},
"source": [
"[geemap](https://github.com/giswqs/geemap) is an open-source Python package that comes with many helpful features that help you use Earth Engine effectively in Python. \n",
"[geemap](https://github.com/giswqs/geemap) is an open-source Python package that comes with many helpful features that help you use Earth Engine effectively in Python.\n",
"\n",
"It comes with a function that can help you translate your javascript earth engine code to Python automatically.\n",
"\n",
"Google Colab doesn't come pre-installed with the package, so we install it via pip."
"The `geemap` package is pre-installed in Colab."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "nlQ8X57t2iMW"
"id": "apXfk7X6Uyz2"
},
"outputs": [],
"source": [
"import geemap\n",
"import ee"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "CpVwpjkjIAJQ"
},
"source": [
"#### Initialization\n",
"\n",
"First of all, you need to run the following cells to initialize the API and authorize your account. You must have a Google Cloud Project associated with your GEE account. Replace the `cloud_project` with your own project from [Google Cloud Console](https://console.cloud.google.com/)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "gjRJhBKF2iLf"
},
"outputs": [],
"source": [
"cloud_project = 'spatialthoughts'\n",
"\n",
"try:\n",
" import geemap\n",
"except ModuleNotFoundError:\n",
" if 'google.colab' in str(get_ipython()):\n",
" print('geemap not found, installing via pip in Google Colab...')\n",
" !pip install geemap --quiet\n",
" import geemap\n",
" else:\n",
" print('geemap not found, please install via conda in your environment')"
" ee.Initialize(project=cloud_project)\n",
"except:\n",
" ee.Authenticate()\n",
" ee.Initialize(project=cloud_project)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "37jsLQJiZQ3A"
},
"source": [
"#### Automatic Conversion using GUI"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "AEr4OXZuVOp0"
},
"source": [
"`geemap` comes with a user interface that can be used to interactively do code conversion. Let's try to convert the following Javascript code to Python.\n",
"\n",
"```\n",
"var geometry = ee.Geometry.Point([77.60412933051538, 12.952912912328241]);\n",
"var s2 = ee.ImageCollection('COPERNICUS/S2_HARMONIZED');\n",
"\n",
"var rgbVis = {min: 0.0, max: 3000, bands: ['B4', 'B3', 'B2']};\n",
"\n",
"var filtered = s2.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 30))\n",
" .filter(ee.Filter.date('2019-01-01', '2020-01-01'))\n",
" .filter(ee.Filter.bounds(geometry));\n",
" \n",
"var medianComposite = filtered.median();\n",
"\n",
"Map.centerObject(geometry, 10);\n",
"Map.addLayer(medianComposite, rgbVis, 'Median Composite');\n",
"```\n",
"\n",
"Run the cell below to load the map widget. Once the map widget loads, click the *Toolbar* icon in the top-right corner and select the *Convert Earth Engine Javascript to Python* tool. Paste your Javascript code and click *Convert*."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "tmTgTEA4VaY1"
},
"outputs": [],
"source": [
"m = geemap.Map(width=800)\n",
"m"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "kGob1XI3aXve"
},
"outputs": [],
"source": [
"geometry = ee.Geometry.Point([77.60412933051538, 12.952912912328241])\n",
"s2 = ee.ImageCollection('COPERNICUS/S2_HARMONIZED')\n",
"\n",
"rgbVis = {'min': 0.0, 'max': 3000, 'bands': ['B4', 'B3', 'B2']}\n",
"\n",
"filtered = s2.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 30)) \\\n",
" .filter(ee.Filter.date('2019-01-01', '2020-01-01')) \\\n",
" .filter(ee.Filter.bounds(geometry))\n",
"\n",
"medianComposite = filtered.median()\n",
"\n",
"m.centerObject(geometry, 10)\n",
"m.addLayer(medianComposite, rgbVis, 'Median Composite')\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "AMnk0ZXUXNNL"
},
"source": [
"You will see the auto-converted code displayed. Copy and paste it into a new cell and run it. Your code will be run using the GEE Python API. If your code loads any layers, they will be loadded on the map widget. To display it, open a new code cell and just type `m` to display the widget.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "OeHqXTRRYuui"
},
"outputs": [],
"source": [
"m"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Yd1eFlklYvr-"
},
"source": [
"> Note The auto-conversion is almost perfect and works flawlessly on most GEE code. One place it misses is during the conversion of function arguments specified as a dicitonary. You will need to prefix the resulting code with `**` to specify them as `**kwargs`. For example, the `geemap` converter produces code such as below.\n",
" ```\n",
" stats = image.reduceRegion({\n",
" 'reducer': ee.Reducer.mean(),\n",
" 'geometry': geometry,\n",
" 'scale': 10,\n",
" 'maxPixels': 1e10\n",
" })\n",
" ```\n",
"To make this valid GEE Python API code - prefix the argument dictionary with `**`.\n",
" ```\n",
" stats = image.reduceRegion(**{\n",
" 'reducer': ee.Reducer.mean(),\n",
" 'geometry': geometry,\n",
" 'scale': 10,\n",
" 'maxPixels': 1e10\n",
" })\n",
" ```"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qfQyzIgpZN1v"
},
"source": [
"#### Automatic Conversion using Code"
]
},
{
Expand All @@ -38,7 +186,7 @@
"id": "vDE9fRr3KqIK"
},
"source": [
"The automatic conversion of code is done by calling the `geemap.js_snippet_to_py()` function. We first create a string with the javascript code."
"`geemap` offers a function `js_snippet_to_py()` that can be used to perform the conversion using code. This is useful for batch conversions. To use this, we first create a string with the javascript code."
]
},
{
Expand Down Expand Up @@ -70,12 +218,12 @@
" .select(\"B.*\")\n",
" .copyProperties(image, [\"system:time_start\"])\n",
"}\n",
" \n",
"\n",
"var filtered = s2\n",
" .filter(ee.Filter.date('2019-01-01', '2019-12-31'))\n",
" .filter(ee.Filter.bounds(geometry))\n",
" .map(maskS2clouds)\n",
" \n",
"\n",
"\n",
"// Write a function that computes NDVI for an image and adds it as a band\n",
"function addNDVI(image) {\n",
Expand Down Expand Up @@ -131,10 +279,10 @@
"source": [
"import ee\n",
"import geemap\n",
"Map = geemap.Map()\n",
"m = geemap.Map()\n",
"\n",
"geometry = ee.Geometry.Point([107.61303468448624, 12.130969369851766])\n",
"Map.centerObject(geometry, 12)\n",
"m.centerObject(geometry, 12)\n",
"s2 = ee.ImageCollection('COPERNICUS/S2_HARMONIZED')\n",
"rgbVis = {\n",
" 'min': 0.0,\n",
Expand Down Expand Up @@ -172,8 +320,8 @@
" '004C00', '023B01', '012E01', '011D01', '011301']\n",
"\n",
"ndviVis = {'min':0, 'max':0.5, 'palette': palette }\n",
"Map.addLayer(withNdvi.select('ndvi'), ndviVis, 'NDVI Composite')\n",
"Map"
"m.addLayer(withNdvi.select('ndvi'), ndviVis, 'NDVI Composite')\n",
"m"
]
},
{
Expand Down
24 changes: 15 additions & 9 deletions docs/code/end_to_end_gee/03_export_a_collection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,42 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {
"id": "JuN5rEoyjmjK"
"id": "CpVwpjkjIAJQ"
},
"outputs": [],
"source": [
"import ee"
"#### Initialization\n",
"\n",
"First of all, you need to run the following cells to initialize the API and authorize your account. You must have a Google Cloud Project associated with your GEE account. Replace the `cloud_project` with your own project from [Google Cloud Console](https://console.cloud.google.com/)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Sz-01eMjg72f"
"id": "JuN5rEoyjmjK"
},
"outputs": [],
"source": [
"ee.Authenticate()"
"import ee"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "UPsm1_Qxg72j"
"id": "gjRJhBKF2iLf"
},
"outputs": [],
"source": [
"ee.Initialize()"
"cloud_project = 'spatialthoughts'\n",
"\n",
"try:\n",
" ee.Initialize(project=cloud_project)\n",
"except:\n",
" ee.Authenticate()\n",
" ee.Initialize(project=cloud_project)"
]
},
{
Expand Down
Loading

0 comments on commit fd51105

Please sign in to comment.