Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 495ddf0

Browse files
committed
landsat.py test script
1 parent 56e0834 commit 495ddf0

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

utils/landsat.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
# HLS LANDSAT test
33
from pystac_client import Client
44
import json
5+
import os
6+
import requests
7+
import boto3
8+
import rasterio
9+
import rioxarray
10+
import os
11+
from rasterio.session import AWSSession
512

613
def BuildSquare(lon, lat, delta):
714
c1 = [lon + delta, lat + delta]
@@ -14,6 +21,15 @@ def BuildSquare(lon, lat, delta):
1421
return geometry
1522

1623

24+
s3_cred_endpoint = {
25+
'lpdaac':'https://data.lpdaac.earthdatacloud.nasa.gov/s3credentials'
26+
}
27+
28+
def get_temp_creds(provider):
29+
return requests.get(s3_cred_endpoint[provider]).json()
30+
31+
32+
1733
###############################################################################
1834
# MAIN
1935
###############################################################################
@@ -42,11 +58,13 @@ def BuildSquare(lon, lat, delta):
4258

4359
itemsDict = results.get_all_items_as_dict()
4460

61+
# Dumped original stack item collection to file, for testing
4562
file = 'hls.geojson'
4663
print(f"Writing reults to file {file}")
4764
with open(file, 'w') as fp:
4865
json.dump(itemsDict, fp)
4966

67+
urlList = []
5068

5169
for i in reversed(range(len(itemsDict["features"]))):
5270
del itemsDict["features"][i]["links"]
@@ -60,9 +78,58 @@ def BuildSquare(lon, lat, delta):
6078
if "title" in assetsDict[val]:
6179
del assetsDict[val]["title"]
6280

81+
# Only needed for testing temp credentials
82+
if "href" in assetsDict[val]:
83+
urlList.append(assetsDict[val]["href"])
84+
85+
86+
# Dumped trimmed dictionary as geojson file, for testing
6387
file = 'hls_trimmed.geojson'
6488
print(f"Writing reults to file {file}")
6589
with open(file, 'w') as fp:
6690
json.dump(itemsDict, fp)
6791

92+
93+
########################################################################
94+
########################################################################
95+
########################################################################
96+
# Code below tests opening rasters with AWS temp credentials from LPDAAC
97+
########################################################################
98+
########################################################################
99+
########################################################################
100+
print("Getting credentials with netrc")
101+
if os.path.isfile(os.path.expanduser('~/.netrc')):
102+
temp_creds_req = get_temp_creds('lpdaac')
103+
104+
print("Getting AWS session tokens...")
105+
session = boto3.Session(aws_access_key_id=temp_creds_req['accessKeyId'],
106+
aws_secret_access_key=temp_creds_req['secretAccessKey'],
107+
aws_session_token=temp_creds_req['sessionToken'],
108+
region_name='us-west-2')
109+
110+
111+
# NOTE: Using rioxarray assumes you are accessing a GeoTIFF
112+
rio_env = rasterio.Env(AWSSession(session),
113+
GDAL_DISABLE_READDIR_ON_OPEN='TRUE',
114+
GDAL_HTTP_COOKIEFILE=os.path.expanduser('~/cookies.txt'),
115+
GDAL_HTTP_COOKIEJAR=os.path.expanduser('~/cookies.txt'))
116+
117+
rio_env.__enter__()
118+
119+
s3List = []
120+
for e in urlList:
121+
#print(e)
122+
s3path = e.replace("https://data.lpdaac.earthdatacloud.nasa.gov/", "s3://")
123+
#print(s3path)
124+
s3List.append(s3path)
125+
126+
# Open one raster and print some info, validates if this is possible with temp credentials
127+
for e in s3List:
128+
print(e)
129+
if '.tif' in e:
130+
da = rioxarray.open_rasterio(e)
131+
print(da)
132+
break
133+
134+
68135
print("Done!")

0 commit comments

Comments
 (0)