def transforms_raster_srs(rasterfile,t_srs,t_file,x_res,y_res,resample_m='bilinear', o_format='GTiff',compress=None, tiled=None, bigtiff=None): """ convert raster file to target SRS(Spatial Reference System) Args: rasterfile:input raster file t_srs: target SRS(Spatial Reference System) t_file:the output target file x_res:set output file x-resolution (in target georeferenced units),assigning this value to make sure the resolution would not change in target file y_res:set output file y-resolution (in target georeferenced units),assigning this value to make sure the resolution would not change in target file Returns:the output file path is successful, False Otherwise """ if io_function.is_file_exist(rasterfile) is False: return False x_res = abs(x_res) y_res = abs(y_res) CommandString = 'gdalwarp -r %s -t_srs '%resample_m + t_srs +' -tr ' +str(x_res)+ ' ' + str(y_res) if compress != None: CommandString += ' -co ' + 'compress=%s'%compress # lzw if tiled != None: CommandString += ' -co ' + 'TILED=%s'%tiled # yes if bigtiff != None: CommandString += ' -co ' + 'bigtiff=%s' % bigtiff # IF_SAFER CommandString += ' ' + rasterfile + ' ' + t_file return basic.exec_command_string_one_file(CommandString,t_file)
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 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 subset_image_projwin(output, imagefile, ulx, uly, lrx, lry, resample_m='bilinear', dst_nondata=0, xres=None, yres=None, o_format='GTiff', compress=None, tiled=None, bigtiff=None, thread_num=None): #bug fix: the origin (x,y) has a difference between setting one when using gdal_translate to subset image 2016.7.20 # CommandString = 'gdal_translate -r bilinear -eco -projwin ' +' '+str(ulx)+' '+str(uly)+' '+str(lrx)+' '+str(lry)\ # + ' '+imagefile + ' '+output xmin = ulx ymin = lry xmax = lrx ymax = uly # if xres is None or yres is None: # CommandString = 'gdalwarp -r bilinear -te ' +' '+str(xmin)+' '+str(ymin)+' '+str(xmax)+' '+str(ymax)\ # + ' ' + ' -dstnodata '+ str(dst_nondata) + ' '+imagefile + ' '+output # else: # CommandString = 'gdalwarp -r bilinear -te ' + ' ' + str(xmin) + ' ' + str(ymin) + ' ' + str(xmax) + ' ' + str(ymax) \ # + ' -tr ' +str(xres) + ' ' +str(yres) +' ' + ' -dstnodata '+ str(dst_nondata) + ' '+ imagefile + ' ' + output # update on 2018 Oct 20, we should not set nodata in subset opertion. It will copy from the source dataset CommandString = 'gdalwarp -r %s ' % resample_m + ' -of ' + o_format CommandString += ' -te ' + str(xmin) + ' ' + str(ymin) + ' ' + str( xmax) + ' ' + str(ymax) # if src_nodata != None: # CommandString += ' -srcnodata ' + str(src_nodata) # if dst_nodata != None: # CommandString += ' -dstnodata ' + str(dst_nodata) if xres != None and yres != None: CommandString += ' -tr ' + str(xres) + ' ' + str(yres) if compress != None: CommandString += ' -co ' + 'compress=%s' % compress # lzw if tiled != None: CommandString += ' -co ' + 'TILED=%s' % tiled # yes if bigtiff != None: CommandString += ' -co ' + 'bigtiff=%s' % bigtiff # IF_SAFER if thread_num != None: CommandString += ' -multi -wo NUM_THREADS=%d ' % thread_num CommandString += ' ' + imagefile + ' ' + output return basic.exec_command_string_one_file(CommandString, output)
def convert_image_to_gray(output_image,input_image,src_min,src_max,dst_min,dst_max): if io_function.is_file_exist(input_image) is False: return False src_min = str(src_min) src_max = str(src_max) dst_min = str(dst_min) dst_max = str(dst_max) CommandString = 'gdal_translate -r bilinear -ot Byte -scale ' + ' ' + src_min + ' ' + src_max + ' ' + dst_min + ' ' + dst_max \ + ' ' + input_image + ' ' + output_image return basic.exec_command_string_one_file(CommandString,output_image)
def subset_image_projwin(output,imagefile,ulx,uly,lrx,lry): #bug fix: the origin (x,y) has a difference between setting one when using gdal_translate to subset image 2016.7.20 # CommandString = 'gdal_translate -r bilinear -eco -projwin ' +' '+str(ulx)+' '+str(uly)+' '+str(lrx)+' '+str(lry)\ # + ' '+imagefile + ' '+output xmin = ulx ymin = lry xmax = lrx ymax = uly CommandString = 'gdalwarp -r bilinear -te ' +' '+str(xmin)+' '+str(ymin)+' '+str(xmax)+' '+str(ymax)\ + ' '+imagefile + ' '+output return basic.exec_command_string_one_file(CommandString,output)
def transforms_vector_srs(shapefile, t_srs, t_file): """ convert vector file to target SRS(Spatial Reference System) Args: shapefile:input vector file t_srs:target SRS(Spatial Reference System) t_file:the output target file Returns:the output file path is successful, False Otherwise """ if io_function.is_file_exist(shapefile) is False: return False CommandString = 'ogr2ogr -t_srs ' + t_srs + ' ' + t_file + ' ' + shapefile # if result.find('ERROR') >=0 or result.find('FAILURE'): # return False return basic.exec_command_string_one_file(CommandString, t_file)
def transforms_raster_srs(rasterfile, t_srs, t_file, x_res, y_res): """ convert raster file to target SRS(Spatial Reference System) Args: rasterfile:input raster file t_srs: target SRS(Spatial Reference System) t_file:the output target file x_res:set output file x-resolution (in target georeferenced units),assigning this value to make sure the resolution would not change in target file y_res:set output file y-resolution (in target georeferenced units),assigning this value to make sure the resolution would not change in target file Returns:the output file path is successful, False Otherwise """ if io_function.is_file_exist(rasterfile) is False: return False x_res = abs(x_res) y_res = abs(y_res) CommandString = 'gdalwarp -r bilinear -t_srs ' + t_srs +' -tr ' +str(x_res)+ ' ' + str(y_res) \ +' '+ rasterfile +' '+ t_file return basic.exec_command_string_one_file(CommandString, t_file)
def mosaics_images(raster_files, outputfile, nodata=None, compress=None, tiled=None, bigtiff=None): """ 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 if nodata is not None: CommandString += ' -n ' + str(nodata) CommandString += ' -a_nodata ' + str(nodata) if compress != None: CommandString += ' -co ' + 'compress=%s' % compress # lzw if tiled != None: CommandString += ' -co ' + 'TILED=%s' % tiled # yes if bigtiff != None: CommandString += ' -co ' + 'bigtiff=%s' % bigtiff # IF_SAFER return basic.exec_command_string_one_file(CommandString, outputfile)
def subset_image_projwin(output,imagefile,ulx,uly,lrx,lry,dst_nondata=0,xres=None,yres=None): #bug fix: the origin (x,y) has a difference between setting one when using gdal_translate to subset image 2016.7.20 # CommandString = 'gdal_translate -r bilinear -eco -projwin ' +' '+str(ulx)+' '+str(uly)+' '+str(lrx)+' '+str(lry)\ # + ' '+imagefile + ' '+output xmin = ulx ymin = lry xmax = lrx ymax = uly # if xres is None or yres is None: # CommandString = 'gdalwarp -r bilinear -te ' +' '+str(xmin)+' '+str(ymin)+' '+str(xmax)+' '+str(ymax)\ # + ' ' + ' -dstnodata '+ str(dst_nondata) + ' '+imagefile + ' '+output # else: # CommandString = 'gdalwarp -r bilinear -te ' + ' ' + str(xmin) + ' ' + str(ymin) + ' ' + str(xmax) + ' ' + str(ymax) \ # + ' -tr ' +str(xres) + ' ' +str(yres) +' ' + ' -dstnodata '+ str(dst_nondata) + ' '+ imagefile + ' ' + output # update on 2018 Oct 20, we should not set nodata in subset opertion. It will copy from the source dataset if xres is None or yres is None: CommandString = 'gdalwarp -r bilinear -te ' +' '+str(xmin)+' '+str(ymin)+' '+str(xmax)+' '+str(ymax)\ + ' ' + ' '+imagefile + ' '+output else: CommandString = 'gdalwarp -r bilinear -te ' + ' ' + str(xmin) + ' ' + str(ymin) + ' ' + str(xmax) + ' ' + str(ymax) \ + ' -tr ' +str(xres) + ' ' +str(yres) +' ' + ' '+ imagefile + ' ' + output return basic.exec_command_string_one_file(CommandString,output)
def subset_image_srcwin(output, imagefile, xoff, yoff, xsize, ysize): CommandString = 'gdal_translate -r bilinear -eco -srcwin ' +' '+str(xoff)+' '+str(yoff)+' '+str(xsize)+' '+str(ysize)\ + ' '+imagefile + ' '+output return basic.exec_command_string_one_file(CommandString, output)
def mosaic_crop_images_gdalwarp(raster_files, outputfile, src_nodata=None, dst_nodata=None, min_x=None, min_y=None, max_x=None, max_y=None, xres=None, yres=None, resampling_method='min', o_format='GTiff', compress=None, tiled=None, bigtiff=None, thread_num=None): ''' create a mosaic from multiple input raster using gdalwarp, crop if min_x, min_y, max_x, max_y are valid Args: raster_files: input raster list outputfile: output file src_nodata: dst_nodata: min_x: min_y: max_x: max_y: xres: yres: resampling_method: including, average, mode, max, min, med, q1, q3, sum https://gdal.org/programs/gdalwarp.html o_format: output format, default is GTiff, GeoTIFF File Format. Use "VRT": GDAL Virtual Format to save disk storage Returns: ''' if isinstance(raster_files, list) is False: # raise ValueError('the type of raster_files must be list') basic.outputlogMessage('the type of raster_files must be list') return False if len(raster_files) < 1: # raise ValueError('file count less than 1') basic.outputlogMessage('file count less than 1') return False CommandString = 'gdalwarp -r ' + resampling_method + ' -of ' + o_format if src_nodata != None: CommandString += ' -srcnodata ' + str(src_nodata) if dst_nodata != None: CommandString += ' -dstnodata ' + str(dst_nodata) if min_x != None and min_y != None and max_x != None and max_y != None: CommandString += ' -te ' + str(min_x) + ' ' + str(min_y) + ' ' + str( max_x) + ' ' + str(max_y) if xres != None and yres != None: CommandString += ' -tr ' + str(xres) + ' ' + str(yres) if compress != None: CommandString += ' -co ' + 'compress=%s' % compress # lzw if tiled != None: CommandString += ' -co ' + 'TILED=%s' % tiled # yes if bigtiff != None: CommandString += ' -co ' + 'bigtiff=%s' % bigtiff # IF_SAFER if thread_num != None: CommandString += ' -multi -wo NUM_THREADS=%d ' % thread_num inputfiles = ' '.join(raster_files) CommandString += ' ' + inputfiles + ' ' + outputfile return basic.exec_command_string_one_file(CommandString, outputfile)
def subset_image_by_shapefile(imagefile, shapefile, bkeepmidfile=True, overwrite=False, format='GTiff', save_path=None, resample_m='bilinear', src_nodata=None, dst_nondata=None, xres=None, yres=None, compress=None, tiled=None, bigtiff=None, thread_num=None): """ subset an image by polygons contained in the shapefile the shapefile and imagefile may have different projections, the gdalwarp can handle Args: imagefile:input image file path shapefile:input shapefile contains polygon bkeepmidfile:indicate whether keep middle file format: output format, default is GTiff, GeoTIFF File Format. Use "VRT": GDAL Virtual Format to save disk storage 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 if save_path is None: Outfilename = io_function.get_name_by_adding_tail(imagefile, 'vsub') else: Outfilename = save_path # ds = ogr.Open(shapefile) # lyr = ds.GetLayer(0) # lyr.ResetReading() # ft = lyr.GetNextFeature() # subprocess.call(['gdalwarp', imagefile, Outfilename, '-cutline', shapefile,\ # '-crop_to_cutline']) if overwrite is False and os.path.isfile(Outfilename): basic.outputlogMessage('warning, crop file: %s already exist, skip' % Outfilename) return Outfilename orgimg_obj = RSImageclass() if orgimg_obj.open(imagefile) is False: return False if xres is None or yres is None: x_res = abs(orgimg_obj.GetXresolution()) y_res = abs(orgimg_obj.GetYresolution()) else: x_res = xres y_res = yres CommandString = 'gdalwarp -r %s '% resample_m+' -tr ' + str(x_res) + ' '+ str(y_res)+ ' -of ' + format + ' ' + \ imagefile +' ' + Outfilename +' -cutline ' +shapefile +' -crop_to_cutline ' + ' -overwrite ' if src_nodata != None: CommandString += ' -srcnodata %d ' % src_nodata if dst_nondata != None: CommandString += ' -dstnodata %d ' % dst_nondata if compress != None: CommandString += ' -co ' + 'compress=%s' % compress # lzw if tiled != None: CommandString += ' -co ' + 'TILED=%s' % tiled # yes if bigtiff != None: CommandString += ' -co ' + 'bigtiff=%s' % bigtiff # IF_SAFER if thread_num != None: CommandString += ' -multi -wo NUM_THREADS=%d ' % thread_num 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 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