def mosaics_images(raster_files, outputfile, nodata): """ mosaic a set of images. All the images must be in the same coordinate system and have a matching number of bands, Args: raster_files:a set of images with same coordinate system and have a matching number of bands, list type outputfile: the mosaic result file Returns: the result path if successful, False otherwise """ if isinstance(raster_files, list) is False: basic.outputlogMessage('the type of raster_files must be list') return False if len(raster_files) < 2: basic.outputlogMessage('file count less than 2') return False inputfile = '' for i in range(0, len(raster_files)): if io_function.is_file_exist(raster_files[i]) is False: return False inputfile = inputfile + ' ' + raster_files[i] CommandString = 'gdal_merge.py ' + inputfile + ' -o ' + outputfile + ' -n ' + str( nodata) return basic.exec_command_string_one_file(CommandString, outputfile)
def copyfiletodir(file_path, dir_name): """ copy file to a destination folder Args: file_path: the copied file dir_name: destination folder name Returns: True if successful or already exist, False otherwise. Notes: if IOError occurs, it will exit the program """ dst_name = os.path.join(dir_name,os.path.split(file_path)[1]) if os.path.isfile(dst_name): basic.outputlogMessage("%s already exist, skip"%dst_name) return True try: shutil.copyfile(file_path,dst_name) except IOError: basic.outputlogMessage(str(IOError)) basic.outputlogMessage('copy file failed: '+ file_path) assert False if not os.path.isfile(dst_name): basic.outputlogMessage('copy file failed') return False else: basic.outputlogMessage('copy file success: '+ file_path) return True
def get_image_max_min_value(imagepath): """ get image first band max vlaue and min value Args: imagepath: image path Returns:(max value list, min value list) if successful, (False,False) otherwise """ max_value = [] min_value = [] CommandString = 'gdalinfo -json -stats ' + imagepath imginfo = basic.exec_command_string_output_string(CommandString) if imginfo is False: return False imginfo_obj = json.loads(imginfo) try: bands_info = imginfo_obj['bands'] for band_info in bands_info: max_value.append(band_info['maximum']) min_value.append(band_info['minimum']) return (max_value, min_value) except KeyError: basic.outputlogMessage(str(KeyError)) pass return (False, False)
def subset_image_baseimage(output_file,input_file,baseimage,same_res=False): """ subset a image base on the extent of another image Args: output_file:the result file input_file:the image need to subset baseimage:the base image which provide the extend for subset same_res: if true, then will resample the output to the resolution of baseimage, otherwise, keep the resolution Returns:True is successful, False otherwise """ (ulx,uly,lrx,lry) = RSImage.get_image_proj_extent(baseimage) if ulx is False: return False # check the save folder is valid or not save_dir = os.path.dirname(output_file) if len(save_dir) < 1: basic.outputlogMessage('output save to current folder') else: basic.outputlogMessage('result save to %s'%save_dir) img_obj = RSImageclass() if same_res: img_obj.open(baseimage) else: img_obj.open(input_file) # the resolution should keep the same xres = img_obj.GetXresolution() yres = img_obj.GetYresolution() img_obj=None if subset_image_projwin(output_file,input_file,ulx,uly,lrx,lry,xres=xres,yres=yres) is False: return False return True
def is_file_exist(file): if not os.path.isfile(file): basic.outputlogMessage( 'coregistration failed, can not find the file: ' + os.path.abspath(file)) return False return True
def get_image_proj_extent(imagepath): """ get the extent of a image Args: imagepath:image path Returns:(ulx:Upper Left X,uly: Upper Left Y,lrx: Lower Right X,lry: Lower Right Y) """ ulx = False uly = False lrx = False lry = False CommandString = 'gdalinfo -json ' + imagepath imginfo = basic.exec_command_string_output_string(CommandString) if imginfo is False: return False imginfo_obj = json.loads(imginfo) # print imginfo_obj # print type(imginfo_obj) # print imginfo_obj.keys() try: cornerCoordinates = imginfo_obj['cornerCoordinates'] upperLeft_value = cornerCoordinates['upperLeft'] lowerRight_value = cornerCoordinates['lowerRight'] ulx = upperLeft_value[0] uly = upperLeft_value[1] lrx = lowerRight_value[0] lry = lowerRight_value[1] except KeyError: basic.outputlogMessage(str(KeyError)) pass return (ulx, uly, lrx, lry)
def get_image_location_value(imagepath, x, y, xy_srs, bandindex): """ get the image value of given location(x,y) in bandindex Args: imagepath:the image path which the information query x:x value y:y value xy_srs:the coordinate system of (x,y), the value is :pixel ,prj or lon_lat_wgs84 bandindex:the bandindex of image want to query Returns:the certain value (float) of given location """ coordinate = '' if xy_srs == 'pixel': coordinate = ' ' elif xy_srs == 'prj': coordinate = ' -geoloc ' elif xy_srs == 'lon_lat_wgs84': coordinate = ' -wgs84 ' else: basic.outputlogMessage('input error: %s is not right' % xy_srs) assert False command_str = 'gdallocationinfo -valonly ' + ' -b ' +str(bandindex) + coordinate \ + ' '+imagepath + ' ' + str(x) +' '+ str(y) result = basic.exec_command_string_output_string(command_str) try: result = float(result) except ValueError: basic.outputlogMessage(str(ValueError)) assert False return result
def get_image_mean_value(imagepath): """ get image first band max vlaue and min value Args: imagepath: image path Returns:(mean value list for each band) if successful, (False) otherwise """ mean_value = [] cmd_list = ['gdalinfo', '-json', '-stats', '-mm', imagepath] # Force computation imginfo = basic.exec_command_args_list_one_string(cmd_list) # print imginfo if imginfo is False: return False imginfo_obj = json.loads(imginfo) try: bands_info = imginfo_obj['bands'] for band_info in bands_info: mean_value.append(band_info['mean']) return mean_value except KeyError: basic.outputlogMessage(str(KeyError)) pass return False
def test(): if len(sys.argv) < 2: return False xml_file = sys.argv[1] pre_meta_obj = ImgProMetaDataClass(xml_file) basic.outputlogMessage('end test')
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 move_file_to_dst(file_path, dst_name): """ move file to a destination file Args: file_path: the moved file dst_name: destination file name Returns: True if successful or already exist, False otherwise. Notes: if IOError occurs, it will exit the program """ if os.path.isfile(dst_name): basic.outputlogMessage("%s already exist, skip move file"%dst_name) return True try: shutil.move(file_path,dst_name) except IOError: basic.outputlogMessage(str(IOError)) basic.outputlogMessage('move file failed: '+ file_path) assert False if not os.path.isfile(dst_name): basic.outputlogMessage('move file failed') return False else: basic.outputlogMessage('move file success: '+ file_path) return True
def wkt_to_proj4(wkt): srs = osr.SpatialReference() srs.importFromWkt(wkt) proj4 = srs.ExportToProj4() if proj4 is False: basic.outputlogMessage('convert wkt to proj4 failed') return False return proj4
def proj4_to_wkt(proj4): srs = osr.SpatialReference() srs.ImportFromProj4(proj4) wkt = srs.ExportToWkt() if wkt is False: basic.outputlogMessage('convert wkt to proj4 failed') return False return wkt
def subset_image_by_shapefile(imagefile, shapefile, bkeepmidfile): """ subset an image by polygons contained in the shapefile Args: imagefile:input image file path shapefile:input shapefile contains polygon bkeepmidfile:indicate whether keep middle file Returns:output file name if succussful, False Otherwise """ if io_function.is_file_exist(imagefile) is False: return False if io_function.is_file_exist(shapefile) is False: return False Outfilename = io_function.get_name_by_adding_tail(imagefile, 'vsub') # ds = ogr.Open(shapefile) # lyr = ds.GetLayer(0) # lyr.ResetReading() # ft = lyr.GetNextFeature() # subprocess.call(['gdalwarp', imagefile, Outfilename, '-cutline', shapefile,\ # '-crop_to_cutline']) orgimg_obj = RSImageclass() if orgimg_obj.open(imagefile) is False: return False x_res = abs(orgimg_obj.GetXresolution()) y_res = abs(orgimg_obj.GetYresolution()) CommandString = 'gdalwarp ' + ' -tr ' + str(x_res) + ' ' + str( y_res ) + ' ' + imagefile + ' ' + Outfilename + ' -cutline ' + shapefile + ' -crop_to_cutline ' + ' -overwrite ' if basic.exec_command_string_one_file(CommandString, Outfilename) is False: return False # while ft: # country_name = ft.GetFieldAsString('admin') # outraster = imagefile.replace('.tif', '_%s.tif' % country_name.replace(' ', '_')) # subprocess.call(['gdalwarp', imagefile, Outfilename, '-cutline', shapefile, # '-crop_to_cutline', '-cwhere', "'admin'='%s'" % country_name]) # # ft = lyr.GetNextFeature() if not bkeepmidfile: io_function.delete_file_or_dir(imagefile) os.remove(imagefile) if io_function.is_file_exist(Outfilename): return Outfilename else: # basic.outputlogMessage(result) basic.outputlogMessage( 'The version of GDAL must be great than 2.0 in order to use the r option ' ) return False
def get_first_path_in_line(input_path): # if multi files in one line, then only consider the first file image_path = input_path if os.path.isfile(image_path) is False: image_path = image_path.split()[0] # only consider first image if os.path.isfile(image_path) is False: basic.outputlogMessage('File not exist %s ' % image_path) return False return image_path
def SetProjection(self, prj_wkt): if not self.ds is None: try: self.ds.SetProjection(prj_wkt) except RuntimeError as e: basic.outputlogMessage(str(e)) return False return True else: return False
def get_offset_tracking_info(self, tag): info_list = self.get_sub_element_text_with_p_tag( 'feature_tracking', tag) if len(info_list) == 1: return info_list[0] if len(info_list) == 0: return '' else: basic.outputlogMessage('waring: the count of %s is more than 1') return info_list[0]
def SetGeoTransform(self, geotransform): if not self.ds is None: try: self.ds.SetGeoTransform(geotransform) except RuntimeError as e: basic.outputlogMessage(str(e)) return False return True else: return False
def add_element(self, parent_name, tag, text='', attrib=None): parent_node_list = self.get_element(parent_name) if len(parent_node_list) == 1: self.create_sub_element(parent_node_list[0], tag, text=text, attrib=attrib) return True else: basic.outputlogMessage( 'the number of %s node is 0 or greater than 1' % parent_name) assert False
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)
def is_folder_exist(folder_path): """ determine whether the folder_path is a exist folder :param folder_path: folder path :return:True if folder exist, False otherwise """ if len(folder_path) < 1: basic.outputlogMessage('error: The input folder path is empty') return False if os.path.isdir(folder_path): return True else: basic.outputlogMessage("Folder : %s not exist"%os.path.abspath(folder_path)) return False
def is_file_exist(file_path): """ determine whether the file_path is a exist file Args: file_path: the file path Returns:True if file exist, False otherwise """ if os.path.isfile(file_path): return True else: basic.outputlogMessage("File : %s not exist"%os.path.abspath(file_path)) return False
def __unpack_gdal_data(self,GDALDataType,offsetvaluestr,width,height): if GDALDataType is 2: # GDT_UInt16 offsetvalue = struct.unpack('H' * width * height, offsetvaluestr) elif GDALDataType is 3: # GDT_Int16 offsetvalue = struct.unpack('h' * width * height, offsetvaluestr) elif GDALDataType is 6: # GDT_Float32 offsetvalue = struct.unpack('f' * width * height, offsetvaluestr) elif GDALDataType is 1: # GDT_Byte = 1 offsetvalue = struct.unpack('B' * width * height, offsetvaluestr) else: basic.outputlogMessage('error: not support datatype currently') return False return offsetvalue
def test_get_tie_points_by_ZY3ImageMatch(): filedir = '/home/hlc/Data/landsat_offset_test/coregistration_test/L7_B8_test_2' os.chdir(filedir) exe_dir = parameters.get_exec_dir('para.ini') file1 = os.path.join(filedir, 'LE70080111999288EDC00_B8.TIF') file2 = os.path.join(filedir, 'LE70080112000083KIS00_B8.TIF') result = get_tie_points_by_ZY3ImageMatch(file1, file2) if not result is False: basic.outputlogMessage( 'get_tie_points_by_ZY3ImageMatch success,result path is:' + result) else: basic.outputlogMessage('get_tie_points_by_ZY3ImageMatch failed') return True
def get_product_path(self): """ get the product path (assume only one product file) Returns:product path if exist, '' otherwise """ product_path = self.get_element_text('product_path') if len(product_path) < 1: return '' if len(product_path) > 1: basic.outputlogMessage('the product number exceed 1') assert False if os.path.isfile(product_path[0]) is False: return '' return product_path[0]
def get_terrain_offset_files(self): """ get file paths of terrain offset Returns:(terrain offset 1,terrain offset 2, terrain offset 3, terrain offset 4) """ ds1 = self.get_element_text('dem1_img1_ds1') ds2 = self.get_element_text('dem1_img2_ds2') ds3 = self.get_element_text('dem2_img1_ds3') ds4 = self.get_element_text('dem2_img2_ds4') if len(ds1) == 1 and len(ds2) == 1 and len(ds3) == 1 and len(ds4) == 1: return ds1[0], ds2[0], ds3[0], ds4[0] else: basic.outputlogMessage('the count of terrain offset is not 1') return '', '', '', ''
def test_get_geoid_height(): LatitudeDeg = 72 LatitudeMin = 0 LatitudeSec = 0 LongitudeDeg = -57 LongitudeMin = 0 LongitudeSec = 0 nodata = 0 value = get_geoid_height(LatitudeDeg, LatitudeMin, LatitudeSec, LongitudeDeg, LongitudeMin, LongitudeSec, nodata) if value is False: return False basic.outputlogMessage(str(value)) return True
def convert_image_to_gray_auto(output_image, input_image): """ convert inputed image to 8bit Args: output_image:output image file path input_image: input imag file path Returns:output_image if successful, False otherwise """ if os.path.isfile(output_image) is True: basic.outputlogMessage('%s already exist,skip' % output_image) return output_image input_image_obj = RSImageclass() if input_image_obj.open(input_image) is False: return False # GDT_Unknown = 0, GDT_Byte = 1, GDT_UInt16 = 2, GDT_Int16 = 3, # GDT_UInt32 = 4, GDT_Int32 = 5, GDT_Float32 = 6, GDT_Float64 = 7, # GDT_CInt16 = 8, GDT_CInt32 = 9, GDT_CFloat32 = 10, GDT_CFloat64 = 11, #GDT_Byte if input_image_obj.GetGDALDataType() == 1: # io_function.copy_file_to_dst(input_image,output_image) output_image = input_image return output_image (max_value_list, min_value_list) = RSImage.get_image_max_min_value(input_image) if max_value_list is False or min_value_list is False: return False input_image_obj = None # CommandString = 'gdal_translate -r bilinear -ot Byte -scale ' + input_image + ' '+output_image # return basic.exec_command_string_one_file(CommandString,output_image) args_list = ['gdal_translate', '-r', 'bilinear', '-ot', 'Byte'] for band_index in range(0, len(max_value_list)): args_list.append('-b') args_list.append(str(band_index + 1)) args_list.append('-scale') args_list.append(str(min_value_list[band_index])) args_list.append(str(max_value_list[band_index])) args_list.append(str(1)) args_list.append(str(254)) args_list.append(input_image) args_list.append(output_image) return basic.exec_command_args_list_one_file(args_list, output_image)
def os_list_folder_dir(top_dir): if not os.path.isdir(top_dir): basic.outputlogMessage('the input string is not a dir, input string: %s'%top_dir) return False sub_folders = [] for file in sorted(os.listdir(top_dir)): file_path = os.path.abspath(os.path.join(top_dir, file)) if os.path.isfile(file_path): continue elif os.path.isdir(file_path): sub_folders.append(file_path) if len(sub_folders) < 1: basic.outputlogMessage('There is no sub folder in %s'%top_dir) return False return sub_folders
def get_name_by_adding_tail(basename,tail): """ create a new file name by add a tail to a exist file name Args: basename: exist file name tail: the tail name Returns: a new name if successfull Notes: if input error, it will exit program """ text = os.path.splitext(basename) if len(text)<2: basic.outputlogMessage('ERROR: incorrect input file name: %s'%basename) assert False return text[0]+'_'+tail+text[1]