为了账号安全,请及时绑定邮箱和手机立即绑定

gdal WriteArray() 在没有堆栈跟踪的情况下使 python 崩溃

gdal WriteArray() 在没有堆栈跟踪的情况下使 python 崩溃

当年话下 2021-11-23 16:45:37
我正在尝试使用 gdal 将数组写入 geotiff。数组的每一行都是相同的,我使用 np.broadcast_to 来创建数组。当我尝试编写它时,我收到一个 Windows 弹出窗口,上面写着“Python 已停止工作:一个问题导致程序停止正常工作。请关闭程序”这近似于我正在采取的步骤:import gdalimport numpy as npdriver = gdal.GetDriverByName('GTiff')outRaster = driver.Create("C:/raster.tif", 1000, 1000, 1, 6)band = outRaster.GetRasterBand(1)# Create  array a = np.arange(0,1000, dtype='float32')a1 = np.broadcast_to(a, (1000,1000))# try writingband.WriteArray(a1) # crash
查看完整描述

1 回答

?
萧十郎

TA贡献1815条经验 获得超13个赞

问题是由 broadcast_to 创建的输入数组在磁盘上不连续。如numpy 文档中所述,多个元素数组可能指向相同的内存地址。这会导致 gdal 出现问题。


不要使用broadcast_to,而是使用将每个元素存储为内存中自己位置的东西。作为说明性示例,请参见以下代码:


import gdal

import numpy as np

import sys


driver = gdal.GetDriverByName('GTiff')

outRaster = driver.Create("C:/raster.tif", 1000, 1000, 1, 6)

band = outRaster.GetRasterBand(1)


# Create 1000 x 1000 array two different ways

a = np.arange(0,1000, dtype='float32')

a1 = a[np.newaxis, :]

a1 = a1.repeat(1000, axis=0)


a2 = np.broadcast_to(a, (1000,1000))


# examine size of objects

sys.getsizeof(a1) # 4000112

sys.getsizeof(a2) # 112


# try writing

band.WriteArray(a1) # writes fine

band.WriteArray(a2) # crash


查看完整回答
反对 回复 2021-11-23
  • 1 回答
  • 0 关注
  • 324 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信