Annonce : Tous les projets non commerciaux enregistrés pour utiliser Earth Engine avant le 15 avril 2025 doivent vérifier leur éligibilité non commerciale pour conserver leur accès à Earth Engine.
Les objets ee.Image disposent d'un ensemble de méthodes relationnelles, conditionnelles et booléennes pour la création d'expressions de prise de décision. Les résultats de ces méthodes sont utiles pour limiter l'analyse à certains pixels ou régions via le masquage, le développement de cartes classées et le réassignement de valeurs.
Opérateurs relationnels et booléens
Les méthodes relationnelles incluent les suivantes:
Pour effectuer des comparaisons par pixel entre des images, utilisez des opérateurs relationnels. Pour extraire les zones urbanisées d'une image, cet exemple utilise des opérateurs relationnels pour définir des seuils sur les indices spectraux, en combinant les seuils avec l'opérateur and:
Comme illustré dans cet exemple, la sortie des opérateurs relationnels et booléens est "true" (1) ou "false" (0). Pour masquer les zéros, vous pouvez masquer l'image binaire obtenue avec elle-même à l'aide de selfMask().
NDVI et NDWI faibles (blanc) de Landsat 8, San Francisco, Californie, États-Unis.
Les images binaires renvoyées par les opérateurs relationnels et booléens peuvent être utilisées avec des opérateurs mathématiques. Cet exemple crée des zones d'urbanisation dans une image de lumières nocturnes à l'aide d'opérateurs relationnels et de add():
Notez que dans l'exemple d'expression précédent, la bande d'intérêt est référencée à l'aide de la fonction b(), plutôt qu'à l'aide d'un dictionnaire de noms de variables. Pour en savoir plus sur les expressions d'image, consultez cette page. Utiliser des opérateurs mathématiques ou une expression produit le même résultat.
Zones arbitraires de l'imagerie nocturne de 2012 pour Paris, France.
Une autre façon d'implémenter des opérations conditionnelles sur les images consiste à utiliser l'opérateur where(). Envisagez de remplacer les pixels masqués par d'autres données. Dans l'exemple suivant, les pixels nuageux sont remplacés par des pixels d'une image sans nuage à l'aide de where():
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/07/25 (UTC).
[null,null,["Dernière mise à jour le 2025/07/25 (UTC)."],[[["\u003cp\u003e\u003ccode\u003eee.Image\u003c/code\u003e objects have relational, conditional, and boolean methods for constructing decision-making expressions for image analysis.\u003c/p\u003e\n"],["\u003cp\u003eRelational operators (eq, gt, gte, lt, lte) enable per-pixel comparisons between images, useful for tasks like thresholding and masking.\u003c/p\u003e\n"],["\u003cp\u003eBoolean operators (and, or, not) combine and modify relational expressions, allowing for complex image analysis logic.\u003c/p\u003e\n"],["\u003cp\u003eConditional operators, such as \u003ccode\u003ewhere()\u003c/code\u003e, facilitate the replacement of pixel values based on specified conditions, like replacing cloudy pixels with clear ones.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eexpression()\u003c/code\u003e provides a flexible way to implement conditional operations using ternary operators, offering similar functionality to mathematical and conditional operators.\u003c/p\u003e\n"]]],[],null,["# Relational, Conditional, and Boolean Operations\n\n|---------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------|\n| [Run in Google Colab](https://colab.research.google.com/github/google/earthengine-community/blob/master/guides/linked/generated/image_relational.ipynb) | [View source on GitHub](https://github.com/google/earthengine-community/blob/master/guides/linked/generated/image_relational.ipynb) |\n\n`ee.Image` objects have a set of relational, conditional, and boolean methods for\nconstructing decision-making expressions. The results of these methods are useful for limiting\nanalysis to certain pixels or regions through masking, developing classified maps, and value\nreassignment.\n\nRelational and boolean operators\n--------------------------------\n\n**Relational** methods include:\n\n`eq()`, `gt()`, `gte()`, `lt()`, and\n`lte()`\n\n**Boolean** methods include:\n\n### Code Editor (JavaScript)\n\n`and()`,`or()`, and `not()`\n\n### Colab (Python)\n\n`And()`,`Or()`, and `Not()`\n\nTo perform per-pixel comparisons between images, use relational operators. To\nextract urbanized areas in an image, this example uses relational operators to threshold\nspectral indices, combining the thresholds with the *and* operator:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Load a Landsat 8 image.\nvar image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318');\n\n// Create NDVI and NDWI spectral indices.\nvar ndvi = image.normalizedDifference(['B5', 'B4']);\nvar ndwi = image.normalizedDifference(['B3', 'B5']);\n\n// Create a binary layer using logical operations.\nvar bare = ndvi.lt(0.2).and(ndwi.lt(0));\n\n// Mask and display the binary layer.\nMap.setCenter(-122.3578, 37.7726, 12);\nMap.setOptions('satellite');\nMap.addLayer(bare.selfMask(), {}, 'bare');\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\n# Load a Landsat 8 image.\nimage = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318')\n\n# Create NDVI and NDWI spectral indices.\nndvi = image.normalizedDifference(['B5', 'B4'])\nndwi = image.normalizedDifference(['B3', 'B5'])\n\n# Create a binary layer using logical operations.\nbare = ndvi.lt(0.2).And(ndwi.lt(0))\n\n# Define a map centered on San Francisco Bay.\nmap_bare = geemap.Map(center=[37.7726, -122.3578], zoom=12)\n\n# Add the masked image layer to the map and display it.\nmap_bare.add_layer(bare.selfMask(), None, 'bare')\ndisplay(map_bare)\n```\n\nAs illustrated by this example, the output of relational and boolean operators is\neither true (1) or false (0). To mask the 0's, you can mask the resultant binary image\nwith itself using `selfMask()`.\nLow NDVI and low NDWI (white) from Landsat 8, San Francisco, California, USA.\n\nThe binary images that are returned by relational and boolean operators can be used with\nmathematical operators. This example creates zones of urbanization in a nighttime lights\nimage using relational operators and `add()`:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Load a 2012 nightlights image.\nvar nl2012 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182012');\nvar lights = nl2012.select('stable_lights');\n\n// Define arbitrary thresholds on the 6-bit stable lights band.\nvar zones = lights.gt(30).add(lights.gt(55)).add(lights.gt(62));\n\n// Display the thresholded image as three distinct zones near Paris.\nvar palette = ['000000', '0000FF', '00FF00', 'FF0000'];\nMap.setCenter(2.373, 48.8683, 8);\nMap.addLayer(zones, {min: 0, max: 3, palette: palette}, 'development zones');\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\n# Load a 2012 nightlights image.\nnl_2012 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182012')\nlights = nl_2012.select('stable_lights')\n\n# Define arbitrary thresholds on the 6-bit stable lights band.\nzones = lights.gt(30).add(lights.gt(55)).add(lights.gt(62))\n\n# Define a map centered on Paris, France.\nmap_zones = geemap.Map(center=[48.8683, 2.373], zoom=8)\n\n# Display the thresholded image as three distinct zones near Paris.\npalette = ['000000', '0000FF', '00FF00', 'FF0000']\nmap_zones.add_layer(\n zones, {'min': 0, 'max': 3, 'palette': palette}, 'development zones'\n)\ndisplay(map_zones)\n```\n\nConditional operators\n---------------------\n\nNote that the code in the previous example is equivalent to using a\n[ternary operator](http://en.wikipedia.org/wiki/%3F:) implemented by\n`expression()`:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Create zones using an expression, display.\nvar zonesExp = nl2012.expression(\n \"(b('stable_lights') \u003e 62) ? 3\" +\n \": (b('stable_lights') \u003e 55) ? 2\" +\n \": (b('stable_lights') \u003e 30) ? 1\" +\n \": 0\"\n);\nMap.addLayer(zonesExp,\n {min: 0, max: 3, palette: palette},\n 'development zones (ternary)');\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\n# Create zones using an expression, display.\nzones_exp = nl_2012.expression(\n \"(b('stable_lights') \u003e 62) ? 3 \"\n \": (b('stable_lights') \u003e 55) ? 2 \"\n \": (b('stable_lights') \u003e 30) ? 1 \"\n ': 0'\n)\n\n# Define a map centered on Paris, France.\nmap_zones_exp = geemap.Map(center=[48.8683, 2.373], zoom=8)\n\n# Add the image layer to the map and display it.\nmap_zones_exp.add_layer(\n zones_exp, {'min': 0, 'max': 3, 'palette': palette}, 'zones exp'\n)\ndisplay(map_zones_exp)\n```\n\nObserve that in the previous expression example, the band of interest is referenced using\nthe `b()` function, rather than a dictionary of variable names. Learn more\nabout image expressions on\n[this page](/earth-engine/guides/image_math#expressions). Using either mathematical\noperators or an expression will produce the same result.\nArbitrary zones of 2012 nightlights imagery for Paris, France.\n\nAnother way to implement conditional operations on images is with the\n`where()` operator. Consider the need to replace masked pixels with some\nother data. In the following example, cloudy pixels are replaced by pixels from a\ncloud-free image using `where()`:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Load a cloudy Sentinel-2 image.\nvar image = ee.Image(\n 'COPERNICUS/S2_SR/20210114T185729_20210114T185730_T10SEG');\nMap.addLayer(image,\n {bands: ['B4', 'B3', 'B2'], min: 0, max: 2000},\n 'original image');\n\n// Load another image to replace the cloudy pixels.\nvar replacement = ee.Image(\n 'COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG');\n\n// Set cloudy pixels (greater than 5% probability) to the other image.\nvar replaced = image.where(image.select('MSK_CLDPRB').gt(5), replacement);\n\n// Display the result.\nMap.setCenter(-122.3769, 37.7349, 11);\nMap.addLayer(replaced,\n {bands: ['B4', 'B3', 'B2'], min: 0, max: 2000},\n 'clouds replaced');\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\n# Load a cloudy Sentinel-2 image.\nimage = ee.Image('COPERNICUS/S2_SR/20210114T185729_20210114T185730_T10SEG')\n\n# Load another image to replace the cloudy pixels.\nreplacement = ee.Image(\n 'COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG'\n)\n\n# Set cloudy pixels (greater than 5% probability) to the other image.\nreplaced = image.where(image.select('MSK_CLDPRB').gt(5), replacement)\n\n# Define a map centered on San Francisco Bay.\nmap_replaced = geemap.Map(center=[37.7349, -122.3769], zoom=11)\n\n# Display the images on a map.\nvis_params = {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 2000}\nmap_replaced.add_layer(image, vis_params, 'original image')\nmap_replaced.add_layer(replaced, vis_params, 'clouds replaced')\ndisplay(map_replaced)\n```"]]