numpy tile(瓦片)函数用法

numpy.tile 函数详解
本文详细介绍了 numpy 中的 tile 函数及其使用方法。该函数能够将输入数组沿着各个轴进行重复,通过 reps 参数指定各维度上的复制次数。文章通过具体实例展示了如何利用 tile 函数实现数组的扩展。

tile,顾名思义瓦片的意思,它的函数功能也是这样。

numpy.tile(A, reps)

Parameters: 
    A : array_like
        The input array.
    reps : array_like
        The number of repetitions of A along each axis.

也就是,A是一个迭代器,就把它当成原始瓦片吧,reps数组从后向前依次指瓦片从低维到高维复制次数。如tile(A,(1,2,3))j就是说A从低位到高位依次复制3,2,1次。
例子:

A=[1,2] 
1. tile(A,2) 
结果:[1,2,1,2] 
2. tile(A,(2,3)) 
结果:[[1,2,1,2,1,2], [1,2,1,2,1,2]] 
3. tile(A,(2,2,3)) 
结果:[[[1,2,1,2,1,2], [1,2,1,2,1,2]], 
[[1,2,1,2,1,2], [1,2,1,2,1,2]]]
import os import sqlite3 import mercantile import rasterio from rasterio.warp import calculate_default_transform, reproject, Resampling from PIL import Image from tqdm import tqdm INPUT_MB = "1-17电子地图_Level_1-17.mbtiles" OUTPUT_MB = "output_wgs84.mbtiles" try: import cupy as cp gpu = True print("✅ GPU 加速启用 (cuPy + CUDA)") except: import numpy as cp gpu = False print("⚠ cuPy 未安装,使用 CPU 模式执行(较慢)") def open_mbtiles(path): """打开 mbtiles 文件""" return sqlite3.connect(path) def create_output_mbtiles(path): """创建输出 mbtiles 文件""" if os.path.exists(path): os.remove(path) # 如果文件已经存在,先删除 db = sqlite3.connect(path) db.execute(""" CREATE TABLE tiles ( zoom_level INTEGER, tile_column INTEGER, tile_row INTEGER, tile_data BLOB ); """) db.commit() return db def reproject_tile(img, z, x, y): """重投影单个瓦片""" bounds = mercantile.bounds(x, y, z) src_transform = rasterio.transform.from_bounds(bounds.west, bounds.south, bounds.east, bounds.north, img.width, img.height) transform, width, height = calculate_default_transform( "EPSG:3857", "EPSG:4326", img.width, img.height, bounds.west, bounds.south, bounds.east, bounds.north ) arr = cp.asarray(img) dst = cp.zeros_like(arr) reproject( arr, dst, src_transform=src_transform, src_crs="EPSG:3857", dst_transform=transform, dst_crs="EPSG:4326", resampling=Resampling.bilinear ) return Image.fromarray(cp.asnumpy(dst)) def convert(): """主转换函数""" src = open_mbtiles(INPUT_MB) dst = create_output_mbtiles(OUTPUT_MB) rows = src.execute("SELECT zoom_level, tile_column, tile_row, tile_data FROM tiles") for z, x, y, blob in tqdm(rows, desc="重投影中"): img = Image.open(bytes(blob)) # 读取瓦片 new_img = reproject_tile(img, z, x, y) # 重投影 dst.execute("INSERT INTO tiles VALUES (?, ?, ?, ?)", (z, x, y, new_img.tobytes())) # 保存瓦片 dst.commit() # 提交更改 src.close() # 关闭源数据库 dst.close() # 关闭目标数据库 print("\n✅ 转换完成 →", OUTPUT_MB) if __name__ == "__main__": convert() 这是我的代码,帮我检查一下为什么会出现那样的报错
最新发布
11-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值