ee.Image.sample
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Échantillonne les pixels d'une image et les renvoie sous forme de FeatureCollection. Chaque caractéristique aura une propriété par bande dans l'image d'entrée. Notez que le comportement par défaut consiste à supprimer les caractéristiques qui croisent les pixels masqués, ce qui entraîne des propriétés à valeur nulle (voir l'argument dropNulls).
Utilisation | Renvoie |
---|
Image.sample(region, scale, projection, factor, numPixels, seed, dropNulls, tileScale, geometries) | FeatureCollection |
Argument | Type | Détails |
---|
ceci : image | Image | Image à échantillonner. |
region | Géométrie, valeur par défaut : null | Région à partir de laquelle effectuer l'échantillonnage. Si elle n'est pas spécifiée, l'empreinte entière de l'image est utilisée. |
scale | Float, valeur par défaut : null | Échelle nominale en mètres de la projection à échantillonner. |
projection | Projection, valeur par défaut : null | Projection dans laquelle échantillonner. Si aucune projection n'est spécifiée, celle de la première bande de l'image est utilisée. Si elle est spécifiée en plus de la mise à l'échelle, elle est remise à l'échelle spécifiée. |
factor | Float, valeur par défaut : null | Facteur de sous-échantillonnage, compris dans la plage (0, 1]. Si elle est spécifiée, "numPixels" ne doit pas l'être. La valeur par défaut est "aucune sous-échantillonnage". |
numPixels | Longue, valeur par défaut : null | Nombre approximatif de pixels à échantillonner. Si elle est spécifiée, le facteur ne doit pas l'être. |
seed | Entier, valeur par défaut : 0 | Graine de randomisation à utiliser pour le sous-échantillonnage. |
dropNulls | Booléen, valeur par défaut : true | Filtrez le résultat après coup pour supprimer les caractéristiques dont les propriétés ont une valeur nulle. |
tileScale | Float, valeur par défaut : 1 | Facteur de scaling utilisé pour réduire la taille des tuiles d'agrégation. Si vous utilisez un tileScale plus grand (par exemple, 2 ou 4) peut permettre d'effectuer des calculs qui manquent de mémoire avec la valeur par défaut. |
geometries | Booléen, valeur par défaut : false | Si la valeur est "true", le centre du pixel échantillonné est ajouté en tant que propriété de géométrie de l'entité de sortie. Sinon, les géométries seront omises (ce qui permet d'économiser de la mémoire). |
Exemples
Éditeur de code (JavaScript)
// Demonstrate extracting pixels from an image as features with
// ee.Image.sample(), and show how the features are aligned with the pixels.
// An image with one band of elevation data.
var image = ee.Image('CGIAR/SRTM90_V4');
var VIS_MIN = 1620;
var VIS_MAX = 1650;
Map.addLayer(image, {min: VIS_MIN, max: VIS_MAX}, 'SRTM');
// Region to sample.
var region = ee.Geometry.Polygon(
[[[-110.006, 40.002],
[-110.006, 39.999],
[-109.995, 39.999],
[-109.995, 40.002]]], null, false);
// Show region on the map.
Map.setCenter(-110, 40, 16);
Map.addLayer(ee.FeatureCollection([region]).style({"color": "00FF0022"}));
// Perform sampling; convert image pixels to features.
var samples = image.sample({
region: region,
// Default (false) is no geometries in the output.
// When set to true, each feature has a Point geometry at the center of the
// image pixel.
geometries: true,
// The scale is not specified, so the resolution of the image will be used,
// and there is a feature for every pixel. If we give a scale parameter, the
// image will be resampled and there will be more or fewer features.
//
// scale: 200,
});
// Visualize sample data using ee.FeatureCollection.style().
var styled = samples
.map(function (feature) {
return feature.set('style', {
pointSize: feature.getNumber('elevation').unitScale(VIS_MIN, VIS_MAX)
.multiply(15),
});
})
.style({
color: '000000FF',
fillColor: '00000000',
styleProperty: 'style',
neighborhood: 6, // increase to correctly draw large points
});
Map.addLayer(styled);
// Each sample feature has a point geometry and a property named 'elevation'
// corresponding to the band named 'elevation' of the image. If there are
// multiple bands they will become multiple properties. This will print:
//
// geometry: Point (-110.01, 40.00)
// properties:
// elevation: 1639
print(samples.first());
Configuration de Python
Consultez la page
Environnement Python pour en savoir plus sur l'API Python et sur l'utilisation de geemap
pour le développement interactif.
import ee
import geemap.core as geemap
Colab (Python)
# Demonstrate extracting pixels from an image as features with
# ee.Image.sample(), and show how the features are aligned with the pixels.
# An image with one band of elevation data.
image = ee.Image('CGIAR/SRTM90_V4')
vis_min = 1620
vis_max = 1650
m = geemap.Map()
m.add_layer(image, {'min': vis_min, 'max': vis_max}, 'SRTM')
# Region to sample.
region = ee.Geometry.Polygon(
[[
[-110.006, 40.002],
[-110.006, 39.999],
[-109.995, 39.999],
[-109.995, 40.002],
]],
None,
False,
)
# Show region on the map.
m.set_center(-110, 40, 16)
m.add_layer(ee.FeatureCollection([region]).style(color='00FF0022'))
# Perform sampling convert image pixels to features.
samples = image.sample(
region=region,
# Default (False) is no geometries in the output.
# When set to True, each feature has a Point geometry at the center of the
# image pixel.
geometries=True,
# The scale is not specified, so the resolution of the image will be used,
# and there is a feature for every pixel. If we give a scale parameter, the
# image will be resampled and there will be more or fewer features.
#
# scale=200,
)
def scale_point_size(feature):
elevation = feature.getNumber('elevation')
point_size = elevation.unitScale(vis_min, vis_max).multiply(15)
feature.set('style', {'pointSize': point_size})
return feature
# Visualize sample data using ee.FeatureCollection.style().
styled = samples.map(scale_point_size).style(
color='000000FF',
fillColor='00000000',
styleProperty='style',
neighborhood=6, # increase to correctly draw large points
)
m.add_layer(styled)
display(m)
# Each sample feature has a point geometry and a property named 'elevation'
# corresponding to the band named 'elevation' of the image. If there are
# multiple bands they will become multiple properties. This will print:
#
# geometry: Point (-110.01, 40.00)
# properties:
# elevation: 1639
display(samples.first())
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/26 (UTC).
[null,null,["Dernière mise à jour le 2025/07/26 (UTC)."],[[["\u003cp\u003e\u003ccode\u003eImage.sample()\u003c/code\u003e extracts pixel values from an image and converts them into a FeatureCollection, with each feature representing a pixel and its properties corresponding to the band values.\u003c/p\u003e\n"],["\u003cp\u003eYou can define a region of interest, control the sampling scale and projection, and adjust the number of sampled pixels using arguments like \u003ccode\u003eregion\u003c/code\u003e, \u003ccode\u003escale\u003c/code\u003e, \u003ccode\u003eprojection\u003c/code\u003e, \u003ccode\u003efactor\u003c/code\u003e, and \u003ccode\u003enumPixels\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eSampled features can optionally include point geometries representing pixel centers using the \u003ccode\u003egeometries\u003c/code\u003e argument.\u003c/p\u003e\n"],["\u003cp\u003eBy default, features associated with masked pixels (resulting in null-valued properties) are excluded, which can be controlled using the \u003ccode\u003edropNulls\u003c/code\u003e argument.\u003c/p\u003e\n"]]],[],null,["# ee.Image.sample\n\nSamples the pixels of an image, returning them as a FeatureCollection. Each feature will have 1 property per band in the input image. Note that the default behavior is to drop features that intersect masked pixels, which result in null-valued properties (see dropNulls argument).\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|--------------------------------------------------------------------------------------------------------------------------------------------------|-------------------|\n| Image.sample`(`*region* `, `*scale* `, `*projection* `, `*factor* `, `*numPixels* `, `*seed* `, `*dropNulls* `, `*tileScale* `, `*geometries*`)` | FeatureCollection |\n\n| Argument | Type | Details |\n|---------------|---------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| this: `image` | Image | The image to sample. |\n| `region` | Geometry, default: null | The region to sample from. If unspecified, uses the image's whole footprint. |\n| `scale` | Float, default: null | A nominal scale in meters of the projection to sample in. |\n| `projection` | Projection, default: null | The projection in which to sample. If unspecified, the projection of the image's first band is used. If specified in addition to scale, rescaled to the specified scale. |\n| `factor` | Float, default: null | A subsampling factor, within (0, 1\\]. If specified, 'numPixels' must not be specified. Defaults to no subsampling. |\n| `numPixels` | Long, default: null | The approximate number of pixels to sample. If specified, 'factor' must not be specified. |\n| `seed` | Integer, default: 0 | A randomization seed to use for subsampling. |\n| `dropNulls` | Boolean, default: true | Post filter the result to drop features that have null-valued properties. |\n| `tileScale` | Float, default: 1 | A scaling factor used to reduce aggregation tile size; using a larger tileScale (e.g., 2 or 4) may enable computations that run out of memory with the default. |\n| `geometries` | Boolean, default: false | If true, adds the center of the sampled pixel as the geometry property of the output feature. Otherwise, geometries will be omitted (saving memory). |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\n// Demonstrate extracting pixels from an image as features with\n// ee.Image.sample(), and show how the features are aligned with the pixels.\n\n// An image with one band of elevation data.\nvar image = ee.Image('CGIAR/SRTM90_V4');\nvar VIS_MIN = 1620;\nvar VIS_MAX = 1650;\nMap.addLayer(image, {min: VIS_MIN, max: VIS_MAX}, 'SRTM');\n\n// Region to sample.\nvar region = ee.Geometry.Polygon(\n [[[-110.006, 40.002],\n [-110.006, 39.999],\n [-109.995, 39.999],\n [-109.995, 40.002]]], null, false);\n// Show region on the map.\nMap.setCenter(-110, 40, 16);\nMap.addLayer(ee.FeatureCollection([region]).style({\"color\": \"00FF0022\"}));\n\n// Perform sampling; convert image pixels to features.\nvar samples = image.sample({\n region: region,\n\n // Default (false) is no geometries in the output.\n // When set to true, each feature has a Point geometry at the center of the\n // image pixel.\n geometries: true,\n\n // The scale is not specified, so the resolution of the image will be used,\n // and there is a feature for every pixel. If we give a scale parameter, the\n // image will be resampled and there will be more or fewer features.\n //\n // scale: 200,\n});\n\n// Visualize sample data using ee.FeatureCollection.style().\nvar styled = samples\n .map(function (feature) {\n return feature.set('style', {\n pointSize: feature.getNumber('elevation').unitScale(VIS_MIN, VIS_MAX)\n .multiply(15),\n });\n })\n .style({\n color: '000000FF',\n fillColor: '00000000',\n styleProperty: 'style',\n neighborhood: 6, // increase to correctly draw large points\n });\nMap.addLayer(styled);\n\n// Each sample feature has a point geometry and a property named 'elevation'\n// corresponding to the band named 'elevation' of the image. If there are\n// multiple bands they will become multiple properties. This will print:\n//\n// geometry: Point (-110.01, 40.00)\n// properties:\n// elevation: 1639\nprint(samples.first());\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# Demonstrate extracting pixels from an image as features with\n# ee.Image.sample(), and show how the features are aligned with the pixels.\n\n# An image with one band of elevation data.\nimage = ee.Image('CGIAR/SRTM90_V4')\nvis_min = 1620\nvis_max = 1650\nm = geemap.Map()\nm.add_layer(image, {'min': vis_min, 'max': vis_max}, 'SRTM')\n\n# Region to sample.\nregion = ee.Geometry.Polygon(\n [[\n [-110.006, 40.002],\n [-110.006, 39.999],\n [-109.995, 39.999],\n [-109.995, 40.002],\n ]],\n None,\n False,\n)\n# Show region on the map.\nm.set_center(-110, 40, 16)\n\nm.add_layer(ee.FeatureCollection([region]).style(color='00FF0022'))\n\n# Perform sampling convert image pixels to features.\nsamples = image.sample(\n region=region,\n # Default (False) is no geometries in the output.\n # When set to True, each feature has a Point geometry at the center of the\n # image pixel.\n geometries=True,\n # The scale is not specified, so the resolution of the image will be used,\n # and there is a feature for every pixel. If we give a scale parameter, the\n # image will be resampled and there will be more or fewer features.\n #\n # scale=200,\n)\n\n\ndef scale_point_size(feature):\n elevation = feature.getNumber('elevation')\n point_size = elevation.unitScale(vis_min, vis_max).multiply(15)\n feature.set('style', {'pointSize': point_size})\n return feature\n\n\n# Visualize sample data using ee.FeatureCollection.style().\nstyled = samples.map(scale_point_size).style(\n color='000000FF',\n fillColor='00000000',\n styleProperty='style',\n neighborhood=6, # increase to correctly draw large points\n)\nm.add_layer(styled)\ndisplay(m)\n\n# Each sample feature has a point geometry and a property named 'elevation'\n# corresponding to the band named 'elevation' of the image. If there are\n# multiple bands they will become multiple properties. This will print:\n#\n# geometry: Point (-110.01, 40.00)\n# properties:\n# elevation: 1639\ndisplay(samples.first())\n```"]]