def query_removed_dams(request: Request, ): """Return dams that were removed for conservation""" log_request(request) df = unpack_domains(get_removed_dams()) return csv_response(df)
def rank_barriers(request: Request, extractor: BarriersRecordExtractor = Depends()): """Rank a subset of small barriers data. Path parameters: <layer> : one of LAYERS Query parameters: * id: list of ids * filters are defined using a lowercased version of column name and a comma-delimited list of values """ log_request(request) df = extractor.extract(ranked_barriers).copy() log.info(f"selected {len(df)} barriers for ranking") # just return tiers and lat/lon cols = ["lat", "lon"] + TIER_FIELDS + CUSTOM_TIER_FIELDS df = df.join(calculate_tiers(df))[cols] # extract extent bounds = df[["lon", "lat"]].agg(["min", "max"]).values.flatten().round(3) return csv_response(df, bounds=bounds)
def get_dam(request: Request, sarp_id: str): log_request(request) matches = dams.loc[dams.SARPID == sarp_id] if not len(matches): raise HTTPException(404, detail=f"dam not found for SARPID: {sarp_id}") # in case there are multiple dams per SARPID, take the first # (data error, should only present during testing) dam = matches.iloc[0:1].reset_index() dam.columns = [c.lower() for c in dam.columns] # use the bulk converter to dict (otherwise float32 serialization issues) # and return singular first item return JSONResponse(content=dam.to_dict(orient="records")[0])
def query_barriers(request: Request, extractor: StateRecordExtractor = Depends()): """Return subset of barriers based on state abbreviations. Query parameters: id: list of state abbreviations """ log_request(request) df = extractor.extract(barriers)[SB_PUBLIC_EXPORT_FIELDS].copy() df = df.sort_values(by="HasNetwork", ascending=False) df = unpack_domains(df) log.info(f"public query selected {len(df.index)} barriers") return csv_response(df)
def query_dams(request: Request, extractor: DamsRecordExtractor = Depends()): """Return subset of dams based on summary unit ids within layer. Path parameters: layer : one of LAYERS Query parameters: id: list of ids """ log_request(request) df = extractor.extract(ranked_dams) # extract extent bounds = df[["lon", "lat"]].agg(["min", "max"]).values.flatten().round(3) df = df[DAM_FILTER_FIELDS].copy() log.info(f"query selected {len(df)} dams") return csv_response(df, bounds)
def download_dams( request: Request, id: str, layer: Layers = "State", extractor: DamsRecordExtractor = Depends(), custom: bool = False, unranked=False, sort: Scenarios = "NCWC", format: Formats = "csv", ): """Download subset of dams or small barriers data. If `unranked` is `True`, all barriers in the summary units are downloaded. Path parameters: <layer> : one of LAYERS <format> : "csv" Query parameters: * id: list of ids * custom: bool (default: False); set to true to perform custom ranking of subset defined here * unranked: bool (default: False); set to true to include unranked barriers in output * sort: str, one of 'NC', 'WC', 'NCWC' * filters are defined using a lowercased version of column name and a comma-delimited list of values """ log_request(request) filename = f"aquatic_barrier_ranks_{date.today().isoformat()}.{format}" # See if we already already cached the response; # we only do this for unfiltered states and the entire region (ranked or unranked) cache_filename = None has_filters = any(q for q in request.query_params if q in DAM_FILTER_FIELD_MAP) if layer == "State" and format == "csv" and id and not (has_filters or custom): state_hash = sha1(id.encode("UTF8")).hexdigest() suffix = "_ranked" if not unranked else "" cache_filename = CACHE_DIRECTORY / f"{state_hash}{suffix}_dams.zip" if cache_filename and cache_filename.exists(): return zip_file_response(cache_filename, filename.replace(".csv", ".zip")) df = extractor.extract(dams).copy() # include unranked dams - these are joined back later if unranked: full_df = df.copy() # can only calculate ranks for those that have networks and are not excluded from ranking df = df.loc[df.Ranked] # calculate custom ranks if custom: df = df.join(calculate_tiers(df)) if unranked: # join back to full dataset tier_cols = df.columns.difference(full_df.columns) df = full_df.join(df[tier_cols], how="left") df[tier_cols] = df[tier_cols].fillna(-1).astype("int8") log.info(f"selected {len(df):,} dams for download") cols = [c for c in DAM_EXPORT_FIELDS if c in set(df.columns)] df = df[cols] # Sort by tier if f"{sort}_tier" in df.columns: sort_field = f"{sort}_tier" else: sort_field = f"SE_{sort}_tier" df = df.sort_values(by=["HasNetwork", sort_field], ascending=[False, True]) df = unpack_domains(df) ### Get metadata readme = get_readme( filename=filename, barrier_type="dams", fields=df.columns, url=request.base_url, layer=extractor.layer, ids=extractor.ids, ) terms = get_terms(url=request.base_url) if format == "csv": return zip_csv_response( df, filename=filename, extra_str={ "README.txt": readme, "TERMS_OF_USE.txt": terms }, extra_path={"SARP_logo.png": LOGO_PATH}, cache_filename=cache_filename, ) raise NotImplementedError("Other formats not yet supported")