cuProj Python 用户指南#
cuProj 是一个 GPU 加速的 Python 库,用于地理坐标投影和坐标参考系统 (CRS) 之间的坐标转换。cuProj Python API 提供了一个易于访问的接口,可利用支持 CUDA 的 GPU 加速高性能投影。该 API 紧密遵循 PyProj API。
目录#
安装 cuProj#
阅读 RAPIDS 快速入门指南,了解如何安装所有 RAPIDS 库,包括 cuProj。
如果您在配有支持 CUDA 的 GPU 并已安装 CUDA 的系统上工作,请取消注释以下单元格并安装 cuSpatial
[1]:
# !conda create -n rapids-25.04 --solver=libmamba -c rapidsai -c conda-forge -c nvidia \
# cuproj-23.12 python=3.10 cuda-version=12.0
我们欢迎对 cuProj 做出贡献。为此,首先使用随附的 开发容器 (Dev Container) 创建一个源构建。只需克隆 github 仓库并在 VSCode 中打开文件夹。如果未安装,VSCode 将提示您安装 开发容器扩展 (Dev Container extension),然后在一个开发容器中打开文件夹。
使用 Transformer 进行转换#
cuProj 中的主要类是 Transformer
类,用于将坐标从一个 CRS 转换为另一个 CRS。Transformer
类通过指定源 CRS 和目标 CRS 创建,可以通过 CRS 字符串、EPSG 代码或 (<authority>, code)
元组来指定。然后,Transformer
类即可用于将坐标从源 CRS 转换为目标 CRS。
目前仅支持 EPSG 授权机构,并且仅支持 EPSG 代码的子集。支持的 EPSG 代码如下:
WGS84 (EPSG:4326)
UTM (EPSG:32600-32660 和 EPSG:32700-32760)
以下简单示例将单个 (lat, lon) 坐标从 WGS84 转换为 UTM。
[1]:
from cuproj.transformer import Transformer
# Tower of London latitude and longitude
lat = 51.5081
lon = -0.0761
# Transform to UTM (x, y) in meters using CuProj
cu_transformer = Transformer.from_crs("epsg:4326", "EPSG:32630")
x, y = cu_transformer.transform(lat, lon)
print(f"WGS84 (lat,lon): ({lat:.2f}, {lon:.2f}) degrees")
print(f"UTM Zone 30N (x,y): ({x:.2f}, {y:.2f}) meters")
WGS84 (lat,lon): (51.51, -0.08) degrees
UTM Zone 30N (x,y): (702900.15, 5710383.71) meters
转换坐标数组#
当您需要转换大量点时,cuProj 的优势尤为突出。以下代码转换了澳大利亚悉尼周围网格中的 10,000 个 (lat, lon) 点。
[2]:
import cupy as cp
# (lat, lon) box around Sydney, NSW, Australia
min_corner = (-34.2, 150.5)
max_corner = (-33.5, 151.5)
crs_to = "EPSG:32756"
num_points_x = 100
num_points_y = 100
# A grid of 100x100 points in the bounding box of London in WGS84 (lat/lon)
# stored as a list of two arrays (x, y) in device memory (cupy)
x, y = cp.meshgrid(
cp.linspace(min_corner[0], max_corner[0], num_points_y),
cp.linspace(min_corner[1], max_corner[1], num_points_x))
grid = [x.reshape(-1), y.reshape(-1)]
transformer = Transformer.from_crs("EPSG:4326", crs_to)
x, y = transformer.transform(*grid)
print(f"min_corner in UTM zone 56S: ({x[0]}, {y[0]}) in meters")
print(f"max_corner in UTM zone 56S: ({x[-1]}, {y[-1]}) in meters")
min_corner in UTM zone 56S: (269645.77400353167, 6212842.207954117) in meters
max_corner in UTM zone 56S: (360665.66806726344, 6292273.972689628) in meters