如需了解如何导出为其他格式(包括图片、视频和表格 [矢量数据]),请参阅导出数据。
概览
如需在在线地图上显示图片数据(光栅数据),您可以使用 Export.map.toCloudStorage() 导出地图图块。此函数会将地图图块金字塔导出到 Cloud Storage 存储桶,以便从该存储桶公开或私下提供。当您的图片数据太大而无法作为单个图片叠加时,此功能非常有用。这些图块使用 Google 地图图块坐标,适合使用 Google Maps Platform(地图 API)、Google 地球网页版和其他地图平台进行显示。您可以使用导出内容中提供的 HTML 文件,在 Google 地图或 Google 地球中轻松预览这些图块。
导出过程涉及许多设置和步骤,这些设置和步骤可能必要,也可能不必要,具体取决于您打算如何使用或发布 3D 图像。 某些地图绘制平台要求图块可公开访问,而某些平台则支持私有图块。如果使用某个 Google Maps API,您可能需要拥有 Google Maps Platform API 密钥。如果您要向 Google 地球或其他地图平台提供图块,则可能需要设置适当的访问权限级别和 JS 访问权限 (CORS) 选项,具体取决于您打算如何使用图块。如需了解所需设置,请参阅下文中的平台专用指南部分。
将地图图块导出到 Cloud Storage
以下示例从 Landsat 影像中导出加利福尼亚州某个区域的图块。
代码编辑器 (JavaScript)
// --- Example Export Map Tiles - basic --- // Specify area to clip/export, setup image and preview on map. var exportRegion = ee.Geometry.BBox(-122.9, 37.1, -121.2, 38.2); var landsatImage = ee.Image('LANDSAT/LC09/C02/T1_TOA/LC09_044034_20220111') .select(['B4', 'B3', 'B2']) .visualize({min: 0.02, max: 0.4, gamma: 1.2}) .clip(exportRegion); Map.addLayer(landsatImage, {}, 'landsatImage'); Map.centerObject(exportRegion); // Set up Export task. Export.map.toCloudStorage({ image: landsatImage, description: 'mapTilesForEE', bucket: 'yourBucketName', // replace with your GCS bucket name fileFormat: 'auto', maxZoom: 13, region: exportRegion, writePublicTiles: true });
import ee import geemap.core as geemap
Colab (Python)
# --- Example Export Map Tiles - basic --- # Specify area to clip/export, setup image and preview on map. export_region = ee.Geometry.BBox(-122.9, 37.1, -121.2, 38.2) landsat_image = ( ee.Image('LANDSAT/LC09/C02/T1_TOA/LC09_044034_20220111') .select(['B4', 'B3', 'B2']) .visualize(min=0.02, max=0.4, gamma=1.2) .clip(export_region) ) m = geemap.Map() m.add_layer(landsat_image, {}, 'landsatImage') m.center_object(export_region) display(m) # Set up Export task. task = ee.batch.Export.map.toCloudStorage( image=landsat_image, description='mapTilesForEE', bucket='yourBucketName', # replace with your GCS bucket name fileFormat='auto', maxZoom=13, region=export_region, writePublicTiles=True, ) task.start()
请注意,这会在指定的 Cloud Storage 存储桶中创建一个新目录,默认情况下,该目录会根据 description 参数命名。您可以使用 path 参数更改目录名称或指定子目录。您可以使用输出目录中的 index.html 和 earth.html 文件在 Google 地图或 Google 地球中预览图块。
ACL 和 CORS 选项
上面的示例设置了图块的 ACL(访问控制列表),以便通过直接请求公开访问这些图块,但未设置存储桶的 JS 访问权限(CORS 设置)以允许 Web 应用访问这些图块。
这意味着,这些导出的图块可以在地图和 Earth 预览中查看,但对需要 JS 访问权限的其他平台没有用。系统会假定您是输出存储桶的所有者。
如果您是指定输出存储桶的写入者(但不是所有者),请将 writePublicTiles 设置为 false,以使用输出存储桶的默认对象 ACL。
如果您希望向所有 Web 应用公开您的图块,请将 bucketCorsUris: ['*'] 添加到任务参数中。请注意,如果存在第三方使用您的图块并产生云费用的风险,则可能不希望提供完整的公开 JS 访问权限。为限制这种情况,您可以将 JS 访问权限限制为仅允许特定网域或网址上的 Web 应用访问。请注意,JS 访问权限 (CORS) 设置是在整个存储桶级别应用的,而不是在文件夹或对象级别应用的。
您可以使用 writePublicTiles 和 bucketCorsUris 参数分别设置 ACL 和 CORS,也可以通过导出任务对话框进行设置,如下所示:
Export.map.toCloudStorage() 任务对话框。
- 查看/下载权限。如果您希望获得任何访问权限(例如,通过代码编辑器或 Earth Engine 应用),则需要将图块设为“公开访问”。
- JS 访问权限。如果您希望代码编辑器、Earth Engine 应用或其他 Web 应用能够访问这些图块,则导出地图的目标存储桶必须允许读取。这些非同源请求设置 (CORS) 必须在存储桶级层进行配置。为方便起见,您可以在导出时通过 Earth Engine 导出对话框为存储桶配置 CORS。
- 注意:JS 访问/CORS 设置在存储桶级别应用,而不是在文件夹或对象级别应用,因此更改设置会更新存储桶中所有文件夹和对象的设置。如需了解详情,请参阅 Cloud Storage CORS 文档,包括如何查看存储桶的当前 CORS 配置。
参数列表
下面列出了地图图块导出任务的所有参数,其中显示了代码中的参数(实参)名称以及任务对话框中的相应字段(如果适用)。请注意,某些字段是互斥的(例如 maxzoom 和 scale)。如需了解详情,请参阅 Export.map 文档。
| 代码中的参数 | 任务对话框中的字段 | 备注 |
|---|---|---|
| 图片 | 无 | 要导出为图块的图片。必须是 Image,而不是 ImageCollection。 |
| 说明 | 任务名称 | 任务的人类可读名称。默认值为“myExportMapTask”。 如果未指定“path”(输出前缀)参数,此参数也会用作图块的文件夹名称。 |
| 存储桶 | GCS 存储桶名称 | 要写入的目标存储桶。 |
| fileFormat | 文件格式 | 地图图块的文件格式,可以是“auto”“png”或“jpg”。 默认值为“auto”,这意味着不透明图块将编码为“jpg”,而透明图块将编码为“png”。 |
| 路径 | 输出前缀 | 用作输出路径的字符串。尾随“/”是可选的。 默认为任务的说明(任务名称) |
| writePublicTiles | 查看/下载访问权限级别 | 是否写入公开图块,而不是使用存储桶的默认对象 ACL。默认值为 true,调用方为存储桶的 OWNER。 |
| maxZoom | 最大分辨率和最大变焦 | 要导出的地图图块的最大缩放级别。 |
| 和 WAF | 最大分辨率和最大缩放比例 | 最大图片分辨率(以米/像素为单位),可替代“maxZoom”。比例将转换为赤道处最合适的缩放级别上限。 |
| minZoom | 最高分辨率和最小缩放比例 | 要导出的地图图块的可选最小缩放级别。 默认值为 0。 |
| 区域 | 无 | 表示要导出的区域的 LinearRing、Polygon 或坐标。 如需了解详情,请参阅文档。 |
| skipEmptyTiles | 无 | 如果为 true,则跳过写入空白(即完全透明)地图图块。 默认值为 false。 |
| mapsApiKey | 地图 API 密钥 | 在 index.html 中用于初始化 Google 地图 API。这会从地图中移除“仅用于开发目的”消息。 |
| bucketCorsUris | 添加 JS 访问权限(和要允许的网站) | 允许通过 JavaScript (CORS) 检索导出图块的网域(例如 https://code.earthengine.google.com)列表。 |
缩放
在前面的示例中,maxZoom 设置为 13。
缩放级别对应于不同大小的像素网格,用于显示全球地图。(如需了解详情,请参阅像素坐标参考文档。)由于地球曲率,在给定缩放级别下,像素的分辨率因纬度而异。具体而言,每像素米数会按 cos(latitude) 的系数递减。下表显示了 Google 墨卡托投影在每个缩放级别下赤道上的每像素米数:
| 缩放级别 | 像素大小(赤道处) | 缩放级别 | 像素大小(赤道处) |
|---|---|---|---|
| 0 | 156 公里 | 10 | 152 米 |
| 1 | 78 公里 | 11 | 76 米 |
| 2 | 39 公里 | 12 | 38 米 |
| 3 | 20 公里 | 13 | 19 米 |
| 4 | 10 公里 | 14 | 9.6 米 |
| 5 | 4.9 公里 | 15 | 4.8 米 |
| 6 | 2.4 公里 | 16 | 2.4 米 |
| 7 | 1.2 公里 | 17 | 1.2 米 |
| 8 | 611 米 | 18 | 0.6 米 |
| 9 | 305 米 | 19 | 0.3 米 |
| 20 | 0.15 米 |
从上表中可以看出,示例中设置的 maxZoom 对应于赤道处的 19 米,在较高纬度处则更小。这低于输入图片的标称分辨率(每像素 30 弧秒)。因此,输出
index.html
中显示的地图可以放大,直到地图中显示原生分辨率的像素。如需将地图显示限制为原生像素分辨率,请将 maxZoom 设置为与原生分辨率对应的值或更低的值。
针对具体平台的准则
以下是您可能希望在其中查看和使用地图图块的多个平台。每个部分都指明了图块与该平台搭配使用所需的设置,以及包含必要参数的示例代码副本。
在 Earth Engine(代码编辑器或 EE 应用)中使用地图图块
如果您只想导出地图图块以便在 Google Earth Engine 或 EE 应用中重复使用,请将“访问权限级别”设置为“可公开访问”,并将“JS 访问权限级别”(在任务对话框中)设置为“添加 Earth Engine 访问权限”或“添加公开访问权限”。或者,您也可以在代码中设置这些参数:
writePublicTiles: true, bucketCorsUris: ['https://code.earthengine.google.com','https://*.earthengine.app']
导出图块后,您便可在 Earth Engine 中访问这些图块。
代码编辑器 (JavaScript)
// --- Export Map Tiles for use in EE --- // Use image setup code from example at top of the page. // Set up Export task. Export.map.toCloudStorage({ image: landsatImage, description: 'mapTilesForEE', bucket: 'yourBucketName', // replace with your GCS bucket name fileFormat: 'auto', maxZoom: 13, region: exportRegion, writePublicTiles: true, bucketCorsUris: ['https://code.earthengine.google.com','https://*.earthengine.app'] });
import ee import geemap.core as geemap
Colab (Python)
# --- Export Map Tiles for use in EE --- # Use image setup code from example at top of the page. # Set up Export task. task = ee.batch.Export.map.toCloudStorage( image=landsat_image, description='mapTilesForEE', bucket='yourBucketName', # replace with your GCS bucket name fileFormat='auto', maxZoom=13, region=export_region, writePublicTiles=True, bucketCorsUris=[ 'https://code.earthengine.google.com', 'https://*.earthengine.app', ], ) task.start()
查看 Google 地图预览
如需在 Google 地图上预览地图图块,请前往 Google Cloud Storage 上的输出文件夹,然后在浏览器中打开“index.html”文件。这将打开一个全页地图,其中使用 Google Maps Platform JavaScript API 在 Google 地图上显示您的图块,如下所示: index.html。基本导出操作会生成灰显且带有“仅用于开发目的”水印的底图图块。为避免出现这种情况并显示标准 Google 底图图块,请生成 API 密钥并将其添加到导出设置中。
如果您在导出时提供 API 密钥,则此预览页面可公开查看,可嵌入到其他页面中,并且不需要查看者是注册的 Earth Engine 用户。
代码编辑器 (JavaScript)
// --- Export Map Tiles for use with Map Preview --- // Use image setup code from example at top of the page. // Set up Export task. Export.map.toCloudStorage({ image: landsatImage, description: 'mapTilesForMapPreview', bucket: 'yourBucketName', // replace with your GCS bucket name fileFormat: 'auto', maxZoom: 13, region: exportRegion, writePublicTiles: true, mapsApiKey: 'fakeMapsApiKey012345' // replace with a valid API Key });
import ee import geemap.core as geemap
Colab (Python)
# --- Export Map Tiles for use with Map Preview --- # Use image setup code from example at top of the page. # Set up Export task. task = ee.batch.Export.map.toCloudStorage( image=landsat_image, description='mapTilesForMapPreview', bucket='yourBucketName', # replace with your GCS bucket name fileFormat='auto', maxZoom=13, region=export_region, writePublicTiles=True, mapsApiKey='fakeMapsApiKey012345', # replace with a valid API Key ) task.start()
查看 Google 地球预览
如需在 Google 地球网页版中预览地图图块,请确保在导出图块时将访问权限级别设置为“公开访问”。然后,前往 Google Cloud Storage 上的输出文件夹,在浏览器中打开“earth.html”文件(如下所示): earth.html 并点击显示的“在 Google 地球中打开”按钮。此操作会打开 Google 地球,并在 3D 地图上显示您的图块。最佳做法是将“JS 访问权限”级别也设置为“可公开访问”,这样您就可以在 Google 地球中使用这些图块,而无需使用预览文件(如果图块没有必要的 JS 访问权限/CORS 设置,预览文件会使用代理服务器)。
如需下载包含数据链接的 KML 文件,请点击 Google 地球中的三点状菜单,然后选择“导出为 KML 文件”。注意:以这种方式生成的 KML 文件与 Google 地球 Pro(Google 地球的桌面版)不兼容。
代码编辑器 (JavaScript)
// --- Export Map Tiles for use with Earth Preview --- // Use image setup code from example at top of the page. // Set up Export task. Export.map.toCloudStorage({ image: landsatImage, description: 'mapTilesForEarthPreview', bucket: 'yourBucketName', // replace with your GCS bucket name fileFormat: 'auto', maxZoom: 13, region: exportRegion, writePublicTiles: true, bucketCorsUris: [] // leaving blank is ok for Earth Preview only // for direct use in Earth, set to: ['https://earth.google.com'] // or set to public: ['*'] (risk of misuse) });
import ee import geemap.core as geemap
Colab (Python)
# --- Export Map Tiles for use with Earth Preview --- # Use image setup code from example at top of the page. # Set up Export task. task = ee.batch.Export.map.toCloudStorage( image=landsat_image, description='mapTilesForEarthPreview', bucket='yourBucketName', # replace with your GCS bucket name fileFormat='auto', maxZoom=13, region=export_region, writePublicTiles=True, bucketCorsUris=[], # leaving blank is ok for Earth Preview only # for direct use in Earth, set to: ['https://earth.google.com'] # or set to public: ['*'] (risk of misuse) ) task.start()
使用 Google Maps Platform(地图 API)发布
如需创建一组可通过 Google Maps Platform 公开发布的地图图块,您需要拥有或创建 API 密钥,并确保将访问权限级别设置为“可公开访问”。根据地图 API 应用访问图块的方式,您可能还需要为网站设置适当的“JS 访问权限级别”。导出图块后,代码编辑器“任务”标签页中的输出会提供图块的网址,其中包含与 Google 地图 API 搭配使用的适当变量,例如:
https://storage.googleapis.com/my_bucket/my_test_tiles/{Z}/{X}/{Y}
代码编辑器 (JavaScript)
// --- Export Map Tiles for use with Maps Platform APIs --- // Use image setup code from example at top of the page. // Set up Export task. Export.map.toCloudStorage({ image: landsatImage, description: 'mapTilesForMapsPlatform', bucket: 'yourBucketName', // replace with your GCS bucket name fileFormat: 'auto', maxZoom: 13, region: exportRegion, writePublicTiles: true, bucketCorsUris: ['*'], // '*' = All domains = risk of misuse // For better protection, specify the domain(s) where the // tiles will be used, eg: ['https://mysite.mydomain.com'] mapsApiKey: 'fakeMapsApiKey012345' // replace with a valid API Key });
import ee import geemap.core as geemap
Colab (Python)
# --- Export Map Tiles for use with Maps Platform APIs --- # Use image setup code from example at top of the page. # Set up Export task. task = ee.batch.Export.map.toCloudStorage( image=landsat_image, description='mapTilesForMapsPlatform', bucket='yourBucketName', # replace with your GCS bucket name fileFormat='auto', maxZoom=13, region=export_region, writePublicTiles=True, bucketCorsUris=['*'], # '*' = All domains = risk of misuse # For better protection, specify the domain(s) where the # tiles will be used, eg: ['https://mysite.mydomain.com'] mapsApiKey='fakeMapsApiKey012345', # replace with a valid API Key ) task.start()
发布到 Google 地球(网页版)
如需创建一组地图图块,以便通过 Google 地球公开发布,您需要将访问权限级别设置为“可公开访问”,并将 JS 访问权限级别设置为允许“https://earth.google.com”(或“可公开访问”)。导出后,您可以使用导出目录中的 earth.html 文件在 Google 地球中预览这些文件(见上文)。
代码编辑器 (JavaScript)
// --- Export Map Tiles for use with Google Earth web --- // Use image setup code from example at top of the page. // Set up Export task. Export.map.toCloudStorage({ image: landsatImage, description: 'mapTilesForEarthWeb', bucket: 'yourBucketName', // replace with your GCS bucket name fileFormat: 'auto', maxZoom: 13, region: exportRegion, writePublicTiles: true, bucketCorsUris: ['https://earth.google.com'] // ['*'] will also work, but risks misuse });
import ee import geemap.core as geemap
Colab (Python)
# --- Export Map Tiles for use with Google Earth web --- # Use image setup code from example at top of the page. # Set up Export task. task = ee.batch.Export.map.toCloudStorage( image=landsat_image, description='mapTilesForEarthWeb', bucket='yourBucketName', # replace with your GCS bucket name fileFormat='auto', maxZoom=13, region=export_region, writePublicTiles=True, bucketCorsUris=['https://earth.google.com'], # ['*'] will also work, but risks misuse ) task.start()
导出图块后,您可以将其添加到 Google 地球网页版中的项目中。代码编辑器“Tasks”(任务)标签页中的输出(如下所示)提供了图块的网址,其中包含可在 Google 地球中使用的适当变量,例如:
https://storage.googleapis.com/my_bucket/my_test_tiles/$[level]/$[x]/$[y]
如需在 Google 地球网页版中使用此功能,请创建一个图块叠加层功能,并将此网址添加为“叠加层网址”。如果您的数据集不是全球性的,并且您希望避免不存在的图块出现图块请求错误,请务必打开“叠加层选项”部分,并调整四个图块覆盖范围参数,使其与导出的图块范围相匹配。
发布到其他地图平台
导出切片以供其他平台或应用使用时,必要的设置将取决于它们访问切片的方式。为了让尽可能多的人能够访问这些图块,请将“访问权限级别”设置为“可公开访问”,并将“JS 访问权限级别”设置为“可公开访问”。
生成 Google Maps Platform API 密钥
如果您打算将地图图块与 Google Maps Platform 搭配使用,或者想在 Google 地图上预览这些图块,而不使用开发者受限底图,则需要拥有或生成 Google Maps Platform API 密钥,并将其包含在导出设置中,或在导出后将其添加到网站中。对于 Google 地球,则无需执行此操作。
当您从 Earth Engine 导出地图图块时,我们会生成一个存储在输出目录中的示例 HTML 页面,供您使用 Google Maps Platform JavaScript API 预览图块。您可以在导出时选择提供 Google Maps Platform API 密钥,该密钥将用于示例查看器的 API 调用。
如需立即生成 Google Maps Platform API 密钥,请按以下四个步骤操作:
- 点击下方的“创建 Maps Platform API 密钥”按钮。
- 选择 API 密钥所属的云项目,然后点击“下一步”。
- 点击 复制新密钥,然后将其粘贴到代码编辑器中的“导出地图”对话框中。
- (推荐)点击新密钥下方的 API 控制台链接,设置引荐来源网址限制。如需了解详情,请参阅引荐来源网址和应用限制。
为 API 密钥添加引荐来源网址限制
添加 API 密钥时,引荐来源网址限制可确保只有指定的应用可以使用您的密钥。您可以随时访问 Cloud 控制台上的凭据页面,并按照以下步骤设置或修改引荐来源网址限制:
- 确保在屏幕顶部的下拉菜单中选择了正确的项目名称。
- 点击上面生成的 Maps Platform API 密钥,查看该密钥的详细信息。
-
选择 HTTP 引荐来源网址(网站),然后使用以下模板指定
Export.map.toCloudStorage中指定的 Cloud Storage 存储桶:https://storage.googleapis.com/{bucket}/* - 点击“保存”。系统应该会显示“正在保存…”指示器。