rio-tiler插件,用于从USGS Topo Quads读取墨卡托瓦片
项目描述
usgs-topo-tiler
Python包,用于从USGS历史地形图读取Web Mercator地图瓦片,以及创建从这些地图中MosaicJSON集合的实用程序。
大峡谷周围USGS历史地图的拼贴,增加了地形阴影。
概述
我偶然发现了一个隐藏的宝藏:整个USGS历史地形图档案,包括从1884年到2006年间创建的183,112张数字化地图,存储在AWS S3上的云优化GeoTIFF (COG) 格式。
地图可公开访问且以COG格式存储的事实意味着您可以在AWS Lambda上轻松且低成本地设置无服务器函数以动态提供地图瓦片。
COG格式 是一种向后兼容的、云本地的栅格文件存储格式,允许在网络中读取文件的选择部分,而无需下载和解析整个文件。这种快速随机读取访问允许按需动态瓦片化地图瓦片,而无需预处理和存储任何地图数据。
提供自己的瓦片有三个部分
usgs-topo-tiler
:一个库,用于从单个来源历史地图中提取单个Web Mercator瓦片。usgs-topo-tiler
的命令行界面,它帮助构建MosaicJSON文件。这些文件告诉usgs-topo-mosaic
哪些源文件应该被组合在一起创建一个单一的Web Mercator瓦片。usgs-topo-mosaic
:一个库,用于使用上面创建的MosaicJSON文件创建无缝瓦片拼贴。它旨在与AWS Lambda和AWS API Gateway一起部署,作为无服务器瓦片端点。
生成Web Mercator瓦片
安装
pip install usgs-topo-tiler 'rio-tiler>=2.0a6'
用法
在这里,我将快速概述从单个USGS历史地图中读取单个mercator瓦片的过程。如果您正在寻找特定的地图,最简单的方法可能是使用国家地图查看器。勾选“历史地形图”选项,确保文件格式为“GeoTIFF”。点击“查找产品”,然后右键单击“下载”以获取S3上的GeoTIFF的HTTPS URL。
对于这个演示,我将使用1897年约塞米蒂山谷的地形图创建mercator瓦片。
从互联网上的文件读取瓦片
from usgs_topo_tiler import tile
url = 'https://prd-tnm.s3.amazonaws.com/StagedProducts/Maps/HistoricalTopo/GeoTIFF/CA/CA_Yosemite_299696_1897_125000_geo.tif'
# Mercator tile
x, y, z = 687, 1583, 12
tile, mask = tile(url, x, y, z, tilesize=512)
print(tile.shape)
# (3, 512, 512)
print(mask.shape)
# (512, 512)
从瓦片创建图像
注意,如果您正在使用rio-tiler
v1,应将render
替换为array_to_image
。
from rio_tiler.utils import render
buffer = render(tile, mask, img_format='png')
将图像写入文件
with open('yose_1897.png', 'wb') as f:
f.write(buffer)
现在您有一个512x512的png图像,与Web Mercator网格对齐,并且由于源是云优化的GeoTIFF,图像是通过最少请求源创建的,并且没有读取整个GeoTIF。
创建MosaicJSON
上面描述的过程是创建一个瓦片。但通常我们想将许多mercator瓦片连接起来创建一个无缝的Web地图。这就是MosaicJSON发挥作用的地方。它描述了如何连接源和它们的位置。
本节概述了如何从USGS历史地形资产中创建MosaicJSON。然后可以使用usgs-topo-mosaic
与AWS Lambda一起使用此MosaicJSON提供瓦片Web地图。
安装
当您安装usgs-topo-tiler
时,它不包括CLI命令所需的依赖项,以便在使用usgs-topo-mosaic
时保持部署大小小。
要安装额外的CLI依赖项,请运行
pip install 'usgs-topo-tiler[cli]'
下载批量元数据
首先,您需要下载至少包括每个地图的地理空间边界和每个地图的URL的元数据。可以使用国家地图API检索此类元数据,但个人更喜欢从S3下载大量元数据的CSV文件。此文件包括其收藏中每个图像的元数据。
mkdir -p data
wget https://prd-tnm.s3-us-west-2.amazonaws.com/StagedProducts/Maps/Metadata/topomaps_all.zip -P data/
unzip topomaps_all.zip
data/topomaps_all.csv
是提取的批量元数据文件。data/readme.txt
包含有关批量元数据文件中字段的有用信息。
下载COG文件列表
有时元数据中列出的一些文件作为GeoTIFF不存在。我下载了S3上的.tif
文件列表,以便我可以与元数据交叉引用并确保只包含MosaicJSON中的存在文件。
此步骤是可选的,但建议使用。
mkdir -p data/
usgs-topo-tiler list-s3 \
--bucket 'prd-tnm' \
--prefix 'StagedProducts/Maps/HistoricalTopo/GeoTIFF/' \
--ext '.tif' \
> data/geotiff_files.txt
> wc -l data/geotiff_files.txt
183112 data/geotiff_files.txt
183112个COG文件!
创建MosaicJSON
现在您已准备好开始创建拼贴。这并不完全简单,因为地图是在不同的时间、不同比例的不同美国地区创建的。因此,通常不简单地将单个比例的所有地图拼接到一起,除非您接受拼贴在空间上有间隙,瓦片可能返回空数据。
足迹
查看元数据过滤查询结果的最佳方法之一是生成足迹并在地图上显示它们。
假设我们想要创建中比例地图的拼贴。我们可以使用--filter-only
CLI标志导出查询的换行分隔GeoJSON。
usgs-topo-tiler mosaic-bulk \
--meta-path data/topomaps_all.csv \
--s3-list-path data/geotiff_files.txt \
--min-scale 63360 \
--max-scale 249000 \
--filter-only \
> mid_scale_footprints.geojsonl
然后我们可以可视化这些数据,例如使用我的命令行界面(CLI)来处理kepler.gl。
kepler mid_scale_footprints.geojsonl
这说明了在制作 MosaicJSON 时这些历史地图的核心问题。一些区域被映射得比其他区域更多,而一些区域在这个比例范围内从未被映射。如果您从这些参数创建 MosaicJSON,则在请求蒙大拿州北部和德克萨斯州西部的数据时,您将得到空图像。
生成 MosaicJSON
一旦您知道查询的所需参数,请移除 --filter-only
标志以生成 MosaicJSON。例如
usgs-topo-tiler mosaic-bulk \
--meta-path data/topomaps_all.csv \
--s3-list-path data/geotiff_files.txt \
--min-scale 63360 \
--max-scale 249000 \
> mid_scale_mosaic.json
mid_scale_mosaic.json
现在是一个可以与 usgs-topo-mosaic
一起使用以渲染网络地图的 MosaicJSON 文件。请注意,然而,这使用的是自定义资产字符串格式,如移除地图袖口中所述,并且可能不会与所有其他 MosaicJSON 工具一起使用。
示例
低缩放,最新可用
usgs-topo-tiler mosaic-bulk \
--meta-path data/topomaps_all.csv \
--s3-list-path data/geotiff_files.txt \
--min-scale 250000 \
> mosaic_low.json
中等缩放,最新可用,在必要时用较低分辨率的地图填充
usgs-topo-tiler mosaic-bulk \
--meta-path data/topomaps_all.csv \
--s3-list-path data/geotiff_files.txt \
--min-scale 63360 \
> mosaic_medium.json
中等缩放,最旧可用,在必要时用较低分辨率的地图填充
usgs-topo-tiler mosaic-bulk \
--meta-path data/topomaps_all.csv \
--s3-list-path data/geotiff_files.txt \
--min-scale 63360 \
--sort-preference oldest \
> mosaic_medium_oldest.json
高缩放,最新可用,仅限美国大陆
usgs-topo-tiler mosaic-bulk \
--meta-path data/topomaps_all.csv \
--s3-list-path data/geotiff_files.txt \
--min-scale 24000 \
--max-scale 63359 \
`# Lower 48 states only` \
--bounds '-161.96,12.85,-55.01,50.53' \
> mosaic_high.json
API
Usage: usgs-topo-tiler mosaic-bulk [OPTIONS]
Create MosaicJSON from CSV of bulk metadata
Options:
--meta-path PATH Path to csv file of USGS bulk metadata dump
from S3 [required]
--s3-list-path PATH Path to txt file of list of s3 GeoTIFF files
--min-scale FLOAT Minimum map scale, inclusive
--max-scale FLOAT Maximum map scale, inclusive
--min-year FLOAT Minimum map year, inclusive
--max-year FLOAT Maximum map year, inclusive
--woodland-tint / --no-woodland-tint
Filter on woodland tint or no woodland tint.
By default no filtering is applied.
--allow-orthophoto Allow orthophoto [default: False]
--bounds TEXT Bounding box for mosaic. Must be of format
"minx,miny,maxx,maxy"
-z, --minzoom INTEGER Force mosaic minzoom
-Z, --maxzoom INTEGER Force mosaic maxzoom
--quadkey-zoom INTEGER Force mosaic quadkey zoom
--sort-preference [newest|oldest|closest-to-year]
Method for choosing assets within a given
mercator tile at the quadkey zoom.
[default: newest]
--closest-to-year INTEGER Year used for comparisons when preference is
closest-to-year.
--filter-only Output filtered GeoJSON features, without
creating the MosaicJSON. Useful for
inspecting the footprints [default: False]
--help Show this message and exit.
补充说明
移除地图袖口
所有美国地质调查局的历史地图都有袖口,这是地图周围的打印元数据的区域。为了从这些地图的集合中创建连续的地图瓦片,这些袖口必须被裁剪,以便只显示地图。
这些地图是地理参考的,这意味着当您知道地图中包含的实际界限时,移除袖口是直接的。然而,我发现没有可靠的方法仅通过图像及其文件名来确定界限。
在预先构建镶嵌图时,您可以访问这些信息,但在常规瓦片设置中,您在瓦片时只能访问 URL 和图像。
为了解决这个问题,我对 MosaicJSON 格式应用了一个“黑客”技术。我不只是编码一个 URL 字符串,而是编码 URL 以及地图的界限作为 JSON 字符串。
总结:当您使用此库中的 CLI 构建镶嵌图时,它编码了一个非标准的 MosaicJSON,它与 usgs-mosaic-tiler
瓦片器配合得很好,但其他 MosaicJSON 工具可能无法读取。
项目详情
usgs-topo-tiler-0.2.0.tar.gz 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 8c391497094bba5544cd187696dc13bd8438d91b27424cf6786859352ca10e8a |
|
MD5 | 852782d6867b8d13a3255cf49b09dc94 |
|
BLAKE2b-256 | 1d05f99ca9d89084b58a0614ddd3a0a96046467a6991c3494d501f5bf981ee55 |