{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using Earth Engine Data\n", "\n", "```{contents}\n", ":local:\n", ":depth: 3\n", "```\n", "\n", "## Introduction\n", "\n", "## Technical requirements\n", "\n", "```bash\n", "conda create -n gee python\n", "conda activate gee\n", "conda install -c conda-forge mamba\n", "mamba install -c conda-forge pygis\n", "```\n", "\n", "```bash\n", "jupyter lab\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# %pip install pygis" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ee\n", "import geemap" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_initialize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Earth Engine data types\n", "\n", "### Image\n", "\n", "#### Loading Earth Engine images" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = ee.Image('USGS/SRTMGL1_003')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.getInfo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Visualizing Earth Engine images" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[21.79, 70.87], zoom=3)\n", "image = ee.Image('USGS/SRTMGL1_003')\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 6000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "Map.addLayer(image, vis_params, 'SRTM')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Loading Cloud GeoTIFFs" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "URL = 'https://bit.ly/3aSZ0fH'\n", "image = geemap.load_GeoTIFF(URL)\n", "vis = {\n", " \"min\": 3000,\n", " \"max\": 13500,\n", " \"bands\": [\"B3\", \"B2\", \"B1\"],\n", "}\n", "Map.addLayer(image, vis, 'Cloud GeoTIFF')\n", "Map.centerObject(image)\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "B3 = 'gs://gcp-public-data-landsat/LC08/01/044/034/LC08_L1TP_044034_20131228_20170307_01_T1/LC08_L1TP_044034_20131228_20170307_01_T1_B3.TIF'\n", "B4 = 'gs://gcp-public-data-landsat/LC08/01/044/034/LC08_L1TP_044034_20131228_20170307_01_T1/LC08_L1TP_044034_20131228_20170307_01_T1_B4.TIF'\n", "B5 = 'gs://gcp-public-data-landsat/LC08/01/044/034/LC08_L1TP_044034_20131228_20170307_01_T1/LC08_L1TP_044034_20131228_20170307_01_T1_B5.TIF'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "URLs = [B3, B4, B5]\n", "collection = geemap.load_GeoTIFFs(URLs)\n", "image = collection.toBands().rename(['Green', 'Red', 'NIR']).selfMask()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "vis = {'bands': ['NIR', 'Red', 'Green'], 'min': 100, 'max': 12000, 'gamma': 0.8}\n", "Map.addLayer(image, vis, 'Image')\n", "Map.centerObject(image, 8)\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ImageCollection\n", "\n", "#### Loading image collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "collection = ee.ImageCollection('COPERNICUS/S2_SR')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "collection.limit(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Visualizing image collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "collection = ee.ImageCollection('COPERNICUS/S2_SR')\n", "image = collection.median()\n", "\n", "vis = {\n", " 'min': 0.0,\n", " 'max': 3000,\n", " 'bands': ['B4', 'B3', 'B2'],\n", "}\n", "\n", "Map.setCenter(83.277, 17.7009, 12)\n", "Map.addLayer(image, vis, 'Sentinel-2')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Filtering image collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "collection = (\n", " ee.ImageCollection('COPERNICUS/S2_SR')\n", " .filterDate('2021-01-01', '2022-01-01')\n", " .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 5))\n", ")\n", "image = collection.median()\n", "\n", "vis = {\n", " 'min': 0.0,\n", " 'max': 3000,\n", " 'bands': ['B4', 'B3', 'B2'],\n", "}\n", "\n", "Map.setCenter(83.277, 17.7009, 12)\n", "Map.addLayer(image, vis, 'Sentinel-2')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Geometry\n", "\n", "#### Geometry types\n", "\n", "#### Creating Geometry objects" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "point = ee.Geometry.Point([1.5, 1.5])\n", "\n", "lineString = ee.Geometry.LineString([[-35, -10], [35, -10], [35, 10], [-35, 10]])\n", "\n", "linearRing = ee.Geometry.LinearRing(\n", " [[-35, -10], [35, -10], [35, 10], [-35, 10], [-35, -10]]\n", ")\n", "\n", "rectangle = ee.Geometry.Rectangle([-40, -20, 40, 20])\n", "\n", "polygon = ee.Geometry.Polygon([[[-5, 40], [65, 40], [65, 60], [-5, 60], [-5, 60]]])\n", "\n", "Map.addLayer(point, {}, 'Point')\n", "Map.addLayer(lineString, {}, 'LineString')\n", "Map.addLayer(linearRing, {}, 'LinearRing')\n", "Map.addLayer(rectangle, {}, 'Rectangle')\n", "Map.addLayer(polygon, {}, 'Polygon')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "point = ee.Geometry.Point([1.5, 1.5])\n", "\n", "lineString = ee.Geometry.LineString(\n", " [[-35, -10], [35, -10], [35, 10], [-35, 10]], None, False\n", ")\n", "\n", "linearRing = ee.Geometry.LinearRing(\n", " [[-35, -10], [35, -10], [35, 10], [-35, 10], [-35, -10]], None, False\n", ")\n", "\n", "rectangle = ee.Geometry.Rectangle([-40, -20, 40, 20], None, False)\n", "\n", "polygon = ee.Geometry.Polygon(\n", " [[[-5, 40], [65, 40], [65, 60], [-5, 60], [-5, 60]]], None, False\n", ")\n", "\n", "Map.addLayer(point, {}, 'Point')\n", "Map.addLayer(lineString, {}, 'LineString')\n", "Map.addLayer(linearRing, {}, 'LinearRing')\n", "Map.addLayer(rectangle, {}, 'Rectangle')\n", "Map.addLayer(polygon, {}, 'Polygon')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Using drawing tools" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if Map.user_roi is not None:\n", " print(Map.user_roi.getInfo())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if Map.user_roi is not None:\n", " print(Map.user_roi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Feature\n", "\n", "#### Creating Feature objects" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "polygon = ee.Geometry.Polygon(\n", " [[[-35, -10], [35, -10], [35, 10], [-35, 10], [-35, -10]]], None, False\n", ")\n", "polyFeature = ee.Feature(polygon, {'foo': 42, 'bar': 'tart'})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "polyFeature" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map.addLayer(polyFeature, {}, 'feature')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "props = {'foo': ee.Number(8).add(88), 'bar': 'hello'}\n", "nowhereFeature = ee.Feature(None, props)\n", "nowhereFeature" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Setting Feature properties" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "feature = (\n", " ee.Feature(ee.Geometry.Point([-122.22599, 37.17605]))\n", " .set('genus', 'Sequoia')\n", " .set('species', 'sempervirens')\n", ")\n", "newDict = {'genus': 'Brachyramphus', 'presence': 1, 'species': 'marmoratus'}\n", "feature = feature.set(newDict)\n", "feature" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Getting Feature properties" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "prop = feature.get('species')\n", "prop" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "props = feature.toDictionary()\n", "props" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### FeatureCollection\n", "\n", "#### Loading feature collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "fc = ee.FeatureCollection('TIGER/2016/Roads')\n", "Map.setCenter(-73.9596, 40.7688, 12)\n", "Map.addLayer(fc, {}, 'Census roads')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fc.limit(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Creating feature collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "features = [\n", " ee.Feature(ee.Geometry.Rectangle(30.01, 59.80, 30.59, 60.15), {'name': 'Voronoi'}),\n", " ee.Feature(ee.Geometry.Point(-73.96, 40.781), {'name': 'Thiessen'}),\n", " ee.Feature(ee.Geometry.Point(6.4806, 50.8012), {'name': 'Dirichlet'}),\n", "]\n", "fromList = ee.FeatureCollection(features)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Filtering feature collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "states = ee.FeatureCollection('TIGER/2018/States')\n", "feat = states.filter(ee.Filter.eq('NAME', 'Texas'))\n", "Map.addLayer(feat, {}, 'Texas')\n", "Map.centerObject(feat)\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "texas = feat.first()\n", "texas.toDictionary()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "states = ee.FeatureCollection('TIGER/2018/States')\n", "fc = states.filter(ee.Filter.inList('NAME', ['California', 'Oregon', 'Washington']))\n", "Map.addLayer(fc, {}, 'West Coast')\n", "Map.centerObject(fc)\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "region = Map.user_roi\n", "if region is None:\n", " region = ee.Geometry.BBox(-88.40, 29.88, -77.90, 35.39)\n", "\n", "fc = ee.FeatureCollection('TIGER/2018/States').filterBounds(region)\n", "Map.addLayer(fc, {}, 'Southeastern U.S.')\n", "Map.centerObject(fc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Visualizing feature collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "Map.addLayer(states, {}, \"US States\")\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "image = ee.Image().paint(states, 0, 3)\n", "Map.addLayer(image, {'palette': 'red'}, \"US States\")\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "style = {'color': '0000ffff', 'width': 2, 'lineType': 'solid', 'fillColor': 'FF000080'}\n", "Map.addLayer(states.style(**style), {}, \"US States\")\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "vis_params = {\n", " 'color': '000000',\n", " 'colorOpacity': 1,\n", " 'pointSize': 3,\n", " 'pointShape': 'circle',\n", " 'width': 2,\n", " 'lineType': 'solid',\n", " 'fillColorOpacity': 0.66,\n", "}\n", "palette = ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5']\n", "Map.add_styled_vector(\n", " states, column=\"NAME\", palette=palette, layer_name=\"Styled vector\", **vis_params\n", ")\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Styling by attribute" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[28.00142, -81.7424], zoom=13)\n", "Map.add_basemap('HYBRID')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "types = [\n", " \"Freshwater Forested/Shrub Wetland\",\n", " \"Freshwater Emergent Wetland\",\n", " \"Freshwater Pond\",\n", " \"Estuarine and Marine Wetland\",\n", " \"Riverine\",\n", " \"Lake\",\n", " \"Estuarine and Marine Deepwater\",\n", " \"Other\",\n", "]\n", "\n", "colors = [\n", " \"#008837\",\n", " \"#7FC31C\",\n", " \"#688CC0\",\n", " \"#66C2A5\",\n", " \"#0190BF\",\n", " \"#13007C\",\n", " \"#007C88\",\n", " \"#B28653\",\n", "]\n", "\n", "fillColor = [c + \"A8\" for c in colors]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fc = ee.FeatureCollection(\"projects/sat-io/open-datasets/NWI/wetlands/FL_Wetlands\")\n", "styled_fc = geemap.ee_vector_style(\n", " fc, column='WETLAND_TY', labels=types, fillColor=fillColor, color='00000000'\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.addLayer(styled_fc, {}, 'NWI')\n", "Map.add_legend(title='Wetland Type', labels=types, colors=colors)\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fuels = [\n", " 'Coal',\n", " 'Oil',\n", " 'Gas',\n", " 'Hydro',\n", " 'Nuclear',\n", " 'Solar',\n", " 'Waste',\n", " 'Wind',\n", " 'Geothermal',\n", " 'Biomass',\n", "]\n", "colors = [\n", " '000000',\n", " '593704',\n", " 'BC80BD',\n", " '0565A6',\n", " 'E31A1C',\n", " 'FF7F00',\n", " '6A3D9A',\n", " '5CA2D1',\n", " 'FDBF6F',\n", " '229A00',\n", "]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fc = ee.FeatureCollection(\"WRI/GPPD/power_plants\").filter(\n", " ee.Filter.inList('fuel1', fuels)\n", ")\n", "styled_fc = geemap.ee_vector_style(fc, column=\"fuel1\", labels=fuels, color=colors)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "Map.addLayer(styled_fc, {}, 'Power Plants')\n", "Map.add_legend(title=\"Power Plant Fuel Type\", labels=fuels, colors=colors)\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "types = ['I', 'U', 'S', 'M', 'C', 'O']\n", "labels = ['Interstate', 'U.S.', 'State recognized', 'Common Name', 'County', 'Other']\n", "colors = ['E31A1C', 'FF7F00', '6A3D9A', '000000', 'FDBF6F', '229A00']\n", "width = [8, 5, 4, 2, 1, 1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fc = ee.FeatureCollection('TIGER/2016/Roads')\n", "styled_fc = geemap.ee_vector_style(\n", " fc, column='rttyp', labels=types, color=colors, width=width\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40.7424, -73.9724], zoom=13)\n", "Map.addLayer(styled_fc, {}, 'Census Roads')\n", "Map.add_legend(title='Route Type', labels=labels, colors=colors)\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Earth Engine Data Catalog\n", "\n", "### Searching for datasets" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dataset_xyz = ee.Image('USGS/SRTMGL1_003')\n", "Map.addLayer(dataset_xyz, {}, \"USGS/SRTMGL1_003\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "dem = ee.Image('USGS/SRTMGL1_003')\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "Map.addLayer(dem, vis_params, 'SRTM DEM')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using the datasets module" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from geemap.datasets import DATA" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "dataset = ee.Image(DATA.USGS_GAP_CONUS_2011)\n", "Map.addLayer(dataset, {}, 'GAP CONUS')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from geemap.datasets import get_metadata\n", "\n", "get_metadata(DATA.USGS_GAP_CONUS_2011)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting image metadata" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = ee.Image('LANDSAT/LC09/C02/T1_L2/LC09_044034_20220503')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.bandNames()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.select('SR_B1').projection()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.select('SR_B1').projection().nominalScale()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.propertyNames()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.get('CLOUD_COVER')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.get('DATE_ACQUIRED')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.get('system:time_start')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "date = ee.Date(image.get('system:time_start'))\n", "date.format('YYYY-MM-dd')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.toDictionary()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "props = geemap.image_props(image)\n", "props" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculating descriptive statistics" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = ee.Image('LANDSAT/LC09/C02/T1_L2/LC09_044034_20220503')\n", "geemap.image_min_value(image)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.image_max_value(image)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.image_mean_value(image)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.image_stats(image)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using the inspector tool" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=(40, -100), zoom=4)\n", "dem = ee.Image('USGS/SRTMGL1_003')\n", "landsat7 = ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003').select(\n", " ['B1', 'B2', 'B3', 'B4', 'B5', 'B7']\n", ")\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "Map.addLayer(dem, vis_params, 'SRTM DEM')\n", "Map.addLayer(\n", " landsat7,\n", " {'bands': ['B4', 'B3', 'B2'], 'min': 20, 'max': 200, 'gamma': 2.0},\n", " 'Landsat 7',\n", ")\n", "Map.addLayer(states, {}, \"US States\")\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Converting JavaScript to Python\n", "\n", "### Interactive conversion" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Load an image.\n", "image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318')\n", "\n", "# Define the visualization parameters.\n", "vizParams = {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 0.5, 'gamma': [0.95, 1.1, 1]}\n", "\n", "# Center the map and display the image.\n", "Map.setCenter(-122.1899, 37.5010, 10)\n", "# San Francisco Bay\n", "Map.addLayer(image, vizParams, 'False color composite')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Batch conversion" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "snippet = \"\"\"\n", "// Load an image.\n", "var image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318');\n", "\n", "// Create an NDWI image, define visualization parameters and display.\n", "var ndwi = image.normalizedDifference(['B3', 'B5']);\n", "var ndwiViz = {min: 0.5, max: 1, palette: ['00FFFF', '0000FF']};\n", "Map.addLayer(ndwi, ndwiViz, 'NDWI');\n", "Map.centerObject(image)\n", "\"\"\"\n", "\n", "geemap.js_snippet_to_py(snippet, add_new_cell=True, import_ee=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Load an image.\n", "image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318')\n", "\n", "# Create an NDWI image, define visualization parameters and display.\n", "ndwi = image.normalizedDifference(['B3', 'B5'])\n", "ndwiViz = {'min': 0.5, 'max': 1, 'palette': ['00FFFF', '0000FF']}\n", "Map.addLayer(ndwi, ndwiViz, 'NDWI')\n", "Map.centerObject(image)\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "from geemap.conversion import *\n", "\n", "out_dir = os.getcwd()\n", "js_dir = get_js_examples(out_dir)\n", "js_to_python_dir(in_dir=js_dir, out_dir=out_dir, use_qgis=False)\n", "py_to_ipynb_dir(js_dir)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calling JavaScript functions from Python" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# %pip install oeel" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import oeel" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "oeel = geemap.requireJS()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ic = ee.ImageCollection(\"COPERNICUS/S2_SR\")\n", "icSize = (\n", " oeel.Algorithms.Sentinel2.cloudfree(maxCloud=20, S2Collection=ic)\n", " .filterDate('2020-01-01', '2020-01-02')\n", " .size()\n", ")\n", "print('Cloud free imagery: ', icSize.getInfo())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "url = 'https://tinyurl.com/27xy4oh9'\n", "lib = geemap.requireJS(lib_path=url)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lib.availability" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "grid = lib.generateGrid(-180, -70, 180, 70, 10, 10, 0, 0)\n", "grid.first()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "style = {'fillColor': '00000000'}\n", "Map.addLayer(grid.style(**style), {}, 'Grid')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "lib = geemap.requireJS(lib_path='grid.js', Map=Map)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "grid = lib.generateGrid(-180, -70, 180, 70, 10, 10, 0, 0)\n", "grid.first()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lib.grid_test()\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lib = geemap.requireJS('users/gena/packages:grid')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "grid = lib.generateGrid(-180, -70, 180, 70, 10, 10, 0, 0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "style = {'fillColor': '00000000'}\n", "Map.addLayer(grid.style(**style), {}, 'Grid')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }