<jupyter_start><jupyter_text>[![colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/davemlz/eemont/blob/master/docs/tutorials/024-Container-Image-ImageCollection.ipynb)[![Open in SageMaker Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/davemlz/eemont/blob/master/docs/tutorials/024-Container-Image-ImageCollection.ipynb)[![Open in Planetary Computer](https://img.shields.io/badge/Open-Planetary%20Computer-black?style=flat&logo=microsoft)](https://pccompute.westeurope.cloudapp.azure.com/compute/hub/user-redirect/git-pull?repo=https://github.com/davemlz/eemont&urlpath=lab/tree/eemont/docs/tutorials/024-Container-Image-ImageCollection.ipynb&branch=master)  Container Emulation Methods for ee.Image and ee.ImageCollection _Tutorial created by **<NAME>**_: [GitHub](https://github.com/davemlz) | [Twitter](https://twitter.com/dmlmont)- GitHub Repo: [https://github.com/davemlz/eemont](https://github.com/davemlz/eemont)- PyPI link: [https://pypi.org/project/eemont/](https://pypi.org/project/eemont/)- Conda-forge: [https://anaconda.org/conda-forge/eemont](https://anaconda.org/conda-forge/eemont)- Documentation: [https://eemont.readthedocs.io/](https://eemont.readthedocs.io/)- More tutorials: [https://github.com/davemlz/eemont/tree/master/docs/tutorials](https://github.com/davemlz/eemont/tree/master/docs/tutorials)  Let's start! If required, please uncomment:<jupyter_code>#!pip install eemont
#!pip install geemap<jupyter_output><empty_output><jupyter_text>Import the required packges.<jupyter_code>import ee, eemont, geemap
import geemap.colormaps as cm<jupyter_output><empty_output><jupyter_text>Authenticate and Initialize Earth Engine and geemap.<jupyter_code>Map = geemap.Map()<jupyter_output><empty_output><jupyter_text>Let's define a point of interest:<jupyter_code>poi = ee.Geometry.PointFromQuery("Oporto, Portugal",user_agent = "eemont-tutorial-024")<jupyter_output><empty_output><jupyter_text>Let's work with Sentinel-2 SR:<jupyter_code>S2 = (ee.ImageCollection("COPERNICUS/S2_SR")
      .filterBounds(poi)
      .filterDate("2020-01-01","2020-07-01")
      .preprocess()
      .spectralIndices())<jupyter_output><empty_output><jupyter_text>Container Emulation Methods  ee.ImageCollection If you want to know how many images has the image collection, you can use `len()`:<jupyter_code>len(S2)<jupyter_output><empty_output><jupyter_text>If you want to select specific bands from the collection, you can use `collection[band]` or `collection[[band1,band2,...,bandn]]`:<jupyter_code>RGB = S2[["B2","B3","B4"]]<jupyter_output><empty_output><jupyter_text>You can also do this by using band indices:<jupyter_code>RGB = S2[[1,2,3]]<jupyter_output><empty_output><jupyter_text>Or regex:<jupyter_code>RGB = S2["B[2-4]"]<jupyter_output><empty_output><jupyter_text>Or even better: slices!<jupyter_code>RGB = S2[1:4]<jupyter_output><empty_output><jupyter_text>Create a composite by using container emulation methods!<jupyter_code>Map = geemap.Map()
Map.addLayer(S2[[3,2,1]].median(),{"min":0,"max":0.3},"RGB")
Map.centerObject(poi)
Map<jupyter_output><empty_output><jupyter_text>If you want to select images from a collection, convert the collection to a list and use container emulation methods!We are going to select the first, the third, and the fifth images from the collection.First, let's convert the collection to a list:<jupyter_code>S2list = S2.toList(S2.size())<jupyter_output><empty_output><jupyter_text>Then, we can select the images!<jupyter_code>S2selected = S2list[[0,2,4]]<jupyter_output><empty_output><jupyter_text>Now we have three images in the `S2selected` list:<jupyter_code>len(S2selected)<jupyter_output><empty_output><jupyter_text>Now, let's select all images from the 21st until the end.Psst! We can use slices!<jupyter_code>S2selected = S2list[20:]<jupyter_output><empty_output><jupyter_text>Let's see how many images do we have!<jupyter_code>len(S2selected)<jupyter_output><empty_output><jupyter_text>If we don't want the last images to be selected, we can use negative indices! Here an example:<jupyter_code>S2selected = S2list[20:-5]<jupyter_output><empty_output><jupyter_text>Now, let's see how many images do we have in the list now!<jupyter_code>len(S2selected)<jupyter_output><empty_output><jupyter_text>But they're ee.Image objects inside an ee.List object. We can leave it that way, or we can convert them into an ee.ImageCollection object!<jupyter_code>S2selected = ee.ImageCollection(S2selected)<jupyter_output><empty_output><jupyter_text>ee.Image We can also select bands for an ee.Image object!<jupyter_code>S2img = S2.first()<jupyter_output><empty_output><jupyter_text>Let's select the NDVI:<jupyter_code>NDVI = S2img['NDVI']<jupyter_output><empty_output><jupyter_text>Or let's select the RGB bands using slices!<jupyter_code>RGBimg = S2img[1:4]<jupyter_output><empty_output><jupyter_text>The same rules for ee.ImageCollection apply for the ee.Image object class!Let's visualize the NDVI for Oporto!<jupyter_code>Map = geemap.Map()
Map.addLayer(S2img["NDVI"],{"min":0,"max":1,"palette":cm.palettes.ndvi},"NDVI")
Map.centerObject(poi)
Map<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup