def init_dirs_srtm(config_file): """ 1) Creates different directories : output, temp... 2) Downloads srtm files Args: - config_file : a json configuratio file """ init_roi(config_file) # create tmp dir and output directory for the experiment, and store a json # dump of the config.cfg dictionary there, download srtm files... if not os.path.exists(cfg['out_dir']): os.makedirs(cfg['out_dir']) if not os.path.exists(cfg['temporary_dir']): os.makedirs(cfg['temporary_dir']) if not os.path.exists(os.path.join(cfg['temporary_dir'], 'meta')): os.makedirs(os.path.join(cfg['temporary_dir'], 'meta')) f = open('%s/config.json' % cfg['out_dir'], 'w') json.dump(cfg, f, indent=2) f.close() # duplicate stdout and stderr to log file tee.Tee('%s/stdout.log' % cfg['out_dir'], 'w') # needed srtm tiles srtm_tiles = srtm.list_srtm_tiles(cfg['images'][0]['rpc'], *cfg['roi'].values()) for s in srtm_tiles: srtm.get_srtm_tile(s, cfg['srtm_dir'])
def main(config_file): """ Launches s2p with the parameters given by a json file. Args: config_file: path to the config json file """ # read the json configuration file f = open(config_file) user_cfg = json.load(f) f.close() # Check that all the mandatory arguments are defined, and warn about # 'unknown' params check_parameters(user_cfg) # fill the config module: updates the content of the config.cfg dictionary # with the content of the user_cfg dictionary cfg.update(user_cfg) # sets keys 'clr', 'cld' and 'roi' of the reference image to None if they # are not already defined. The default values of these optional arguments # can not be defined directly in the config.py module. They would be # overwritten by the previous update, because they are in a nested dict. cfg['images'][0].setdefault('clr') cfg['images'][0].setdefault('cld') cfg['images'][0].setdefault('roi') # update roi definition if the full_img flag is set to true if ('full_img' in cfg) and cfg['full_img']: sz = common.image_size_tiffinfo(cfg['images'][0]['img']) cfg['roi'] = {} cfg['roi']['x'] = 0 cfg['roi']['y'] = 0 cfg['roi']['w'] = sz[0] cfg['roi']['h'] = sz[1] # check that the roi is well defined if 'roi' not in cfg or any(p not in cfg['roi'] for p in ['x', 'y', 'w', 'h']): print "missing or incomplete ROI definition" print "ROI will be redefined by interactive selection" x, y, w, h = common.get_roi_coordinates(cfg['images'][0]['img'], cfg['images'][0]['prv']) cfg['roi'] = {} cfg['roi']['x'] = x cfg['roi']['y'] = y cfg['roi']['w'] = w cfg['roi']['h'] = h # check the zoom factor z = cfg['subsampling_factor'] assert(z > 0 and z == np.floor(z)) # create tmp dir and output directory for the experiment, and store a json # dump of the config.cfg dictionary there if not os.path.exists(cfg['temporary_dir']): os.makedirs(cfg['temporary_dir']) if not os.path.exists(os.path.join(cfg['temporary_dir'], 'meta')): os.makedirs(os.path.join(cfg['temporary_dir'], 'meta')) if not os.path.exists(cfg['out_dir']): os.makedirs(cfg['out_dir']) f = open('%s/config.json' % cfg['out_dir'], 'w') json.dump(cfg, f, indent=2) f.close() # measure total runtime t0 = time.time() # needed srtm tiles srtm_tiles = srtm.list_srtm_tiles(cfg['images'][0]['rpc'], *cfg['roi'].values()) for s in srtm_tiles: srtm.get_srtm_tile(s, cfg['srtm_dir']) # height map if len(cfg['images']) == 2: height_map = process_pair(cfg['out_dir'], cfg['images'][0]['img'], cfg['images'][0]['rpc'], cfg['images'][1]['img'], cfg['images'][1]['rpc'], cfg['roi']['x'], cfg['roi']['y'], cfg['roi']['w'], cfg['roi']['h'], None, None, None, cfg['images'][0]['cld'], cfg['images'][0]['roi']) else: height_map = process_triplet(cfg['out_dir'], cfg['images'][0]['img'], cfg['images'][0]['rpc'], cfg['images'][1]['img'], cfg['images'][1]['rpc'], cfg['images'][2]['img'], cfg['images'][2]['rpc'], cfg['roi']['x'], cfg['roi']['y'], cfg['roi']['w'], cfg['roi']['h'], cfg['fusion_thresh'], None, None, None, None, cfg['images'][0]['cld'], cfg['images'][0]['roi']) # point cloud generate_cloud(cfg['out_dir'], height_map, cfg['images'][0]['rpc'], cfg['roi']['x'], cfg['roi']['y'], cfg['roi']['w'], cfg['roi']['h'], cfg['images'][0]['img'], cfg['images'][0]['clr'], cfg['offset_ply']) # digital surface model out_dsm = '%s/dsm.tif' % cfg['out_dir'] point_clouds_list = glob.glob('%s/cloud.ply' % cfg['out_dir']) generate_dsm(out_dsm, point_clouds_list, cfg['dsm_resolution']) # crop corresponding areas in the secondary images if not cfg['full_img']: crop_corresponding_areas(cfg['out_dir'], cfg['images'], cfg['roi']) # runtime t = int(time.time() - t0) h = t/3600 m = (t/60) % 60 s = t % 60 print "Total runtime: %dh:%dm:%ds" % (h, m, s) common.garbage_cleanup()
def init_dirs_srtm_roi(config_file): """ 1) Loads configuration file 2) Checks parameters 3) Selects the ROI 4) Checks the zoom factor 5) Creates different directories : output, temp... Args: config_file : path to a json configuration file """ # read the json configuration file f = open(config_file) user_cfg = json.load(f) f.close() # Check that all the mandatory arguments are defined, and warn about # 'unknown' params check_parameters(user_cfg) # fill the config module: updates the content of the config.cfg dictionary # with the content of the user_cfg dictionary cfg.update(user_cfg) # sets keys 'clr', 'cld' and 'roi' of the reference image to None if they # are not already defined. The default values of these optional arguments # can not be defined directly in the config.py module. They would be # overwritten by the previous update, because they are in a nested dict. cfg['images'][0].setdefault('clr') cfg['images'][0].setdefault('cld') cfg['images'][0].setdefault('roi') # update roi definition if the full_img flag is set to true if ('full_img' in cfg) and cfg['full_img']: sz = common.image_size_tiffinfo(cfg['images'][0]['img']) cfg['roi'] = {} cfg['roi']['x'] = 0 cfg['roi']['y'] = 0 cfg['roi']['w'] = sz[0] cfg['roi']['h'] = sz[1] # check that the roi is well defined if 'roi' not in cfg or any(p not in cfg['roi'] for p in ['x', 'y', 'w', 'h']): print "missing or incomplete ROI definition" print "ROI will be redefined by interactive selection" x, y, w, h = common.get_roi_coordinates(cfg['images'][0]['img'], cfg['images'][0]['prv']) cfg['roi'] = {} cfg['roi']['x'] = x cfg['roi']['y'] = y cfg['roi']['w'] = w cfg['roi']['h'] = h else: x = cfg['roi']['x'] y = cfg['roi']['y'] w = cfg['roi']['w'] h = cfg['roi']['h'] try: print "ROI x, y, w, h = %d, %d, %d, %d" % (x, y, w, h) except TypeError: print 'Neither a ROI nor a preview file are defined. Aborting.' return # check the zoom factor z = cfg['subsampling_factor'] assert(z > 0 and z == np.floor(z)) # create tmp dir and output directory for the experiment, and store a json # dump of the config.cfg dictionary there, download srtm files... if not os.path.exists(cfg['out_dir']): os.makedirs(cfg['out_dir']) if not os.path.exists(cfg['temporary_dir']): os.makedirs(cfg['temporary_dir']) if not os.path.exists(os.path.join(cfg['temporary_dir'], 'meta')): os.makedirs(os.path.join(cfg['temporary_dir'], 'meta')) f = open('%s/config.json' % cfg['out_dir'], 'w') json.dump(cfg, f, indent=2) f.close() # duplicate stdout and stderr to log file tee.Tee('%s/stdout.log' % cfg['out_dir'], 'w') # needed srtm tiles srtm_tiles = srtm.list_srtm_tiles(cfg['images'][0]['rpc'], *cfg['roi'].values()) for s in srtm_tiles: srtm.get_srtm_tile(s, cfg['srtm_dir'])