def delete_fouth_coloumn_csv(csv_path): with open(csv_path, 'rb') as f: reader = csv.reader(f) input_list = map(tuple, reader) f.close() save_path = io_function.get_name_by_adding_tail(csv_path, 'modify') save_csv = open(save_path, 'wb') csv_writer = csv.writer(save_csv) number = 1 for line in input_list: # if number ==1: # number = number+1 # continue number = number + 1 save_ele = list(line) # line[3] = str(1) save_ele[3] = str(1) csv_writer.writerows([save_ele]) # csv_writer.writerows([line]) save_csv.close() return save_path
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 test(): # input_image = '/Users/huanglingcao/Data/getVelocityfromRSimage_test/pre_processing_saved/LC81400412015065LGN00_B8.TIF' # # input_image = '/Users/huanglingcao/Data/getVelocityfromRSimage_test/pre_processing_saved/19900624_19900710_abs_m.jpg' # output_image = io_function.get_name_by_adding_tail(input_image,'gray') # convert_image_to_gray_auto(output_image,input_image) input_image = '/Users/huanglingcao/Data/getVelocityfromRSimage_test/pre_processing_saved/LE70080112000115EDC00_B4_prj.TIF' out_img = io_function.get_name_by_adding_tail(input_image,'sub') result = subset_image_projwin(out_img,input_image,472335,7705320,600645,7638360) print(result)
def prepare_GTOPO30_for_Jakobshavn(workdir): #subset dem file GTOPO30_dem = "gt30w060n90.tif" ulx = -56 #degree uly = 72 lrx = -42 lry = 67 GTOPO30_sub = io_function.get_name_by_adding_tail(GTOPO30_dem, 'jako') RSImageProcess.subset_image_projwin(GTOPO30_sub, GTOPO30_dem, ulx, uly, lrx, lry) #transform projection x_res = 30 y_res = 30 srs_UTM_prj4 = '\'+proj=utm +zone=22 +datum=WGS84 +units=m +no_defs\' ' GTOPO30_utm = io_function.get_name_by_adding_tail(GTOPO30_sub, 'utm22') if os.path.isfile(GTOPO30_utm) is False: if RSImageProcess.transforms_raster_srs( GTOPO30_sub, srs_UTM_prj4, GTOPO30_utm, x_res, y_res) is False: return False 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
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 output_tie_points_vector_on_base_image(drawed_image, tiepoint_rms_file, outputfile): if not os.path.isfile(drawed_image): basic.outputlogMessage('file not exist: ' + os.path.abspath(drawed_image)) return False if not os.path.isfile(tiepoint_rms_file): basic.outputlogMessage('file not exist: ' + os.path.abspath(tiepoint_rms_file)) return False #read points (x,y) and rms draw_scale = parameters.get_draw_tie_points_rms_vector_scale() if draw_scale is False: return False x0 = [] y0 = [] x1 = [] y1 = [] tiepoint_rms_file_object = open(tiepoint_rms_file, 'r') lines_str = tiepoint_rms_file_object.readlines() if (len(lines_str) < 5): basic.outputlogMessage('error, intput file contains lines less than 5') return False for i in range(4, len(lines_str)): digits = lines_str[i].split() tempx = 0 tempy = 0 dx = 0 dy = 0 try: tempx = float(digits[0]) tempy = float(digits[1]) dx = float( digits[6] ) * draw_scale #(float(digits[2]) - tempx )*draw_scale #float(digits[6])*draw_scale dy = float( digits[7] ) * draw_scale #(float(digits[3]) - tempy)*draw_scale #float(digits[7])*draw_scale except ValueError: basic.outputlogMessage(str(ValueError)) x0.append(tempx) y0.append(tempy) x1.append(tempx + dx) y1.append(tempy + dy) tiepoint_rms_file_object.close() drawed_image_gray = io_function.get_name_by_adding_tail( drawed_image, 'gray') drawed_image_gray = RSImageProcess.convert_image_to_gray_auto( drawed_image_gray, drawed_image) im = Image.open(drawed_image_gray).convert( 'RGB') #needing convert to RGB, or can not draw color line and text draw_content = aggdraw.Draw(im) # draw_content2 = ImageDraw.Draw(im) for k in range(0, len(x0)): test_aggdraw.line(draw_content, (x0[k], y0[k], x1[k], y1[k]), color='red', width=1, arrow='last', arrowshape=(4, 5, 3)) #draw legend x0 = 100 y0 = 100 x1 = 0.5 * draw_scale + x0 y1 = y0 test_aggdraw.line(draw_content, (x0, y0, x1, y1), color='red', width=1, arrow='last', arrowshape=(8, 10, 3)) test_aggdraw.draw_text(draw_content, (100, 80), '0.5 pixel') # draw_content2.text((3000,90),'1000 m/year') # draw_content2.flush() # del draw_content2 draw_content.flush() im.save(outputfile) return True
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
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 = io_function.get_name_by_adding_tail( imagefile, 'new') # imagefile.split('.')[0] + '_new.tif' format = "GTiff" driver = gdal.GetDriverByName(format) metadata = driver.GetMetadata() if gdal.DCAP_CREATE in metadata 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
def main(options, args): # syslog = LogMessage() # length = len(sys.argv) # test_get_geoid_height(syslog) # sys.exit(1) # # if length >=3 : # work_dir = sys.argv[1] # what_to_do = sys.argv[2] # else: # print 'Input error, Try to do like this:' # print 'DemConvert.py work_dir what_to_do' # help() # sys.exit(1) length = len(args) want_to_do = options.what_to_do # (what_to_do)#options.what_to_do work_dir = args[0] basic.setlogfile('dem_prepare_log.txt') parameters.set_saved_parafile_path(options.para_file) if want_to_do == 1: if prepare_gimpdem_for_Jakobshavn(work_dir): basic.outputlogMessage('process sucess') else: basic.outputlogMessage('process failed') elif want_to_do == 2: outputfile = options.output up = 72 down = 69 left = -57 right = -40 get_range_geoid_height(outputfile, up, down, left, right) elif want_to_do == 3: if length == 2: outputfile = options.output ref_image = args[1] if get_geoimage_range_geoid_height(outputfile, ref_image) != False: return True print 'Input error, Try to do like this: ' print 'DemConvert.py -a 3 -o outputfile work_dir ref_image' sys.exit(1) elif want_to_do == 4: if length == 3: orthometric_demfile = args[1] geoid_demfile = args[2] outputfile = io_function.get_name_by_adding_tail( orthometric_demfile, "ellip") if convert_orthometricH_to_elliopsoidalH( outputfile, orthometric_demfile, geoid_demfile) != False: return True print 'Input error, Try to do like this: ' print 'DemConvert.py -a 4 work_dir orthometric_demfile geoid_demfile' sys.exit(1) elif want_to_do == 5: if length == 3: outputfile = options.output dem_file = args[1] image_file = args[2] exec_dir = os.path.expanduser('~') + '/bin/' if calculate_terrain_offset(outputfile, dem_file, image_file, exec_dir) != False: return True print 'Input error, Try to do like this: ' print 'DemConvert.py -a 5 -o output work_dir dem_file image_file' sys.exit(1) elif want_to_do == 6: if length != 3: print 'Input error, Try to do like this: ' print 'DemConvert.py work_dir what_to_do' sys.exit(1) print prepare_GTOPO30_for_Jakobshavn(work_dir) else: basic.outputlogMessage('nothing to do') #test # print get_geoid_height(22,31,32,114,15,16,-999999,syslog) return True
def calculate_terrain_offset(output, dem_file, image_file, exec_dir, bkeepmidfile): if io_function.is_file_exist( image_file) is False or io_function.is_file_exist( dem_file, ) is False: return False exefile = os.path.join(exec_dir, 'geometry_pro') nodata = parameters.get_nodata_value() (centre_lat, centre_lon) = RSImage.get_image_latlon_centre(image_file) if centre_lat is False or centre_lon is False: return False image_obj = RSImageclass() if image_obj.open(image_file) is False: return False dem_obj = RSImageclass() if dem_obj.open(dem_file) is False: return False x_res = image_obj.GetXresolution() y_res = image_obj.GetYresolution() x_res_dem = dem_obj.GetXresolution() y_res_dem = dem_obj.GetYresolution() image_prj = image_obj.GetProjection() dem_prj = dem_obj.GetProjection() #check projection and resolution, and convert it if need #use orthometricH_file as base image dem_convertedfile = io_function.get_name_by_adding_tail(dem_file, 'tran') if x_res != x_res_dem or y_res != y_res_dem or image_prj != dem_prj: if os.path.isfile(dem_convertedfile) is False: if map_projection.transforms_raster_srs( dem_file, image_prj, dem_convertedfile, abs(x_res), abs(y_res)) is False: return False if os.path.isfile(dem_convertedfile): dem_file = dem_convertedfile #sub dem file (ulx, uly, lrx, lry) = RSImage.get_image_proj_extent(image_file) if ulx is False: return False tail = os.path.splitext(os.path.basename(image_file))[0] dem_file_sub = io_function.get_name_by_adding_tail(dem_file, tail) if os.path.isfile(dem_file_sub) is False: if RSImageProcess.subset_image_projwin(dem_file_sub, dem_file, ulx, uly, lrx, lry) is False: return False #calculateing terrain contains a lot of I/O operations, the parallel computing will slow down it nblockwidth = 8000 nblcckheight = 8000 njobs = 1 logfile = 'cal_terrain_offset_log.txt' CommandString = exefile \ + ' -i ' + image_file + ' -d ' + dem_file_sub \ + ' -o ' + output + ' -n ' + str(nodata)\ + ' -w ' + str(nblockwidth) + ' -h ' + str(nblcckheight) + ' -j ' +str(njobs) \ + ' --centre_lat=' + str(centre_lat) \ + ' --logfile=' + logfile basic.outputlogMessage(CommandString) (status, result) = commands.getstatusoutput(CommandString) basic.outputlogMessage(result) if bkeepmidfile is False: # if os.path.isfile(dem_convertedfile): # io_function.delete_file_or_dir(dem_convertedfile) if os.path.isfile(dem_file_sub): io_function.delete_file_or_dir(dem_file_sub) if os.path.isfile(output): if os.path.getsize(output) > 0: return output else: basic.outputlogMessage('error: the size of file %s is 0' % os.path.basename(output)) return False else: return False
def prepare_gimpdem_for_Jakobshavn(workdir): nodata = parameters.get_nodata_value() #mosaics gimdem files os.chdir(workdir) gimpdem_file = ['gimpdem1_2.tif', 'gimpdem2_2.tif'] gimpdem_output = 'dem_gimp_jako.tif' if os.path.isfile(gimpdem_output) is False: if RSImageProcess.mosaics_images(gimpdem_file, gimpdem_output) is False: return False geoid_file = 'geoid_hegith_jako.tif' #convert srs img_temp = RSImageclass() if not img_temp.open(gimpdem_output): return False x_res = img_temp.GetXresolution() y_res = img_temp.GetYresolution() srs_UTM_prj4 = '\'+proj=utm +zone=22 +datum=WGS84 +units=m +no_defs\' ' gimpdem_utm = io_function.get_name_by_adding_tail(gimpdem_output, 'utm22') if os.path.isfile(gimpdem_utm) is False: if RSImageProcess.transforms_raster_srs(gimpdem_output, srs_UTM_prj4, gimpdem_utm, abs(x_res), abs(y_res)) is False: return False # print img_temp.GetGDALDataType(),type(img_temp.GetGDALDataType()) # print img_temp.GetProjection(),type(img_temp.GetProjection()) # print img_temp.GetGeoTransform(),type(img_temp.GetGeoTransform()) #get geoid_height # srs_longlat_wkt = "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.01745329251994328]]" # srs_longlat_prj4 = '\'+proj=longlat +datum=WGS84 +no_defs\' ' # image = RSImageclass(syslog) # if not image.open(gimpdem_utm): # return False # # img_pro = RSImgProclass(syslog) # dem_data = img_pro.Read_Image_band_data_to_numpy_array_all_pixel(1,gimpdem_utm) # if dem_data is False: # return False # # # geoid_height = geoid_height + -9999 # geoid_utm_file = basic.get_name_by_adding_tail(geoid_file,'tran',syslog) # if RSImageProcess.transforms_raster_srs_to_base_image(geoid_file,gimpdem_utm,geoid_utm_file,\ # x_res,y_res,syslog) is False: # return False # geoid_height = img_pro.Read_Image_band_data_to_numpy_array_all_pixel(1,geoid_utm_file) # if geoid_height is False: # return False # # #get ellipsoidal height # Image_array = dem_data + geoid_height # gimpdem_utm_elli = basic.get_name_by_adding_tail(gimpdem_utm,'elli',syslog) # RSImageProcess.save_numpy_2d_array_to_image_tif(gimpdem_utm_elli,Image_array,\ # image.GetGDALDataType(),image.GetGeoTransform(),image.GetProjection(),nodata) # image = None # img_pro = None return True
def convert_orthometricH_to_elliopsoidalH(output, orthometricH_file, geoidHfile): if io_function.is_file_exist( orthometricH_file) is False or io_function.is_file_exist( geoidHfile) is False: return False orthom_obj = RSImageclass() if orthom_obj.open(orthometricH_file) is False: return False geoidH_obj = RSImageclass() if geoidH_obj.open(geoidHfile) is False: return False Nodata = parameters.get_nodata_value() x_res = orthom_obj.GetXresolution() y_res = orthom_obj.GetYresolution() x_res_geoid = geoidH_obj.GetXresolution() y_res_geoid = geoidH_obj.GetYresolution() orthom_prj = orthom_obj.GetProjection() geoid_prj = geoidH_obj.GetProjection() #check projection and resolution, and convert it if need #use orthometricH_file as base image if x_res != x_res_geoid or y_res != y_res_geoid or orthom_prj != geoid_prj: geoid_convertfile = io_function.get_name_by_adding_tail( geoidHfile, 'tran') if os.path.isfile(geoid_convertfile) is False: if RSImageProcess.transforms_raster_srs( geoidHfile, orthom_prj, geoid_convertfile, abs(x_res), abs(y_res)) is False: return False else: basic.outputlogMessage(geoid_convertfile + ' already exist') #sub geoidHfile base on the small one (ulx, uly, lrx, lry) = RSImageProcess.get_image_proj_extent(orthometricH_file) if ulx is False: return False geoid_convertfile_sub = io_function.get_name_by_adding_tail( geoid_convertfile, 'sub') if os.path.isfile(geoid_convertfile_sub) is False: result = RSImageProcess.subset_image_projwin(geoid_convertfile_sub, geoid_convertfile, ulx, uly, lrx, lry) if result is False: return False else: basic.outputlogMessage(geoid_convertfile_sub + ' already exist') ##caculate elliopsoidal height # orthometricH_data = img_pro.Read_Image_band_data_to_numpy_array_all_pixel(1,orthometricH_file) # img_pro = None # img_pro = RSImgProclass(syslog) # geoidH_data = img_pro.Read_Image_band_data_to_numpy_array_all_pixel(1,geoid_convertfile_sub) # img_pro = None # if orthometricH_data.shape != geoidH_data.shape: # syslog.outputlogMessage("the shape of orthometricH_data and geoidH_data is different") # return False # # nodata = parameters.get_nodata_value(syslog) # width = orthom_obj.GetWidth() # height = orthom_obj.GetHeight() # orthometricH_data = orthometricH_data.astype(numpy.float32) # geoidH_data = geoidH_data.astype(numpy.float32) # elliopsoidalH = orthometricH_data + geoidH_data # elliopsoidalH = elliopsoidalH.reshape(height,width) # # RSImageProcess.save_numpy_2d_array_to_image_tif(output,elliopsoidalH,6,\ # orthom_obj.GetGeoTransform(),orthom_obj.GetProjection(),nodata,syslog) # # orthom_obj = None # geoidH_obj = None CommandString = 'gdal_calc.py -A '+orthometricH_file + ' -B ' + geoid_convertfile_sub +\ ' --NoDataValue='+str(Nodata) +' --outfile='+output + ' --calc="A+B"' if RSImageProcess.exec_commond_string_one_file(CommandString, output) is False: return False else: basic.outputlogMessage( "converting orthometric Height to elliopsoidal Height is completed" ) return True