31_geemap学习笔记 | 非监督分类

原文:31 Unsupervised Classification
https://developers.google.com/earth-engine/clustering

The ee.Clusterer package handles unsupervised classification (or clustering) in Earth Engine. These algorithms are currently based on the algorithms with the same name in Weka. More details about each Clusterer are available in the reference docs in the Code Editor.

Clusterers are used in the same manner as classifiers in Earth Engine. The general workflow for clustering is:

Assemble features with numeric properties in which to find clusters.
Instantiate a clusterer. Set its parameters if necessary. Train the
clusterer using the training data. Apply the clusterer to an image or
feature collection. Label the clusters. The training data is a
FeatureCollection with properties that will be input to the clusterer.
Unlike classifiers, there is no input class value for an Clusterer.
Like classifiers, the data for the train and apply steps are expected
to have the same number of values. When a trained clusterer is applied
to an image or table, it assigns an integer cluster ID to each pixel
or feature.

Here is a simple example of building and using an ee.Clusterer:
在这里插入图片描述

import ee
import geemap
geemap.set_proxy(port=10809)
# geemap.ee_search()
Map = geemap.Map()
Map
point = ee.Geometry.Point([116.5712, 30.6311])

image = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')\
                   .filterBounds(point)\
                   .filterDate('2019-01-01','2019-12-31')\
                   .sort('CLOUD_COVER')\
                   .first()\
                   .select('B[1-7]')

vis_params = {
    'min': 0,
    'max': 3000,
    'bands': ['B5', 'B4', 'B3']
}

Map.centerObject(point, 8)
Map.addLayer(image, vis_params, 'Landsat')

在这里插入图片描述

Landsat_props = geemap.image_props(image)
Landsat_props.getInfo()

# {'CLOUD_COVER': 0.07,
#  'CLOUD_COVER_LAND': 0.07,
#  'EARTH_SUN_DISTANCE': 0.98418,
#  'ESPA_VERSION': '2_23_0_1b',
#  'GEOMETRIC_RMSE_MODEL': 8.085,
#  'GEOMETRIC_RMSE_MODEL_X': 5.018,
#  'GEOMETRIC_RMSE_MODEL_Y': 6.339,
#  'IMAGE_DATE': '2019-01-23',
#  'IMAGE_QUALITY_OLI': 9,
#  'IMAGE_QUALITY_TIRS': 9,
#  'LANDSAT_ID': 'LC08_L1TP_121039_20190123_20190205_01_T1',
#  'LEVEL1_PRODUCTION_DATE': 1549390639000,
#  'NOMINAL_SCALE': 30,
#  'PIXEL_QA_VERSION': 'generate_pixel_qa_1.6.0',
#  'SATELLITE': 'LANDSAT_8',
#  'SENSING_TIME': '2019-01-23T02:43:44.8791959Z',
#  'SOLAR_AZIMUTH_ANGLE': 150.995743,
#  'SOLAR_ZENITH_ANGLE': 55.360382,
#  'SR_APP_VERSION': 'LaSRC_1.3.0',
#  'WRS_PATH': 121,
#  'WRS_ROW': 39,
#  'system:asset_size': '637.081655 MB',
#  'system:band_names': ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7'],
#  'system:id': 'LANDSAT/LC08/C01/T1_SR/LC08_121039_20190123',
#  'system:index': 'LC08_121039_20190123',
#  'system:time_end': '2019-01-23 02:43:44',
#  'system:time_start': '2019-01-23 02:43:44',
#  'system:version': 1563396743245792}
Landsat_props.get('IMAGE_DATE').getInfo()

# '2019-01-23'
Landsat_props.get('CLOUD_COVER').getInfo()

# 0.07

制作训练样本

#### 创建感兴趣区的几种方法:######

# region = Map.user_roi
# region = ee.Geometry.Rectangle([-122.6003, 37.4831, -121.8036, 37.8288])
# region = ee.Geometry.Point([-122.4439, 37.7538]).buffer(10000)
qsshp = "G:/learnpy/shp/潜山市.shp"
qs_ee = geemap.shp_to_ee(qsshp)

training = image.sample(**{
#     'region':qs_ee,
    'scale':30,
    'numPixels': 5000,
    'seed': 0,
    'geometries':True
})

Map.addLayer(training, {}, 'training', )
Map

在这里插入图片描述
Train the Cluster

#Instantiate the cluster and train it
n_cluster = 5
clusterer = ee.Clusterer.wekaKMeans(n_cluster).train(training)

Classify the image

#Cluster the input using the trained cluster
result = image.cluster(clusterer)

Map.addLayer(result.randomVisualizer(), {}, 'clusters')
Map

在这里插入图片描述

legend_keys = ['water', 'Two', 'Three', 'Four', 'ect']
legend_colors = ['#8DD3C7', '#FFFFB3', '#BEBADA', '#FB8072', '#80B1D3']

# Relassify the map
result = result.remap([0, 1, 2, 3, 4], [1, 2, 3, 4, 5])

Map.addLayer(result, {'min': 1, 'max': 5, 'palette': legend_colors}, 'Labelled clusters')
Map.add_legend(legend_keys = legend_keys, legend_colors=legend_colors, position='bottomright')
Map

在这里插入图片描述

print('Change layer opacity')
cluster_layer = Map.layers[-1]
cluster_layer.interact(opacity=(0, 1, 0.1))

在这里插入图片描述

import os
out_dir = os.path.join(os.path.expanduser('~'),'Downloads')
out_file = os.path.join(out_dir, 'cluster.tif')
geemap.ee_export_image(result, filename=out_file, scale=90)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值