ラスターからベクターへの変換

Image(ラスター)から FeatureCollection(ベクター)データ型に変換するには、image.reduceToVectors() を使用します。これは Earth Engine でのベクトル化の主なメカニズムであり、他のタイプのリデューサーへの入力用に領域を生成する場合に役立ちます。reduceToVectors() メソッドは、接続されたピクセルの均質なグループの境界にポリゴンエッジ(必要に応じて重心または境界ボックス)を作成します。

たとえば、2012 年の日本の夜間照明の画像について考えてみましょう。ナイトライトのデジタル番号を開発の強度の代用として使用します。ナイトライトの任意のしきい値を使用してゾーンを定義し、ゾーンを単一バンド画像に結合し、reduceToVectors() を使用してゾーンをベクトル化します。

コードエディタ(JavaScript)

// Load a Japan boundary from the Large Scale International Boundary dataset.
var japan = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017')
  .filter(ee.Filter.eq('country_na', 'Japan'));

// Load a 2012 nightlights image, clipped to the Japan border.
var nl2012 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182012')
  .select('stable_lights')
  .clipToCollection(japan);

// Define arbitrary thresholds on the 6-bit nightlights image.
var zones = nl2012.gt(30).add(nl2012.gt(55)).add(nl2012.gt(62));
zones = zones.updateMask(zones.neq(0));

// Convert the zones of the thresholded nightlights to vectors.
var vectors = zones.addBands(nl2012).reduceToVectors({
  geometry: japan,
  crs: nl2012.projection(),
  scale: 1000,
  geometryType: 'polygon',
  eightConnected: false,
  labelProperty: 'zone',
  reducer: ee.Reducer.mean()
});

// Display the thresholds.
Map.setCenter(139.6225, 35.712, 9);
Map.addLayer(zones, {min: 1, max: 3, palette: ['0000FF', '00FF00', 'FF0000']}, 'raster');

// Make a display image for the vectors, add it to the map.
var display = ee.Image(0).updateMask(0).paint(vectors, '000000', 3);
Map.addLayer(display, {palette: '000000'}, 'vectors');

Python の設定

Python API とインタラクティブな開発で geemap を使用する方法については、 Python 環境のページをご覧ください。

import ee
import geemap.core as geemap

Colab(Python)

# Load a Japan boundary from the Large Scale International Boundary dataset.
japan = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017').filter(
    ee.Filter.eq('country_na', 'Japan')
)

# Load a 2012 nightlights image, clipped to the Japan border.
nl_2012 = (
    ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182012')
    .select('stable_lights')
    .clipToCollection(japan)
)

# Define arbitrary thresholds on the 6-bit nightlights image.
zones = nl_2012.gt(30).add(nl_2012.gt(55)).add(nl_2012.gt(62))
zones = zones.updateMask(zones.neq(0))

# Convert the zones of the thresholded nightlights to vectors.
vectors = zones.addBands(nl_2012).reduceToVectors(
    geometry=japan,
    crs=nl_2012.projection(),
    scale=1000,
    geometryType='polygon',
    eightConnected=False,
    labelProperty='zone',
    reducer=ee.Reducer.mean(),
)

# Display the thresholds.
m = geemap.Map()
m.set_center(139.6225, 35.712, 9)
m.add_layer(
    zones,
    {'min': 1, 'max': 3, 'palette': ['0000FF', '00FF00', 'FF0000']},
    'raster',
)

# Make a display image for the vectors, add it to the map.
display_image = ee.Image(0).updateMask(0).paint(vectors, '000000', 3)
m.add_layer(display_image, {'palette': '000000'}, 'vectors')
m

入力の最初のバンドは均質な領域の特定に使用され、残りのバンドは指定されたレジューサーに従って減算されます。その出力は、結果ベクトルにプロパティとして追加されます。geometry パラメータは、ベクトルを作成する範囲を指定します。一般的に、ベクトルを作成する最小ゾーンを指定することをおすすめします。曖昧さを回避するために、scalecrs を指定することもおすすめします。出力タイプは ‘polygon’ で、ポリゴンは 4 つの隣接する同種のゾーンから形成されます(つまり、eightConnected は false です)。最後の 2 つのパラメータ labelPropertyreducer は、出力ポリゴンに、ゾーンラベルとナイトライトのバンドの平均を含むプロパティをそれぞれ受け取るように指定します。

マッピングされた結果は、図 1 に示す東京エリアのようになります。 出力ポリゴンを調べると、平均レジューサーが指定されているため、各ポリゴンにゾーンのラベル({1, 2, 3})と夜間照明帯の平均を格納するプロパティがあることがわかります。

reduceToVectors の出力
図 1. 日本、東京の夜間照明のゾーン。ベクター境界は黒で表示されます。