geowatch.cli.stac_search module¶
Performs the stac search to create the .input file needed for
geowatch.cli.queue_cli.prepare_ta2_dataset
.
- SeeAlso:
../demo/demo_region.py ../stac/stac_search_builder.py
CommandLine
# Create a demo region file
xdoctest geowatch.demo.demo_region demo_khq_region_fpath
DATASET_SUFFIX=DemoKHQ-2022-06-10-V2
DEMO_DPATH=$HOME/.cache/geowatch/demo/datasets
REGION_FPATH="$HOME/.cache/geowatch/demo/annotations/KHQ_R001.geojson"
SITE_GLOBSTR="$HOME/.cache/geowatch/demo/annotations/KHQ_R001_sites/*.geojson"
START_DATE=$(jq -r '.features[] | select(.properties.type=="region") | .properties.start_date' "$REGION_FPATH")
END_DATE=$(jq -r '.features[] | select(.properties.type=="region") | .properties.end_date' "$REGION_FPATH")
REGION_ID=$(jq -r '.features[] | select(.properties.type=="region") | .properties.region_id' "$REGION_FPATH")
SEARCH_FPATH=$DEMO_DPATH/stac_search.json
RESULT_FPATH=$DEMO_DPATH/all_sensors_kit/${REGION_ID}.input
mkdir -p "$DEMO_DPATH"
# Create the search json wrt the sensors and processing level we want
python -m geowatch.stac.stac_search_builder \
--start_date="$START_DATE" \
--end_date="$END_DATE" \
--cloud_cover=40 \
--sensors=sentinel-2-l2a \
--out_fpath "$SEARCH_FPATH"
cat "$SEARCH_FPATH"
# Delete this to prevent duplicates
rm -f "$RESULT_FPATH"
# Create the .input file
python -m geowatch.cli.stac_search \
-rf "$REGION_FPATH" \
-sj "$SEARCH_FPATH" \
-m area \
--verbose 2 \
-o "${RESULT_FPATH}"
# Construct the TA2-ready dataset
python -m geowatch.cli.queue_cli.prepare_ta2_dataset \
--dataset_suffix=$DATASET_SUFFIX \
--s3_fpath "${RESULT_FPATH}" \
--collated False \
--dvc_dpath="$DEMO_DPATH" \
--aws_profile=iarpa \
--region_globstr="$REGION_FPATH" \
--site_globstr="$SITE_GLOBSTR" \
--fields_workers=8 \
--convert_workers=8 \
--align_workers=26 \
--cache=0 \
--ignore_duplicates=0 \
--visualize=True \
--backend=serial --run=0
CommandLine
# Alternate invocation
# Create a demo region file
xdoctest geowatch.demo.demo_region demo_khq_region_fpath
DATASET_SUFFIX=DemoKHQ-2022-06-10-V3
DEMO_DPATH=$HOME/.cache/geowatch/demo/datasets
REGION_FPATH="$HOME/.cache/geowatch/demo/annotations/KHQ_R001.geojson"
REGION_ID=$(jq -r '.features[] | select(.properties.type=="region") | .properties.region_id' "$REGION_FPATH")
RESULT_FPATH=$DEMO_DPATH/all_sensors_kit/${REGION_ID}.input
mkdir -p "$DEMO_DPATH"
# Define SMART_STAC_API_KEY
source "$HOME"/code/watch/secrets/secrets
# Delete this to prevent duplicates
rm -f "$RESULT_FPATH"
# Create the .input file
python -m geowatch.cli.stac_search \
--region_file "$REGION_FPATH" \
--api_key=env:SMART_STAC_API_KEY \
--search_json "auto" \
--cloud_cover 10 \
--sensors=TA1-L8-ACC \
--mode area \
--verbose 2 \
--outfile "${RESULT_FPATH}"
CommandLine
# Alternate invocation
# Create a demo region file
DVC_DPATH=$(geowatch_dvc --tags="phase2_data" --hardware=auto)
REGION_FPATH=$DVC_DPATH/annotations/region_models/BR_R005.geojson
# Define SMART_STAC_API_KEY
source "$HOME"/code/watch/secrets/secrets
# Delete this to prevent duplicates
rm -f "$RESULT_FPATH"
# Create the .input file
python -m geowatch.cli.stac_search \
--region_file "$REGION_FPATH" \
--api_key=env:SMART_STAC_API_KEY \
--search_json "auto" \
--cloud_cover 100 \
--sensors=TA1-S2-L8-WV-PD-ACC \
--mode area \
--verbose 2 \
--outfile "./result.input"
###
### - Debug case
DVC_DPATH=$(geowatch_dvc --tags="phase2_data" --hardware=auto)
# Load SMART_STAC_API_KEY
source "$HOME"/code/watch/secrets/secrets
python -m geowatch.cli.stac_search \
--region_file "$DVC_DPATH/annotations/region_models/US_R007.geojson" \
--search_json "auto" \
--cloud_cover "0" \
--sensors "TA1-S2-L8-WV-PD-ACC-1" \
--api_key "env:SMART_STAC_API_KEY" \
--max_products_per_region "None" \
--append_mode=False \
--mode area \
--verbose 100000 \
--outfile "./test_result.input"
- class geowatch.cli.stac_search.StacSearchConfig(*args, **kwargs)[source]¶
Bases:
DataConfig
Execute a STAC query
Valid options: []
- Parameters:
*args – positional arguments for this data config
**kwargs – keyword arguments for this data config
- default = {'allow_failure': <Value(False)>, 'api_key': <Value('env:SMART_STAC_API_KEY')>, 'append_mode': <Value(True)>, 'cloud_cover': <Value(10)>, 'max_products_per_region': <Value(None)>, 'mode': <Value('id')>, 'outfile': <Value(None)>, 'query_workers': <Value(0)>, 'region_file': <Value(None)>, 'region_globstr': <Value(None)>, 's3_dest': <Value(None)>, 'search_json': <Value(None)>, 'sensors': <Value('L2')>, 'site_file': <Value(None)>, 'verbose': <Value(2)>}¶
- geowatch.cli.stac_search.main(cmdline=True, **kwargs)[source]¶
Execute the stac search and write the input file
Example
>>> # xdoctest: +REQUIRES(env:SLOW_DOCTEST) >>> # xdoctest: +REQUIRES(--network) >>> from geowatch.cli.stac_search import * # NOQA >>> from geowatch.demo import demo_region >>> from geowatch.stac import stac_search_builder >>> from kwgis.utils import util_gis >>> import ubelt as ub >>> dpath = ub.Path.appdir('geowatch/tests/test-stac-search').ensuredir() >>> search_fpath = dpath / 'stac_search.json' >>> region_fpath = demo_region.demo_khq_region_fpath() >>> region = util_gis.load_geojson(region_fpath) >>> result_fpath = dpath / 'demo.input' >>> start_date = region['start_date'].iloc[0] >>> end_date = region['end_date'].iloc[0] >>> stac_search_builder.main( >>> cmdline=0, >>> start_date=start_date, >>> end_date=end_date, >>> cloud_cover=10, >>> out_fpath=search_fpath, >>> ) >>> kwargs = { >>> 'region_file': str(region_fpath), >>> 'search_json': str(search_fpath), >>> 'mode': 'area', >>> 'verbose': 2, >>> 'outfile': str(result_fpath), >>> } >>> result_fpath.delete() >>> cmdline = 0 >>> main(cmdline=cmdline, **kwargs) >>> # results are in the >>> from geowatch.cli.baseline_framework_ingress import read_input_stac_items >>> items = read_input_stac_items(result_fpath) >>> len(items) >>> for item in items: >>> print(item['properties']['eo:cloud_cover']) >>> print(item['properties']['datetime'])
- class geowatch.cli.stac_search.StacSearcher(logger=None)[source]¶
Bases:
object
Example
>>> # xdoctest: +REQUIRES(env:GEOWATCH_ENABLE_NETWORK_TESTS) >>> # xdoctest: +REQUIRES(env:SLOW_DOCTEST) >>> from geowatch.cli.stac_search import * # NOQA >>> import tempfile >>> provider = "https://earth-search.aws.element84.com/v0" >>> geom = {'type': 'Polygon', >>> 'coordinates': [[[54.960669, 24.782276], >>> [54.960669, 25.03516], >>> [55.268326, 25.03516], >>> [55.268326, 24.782276], >>> [54.960669, 24.782276]]]} >>> collections = ["sentinel-2-l2a"] >>> start = '2020-03-14' >>> end = '2020-05-04' >>> query = {} >>> headers = {} >>> self = StacSearcher() >>> features = self.by_geometry(provider, geom, collections, start_date, end_date, >>> query, headers) >>> print('features = {}'.format(ub.urepr(features, nl=1)))