rasterFunc.py 文件源码

python
阅读 16 收藏 0 点赞 0 评论 0

项目:chorospy 作者: spyrostheodoridis 项目源码 文件源码
def getRasterValues(indir, rasterfileList, skipNoData = True):

    for i, rs in enumerate(rasterfileList):

        if i == 0:
            vList = []
            gdata = gdal.Open('{}/{}.tif'.format(indir,rs))
            gt = gdata.GetGeoTransform()
            band = gdata.GetRasterBand(1)
            nodata = band.GetNoDataValue()
            data = band.ReadAsArray().astype(numpy.float)
            #free memory
            del gdata

            x0, y0 , w , h = gt[0], gt[3], gt[1], gt[5]

            for r, row in enumerate(data):
                x = 0
                for c, column in enumerate(row):
                    if skipNoData == True:
                        if column == nodata:
                            pass
                        else:
                            x = x0 + c*w + w/2
                            y = y0 + r*h + h/2
                            vList.append(['{:.6f}'.format(x),'{:.6f}'.format(y),column])
                    elif skipNoData == False:
                        x = x0 + c*w + w/2
                        y = y0 + r*h + h/2
                        vList.append(['{:.6f}'.format(x),'{:.6f}'.format(y),column])

            df = pandas.DataFrame(vList, columns=['Xc', 'Yc', rs])

        else:
            gdata = gdal.Open('{}/{}.tif'.format(indir,rs))
            gt = gdata.GetGeoTransform()
            band = gdata.GetRasterBand(1)
            nodata = band.GetNoDataValue()
            data = band.ReadAsArray().astype(numpy.float)
            #free memory
            del gdata
            if skipNoData == True: 
                vList = [c for r in data for c in r if c != nodata]
            elif skipNoData == False:
                vList = [c for r in data for c in r]

            df[rs] = pandas.Series(vList)

    del data, band       
    return(df)


# geo raster to numpy array
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号