Esempio n. 1
0
def cal_the_mean_of_bands(image_path):
    img_obj = RSImageclass()
    if img_obj.open(image_path):
        width = img_obj.GetWidth()
        height = img_obj.GetHeight()
        mean_of_bands = RSImage.get_image_mean_value(image_path)

        if mean_of_bands is False:
            return (False,False,False)
        return (width,height,mean_of_bands)
    else:
        basic.outputlogMessage('error, Open image %s failed' %image_path)
        return (False,False,False)
Esempio n. 2
0
def cal_the_mean_of_bands(input_path):

    # if multi files in one line, then only consider the first file
    image_path = get_first_path_in_line(input_path)
    if image_path is False:
        return False

    img_obj = RSImageclass()
    if img_obj.open(image_path):
        width = img_obj.GetWidth()
        height = img_obj.GetHeight()
        mean_of_bands = RSImage.get_image_mean_value(image_path)

        if mean_of_bands is False:
            return (False,False,False)
        return (width,height,mean_of_bands)
    else:
        basic.outputlogMessage('error, Open image %s failed' %image_path)
        return (False,False,False)
Esempio n. 3
0
    def compose_two_image(self, main_image, second_image, nodata):

        if io_function.is_file_exist(main_image) is False:
            return False
        if io_function.is_file_exist(second_image) is False:
            return False
        main_img = RSImageclass()
        if main_img.open(main_image) is False:
            return False
        width_main = main_img.GetWidth()
        height_main = main_img.GetHeight()
        bandcount_main = main_img.GetBandCount()

        sec_img = RSImageclass()
        if sec_img.open(second_image) is False:
            return False
        width_sec = sec_img.GetWidth()
        height_sec = sec_img.GetHeight()
        bandcount_sec = sec_img.GetBandCount()

        if width_main != width_sec or height_main != height_sec or bandcount_main != bandcount_sec:
            basic.outputlogMessage(
                'Error: The dimension of two composed images is different')
            return False
        if main_img.GetGDALDataType() != sec_img.GetGDALDataType(
        ) or main_img.GetGDALDataType() != 6:
            basic.outputlogMessage(
                'Error: The Data type of two composed imagaes is different or is not float'
            )
            return False

        outputfile = io_function.get_name_by_adding_tail(main_image, 'comp')
        imagenew = RSImageclass()
        width = width_main
        height = height_main
        if not imagenew.New(outputfile, width, height, bandcount_main,
                            main_img.GetGDALDataType()):
            return False
        for i in range(0, bandcount_main):
            bandindex = i + 1
            band_main_str = main_img.ReadbandData(bandindex, 0, 0, width,
                                                  height,
                                                  main_img.GetGDALDataType())
            band_sec_str = sec_img.ReadbandData(bandindex, 0, 0, width, height,
                                                sec_img.GetGDALDataType())

            band_main_data = struct.unpack('f' * width * height, band_main_str)
            band_main_numpy = numpy.asarray(band_main_data)

            band_sec_data = struct.unpack('f' * width * height, band_sec_str)
            band_sec_numpy = numpy.asarray(band_sec_data)

            compose_loc = numpy.where(
                (numpy.fabs(band_main_numpy - nodata) < 0.0001)
                & (numpy.fabs(band_sec_numpy - nodata) > 0.0001))
            band_main_numpy[compose_loc] = band_sec_numpy[compose_loc]
            basic.outputlogMessage('outputfortest2: compose_loc_num = %d' %
                                   numpy.array(compose_loc).size)

            templist = band_main_numpy.tolist()
            band_composed_str = struct.pack('%sf' % width * height, *templist)
            if imagenew.WritebandData(bandindex, 0, 0, width, height,
                                      band_composed_str,
                                      imagenew.GetGDALDataType()) is False:
                return False
            imagenew.SetBandNoDataValue(bandindex, nodata)

        imagenew.SetGeoTransform(main_img.GetGeoTransform())
        imagenew.SetProjection(main_img.GetProjection())

        main_img = None
        sec_img = None
        imagenew = None

        return outputfile
Esempio n. 4
0
class RSImgProclass(object):
    def __init__(self):
        self.imgpath = ''
        self.img__obj = None  #RSImageclass  object

    def __del__(self):
        # close dataset
        # print 'RSImageclass__del__'
        self.img__obj = None

    def Read_Image_band_data_to_numpy_array_all_pixel(self, bandindex,
                                                      image_path):
        if io_function.is_file_exist(image_path) is False:
            return False
        self.img__obj = RSImageclass()
        if self.img__obj.open(image_path) is False:
            return False
        width = self.img__obj.GetWidth()
        height = self.img__obj.GetHeight()
        return self.__Read_band_data_to_numpy_array(bandindex, 0, 0, width,
                                                    height, self.img__obj)

    def __Read_Image_band_data_to_numpy_array(self, bandindex, xoff, yoff,
                                              width, height, image_path):
        return True

    def __Read_band_data_to_numpy_array(self,
                                        bandindex,
                                        xoff,
                                        yoff,
                                        width,
                                        height,
                                        image_obj=None):
        if image_obj is None:
            image_obj = self.img__obj
        offsetvaluestr = image_obj.ReadbandData(
            bandindex, xoff, yoff, width, height,
            image_obj.GetGDALDataType())  #first band offset, may be xoffset
        if offsetvaluestr is False:
            return False
        # offsetvalue = None
        # print image_obj.GetGDALDataType()
        if image_obj.GetGDALDataType() == 3:  #GDT_Int16
            offsetvalue = struct.unpack('h' * width * height, offsetvaluestr)
        elif image_obj.GetGDALDataType() == 6:
            offsetvalue = struct.unpack('f' * width * height, offsetvaluestr)
        else:
            basic.outputlogMessage('error: not support datatype currently')
            return False

        return numpy.asarray(offsetvalue)

    def statistic_element_count(self, value, myarray):
        loc_nodata = numpy.where(numpy.fabs(myarray - value) < 0.001)
        loc_nodatanum = numpy.array(loc_nodata).size
        return loc_nodatanum

    def statistic_not_element_count(self, not_value, myarray):
        loc_not_value = numpy.where(numpy.fabs(myarray - not_value) > 0.0001)
        loc_not_value_num = numpy.array(loc_not_value).size
        return loc_not_value_num

    def statistic_pixel_count(self, pixel_value, RSImageclass_object):

        return True

    def compose_two_image(self, main_image, second_image, nodata):

        if io_function.is_file_exist(main_image) is False:
            return False
        if io_function.is_file_exist(second_image) is False:
            return False
        main_img = RSImageclass()
        if main_img.open(main_image) is False:
            return False
        width_main = main_img.GetWidth()
        height_main = main_img.GetHeight()
        bandcount_main = main_img.GetBandCount()

        sec_img = RSImageclass()
        if sec_img.open(second_image) is False:
            return False
        width_sec = sec_img.GetWidth()
        height_sec = sec_img.GetHeight()
        bandcount_sec = sec_img.GetBandCount()

        if width_main != width_sec or height_main != height_sec or bandcount_main != bandcount_sec:
            basic.outputlogMessage(
                'Error: The dimension of two composed images is different')
            return False
        if main_img.GetGDALDataType() != sec_img.GetGDALDataType(
        ) or main_img.GetGDALDataType() != 6:
            basic.outputlogMessage(
                'Error: The Data type of two composed imagaes is different or is not float'
            )
            return False

        outputfile = io_function.get_name_by_adding_tail(main_image, 'comp')
        imagenew = RSImageclass()
        width = width_main
        height = height_main
        if not imagenew.New(outputfile, width, height, bandcount_main,
                            main_img.GetGDALDataType()):
            return False
        for i in range(0, bandcount_main):
            bandindex = i + 1
            band_main_str = main_img.ReadbandData(bandindex, 0, 0, width,
                                                  height,
                                                  main_img.GetGDALDataType())
            band_sec_str = sec_img.ReadbandData(bandindex, 0, 0, width, height,
                                                sec_img.GetGDALDataType())

            band_main_data = struct.unpack('f' * width * height, band_main_str)
            band_main_numpy = numpy.asarray(band_main_data)

            band_sec_data = struct.unpack('f' * width * height, band_sec_str)
            band_sec_numpy = numpy.asarray(band_sec_data)

            compose_loc = numpy.where(
                (numpy.fabs(band_main_numpy - nodata) < 0.0001)
                & (numpy.fabs(band_sec_numpy - nodata) > 0.0001))
            band_main_numpy[compose_loc] = band_sec_numpy[compose_loc]
            basic.outputlogMessage('outputfortest2: compose_loc_num = %d' %
                                   numpy.array(compose_loc).size)

            templist = band_main_numpy.tolist()
            band_composed_str = struct.pack('%sf' % width * height, *templist)
            if imagenew.WritebandData(bandindex, 0, 0, width, height,
                                      band_composed_str,
                                      imagenew.GetGDALDataType()) is False:
                return False
            imagenew.SetBandNoDataValue(bandindex, nodata)

        imagenew.SetGeoTransform(main_img.GetGeoTransform())
        imagenew.SetProjection(main_img.GetProjection())

        main_img = None
        sec_img = None
        imagenew = None

        return outputfile
Esempio n. 5
0
def setGCPsfromptsFile(imagefile, projection, GeoTransform, ptsfile):
    if not is_file_exist(imagefile):
        return False
    image = RSImageclass()
    if not image.open(imagefile):
        return False
    ngcpcount = image.ds.GetGCPCount()
    if ngcpcount > 0:
        basic.outputlogMessage(
            'warning: The file already have GCP,GCP count is ' +
            str(ngcpcount))

    allgcps = []
    inputfile_object = open(ptsfile, 'r')
    all_points = inputfile_object.readlines()
    for linestr in all_points:
        if linestr[0:1] == '#' or linestr[0:1] == ';' or len(linestr) < 2:
            continue
        if len(allgcps) >= 10000:
            basic.outputlogMessage(
                'warning: the count of gcps already greater than 10000, and ignore the others to make geotiff work correctly'
            )
            continue
        tiepointXY = linestr.split()
        base_x = float(tiepointXY[0])
        base_y = float(tiepointXY[1])
        Xp = GeoTransform[
            0] + base_x * GeoTransform[1] + base_y * GeoTransform[2]
        Yp = GeoTransform[
            3] + base_x * GeoTransform[4] + base_y * GeoTransform[5]

        warp_x = float(tiepointXY[2])
        warp_y = float(tiepointXY[3])
        info = 'GCPbysiftgpu_%d' % len(allgcps)
        id = str(len(allgcps))
        gcp1 = gdal.GCP(Xp, Yp, 0, warp_x, warp_y, info, id)
        allgcps.append(gcp1)
    inputfile_object.close()

    Outputtiff = imagefile.split('.')[0] + '_new.tif'
    format = "GTiff"
    driver = gdal.GetDriverByName(format)
    metadata = driver.GetMetadata()
    if metadata.has_key(
            gdal.DCAP_CREATE) and metadata[gdal.DCAP_CREATE] == 'YES':
        basic.outputlogMessage('Driver %s supports Create() method.' % format)
    else:
        basic.outputlogMessage('Driver %s not supports Create() method.' %
                               format)
        return False
    # if metadata.has_key(gdal.DCAP_CREATECOPY) and metadata[gdal.DCAP_CREATECOPY] == 'YES':
    #     syslog.outputlogMessage('Driver %s supports CreateCopy() method.' % format)

    # dst_ds = driver.CreateCopy(Outputtiff, dataset, 0)
    datatype = image.GetGDALDataType()
    dst_ds = driver.Create(Outputtiff, image.GetWidth(), image.GetHeight(),
                           image.GetBandCount(), datatype)
    for bandindex in range(0, image.GetBandCount()):
        bandobject = image.Getband(bandindex + 1)
        banddata = bandobject.ReadRaster(0, 0, image.GetWidth(),
                                         image.GetHeight(), image.GetWidth(),
                                         image.GetHeight(), datatype)
        #byte
        # if banddata is 1:
        #     bandarray = struct.unpack('B'*image.GetWidth()*image.GetHeight(), banddata)
        dst_ds.GetRasterBand(bandindex + 1).WriteRaster(
            0, 0, image.GetWidth(), image.GetHeight(), banddata,
            image.GetWidth(), image.GetHeight(), datatype)

    dst_ds.SetGCPs(allgcps, projection)

    # if I have set the GCPs, should not do this again, or SetGCPs will be undo
    # dst_ds.SetGeoTransform(image.GetGeoTransform())
    # dst_ds.SetProjection(image.GetProjection())

    if not os.path.isfile(Outputtiff):
        basic.outputlogMessage(
            'result file not exist, the operation of create set gcp failed')
        return False
    dst_ds = None
    image = None

    return Outputtiff
Esempio n. 6
0
def coregistration_siftGPU(basefile, warpfile, bkeepmidfile, xml_obj):
    tiepointfile = '0_1_after.pts'
    if os.path.isfile(tiepointfile):
        basic.outputlogMessage(
            'warning:tie points already exist in dir, skip get_tie_points_by_ZY3ImageMatch'
        )
    else:
        tiepointfile = tiepoints.get_tie_points_by_ZY3ImageMatch(
            basefile, warpfile, bkeepmidfile)

    if tiepointfile is False:
        basic.outputlogMessage('Get tie points by ZY3ImageMatch failed')
        return False

    xml_obj.add_coregistration_info('tie_points_file', tiepointfile)
    # draw tie points rms vector on base image
    result_rms_files = '0_1_fs.txt'
    tiepoint_vector_ = 'tiepoints_vector.png'
    tmpImg_obj = RSImageclass()
    tmpImg_obj.open(basefile)
    if tmpImg_obj.GetWidth() * tmpImg_obj.GetHeight() < 10000 * 10000:
        output_tie_points_vector_on_base_image(basefile, result_rms_files,
                                               tiepoint_vector_)
        xml_obj.add_coregistration_info('tie_points_drawed_image',
                                        os.path.abspath(tiepoint_vector_))
    else:
        basic.outputlogMessage(
            'warning: the width*height of the image > 10000*10000, skip drawing tie point vectors'
        )

    # check the tie points
    try:
        rms_files_obj = open(result_rms_files, 'r')
        rms_lines = rms_files_obj.readlines()
        if len(rms_lines) < 2:
            basic.outputlogMessage("%s do not contain tie points information" %
                                   os.path.abspath(result_rms_files))
            return False
        required_point_count = parameters.get_required_minimum_tiepoint_number(
        )
        acceptable_rms = parameters.get_acceptable_maximum_RMS()
        xml_obj.add_coregistration_info('required_tie_point_count',
                                        str(required_point_count))
        xml_obj.add_coregistration_info('acceptable_rms', str(acceptable_rms))
        try:
            digit_str = re.findall('\d+', rms_lines[0])
            tiepoints_count = int(digit_str[0])
            xml_obj.add_coregistration_info('tie_points_count',
                                            str(tiepoints_count))
            if tiepoints_count < required_point_count:
                basic.outputlogMessage(
                    "ERROR: tiepoints count(%d) is less than required one(%d)"
                    % (tiepoints_count, required_point_count))
                return False
            digit_str = re.findall('\d+\.?\d*', rms_lines[1])
            totalrms_value = float(digit_str[2])
            xml_obj.add_coregistration_info('total_rms_value',
                                            str(totalrms_value))
            if totalrms_value > acceptable_rms:
                basic.outputlogMessage(
                    "ERROR:Total RMS(%f) exceeds the acceptable one(%f)" %
                    (totalrms_value, acceptable_rms))
                return False
        except ValueError:
            return basic.outputlogMessage(str(ValueError))
            # return False
        rms_files_obj.close()
    except IOError:
        # basic.outputlogMessage(str(IOError))
        raise IOError('check the tie points')
        # return False

    baseimg = RSImageclass()
    if not baseimg.open(basefile):
        return False
    proj = baseimg.GetProjection()
    geotransform = baseimg.GetGeoTransform()
    # xres = baseimg.GetXresolution()
    # yres = baseimg.GetYresolution()
    # keep the output resolution the same as orginal image not the base image hlc Jan 31, 2019
    warpimg = RSImageclass()
    if not warpimg.open(warpfile):
        return False
    xres = warpimg.GetXresolution()
    yres = warpimg.GetYresolution()

    try:
        Outputtiff = setGCPsfromptsFile(warpfile, proj, geotransform,
                                        tiepointfile)
    except RuntimeError as e:
        basic.outputlogMessage('setGCPsfromptsFile failed: ')
        basic.outputlogMessage(str(e))
        return False
    if Outputtiff is False:
        return False
    else:
        basic.outputlogMessage('setGCPsfromptsFile completed, Out file: ' +
                               Outputtiff)

    # if not bkeepmidfile:
    #     os.remove(warpfile)

    xml_obj.add_coregistration_info('setted_gcps_file', Outputtiff)

    # warp image
    warpresultfile = io_function.get_name_by_adding_tail(
        Outputtiff, 'warp')  # Outputtiff.split('.')[0] + '_warp.tif'
    # -order 1  -tps
    # -tr xres yres: set output file resolution (in target georeferenced units)
    # set resolution as the same as base image is important
    order_number = parameters.get_gdalwarp_polynomial_order()
    xml_obj.add_coregistration_info('warp_polynomial_order_number',
                                    str(order_number))
    if order_number is False:
        return False
    CommandString = 'gdalwarp ' + ' -order ' + str(
        order_number) + ' -r bilinear -tr ' + str(xres) + ' ' + str(
            yres) + ' ' + Outputtiff + ' ' + warpresultfile
    # basic.outputlogMessage(CommandString)
    # (status, result) = commands.getstatusoutput(CommandString)
    # basic.outputlogMessage(result)
    # if not os.path.isfile(warpresultfile):
    #     return False
    if basic.exec_command_string_one_file(CommandString,
                                          warpresultfile) is False:
        return False

    if not bkeepmidfile:
        os.remove(Outputtiff)

    return warpresultfile
Esempio n. 7
0
def get_geoimage_range_geoid_height(outputfile, ref_image):
    #convert srs
    ref_img_obj = RSImageclass()
    if not ref_img_obj.open(ref_image):
        return False
    # x_res = ref_img_obj.GetXresolution()
    # y_res = ref_img_obj.GetYresolution()
    width = ref_img_obj.GetWidth()
    height = ref_img_obj.GetHeight()

    img_pro = RSImgProclass()
    ref_image_data = img_pro.Read_Image_band_data_to_numpy_array_all_pixel(
        1, ref_image)
    if ref_image_data is False:
        return False

    nodata = parameters.get_nodata_value()
    Image_array = ref_image_data.reshape(height, width)
    start_x = ref_img_obj.GetStartX()
    start_y = ref_img_obj.GetStartY()
    resolution_x = ref_img_obj.GetXresolution()
    resolution_y = ref_img_obj.GetYresolution()
    ref_img_WKT = ref_img_obj.GetProjection()
    # ref_img_WKT = RSImageProcess.get_raster_or_vector_srs_info_wkt(ref_image,syslog)

    (i, j) = numpy.where(Image_array != nodata)
    input_x = start_x + j * resolution_x
    input_y = start_y + i * resolution_y

    # srs_longlat_prj4 = '\'+proj=longlat +datum=WGS84 +no_defs\''
    # intput_proj4 = RSImage.wkt_to_proj4(ref_img_WKT,syslog)
    # intput_proj4 = RSImageProcess.get_raster_or_vector_srs_info_proj4(ref_image,syslog)
    # map_projection.convert_points_coordinate_proj4(input_x,input_y,intput_proj4,srs_longlat_prj4,syslog)

    srs_longlat_wkt = "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.01745329251994328]]"
    map_projection.convert_points_coordinate(input_x, input_y, ref_img_WKT,
                                             srs_longlat_wkt)

    tempsave_str = []
    save_point_txt_file = 'TXTgeoid_' + os.path.splitext(
        os.path.basename(ref_image))[0] + '.txt'
    if os.path.isfile(save_point_txt_file):
        file_object = open(save_point_txt_file, 'r')
        savepoints = file_object.readlines()
        for point in savepoints:
            tempsave_str.append(point)
        file_object.close()
        io_function.delete_file_or_dir(save_point_txt_file)
    file_object = open(save_point_txt_file, 'a')

    nsize = Image_array.size
    Image_array = Image_array.astype(numpy.float32)
    for index in range(0, nsize):
        lon_deg = input_x[index]
        lat_deg = input_y[index]
        if index < len(tempsave_str):
            temp_point = tempsave_str[index].split()
            value = float(temp_point[2])
        else:
            (LongitudeDeg, LongitudeMin, LongitudeSec) = degree_to_dms(lon_deg)
            (LatitudeDeg, LatitudeMin, LatitudeSec) = degree_to_dms(lat_deg)
            value = get_geoid_height(LatitudeDeg, LatitudeMin, LatitudeSec,
                                     LongitudeDeg, LongitudeMin, LongitudeSec,
                                     nodata)
            if value is False:
                break
        saved_point = ('%f  %f  %f' % (lon_deg, lat_deg, value))
        print saved_point
        # tempsave_str.append(saved_point)
        file_object.writelines(saved_point + '\n')
        file_object.flush()
        basic.outputlogMessage('Longitude=%f, Latitude=%f, geoid = %f' %
                               (lon_deg, lat_deg, value))
        # Image_array[index] = value
        Image_array[i[index], j[index]] = value
        print(i[index], j[index], Image_array[i[index], j[index]])

    file_object.close()
    if index != (nsize - 1):
        return False
    #save geoid height
    RSImageProcess.save_numpy_2d_array_to_image_tif(outputfile,Image_array,\
        6,ref_img_obj.GetGeoTransform(),ref_img_WKT,nodata)

    return True