Python 使用gdal优化读取大型光栅

Python 使用gdal优化读取大型光栅,python,raster,gdal,Python,Raster,Gdal,我是python/Gdal的新手。我尝试计算从一个点到一个大光栅的每个单元的距离(10000x1000)。 我使用python/gdal成功地完成了以下代码,但速度非常慢(大约18分钟) 我也尝试过使用numpy数组进行top-do,但出现内存错误。 如果可能的话,我非常感谢您的帮助 这是我的密码: raster = gdal.Open('/raster.tif') rows = raster.RasterXSize cols = raster.RasterYSize band = raste

我是python/Gdal的新手。我尝试计算从一个点到一个大光栅的每个单元的距离(10000x1000)。
我使用python/gdal成功地完成了以下代码,但速度非常慢(大约18分钟)
我也尝试过使用numpy数组进行top-do,但出现内存错误。
如果可能的话,我非常感谢您的帮助

这是我的密码:

raster = gdal.Open('/raster.tif')

rows = raster.RasterXSize
cols = raster.RasterYSize
band = raster.GetRasterBand(1)

geotransform = raster.GetGeoTransform()
xUpperleft = geotransform[0]
yUpperLeft = geotransform[3]
pixelSizex = geotransform[1]
pixelSizey = geotransform[5]

data = band.ReadAsArray(0,0,rows,cols)

for x in range(0, rows):
    for y in range(0, cols):
        #xp,yp centroid coordinates for each cell
        #Xreference,YReference are the coordiantes to calculate the distance to each cell grid. 
        xp = y * pixelSizex + xUpperleft + (pixelSizex / 2) #add half the cell size
        yp = x * pixelSizey + yUpperLeft + (pixelSizey / 2) #to centre the point
        distance = math.sqrt((xp-xReference)**2 + (yp-yReference)**2)

        data[x,y]=distance
非常感谢。

帕什

我看到在您的代码中,您将数据读取为数组,然后在for循环的末尾将其设置为距离值

    data = band.ReadAsArray(0,0,rows,cols)
    for x in range(0, rows):
        for y in range(0, cols):
            ...
             data[x,y]=distance
我不知道以后如何处理这些数据,但是试着将这个距离值放入字典中进行进一步处理,甚至循环它以保存一个新的光栅,而不是在原地更改numpy数组

比如:


最慢的部分是什么?读取光栅还是迭代for循环?由于您没有修改循环中的任何共享数据,因此这是多线程处理的一个很好的候选者。否则,给定的文件大小可能会限制您的选项,因为当您通过SWIG进行C++到Python转换时,文件IO可能没有优化,因为最慢的部分是在for循环上的迭代。你有多线程的例子吗?谢谢你的回答。
    data = {{}}
    for x in range(0, rows):
        for y in range(0, cols):
         ...
            data[x][y]=distance