def check_input_image_and_label(image_path, label_path):
    """
    check the input image and label, they should have same width, height, and projection
    :param image_path: the path of image
    :param label_path: the path of label
    :return: (width, height) of image if successful, Otherwise (None, None).
    """

    img_obj = RSImageclass()
    if img_obj.open(image_path) is False:
        assert False
    label_obj = RSImageclass()
    if label_obj.open(label_path) is False:
        assert False
    # check width and height
    width = img_obj.GetWidth()
    height = img_obj.GetHeight()
    if width != label_obj.GetWidth() or height != label_obj.GetHeight():
        basic.outputlogMessage("Error, not the same width and height of image (%s) and label (%s)"%(image_path,label_path))
        assert False

    # check resolution
    if img_obj.GetXresolution() != label_obj.GetXresolution() or img_obj.GetYresolution() != label_obj.GetYresolution():
        basic.outputlogMessage(
            "Error, not the same resolution of image (%s) and label (%s)" % (image_path, label_path))
        assert False

    # check projection
    if img_obj.GetProjection() != label_obj.GetProjection():
        basic.outputlogMessage(
            "warning, not the same projection of image (%s) and label (%s)" % (image_path, label_path))
    #     assert False

    return (width, height)
Esempio n. 2
0
def save_oneband_list(save_path, ndvi_list, band_name_list, org_img):
    '''
    save ndvi of multiple images to a file
    :param save_path:
    :param ndvi_list:
    :param band_name_list:
    :param org_img:
    :return:
    '''
    if len(ndvi_list) < 1 or len(band_name_list) < 1:
        raise ValueError('the input of ndvi or name list is empty')

    if len(ndvi_list) != len(band_name_list):
        raise ValueError(
            'the length of ndvi list and band name list is different')

    rsImg_org = RSImageclass()
    rsImg_org.open(org_img)
    org_width = rsImg_org.GetWidth()
    org_height = rsImg_org.GetHeight()
    datetype = rsImg_org.GetGDALDataType()
    prj = rsImg_org.GetProjection()
    geo_tran = rsImg_org.GetGeoTransform()

    save_bandcount = len(ndvi_list)

    rsImg_obj = RSImageclass()
    if rsImg_obj.New(save_path, org_width, org_height, save_bandcount,
                     6):  # 6 for float 32
        for idx in range(save_bandcount):
            height, width = ndvi_list[idx].shape

            ndvi_str = ndvi_list[idx].tobytes(
            )  # struct.pack('%f' % width * height, *templist)
            if rsImg_obj.WritebandData(idx + 1, 0, 0, width, height, ndvi_str,
                                       6):
                rsImg_obj.set_band_name(idx + 1, band_name_list[idx])

        # set projection and transform
        rsImg_obj.SetProjection(prj)
        rsImg_obj.SetGeoTransform(geo_tran)