构造Gdal Open的类

时间:2018-07-04 21:25:23

标签: python image class gdal

我是python(2.6)的新手,具有使用gdal打开geotif的简单功能。它返回图像数组和图像大小(x,y)。

def gdal_open(raster_file):

    if type(raster_file) is str and os.path.isfile(raster_file):
        gd_img = gdal.Open(raster_file)
        img_x = gd_img.RasterXSize # column
        img_y = gd_img.RasterYSize # row
        img = gd_img.ReadAsArray(0, 0, img_x, img_y)

    elif type(raster_file) is str and not os.path.isfile(raster_file):
        raise IOError

    return img, img_x, img_y

我想将此转换为类以获取img,img.x和img.y作为输出。有人可以帮我吗?

谢谢, 杰伊

1 个答案:

答案 0 :(得分:0)

这是名称为IMG的类

class IMG:

def __init__(self,path):
    if type(path)==str and os.path.isfile(path):
        self.path=path
        self.Raster=gdal.Open(path)
    elif type(path)==str and not os.path.isfile(path):
        raise IOError

def X(self):
    return self.Raster.RasterXSize

def Y(self):
    return self.Raster.RasterYSize

def RasterArray(self):
    return self.Raster.ReadAsArray()

并使用它

filepath =“ path / raster_name.tif”

raster = IMG(文件路径)

xsize = raster.X()

ysize = raster.Y()

array = raster.RasterArray()