用gd优化大光栅读数

2024-09-30 00:22:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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

非常感谢。
帕希。在


Tags: theto代码forcellxprowsdistance
1条回答
网友
1楼 · 发布于 2024-09-30 00:22:46

我看到在代码中,您将数据作为数组读取,然后在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数组。在

比如:

^{pr2}$

相关问题 更多 >

    热门问题