-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathancillarydata.py
175 lines (165 loc) · 6.15 KB
/
ancillarydata.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import numpy as np
import rasterio
from osgeo import ogr
def glo30list(bounds:tuple) -> list:
"""
Get the intersect copernicus glo30 data file list based on bounds and offical geojson
"""
# Open the geojson file
DEM_GEOJSON = '/vsicurl/https://asf-dem-west.s3.amazonaws.com/v2/cop30-2021.geojson'
dataSource = ogr.Open(DEM_GEOJSON)
layer = dataSource.GetLayer()
# Create an OGR geometry object from the bounds
# Create an empty polygon
extent_geom = ogr.Geometry(ogr.wkbPolygon)
# Add the vertices of the bounding box
ring = ogr.Geometry(ogr.wkbLinearRing)
ring.AddPoint(bounds[0], bounds[1])
ring.AddPoint(bounds[2], bounds[1])
ring.AddPoint(bounds[2], bounds[3])
ring.AddPoint(bounds[0], bounds[3])
ring.AddPoint(bounds[0], bounds[1])
extent_geom.AddGeometry(ring)
# Loop over features and find intersecting features
elements = []
for feature in layer:
geom = feature.GetGeometryRef()
if extent_geom.Intersects(geom):
file_path = feature.GetField('file_path')
print(file_path)
elements.append(file_path)
# Close the data source
dataSource = None
return elements
def getWO(bounds:tuple) -> list:
"""
Get water occurence data from ESA, based on the geobounds of reference tif or src.
Resample and clip with rasterio.
Returns
-------
list of webfiles
"""
Hcrop_Ex = bounds
maxLoESA = int(np.ceil(Hcrop_Ex[2] / 10) * 10)
minLoESA = int(np.floor(Hcrop_Ex[0] / 10) * 10)
maxLaESA = int(np.ceil(Hcrop_Ex[3] / 10) * 10 + 10)
minLaESA = int(np.ceil(Hcrop_Ex[1] / 10) * 10)
if maxLoESA > 0 and minLoESA > 0:
lons = [str(e) + 'E' for e in range(minLoESA, maxLoESA, 10)]
elif maxLoESA < 0 and minLoESA < 0:
lons = [str(w) + 'W' for w in np.absolute(range(minLoESA, maxLoESA, 10))]
else:
lons = [str(w) + 'W' for w in np.absolute(range(minLoESA, 0, 10))]
lons.extend([str(e) + 'E' for e in range(0, maxLoESA, 10)])
if maxLaESA > 0 and minLaESA > 0:
lats = [str(n) + 'N' for n in range(minLaESA, maxLaESA, 10)]
elif maxLaESA < 0 and minLaESA < 0:
lats = [str(s) + 'S' for s in np.absolute(range(minLaESA, maxLaESA, 10))]
else:
lats = [str(s) + 'S' for s in np.absolute(range(minLaESA, 0, 10))]
lats.extend([str(n) + 'N' for n in range(0, maxLaESA, 10)])
ESArevision = '1_3_2020'
ESAdatasets = ['occurrence']
url_tmpl, file_tmpl, _ = templatesESAwater(ESArevision)
elements=[]
for ds_name in ESAdatasets:
for lon in lons:
for lat in lats:
filename = file_tmpl.format(ds=ds_name, lon=lon, lat=lat)
url = url_tmpl.format(ds=ds_name, file=filename)
print(url)
elements.append(url)
return elements
def getGPLCC(bounds:tuple) -> list:
"""
Get the intersect copernicus glo30 data file list based on bounds and offical geojson
"""
s3_url_prefix = "https://esa-worldcover.s3.eu-central-1.amazonaws.com"
# Open the geojson file
DEM_GEOJSON = f'{s3_url_prefix}/v100/2020/esa_worldcover_2020_grid.geojson'
dataSource = ogr.Open(DEM_GEOJSON)
layer = dataSource.GetLayer()
# Create an OGR geometry object from the bounds
# Create an empty polygon
extent_geom = ogr.Geometry(ogr.wkbPolygon)
# Add the vertices of the bounding box
ring = ogr.Geometry(ogr.wkbLinearRing)
ring.AddPoint(bounds[0], bounds[1])
ring.AddPoint(bounds[2], bounds[1])
ring.AddPoint(bounds[2], bounds[3])
ring.AddPoint(bounds[0], bounds[3])
ring.AddPoint(bounds[0], bounds[1])
extent_geom.AddGeometry(ring)
# Loop over features and find intersecting features
elements = []
for feature in layer:
geom = feature.GetGeometryRef()
if extent_geom.Intersects(geom):
tile = feature.GetField('ll_tile')
file_path = f"{s3_url_prefix}/v100/2020/map/ESA_WorldCover_10m_2020_v100_{tile}_Map.tif"
print(file_path)
elements.append(file_path)
# Close the data source
dataSource = None
return elements
def getGPLCC_ori(bounds:tuple) -> list:
"""
Get land cover data from GongPeng LCC, based on the geobounds of reference tif or src.
Resample and clip with rasterio.
Returns
-------
list of webfiles
Update, 05032023
tsinghua.edu.cn is closed, use ESA world cover instead.
"""
Hcrop_Ex = bounds
LCCmainURL = "http://data.ess.tsinghua.edu.cn/data/fromglc10_2017v01/fromglc10v01"
LCCfileregex = "_LA_LO.tif"
maxLoLCC = np.ceil(Hcrop_Ex[2])
minLoLCC = np.floor(Hcrop_Ex[0])
maxLaLCC = np.ceil(Hcrop_Ex[3])
minLaLCC = np.floor(Hcrop_Ex[1])
Lo_arange = np.arange(minLoLCC - 1, maxLoLCC + 1, 1)
La_arange = np.arange(minLaLCC - 1, maxLaLCC + 1, 1)
Lo_arangeGPLCC = Lo_arange[Lo_arange % 2 == 0]
La_arangeGPLCC = La_arange[La_arange % 2 == 0]
elements = []
for Lo in Lo_arangeGPLCC:
for La in La_arangeGPLCC:
LCCfileName = LCCfileregex.replace("LO", str(int(Lo)))
LCCfileName = LCCfileName.replace("LA", str(int(La)))
url = LCCmainURL+LCCfileName
print(url)
elements.append(url)
return elements
def templatesESAwater(revision):
"""
Return url templates for ESA data download.
Parameters
----------
revision: version of the data
Returns
-------
url and filename templates as well as padding
"""
REVISIONS = ['1_0', '1_1', '1_1_2019', '1_3_2020']
v10, v11, v11_2019, v13_2020 = REVISIONS
url_tmpl = 'http://storage.googleapis.com/global-surface-water/downloads'
file_tmpl = '{ds}_{lon}_{lat}'
if revision == v10:
padding = 15
elif revision == v11:
url_tmpl += '2'
file_tmpl += '_v' + v11
padding = 20
elif revision == v11_2019:
url_tmpl += '2019v2'
file_tmpl += 'v' + v11_2019
padding = 24
elif revision == v13_2020:
url_tmpl += '2020'
file_tmpl += 'v' + v13_2020
padding = 24
url_tmpl += '/{ds}/{file}'
file_tmpl += '.tif'
return (url_tmpl, file_tmpl, padding)