ee.Image.sample

สุ่มตัวอย่างพิกเซลของรูปภาพและแสดงผลเป็น FeatureCollection ฟีเจอร์แต่ละรายการจะมี 1 พร็อพเพอร์ตี้ต่อแถบในรูปภาพอินพุต โปรดทราบว่าลักษณะการทำงานเริ่มต้นคือการทิ้งฟีเจอร์ที่ตัดกับพิกเซลที่มาสก์ ซึ่งจะทำให้พร็อพเพอร์ตี้มีค่าเป็น Null (ดูอาร์กิวเมนต์ dropNulls)

การใช้งานการคืนสินค้า
Image.sample(region, scale, projection, factor, numPixels, seed, dropNulls, tileScale, geometries)FeatureCollection
อาร์กิวเมนต์ประเภทรายละเอียด
ดังนี้ imageรูปภาพรูปภาพที่จะสุ่มตัวอย่าง
regionเรขาคณิต ค่าเริ่มต้น: nullภูมิภาคที่จะสุ่มตัวอย่าง หากไม่ได้ระบุ ระบบจะใช้ร่องรอยทั้งหมดของรูปภาพ
scaleลอย ค่าเริ่มต้น: nullมาตราส่วนที่ระบุเป็นเมตรของภาพฉายที่จะสุ่มตัวอย่าง
projectionการฉายภาพ ค่าเริ่มต้น: nullการฉายภาพที่จะสุ่มตัวอย่าง หากไม่ได้ระบุ ระบบจะใช้การฉายภาพของแถบแรกของรูปภาพ หากระบุเพิ่มเติมจากสเกล ระบบจะปรับขนาดใหม่เป็นสเกลที่ระบุ
factorลอย ค่าเริ่มต้น: nullปัจจัยการสุ่มตัวอย่างย่อยภายใน (0, 1] หากระบุไว้ จะต้องไม่ระบุ "numPixels" ค่าเริ่มต้นคือไม่มีการสุ่มตัวอย่างย่อย
numPixelsLong, default: nullจำนวนพิกเซลโดยประมาณที่จะสุ่มตัวอย่าง หากระบุไว้ จะต้องไม่ระบุ "factor"
seedจำนวนเต็ม ค่าเริ่มต้น: 0ค่าเริ่มต้นของการสุ่มที่จะใช้สำหรับการสุ่มตัวอย่างย่อย
dropNullsบูลีน ค่าเริ่มต้น: จริงกรองผลลัพธ์หลังการประมวลผลเพื่อทิ้งฟีเจอร์ที่มีพร็อพเพอร์ตี้เป็นค่าว่าง
tileScaleFloat, ค่าเริ่มต้น: 1ปัจจัยการปรับขนาดที่ใช้เพื่อลดขนาดไทล์การรวม การใช้ tileScale ที่ใหญ่ขึ้น (เช่น 2 หรือ 4) อาจเปิดใช้การคำนวณที่หน่วยความจำหมดด้วยค่าเริ่มต้น
geometriesบูลีน ค่าเริ่มต้น: falseหากเป็นจริง จะเพิ่มจุดกึ่งกลางของพิกเซลที่สุ่มเป็นพร็อพเพอร์ตี้เรขาคณิตของฟีเจอร์เอาต์พุต ไม่เช่นนั้น ระบบจะละเว้นรูปทรงเรขาคณิต (ประหยัดหน่วยความจำ)

ตัวอย่าง

โปรแกรมแก้ไขโค้ด (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());

การตั้งค่า Python

ดูข้อมูลเกี่ยวกับ Python API และการใช้ geemap เพื่อการพัฒนาแบบอินเทอร์แอกทีฟได้ที่หน้า สภาพแวดล้อม Python

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())