def multilook_selection(out_ids: str, out_overlaps: str = None, min_pairs: int = DEF_MIN_PAIRS, min_area: int = DEF_MIN_AREA, aoi: str = None): logger.info('Searching for multilook pairs meeting thresholds:\n' 'Min. Pairs: {:,}\n' 'Min. Area: {:,}'.format(min_pairs, min_area)) multilook_pairs = get_multilook_pairs(min_pairs=min_pairs, min_area=min_area, aoi=aoi) logger.info('Writing multilook pairs to file: ' '{}'.format(out_overlaps)) write_gdf(multilook_pairs, out_overlaps) if out_ids: logger.info('Writing IDs to file: {}'.format(out_ids)) # TODO: Confirm the ID field is correct (should it be 'src_id'?) all_ids = get_ids_from_multilook(multilook_pairs, src_id_fld=constants.ID, pairname_fld=constants.PAIRNAME) with open(out_ids, 'w') as dst: for each_id in all_ids: dst.write(each_id) dst.write('\n') logger.info('Done.')
def write_scenes(scenes, out_name=None, out_path=None, out_dir=None): if out_dir: # Use search request name as output out_path = os.path.join(out_dir, '{}.geojson'.format(out_name)) # TODO: Check if out_scenes exists (earlier) abort if not overwrite logger.info('Writing selected features to file: {}'.format(out_path)) write_gdf(scenes, out_path)
def main(args): out_footprint = Path(args.out_footprint) out_format = args.format parse_directory = args.input_directory relative_directory = args.relative_directory if not relative_directory: relative_directory = parse_directory logger.info('Searching for scenes in: {}'.format(parse_directory)) planet_scenes = find_planet_scenes(parse_directory) logger.info('Found {:,} scenes to parse...'.format(len(planet_scenes))) rows = [ ps.get_footprint_row(rel_to=relative_directory) for ps in planet_scenes ] gdf = gpd.GeoDataFrame(rows) # Drop centroid column (can only write one geometry column and # center_x and center_y remain) gdf = gdf.drop(columns=constants.CENTROID) gdf.crs = CRS logger.info('Footprint created with {:,} records.'.format(len(gdf))) write_gdf(gdf, out_footprint=out_footprint, out_format=out_format)
def select_scenes(att_args, aoi_path=None, ids=None, ids_field='id', months=None, month_min_days=None, month_max_days=None, out_selection=None, onhand=False, dryrun=False): if onhand: tbl = constants.SCENES_ONHAND else: tbl = constants.SCENES if ids: selection_ids = read_ids(ids, field=ids_field) # with open(ids, 'r') as src: # selection_ids = src.readlines() # selection_ids = [i.strip() for i in selection_ids] else: selection_ids = None sql_str = build_argument_sql(att_args=att_args, months=months, ids=selection_ids, ids_field=ids_field, month_min_days=month_min_days, month_max_days=month_max_days, aoi_path=aoi_path, table=tbl) selection = make_selection(sql_str=sql_str) if out_selection and not dryrun: logger.info('Writing selected features to: {}'.format(out_selection)) write_gdf(selection, out_selection) logger.info('Done.')
def scene_retreiver(scene_ids_path=None, footprint_path=None, destination_path=None, use_shelved_struct=False, transfer_method=TM_COPY, out_footprint=None, dryrun=False): # Convert string paths to pathlib.Path if scene_ids_path: scene_ids_path = Path(scene_ids_path) if footprint_path: footprint_path = Path(footprint_path) if destination_path: destination_path = Path(destination_path) # Load selection selection = load_selection(scene_ids_path=scene_ids_path, footprint_path=footprint_path) # Locate source files scenes = locate_scenes(selection=selection, destination_path=destination_path) # Copy to destination copy_files(scenes=scenes, destination_path=destination_path, use_shelved_struct=use_shelved_struct, transfer_method=transfer_method, dryrun=dryrun) if out_footprint: if footprint_path: logger.warning('Footprint selection provided - output footprint ' 'will be identical.') # If IDs were passed and a footprint is desired # (otherwise would be identical to input footprint) logger.info('Writing footprint to file: {}'.format(out_footprint)) write_gdf(selection, out_footprint=out_footprint) logger.info('Footprint writing complete.')
def select_xtrack(aoi_path=None, where=None, out_ids=None, out_pairs_footprint=None, out_scene_footprint=None, out_pairs_csv=None, onhand=True): # Constants stereo_candidates_tbl = 'stereo_candidates' stereo_candidates_tbl_oh = 'stereo_candidates_onhand' scene_tbl = 'scenes' scene_tbl_oh = 'scenes_onhand' sid_col = 'id' id1_col = 'id1' id2_col = 'id2' geom_col = 'ovlp_geom' if onhand: stereo_tbl = stereo_candidates_tbl_oh scenes = scene_tbl_oh else: stereo_tbl = stereo_candidates_tbl scenes = scene_tbl if not where: where = '' if aoi_path: aoi = gpd.read_file(aoi_path) aoi_where = intersect_aoi_where(aoi, geom_col=geom_col) if where: where += " AND ({})".format(aoi_where) else: where = aoi_where sql_str = """SELECT * FROM {} WHERE {}""".format(stereo_tbl, where) logger.debug('SQL for stereo selection:\n{}'.format(sql_str)) with Postgres(host=constants.SANDWICH, database=constants.PLANET) as db: gdf = db.sql2gdf(sql_str=sql_str, geom_col='ovlp_geom') logger.info('Pairs found: {:,}'.format(len(gdf))) # Write footprint of pairs if out_pairs_footprint: # Convert datetime columns to str date_cols = gdf.select_dtypes(include=['datetime64']).columns for dc in date_cols: gdf[dc] = gdf[dc].apply(lambda x: x.strftime('%Y-%m-%d %H:%M:%S')) write_gdf(gdf, out_pairs_footprint) # Write footprint of individual scenes if out_scene_footprint: all_sids = list(gdf[id1_col]) + list(gdf[id2_col]) # logger.info('Total IDs before removing duplicates: {:,}'.format(len(all_sids))) all_sids = list(set(all_sids)) logger.info('Unique scene ids: {:,}'.format(len(all_sids))) sql_str = """SELECT * FROM {} WHERE {} IN ({})""".format(scenes, sid_col, str(all_sids)[1:-1]) logger.debug('SQL for selecting scenes footprint:\n{}...'.format(sql_str[:500])) with Postgres(host=constants.SANDWICH, database=constants.PLANET) as db: scene_footprint = db.sql2gdf(sql_str=sql_str) write_gdf(scene_footprint, out_scene_footprint) # Write IDs if out_ids: all_sids = list(gdf[id1_col] + list(gdf[id2_col])) logger.info('Total IDs before removing duplicates: {:,}'.format(len(all_sids))) all_sids = set(all_sids) logger.info('Unique scene ids: {:,}'.format(len(all_sids))) with open(out_ids, 'w') as dst: for sid in all_sids: dst.write(sid) dst.write('\n') if out_pairs_csv: logger.info('Writing pairs to CSV: {}'.format(out_pairs_csv)) gdf.drop(columns=gdf.geometry.name).to_csv(out_pairs_csv)