geemap学习笔记 05 geemap 基于shapefile矢量的影像和影像集导出


前言

本节主要介绍两部分内容:1)shapefile 矢量数据的使用; 2) 在geemap中导出影像或影像集。


一、shapefile 矢量数据的使用

① 提前在自己的jupyter notebook的文件夹中上传shapefile矢量数据,得到对应的存放路径。
上传shp数据
② 在geemap中加载地图底图,详细代码如下:

# class 10 使用shapefile 矢量数据的使用
import geemap
Map = geemap.Map()
Map

底图加载
③ 在geemap中加载黄河口自然保护区shapefile矢量范围,详细代码如下:

# class_10 在geemap中加载黄河口自然保护区矢量范围(需提前在自己的jupyter中上传shp矢量数据,得到对应的存放路径)
huanghekouClip_shp = '../gee/shptest/huanghekouClip.shp'
huanghekouClip = geemap.shp_to_ee(huanghekouClip_shp)
Map.addLayer(huanghekouClip, {}, 'huanghekouClip')

shapefile矢量范围

二、在geemap中导出影像或影像集

1. 在geemap中导出单景影像

利用已有的shapefile矢量裁剪影像后,并导出单景影像,详细代码如下:
① 在geemap中加载Landsat-7多光谱影像数据;

# part 1 导出单景影像

import ee
import geemap
import os
Map = geemap.Map()
Map
image = ee.Image('LE7_TOA_5YEAR/1999_2003')

landsat_vis = {'bands': ['B4', 'B3', 'B2'], 'gamma': 1.4}
Map.addLayer(image, landsat_vis, "LE7_TOA_5YEAR/1999_2003", True, 0.7)

Landsat-7多光谱影像
② 利用shp裁剪Landsat-7影像,详细代码如下:

# 使用shapefile矢量数据,shapes on the map using the Drawing tools before executing this code block
huanghekouClip_shp = '../gee/shptest/huanghekouClip.shp'
feature = geemap.shp_to_ee(huanghekouClip_shp)

if feature is None:
    geom = ee.Geometry.Polygon(
        [
            [
                [-115.413031, 35.889467],
                [-115.413031, 36.543157],
                [-114.034328, 36.543157],
                [-114.034328, 35.889467],
                [-115.413031, 35.889467],
            ]
        ]
    )
    feature = ee.Feature(geom, {})

roi = feature.geometry()
Map.addLayer(feature, {}, 'huanghekouClip')
out_dir = os.path.join(os.path.expanduser('E:\Project\gee'), 'exportmap1')
filename = os.path.join(out_dir, 'hhklandsat.tif')
#裁剪影像导出
image = image.clip(roi).unmask()
Map.addLayer(image, {}, 'image')

裁剪后的影像加载
③ 导出通过shapefile裁剪导出单景影像,详细代码如下:

# 导出单景影像
image = image.clip(roi).unmask()
geemap.ee_export_image(
    image, filename=filename, scale=90, region=roi, file_per_band=False
)

2. 在geemap中导出影像数据集

① 在影像中导出2008-2020年”USDA/NAIP/DOQQ“对应loc的影像数据集,详细代码如下:

import ee
import geemap
import os
loc = ee.Geometry.Point(-99.2222, 46.7816)
collection = (
    ee.ImageCollection('USDA/NAIP/DOQQ')
    .filterBounds(loc)
    .filterDate('2008-01-01', '2020-01-01')
    .filter(ee.Filter.listContains("system:band_names", "N"))
)
out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
geemap.ee_export_image_collection(collection, out_dir=out_dir)
geemap.ee_export_image_collection_to_drive(collection, folder='export', scale=10)

导出的影像数据集
Geometry 2008-2020年共有9景影像,依次下载9景影像数据。


3. 将像元提取为Numpy数组

① 将ROI矢量范围内的像元提取为numpy数组,以Landsat-8 2018.08.10的影像为例,选在三个波段展示,在Geometry的ROI范围裁剪下,得到对应裁剪后的影像,后提取shape的Numpy数组。

# class 11_03 将矢量范围内的像元提取为numpy数组
import ee
import geemap
import numpy as np
import matplotlib.pyplot as plt

img = ee.Image('LANDSAT/LC08/C01/T1_SR/LC08_038029_20180810').select(['B4', 'B5', 'B6'])

aoi = ee.Geometry.Polygon(
    [[[-110.8, 44.7], [-110.8, 44.6], [-110.6, 44.6], [-110.6, 44.7]]], None, False
)

rgb_img = geemap.ee_to_numpy(img, region=aoi)
print(rgb_img.shape)

提取结果
② 将数据范围设置在[0,255]之间,展示为RGB图像

rgb_img_test = (255 * ((rgb_img[:, :, 0:3] - 100) / 3500)).astype('uint8')
plt.imshow(rgb_img_test)
plt.show()

rgb结果图

总结

以上就是今天要讲的内容,本文仅仅简单介绍了基于shapefile矢量的影像和影像集导出,后续会继续更新使用geemap的具体实例~
哈哈哈

  • 11
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
geemap是基于Google Earth Engine的Python包,可以通过Earth Engine API调用其强大的计算资源和数据。如果想要在geemap中进行矢量数据的去云计算,可以使用以下步骤: 1. 使用geemap中的ee.FeatureCollection()函数加载矢量数据,例如: ```python import geemap.eefolium as geemap # 加载矢量数据 fc = geemap.shp_to_ee('path/to/shapefile.shp') ``` 2. 使用Earth Engine API中的图像合去云算法(Cloud Masking)对图像进行处理。可以使用ee.ImageCollection()函数加载图像合,然后调用map()函数对每个图像进行处理。例如,可以使用Landsat 8数据的CFMask算法进行去云处理: ```python import ee # 加载Landsat 8数据 collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR') # 定义云掩码算法 def maskL8sr(image): # 提取像元的云掩码位 cloudBitMask = ee.Number(2).pow(3).int() # 根据云掩码位生成云掩码 qa = image.select('pixel_qa') mask = qa.bitwiseAnd(cloudBitMask).eq(0) # 应用云掩码 return image.updateMask(mask) # 对图像合应用云掩码 collection = collection.map(maskL8sr) ``` 3. 使用Earth Engine API中的矢量化算法(Vectorization)将处理后的图像转换为矢量数据。可以使用ee.Image.reduceToVectors()函数进行矢量化,然后使用ee.FeatureCollection()函数将结果转换为矢量数据。例如: ```python # 定义矢量化参数 vectorParams = { 'geometryType': 'polygon', 'eightConnected': False, 'reducePixels': ee.Reducer.mean(), 'scale': 30 } # 对图像进行矢量化 vectors = collection.reduceToVectors(**vectorParams) # 将结果转换为矢量数据 fc = ee.FeatureCollection(vectors) ``` 通过以上步骤,就可以在geemap中进行矢量数据的去云处理,并将结果转换为矢量数据。需要注意的是,这里使用的是Earth Engine API的算法,因此需要先在geemap中进行Earth Engine API的身份验证。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值