forked from MrChebur/usgs-machine-to-machine-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsageExample.py
109 lines (92 loc) · 4.19 KB
/
UsageExample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import json
from usgsDataTypes import usgsDataTypes
from usgsMethods import usgsMethods
from otherMethods import otherMethods
from pprint import pprint
from multiprocessing.pool import ThreadPool
def main():
# In order not to store the login/password in the code - auth with json-formatted text file:
# {"username": "username", "password": "password"}
txt_path = r"G:\!auth\query_usgs_auth.json"
with open(txt_path, 'r') as file:
json_data = json.load(file)
usgs_username = json_data['usgs_username1']
usgs_password = json_data['usgs_password1']
api = usgsMethods()
api.login(usgs_username, usgs_password)
api.loud_mode = True
permissions = api.permissions()
print(f"Your login permissions is {permissions['data']}")
datasetName = 'LANDSAT_8_C1'
# Let's find some scenes by location!
# Region of interest coordinates. Too long coordinates list may throw 404 HTTP errors!
# Examples:
# 'Point' [lat ,lon]
# 'Polygon' [[ [lat ,lon], [lat ,lon], ... ]]
ROI = [[
[59.19852, 63.06039],
[59.62473, 64.80140],
[62.05751, 65.70580],
[62.86149, 65.26510],
[63.24590, 64.51990],
[65.99469, 64.58008],
[66.97107, 64.50871],
[67.49873, 64.08241],
[68.96698, 64.44441],
[70.34046, 64.31480],
[71.58613, 63.35573],
[73.10955, 63.37922],
[76.69435, 63.02787],
[77.98457, 62.51861],
[79.89941, 62.77741],
[81.03670, 63.14609],
[83.96038, 62.48975],
[85.97549, 61.48612],
[84.16387, 60.85305],
[82.12599, 60.53366],
[77.11535, 60.73926],
[76.67393, 59.58814],
[74.99998, 58.69959],
[72.52650, 59.15018],
[69.39066, 59.91733],
[66.74067, 58.64882],
[65.70599, 58.65047],
[61.15117, 61.67129],
[59.40793, 62.09941],
[59.19852, 63.06039],
]]
geoJson = usgsDataTypes.GeoJson(type='Polygon', coordinates=ROI)
spatialFilter = usgsDataTypes.SpatialFilterGeoJson(filterType='geojson', geoJson=geoJson)
acquisitionFilter = usgsDataTypes.AcquisitionFilter(start="2020-07-30", end="2020-07-31")
sceneFilter = usgsDataTypes.SceneFilter(acquisitionFilter=acquisitionFilter,
cloudCoverFilter=None,
datasetName=datasetName,
ingestFilter=None,
metadataFilter=None,
seasonalFilter=None,
spatialFilter=spatialFilter)
print('sceneFilter=', sceneFilter)
sceneSearchResult = api.sceneSearch(datasetName=datasetName, maxResults=1, startingNumber=None,
metadataType='full',
sortField=None,
sortDirection='ASC',
sceneFilter=sceneFilter,
compareListName=None,
bulkListName=None,
orderListName=None,
excludeListName=None)
print('sceneSearchResult=', sceneSearchResult)
print(f"\nDownloading:")
productName = 'LandsatLook Quality Image' # set 'Level-1 GeoTIFF Data Product' for archive files
for searchResult in sceneSearchResult['data']['results']:
entityId = searchResult['entityId']
filesize = otherMethods.request_filesize(api, datasetName=datasetName,
productName=productName, entityId=entityId)
print(f"The file size of {productName} with entityId={entityId} is {filesize} bytes", end='\n' * 2)
results = otherMethods.download(api, datasetName=datasetName, entityIds=entityId,
productName=productName, output_dir=r'G:\!Download')
print(results)
api.logout()
print('Done!')
if __name__ == '__main__':
main()