-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from ASFHyP3/develop
Release 0.3.0
- Loading branch information
Showing
19 changed files
with
304 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,17 +5,19 @@ on: push | |
jobs: | ||
pytest: | ||
runs-on: ubuntu-latest | ||
defaults: | ||
run: | ||
shell: bash -l {0} | ||
|
||
steps: | ||
- uses: actions/[email protected] | ||
|
||
- uses: actions/setup-python@v4 | ||
- uses: conda-incubator/setup-miniconda@v2 | ||
with: | ||
python-version: 3.9 | ||
|
||
- run: | | ||
python -m pip install --upgrade pip | ||
make install | ||
mamba-version: '*' | ||
python-version: '3.9' | ||
activate-environment: asf-stac | ||
environment-file: environment.yml | ||
|
||
- name: run pytest | ||
run: make test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,7 +131,6 @@ dist/ | |
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import argparse | ||
import urllib.parse | ||
from datetime import datetime, timezone | ||
from pathlib import Path, PurePath | ||
|
||
import asf_stac_util | ||
import boto3 | ||
from osgeo import gdal | ||
from shapely import geometry | ||
|
||
gdal.SetConfigOption('GDAL_DISABLE_READDIR_ON_OPEN', 'EMPTY_DIR') | ||
|
||
s3 = boto3.client('s3') | ||
|
||
COLLECTION_ID = 'glo-30-hand' | ||
|
||
|
||
def get_s3_url() -> str: | ||
bucket = 'glo-30-hand' | ||
location = s3.get_bucket_location(Bucket=bucket)['LocationConstraint'] | ||
return f'https://{bucket}.s3.{location}.amazonaws.com/' | ||
|
||
|
||
def write_stac_items(s3_keys: list[str], s3_url: str, output_file: Path) -> None: | ||
with output_file.open('w') as f: | ||
for count, s3_key in enumerate(s3_keys, start=1): | ||
print(f'Creating STAC items: {count}/{len(s3_keys)}', end='\r') | ||
gdal_info_output = gdal_info(s3_key, s3_url) | ||
stac_item = create_stac_item(s3_key, s3_url, gdal_info_output) | ||
f.write(asf_stac_util.jsonify_stac_item(stac_item) + '\n') | ||
|
||
|
||
def gdal_info(s3_key: str, s3_url: str) -> dict: | ||
url = f'/vsicurl/{urllib.parse.urljoin(s3_url, s3_key)}' | ||
return gdal.Info(url, format='json') | ||
|
||
|
||
def create_stac_item(s3_key: str, s3_url: str, gdal_info_output: dict) -> dict: | ||
item_id = PurePath(s3_key).stem | ||
item_geometry = gdal_info_output['wgs84Extent'] | ||
return { | ||
'type': 'Feature', | ||
'stac_version': '1.0.0', | ||
'id': item_id, | ||
'properties': { | ||
'datetime': None, | ||
'start_datetime': datetime(2010, 12, 1, tzinfo=timezone.utc), | ||
'end_datetime': datetime(2015, 2, 1, tzinfo=timezone.utc), | ||
}, | ||
'geometry': item_geometry, | ||
'assets': { | ||
'data': { | ||
'href': urllib.parse.urljoin(s3_url, s3_key), | ||
'type': 'image/tiff; application=geotiff', | ||
}, | ||
}, | ||
'bbox': geometry.shape(item_geometry).bounds, | ||
'stac_extensions': [], | ||
'collection': COLLECTION_ID, | ||
} | ||
|
||
|
||
def parse_args() -> argparse.Namespace: | ||
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) | ||
parser.add_argument('s3_objects', type=Path, help='Path to a text file containing the list of S3 objects') | ||
parser.add_argument('-o', '--output-file', type=Path, help='Path for the output file', | ||
default='glo-30-hand.ndjson') | ||
parser.add_argument('-n', '--number-of-items', type=int, help='Number of items to create') | ||
return parser.parse_args() | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
|
||
with args.s3_objects.open() as f: | ||
s3_keys = f.read().splitlines()[:args.number_of_items] | ||
|
||
s3_url = get_s3_url() | ||
write_stac_items(s3_keys, s3_url, args.output_file) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"type": "Collection", | ||
"id": "glo-30-hand", | ||
"title": "Global 30m Height Above Nearest Drainage (HAND)", | ||
"stac_version": "1.0.0", | ||
"description": "Height Above Nearest Drainage (HAND) is a terrain model that normalizes topography to the relative heights along the drainage network and is used to describe the relative soil gravitational potentials or the local drainage potentials. Each pixel value represents the vertical distance to the nearest drainage. The HAND data provides near-worldwide land coverage at 30 meters and was produced from the 2021 release of the Copernicus GLO-30 Public DEM as distributed in the Registry of Open Data on AWS. To generate the GLO-30 HAND, we used the ASF Tools Python package, which is based on the HydroSAR Big_Hand_notebook.ipynb and uses PySheds, a library for simple and fast watershed delineation in Python. The HAND data are provided as a tiled set of Cloud Optimized GeoTIFFs (COGs) with 30-meter (1 arcsecond) pixel spacing. The COGs are organized into the same 1 degree by 1 degree grid tiles as the GLO-30 DEM, and individual tiles are pixel-aligned to the corresponding COG DEM tile.", | ||
"links": [ | ||
{ | ||
"rel": "about", | ||
"href": "https://glo-30-hand.s3.us-west-2.amazonaws.com/readme.html", | ||
"type": "text/html" | ||
} | ||
], | ||
"extent": { | ||
"spatial": { | ||
"bbox": [ | ||
-180.0, | ||
-90.0, | ||
180.0, | ||
90.0 | ||
] | ||
}, | ||
"temporal": { | ||
"interval": [ | ||
[ | ||
"2010-12-01T00:00:00Z", | ||
"2015-02-01T00:00:00Z" | ||
] | ||
] | ||
} | ||
}, | ||
"stac_extensions": [], | ||
"license": "Creative Commons Attribution 4.0 International Public License." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env bash | ||
|
||
aws s3 ls --no-sign-request --recursive s3://glo-30-hand/v1/2021/ | cut -c '32-' | grep '.tif$' > hand-s3-objects.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import argparse | ||
import json | ||
from pathlib import Path | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--output-file', type=Path, default='collections.ndjson') | ||
parser.add_argument('collections', type=Path, nargs='+') | ||
args = parser.parse_args() | ||
|
||
with args.output_file.open('w') as output_file: | ||
for collection in args.collections: | ||
with collection.open() as f: | ||
data = json.load(f) | ||
json.dump(data, output_file) | ||
output_file.write('\n') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ channels: | |
dependencies: | ||
- python=3.9 | ||
- pip | ||
- gdal=3.6.0 | ||
- postgresql | ||
- pip: | ||
- -r requirements.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import json | ||
from datetime import datetime, timezone | ||
|
||
|
||
def jsonify_stac_item(stac_item: dict) -> str: | ||
class DateTimeEncoder(json.JSONEncoder): | ||
def default(self, obj): | ||
if isinstance(obj, datetime) and obj.tzinfo == timezone.utc: | ||
return obj.isoformat().removesuffix('+00:00') + 'Z' | ||
return json.JSONEncoder.default(self, obj) | ||
|
||
return json.dumps(stac_item, cls=DateTimeEncoder) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from setuptools import find_packages, setup | ||
|
||
setup( | ||
name='asf-stac-util', | ||
license='BSD', | ||
include_package_data=True, | ||
install_reqires=[], | ||
python_requires='~=3.9', | ||
packages=find_packages(), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.