modland 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- modland/MODIS_land_tile.py +314 -0
- modland/__init__.py +9 -0
- modland/constants.py +1 -0
- modland/dimensions.py +66 -0
- modland/find_modland_tiles.py +76 -0
- modland/get_proj4.py +29 -0
- modland/indices.py +105 -0
- modland/latlon_to_modland.py +12 -0
- modland/latlon_to_sinusoidal.py +11 -0
- modland/modland.py +37 -0
- modland/modland_affine.py +53 -0
- modland/modland_pixels.py +24 -0
- modland/outlines.py +22 -0
- modland/parsehv.py +7 -0
- modland/projection.py +13 -0
- modland/sinusoidal_to_latlon.py +20 -0
- modland/sinusoidal_to_modland.py +20 -0
- modland/transform_shape.py +27 -0
- modland/version.txt +1 -0
- modland-1.0.0.dist-info/LICENSE +201 -0
- modland-1.0.0.dist-info/METADATA +26 -0
- modland-1.0.0.dist-info/RECORD +24 -0
- modland-1.0.0.dist-info/WHEEL +5 -0
- modland-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from affine import Affine
|
|
3
|
+
from scipy.spatial.qhull import ConvexHull
|
|
4
|
+
from shapely.geometry import Polygon
|
|
5
|
+
from shapely.geometry.polygon import LinearRing
|
|
6
|
+
|
|
7
|
+
from .constants import *
|
|
8
|
+
from .dimensions import *
|
|
9
|
+
from .latlon_to_sinusoidal import latlon_to_sinusoidal
|
|
10
|
+
from .sinusoidal_to_latlon import sinusoidal_to_latlon
|
|
11
|
+
from .sinusoidal_to_modland import sinusoidal_to_modland
|
|
12
|
+
from .transform_shape import *
|
|
13
|
+
|
|
14
|
+
# encapsulation of MODIS land target at given indices
|
|
15
|
+
# affine transform of raster can be calculated given count of rows and columns
|
|
16
|
+
class MODISLandTile:
|
|
17
|
+
def __init__(self, horizontal_index, vertical_index, rows, columns):
|
|
18
|
+
|
|
19
|
+
if horizontal_index >= TOTAL_COLUMNS or horizontal_index < 0:
|
|
20
|
+
raise IndexError('horizontal index (%d) out of bounds' % horizontal_index)
|
|
21
|
+
|
|
22
|
+
if vertical_index >= TOTAL_ROWS or vertical_index < 0:
|
|
23
|
+
raise IndexError('vertical index (%d) out of bounds' % vertical_index)
|
|
24
|
+
|
|
25
|
+
if rows < 0:
|
|
26
|
+
raise ValueError('rows cannot be negative (%d)' % rows)
|
|
27
|
+
|
|
28
|
+
if columns < 0:
|
|
29
|
+
raise ValueError('columns cannot be negative (%d)' % columns)
|
|
30
|
+
|
|
31
|
+
self.horizontal_index = horizontal_index
|
|
32
|
+
self.vertical_index = vertical_index
|
|
33
|
+
self.rows = rows
|
|
34
|
+
self.columns = columns
|
|
35
|
+
|
|
36
|
+
def __str__(self):
|
|
37
|
+
return "<MODISLandTile h%02dv%02d, rows: %d, columns: %d>" \
|
|
38
|
+
% (self.horizontal_index, self.vertical_index, self.rows, self.columns)
|
|
39
|
+
|
|
40
|
+
# x coordinate of left side of sinusoidal target
|
|
41
|
+
# (upper-left corner of pixel)
|
|
42
|
+
@property
|
|
43
|
+
def x_min(self):
|
|
44
|
+
return modland_left_x_meters(self.horizontal_index)
|
|
45
|
+
|
|
46
|
+
# x coordinate of center of upper-left pixel
|
|
47
|
+
@property
|
|
48
|
+
def x_min_center(self):
|
|
49
|
+
return self.x_min + self.cell_width_meters / 2.0
|
|
50
|
+
|
|
51
|
+
# x coordinate of right side right-most pixels of sinusoidal target
|
|
52
|
+
@property
|
|
53
|
+
def x_max(self):
|
|
54
|
+
return modland_right_x_meters(self.horizontal_index)
|
|
55
|
+
|
|
56
|
+
# y coordinate of top side of sinusoidal target
|
|
57
|
+
# (upper-left corner of pixel)
|
|
58
|
+
@property
|
|
59
|
+
def y_max(self):
|
|
60
|
+
return modland_top_y_meters(self.vertical_index)
|
|
61
|
+
|
|
62
|
+
# y coordinate of center of upper-left pixel
|
|
63
|
+
@property
|
|
64
|
+
def y_max_center(self):
|
|
65
|
+
return self.y_max - self.cell_height_meters / 2.0
|
|
66
|
+
|
|
67
|
+
# y coordinate of the bottom side of bottom-most pixels of sinusoidal target
|
|
68
|
+
@property
|
|
69
|
+
def y_min(self):
|
|
70
|
+
return modland_bottom_y_meters(self.vertical_index)
|
|
71
|
+
|
|
72
|
+
# width of cell in meters given number of columns
|
|
73
|
+
@property
|
|
74
|
+
def cell_width_meters(self):
|
|
75
|
+
return modland_cell_size_meters(self.columns)
|
|
76
|
+
|
|
77
|
+
# positive height of cell in meters given number of rows
|
|
78
|
+
@property
|
|
79
|
+
def cell_height_meters(self):
|
|
80
|
+
return modland_cell_size_meters(self.rows)
|
|
81
|
+
|
|
82
|
+
# tuple of cell width and height
|
|
83
|
+
@property
|
|
84
|
+
def cell_size_meters(self):
|
|
85
|
+
return (self.cell_width_meters, self.cell_height_meters)
|
|
86
|
+
|
|
87
|
+
# affine transform (as tuple) of target given cells across target
|
|
88
|
+
@property
|
|
89
|
+
def affine_tuple(self):
|
|
90
|
+
|
|
91
|
+
# width of pixel
|
|
92
|
+
a = self.cell_width_meters
|
|
93
|
+
|
|
94
|
+
# row rotation
|
|
95
|
+
b = 0.0
|
|
96
|
+
|
|
97
|
+
# x-coordinate of upper-left corner of upper-left pixel
|
|
98
|
+
c = self.x_min
|
|
99
|
+
|
|
100
|
+
# column rotation
|
|
101
|
+
d = 0.0
|
|
102
|
+
|
|
103
|
+
# height of pixel
|
|
104
|
+
e = -1.0 * self.cell_height_meters
|
|
105
|
+
|
|
106
|
+
# y-coordinate of the upper-left corner of upper-left pixel
|
|
107
|
+
f = self.y_max
|
|
108
|
+
|
|
109
|
+
affine_transform = (a, b, c, d, e, f)
|
|
110
|
+
|
|
111
|
+
return affine_transform
|
|
112
|
+
|
|
113
|
+
# affine transform as affine.Affine
|
|
114
|
+
@property
|
|
115
|
+
def affine_transform(self):
|
|
116
|
+
return Affine(*self.affine_tuple)
|
|
117
|
+
|
|
118
|
+
@property
|
|
119
|
+
def affine_center(self):
|
|
120
|
+
return self.affine_transform * Affine.translation(0.5, 0.5)
|
|
121
|
+
|
|
122
|
+
# affine transform as tuple in ESRI world format
|
|
123
|
+
@property
|
|
124
|
+
def affine_esri(self):
|
|
125
|
+
|
|
126
|
+
# width of pixel
|
|
127
|
+
a = self.cell_width_meters
|
|
128
|
+
|
|
129
|
+
# column rotation
|
|
130
|
+
d = 0.0
|
|
131
|
+
|
|
132
|
+
# row rotation
|
|
133
|
+
b = 0.0
|
|
134
|
+
|
|
135
|
+
# height of pixel
|
|
136
|
+
e = -1.0 * self.cell_height_meters
|
|
137
|
+
|
|
138
|
+
# x coordinate of center of upper-left pixel
|
|
139
|
+
c = self.x_min_center
|
|
140
|
+
|
|
141
|
+
# y coordinate of center of upper-left pixel
|
|
142
|
+
f = self.y_max_center
|
|
143
|
+
|
|
144
|
+
affine_transform = (a, d, b, e, c, f)
|
|
145
|
+
|
|
146
|
+
return affine_transform
|
|
147
|
+
|
|
148
|
+
# save esri world file for target
|
|
149
|
+
def save_world_file(self, filename):
|
|
150
|
+
with open(filename, 'w') as f:
|
|
151
|
+
f.write('\n'.join([str(parameter) for parameter in (self.affine_esri)]))
|
|
152
|
+
|
|
153
|
+
# affine transform as tuple in GDAL format
|
|
154
|
+
@property
|
|
155
|
+
def affine_gdal(self):
|
|
156
|
+
|
|
157
|
+
# x-coordinate of upper-left corner of upper-left pixel
|
|
158
|
+
c = self.x_min
|
|
159
|
+
|
|
160
|
+
# width of pixel
|
|
161
|
+
a = self.cell_width_meters
|
|
162
|
+
|
|
163
|
+
# row rotation
|
|
164
|
+
b = 0.0
|
|
165
|
+
|
|
166
|
+
# y-coordinate of the upper-left corner of upper-left pixel
|
|
167
|
+
f = self.y_max
|
|
168
|
+
|
|
169
|
+
# column rotation
|
|
170
|
+
d = 0.0
|
|
171
|
+
|
|
172
|
+
# height of pixel
|
|
173
|
+
e = -1.0 * self.cell_height_meters
|
|
174
|
+
|
|
175
|
+
affine_transform = (c, a, b, f, d, e)
|
|
176
|
+
|
|
177
|
+
return affine_transform
|
|
178
|
+
|
|
179
|
+
# x, y sinusoidal coordinate in meters from row and column
|
|
180
|
+
# top left of pixel if center is set to False
|
|
181
|
+
# center of pixel if center is set to True
|
|
182
|
+
def sinusoidal(self, row, column, center=CENTER_PIXEL_COORDINATES_DEFAULT):
|
|
183
|
+
if row >= self.rows or row < 0:
|
|
184
|
+
raise IndexError('row (%d) out of bounds' % row)
|
|
185
|
+
|
|
186
|
+
if column >= self.columns or column < 0:
|
|
187
|
+
raise IndexError('column (%d) out of bounds' % column)
|
|
188
|
+
|
|
189
|
+
# top-left corner coordinates of pixel
|
|
190
|
+
x_sinusoidal, y_sinusoidal = self.affine_transform * (column, row)
|
|
191
|
+
|
|
192
|
+
# offset coordinates to center of pixel
|
|
193
|
+
if center:
|
|
194
|
+
x_sinusoidal += self.cell_width_meters / 2.0
|
|
195
|
+
y_sinusoidal -= self.cell_height_meters / 2.0
|
|
196
|
+
|
|
197
|
+
return (x_sinusoidal, y_sinusoidal)
|
|
198
|
+
|
|
199
|
+
# get row and column of cell at sinusoidal coordinates
|
|
200
|
+
def row_column_from_sinusoidal(self, x_sinusoidal, y_sinusoidal):
|
|
201
|
+
if x_sinusoidal < self.x_min or x_sinusoidal >= self.x_max:
|
|
202
|
+
raise ValueError('sinusoidal x coordinate (%f) out of bounds' % x_sinusoidal)
|
|
203
|
+
|
|
204
|
+
if y_sinusoidal <= self.y_min or y_sinusoidal > self.y_max:
|
|
205
|
+
raise ValueError('sinusoidal y (%f) coordinate out of bounds' % y_sinusoidal)
|
|
206
|
+
|
|
207
|
+
return tuple([int(index)
|
|
208
|
+
for index
|
|
209
|
+
in ~(self.affine_transform) * (x_sinusoidal, y_sinusoidal)])
|
|
210
|
+
|
|
211
|
+
def row_column_from_latlon(self, latitude, longitude):
|
|
212
|
+
return self.row_column_from_sinusoidal(*latlon_to_sinusoidal(latitude, longitude))
|
|
213
|
+
|
|
214
|
+
# get latitude and longitude of cell at row and column
|
|
215
|
+
# top left of pixel if center is set to False
|
|
216
|
+
# center of pixel if center is set to True
|
|
217
|
+
def latlon(self, row, column, center=CENTER_PIXEL_COORDINATES_DEFAULT):
|
|
218
|
+
if row >= self.rows or row < 0:
|
|
219
|
+
raise IndexError('row (%d) out of bounds' % row)
|
|
220
|
+
|
|
221
|
+
if column >= self.columns or column < 0:
|
|
222
|
+
raise IndexError('column (%d) out of bounds' % column)
|
|
223
|
+
|
|
224
|
+
latitude, longitude = sinusoidal_to_latlon(*self.sinusoidal(row, column, center=center))
|
|
225
|
+
|
|
226
|
+
return (latitude, longitude)
|
|
227
|
+
|
|
228
|
+
# latitude matrix and longitude matrix for coordinates of each cell in target
|
|
229
|
+
# top left of pixel if center is set to False
|
|
230
|
+
# center of pixel if center is set to True
|
|
231
|
+
def latlon_matrices(self, center=CENTER_PIXEL_COORDINATES_DEFAULT):
|
|
232
|
+
# lon, lat = sinusoidal_projection(*self.sinusoidal_matrices(center=center), inverse=True)
|
|
233
|
+
_, _, lon, lat = self.sinusoidal_matrices(center=center)
|
|
234
|
+
|
|
235
|
+
return lat, lon
|
|
236
|
+
|
|
237
|
+
# sinusoidal x and y matrices of each cell in target
|
|
238
|
+
# top left of pixel if center is set to False
|
|
239
|
+
# center of pixel if center is set to True
|
|
240
|
+
def sinusoidal_matrices(self, center=CENTER_PIXEL_COORDINATES_DEFAULT):
|
|
241
|
+
|
|
242
|
+
if center:
|
|
243
|
+
affine = self.affine_center
|
|
244
|
+
else:
|
|
245
|
+
affine = self.affine_transform
|
|
246
|
+
|
|
247
|
+
x_matrix, y_matrix = np.meshgrid(np.arange(self.columns), np.arange(self.rows)) * affine
|
|
248
|
+
lon, lat = SINUSOIDAL(x_matrix, y_matrix, inverse=True)
|
|
249
|
+
|
|
250
|
+
if self.horizontal_index < 18:
|
|
251
|
+
valid = lon < 0
|
|
252
|
+
else:
|
|
253
|
+
valid = lon >= 0
|
|
254
|
+
|
|
255
|
+
x_matrix = np.where(valid, x_matrix, np.nan)
|
|
256
|
+
y_matrix = np.where(valid, y_matrix, np.nan)
|
|
257
|
+
lon = np.where(valid, lon, np.nan)
|
|
258
|
+
lat = np.where(valid, lat, np.nan)
|
|
259
|
+
|
|
260
|
+
return x_matrix, y_matrix, lon, lat
|
|
261
|
+
|
|
262
|
+
# checks if a sinusoidal coordinate falls within the target
|
|
263
|
+
def contains_sinusoidal(self, x_sinusoidal, y_sinusoidal):
|
|
264
|
+
if x_sinusoidal < UPPER_LEFT_X_METERS or x_sinusoidal > LOWER_RIGHT_X_METERS:
|
|
265
|
+
raise ValueError('sinusoidal x coordinate (%f) out of bounds' % x_sinusoidal)
|
|
266
|
+
|
|
267
|
+
if y_sinusoidal < LOWER_RIGHT_Y_METERS or y_sinusoidal > UPPER_LEFT_Y_METERS:
|
|
268
|
+
raise ValueError('sinusoidal y (%f) coordinate out of bounds' % y_sinusoidal)
|
|
269
|
+
|
|
270
|
+
h, v = sinusoidal_to_modland(x_sinusoidal, y_sinusoidal)
|
|
271
|
+
|
|
272
|
+
return h == self.horizontal_index and v == self.vertical_index
|
|
273
|
+
|
|
274
|
+
# checks if a latitude and longitude coordinate falls within the target
|
|
275
|
+
def contains_latlon(self, latitude, longitude):
|
|
276
|
+
return self.contains_sinusoidal(*latlon_to_sinusoidal(latitude, longitude))
|
|
277
|
+
|
|
278
|
+
@property
|
|
279
|
+
def outline_sinusoidal(self):
|
|
280
|
+
upper_left_x = lower_left_x = self.x_min
|
|
281
|
+
upper_right_x = lower_right_x = self.x_max
|
|
282
|
+
upper_left_y = upper_right_y = self.y_max
|
|
283
|
+
lower_left_y = lower_right_y = self.y_min
|
|
284
|
+
|
|
285
|
+
# tests if corners exist
|
|
286
|
+
upper_left_valid = any(sinusoidal_to_latlon(upper_left_x, upper_left_y))
|
|
287
|
+
upper_right_valid = any(sinusoidal_to_latlon(upper_right_x, upper_right_y))
|
|
288
|
+
lower_right_valid = any(sinusoidal_to_latlon(lower_right_x, lower_right_y))
|
|
289
|
+
lower_left_valid = any(sinusoidal_to_latlon(lower_left_x, lower_left_y))
|
|
290
|
+
|
|
291
|
+
# case where all corners exist
|
|
292
|
+
if all([upper_left_valid, upper_right_valid, lower_right_valid, lower_left_valid]):
|
|
293
|
+
return Polygon(LinearRing([
|
|
294
|
+
[upper_left_x, upper_left_y],
|
|
295
|
+
[upper_right_x, upper_right_y],
|
|
296
|
+
[lower_right_x, lower_right_y],
|
|
297
|
+
[lower_left_x, lower_left_y]
|
|
298
|
+
]))
|
|
299
|
+
else:
|
|
300
|
+
x_matrix, y_matrix, lon, lat = self.sinusoidal_matrices()
|
|
301
|
+
points = np.dstack([x_matrix.flatten(), y_matrix.flatten()])[0]
|
|
302
|
+
points = points[~all(np.isnan(points), axis=1)]
|
|
303
|
+
hull = ConvexHull(points)
|
|
304
|
+
vertices = hull.points[hull.vertices]
|
|
305
|
+
poly = Polygon(vertices)
|
|
306
|
+
return poly
|
|
307
|
+
|
|
308
|
+
@property
|
|
309
|
+
def outline_latlon(self):
|
|
310
|
+
return transform_sinusoidal_to_latlon(self.outline_sinusoidal)
|
|
311
|
+
|
|
312
|
+
@property
|
|
313
|
+
def bounds_sinusoidal(self):
|
|
314
|
+
return self.x_min, self.y_min, self.x_max, self.y_max
|
modland/__init__.py
ADDED
modland/constants.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
CENTER_PIXEL_COORDINATES_DEFAULT = True
|
modland/dimensions.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# constants and formulas based on
|
|
2
|
+
# https://code.env.duke.edu/projects/mget/wiki/SinusoidalMODIS
|
|
3
|
+
# with slight modifications to boundaries based on proj4
|
|
4
|
+
|
|
5
|
+
# spherical earth radius (authalic radius)
|
|
6
|
+
EARTH_RADIUS_METERS = 6371007.181000
|
|
7
|
+
|
|
8
|
+
# boundaries of sinusodial projection
|
|
9
|
+
UPPER_LEFT_X_METERS = -20015109.355798
|
|
10
|
+
UPPER_LEFT_Y_METERS = 10007554.677899
|
|
11
|
+
LOWER_RIGHT_X_METERS = 20015109.355798
|
|
12
|
+
LOWER_RIGHT_Y_METERS = -10007554.677899
|
|
13
|
+
|
|
14
|
+
# size across (width or height) of any equal-area sinusoidal target
|
|
15
|
+
TILE_SIZE_METERS = 1111950.5197665554
|
|
16
|
+
|
|
17
|
+
# boundaries of MODIS land grid
|
|
18
|
+
TOTAL_ROWS = 18
|
|
19
|
+
TOTAL_COLUMNS = 36
|
|
20
|
+
|
|
21
|
+
MODLAND_TILE_SIZES = {
|
|
22
|
+
250: 4800,
|
|
23
|
+
500: 2400,
|
|
24
|
+
1000: 1200,
|
|
25
|
+
5000: 240,
|
|
26
|
+
10000: 120
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
# x coordinate of left side of sinusoidal target
|
|
30
|
+
# (upper-left corner of pixel)
|
|
31
|
+
def modland_left_x_meters(horizontal_index):
|
|
32
|
+
if horizontal_index >= TOTAL_COLUMNS or horizontal_index < 0:
|
|
33
|
+
raise IndexError('horizontal index (%d) out of bounds' % horizontal_index)
|
|
34
|
+
|
|
35
|
+
return UPPER_LEFT_X_METERS + int(horizontal_index) * TILE_SIZE_METERS
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
# x coordinate of right side of right-most pixels of sinusoidal target
|
|
39
|
+
def modland_right_x_meters(horizontal_index):
|
|
40
|
+
if horizontal_index >= TOTAL_COLUMNS or horizontal_index < 0:
|
|
41
|
+
raise IndexError('horizontal (%d) index out of bounds' % horizontal_index)
|
|
42
|
+
|
|
43
|
+
return modland_left_x_meters(horizontal_index) + TILE_SIZE_METERS
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# y coordinate of top side of sinusoidal target
|
|
48
|
+
# (upper-left corner of pixel)
|
|
49
|
+
def modland_top_y_meters(vertical_index):
|
|
50
|
+
if vertical_index >= TOTAL_ROWS or vertical_index < 0:
|
|
51
|
+
raise IndexError('vertical index (%d) out of bounds' % vertical_index)
|
|
52
|
+
|
|
53
|
+
return LOWER_RIGHT_Y_METERS + (TOTAL_ROWS - vertical_index) * TILE_SIZE_METERS
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
# y coordinate of the bottom side of bottom-most pixels of sinusoidal target
|
|
57
|
+
def modland_bottom_y_meters(vertical_index):
|
|
58
|
+
if vertical_index >= TOTAL_ROWS or vertical_index < 0:
|
|
59
|
+
raise IndexError('vertical index (%d) out of bounds' % vertical_index)
|
|
60
|
+
|
|
61
|
+
return LOWER_RIGHT_Y_METERS + (TOTAL_ROWS - 1 - vertical_index) * TILE_SIZE_METERS
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
# size across each cell in meters given the number of cells across the target
|
|
65
|
+
def modland_cell_size_meters(cells_across_tile):
|
|
66
|
+
return TILE_SIZE_METERS / cells_across_tile
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
from shapely.geometry import Polygon, MultiPolygon, Point, mapping
|
|
2
|
+
|
|
3
|
+
from .latlon_to_modland import latlon_to_modland
|
|
4
|
+
from .outlines import outline_latlon
|
|
5
|
+
|
|
6
|
+
# calculate MODIS land tiles intersecting a polygon in latitude and longitude
|
|
7
|
+
def find_modland_tiles(target_geometry_latlon, return_names=True, land_only=True):
|
|
8
|
+
"""
|
|
9
|
+
Calculate MODIS land tiles intersecting a polygon in latitude and longitude.
|
|
10
|
+
:param target_geometry_latlon: target polygon as a shapely geometry object with latitude and longitude coordinates
|
|
11
|
+
:return: set of tuples of h and v indices of MODIS land tiles intersecting target polygon
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
# list of tiles intersecting target polygon
|
|
15
|
+
intersecting_tiles = []
|
|
16
|
+
|
|
17
|
+
# list of tiles at boundary coordinates of target polygon
|
|
18
|
+
boundary_tiles = []
|
|
19
|
+
|
|
20
|
+
# calculate target at each point in the target polygon
|
|
21
|
+
|
|
22
|
+
if isinstance(target_geometry_latlon, Polygon):
|
|
23
|
+
shapes = mapping(target_geometry_latlon)['coordinates']
|
|
24
|
+
elif isinstance(target_geometry_latlon, MultiPolygon):
|
|
25
|
+
shapes = mapping(target_geometry_latlon.convex_hull)['coordinates']
|
|
26
|
+
elif isinstance(target_geometry_latlon, Point):
|
|
27
|
+
shapes = [target_geometry_latlon]
|
|
28
|
+
else:
|
|
29
|
+
raise ValueError("unsupported target geometry: {}".format(target_geometry_latlon))
|
|
30
|
+
|
|
31
|
+
# iterate through shapes
|
|
32
|
+
for shape in shapes:
|
|
33
|
+
|
|
34
|
+
if isinstance(shape, Point):
|
|
35
|
+
shape = [shape]
|
|
36
|
+
|
|
37
|
+
# iterate through coordinates
|
|
38
|
+
for coordinate in shape:
|
|
39
|
+
if isinstance(coordinate, Point):
|
|
40
|
+
longitude = coordinate.x
|
|
41
|
+
latitude = coordinate.y
|
|
42
|
+
else:
|
|
43
|
+
# pull latitude and longitude from coordinate
|
|
44
|
+
longitude, latitude = coordinate
|
|
45
|
+
|
|
46
|
+
# calculate target at coordinate
|
|
47
|
+
tile = latlon_to_modland(latitude, longitude)
|
|
48
|
+
|
|
49
|
+
# add target to list
|
|
50
|
+
boundary_tiles += [tile]
|
|
51
|
+
|
|
52
|
+
# set of tiles at boundary coordinates of target polygon
|
|
53
|
+
boundary_tiles = set(boundary_tiles)
|
|
54
|
+
|
|
55
|
+
# pull h and v indices from set of tiles
|
|
56
|
+
horizontal_indices, vertical_indices = zip(*boundary_tiles)
|
|
57
|
+
|
|
58
|
+
# iterate through range of horizontal indices
|
|
59
|
+
for h in range(min(horizontal_indices), max(horizontal_indices) + 1):
|
|
60
|
+
|
|
61
|
+
# iterate through range of vertical indices
|
|
62
|
+
for v in range(min(vertical_indices), max(vertical_indices) + 1):
|
|
63
|
+
polygon = outline_latlon(h, v)
|
|
64
|
+
|
|
65
|
+
# check if target intersects target polygon
|
|
66
|
+
if polygon.intersects(target_geometry_latlon):
|
|
67
|
+
# add target to list
|
|
68
|
+
intersecting_tiles += [(h, v)]
|
|
69
|
+
|
|
70
|
+
# set of tiles intersecting target polygon
|
|
71
|
+
intersecting_tiles = set(intersecting_tiles)
|
|
72
|
+
|
|
73
|
+
if return_names:
|
|
74
|
+
return sorted(["h{:02d}v{:02d}".format(h, v) for h, v in intersecting_tiles])
|
|
75
|
+
else:
|
|
76
|
+
return intersecting_tiles
|
modland/get_proj4.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
import warnings
|
|
3
|
+
import pyproj
|
|
4
|
+
from pyproj import Proj
|
|
5
|
+
import rasterio
|
|
6
|
+
|
|
7
|
+
def get_proj4(projection: Union[Proj, str]) -> str:
|
|
8
|
+
with warnings.catch_warnings():
|
|
9
|
+
warnings.simplefilter("ignore")
|
|
10
|
+
|
|
11
|
+
if isinstance(projection, Proj):
|
|
12
|
+
result = projection.crs.to_proj4()
|
|
13
|
+
|
|
14
|
+
elif isinstance(projection, rasterio.crs.CRS):
|
|
15
|
+
result = projection.to_proj4()
|
|
16
|
+
|
|
17
|
+
elif isinstance(projection, pyproj.crs.crs.CRS):
|
|
18
|
+
result = projection.to_proj4()
|
|
19
|
+
|
|
20
|
+
elif isinstance(projection, str):
|
|
21
|
+
# result = pycrs.parse.from_unknown_text(projection).to_proj4()
|
|
22
|
+
result = pyproj.CRS(projection).to_proj4()
|
|
23
|
+
|
|
24
|
+
else:
|
|
25
|
+
raise ValueError(f"projection not recognized ({type(projection)})")
|
|
26
|
+
|
|
27
|
+
result = result.replace("+shell=epsg:", "epsg:")
|
|
28
|
+
|
|
29
|
+
return result
|
modland/indices.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
import rasters as rt
|
|
5
|
+
from rasters import Raster, RasterGeometry
|
|
6
|
+
|
|
7
|
+
from .find_modland_tiles import find_modland_tiles
|
|
8
|
+
from .modland_affine import *
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def calculate_global_modland_indices(h, v, tile_size):
|
|
12
|
+
global_modis_affine = calculate_modland_affine(0, 0, tile_size)
|
|
13
|
+
affine = calculate_modland_affine(h, v, tile_size)
|
|
14
|
+
tile_col_indices, tile_row_indices = np.meshgrid(np.arange(tile_size), np.arange(tile_size))
|
|
15
|
+
x, y = affine * (tile_col_indices, tile_row_indices)
|
|
16
|
+
global_col_indices, global_row_indices = ~global_modis_affine * (x, y)
|
|
17
|
+
|
|
18
|
+
return global_row_indices, global_col_indices
|
|
19
|
+
|
|
20
|
+
def calculate_global_modland_serial_indices_hv(h, v, tile_size):
|
|
21
|
+
global_row_indices, global_col_indices = calculate_global_modland_indices(h, v, tile_size)
|
|
22
|
+
serial_index = global_row_indices * tile_size * 36 + global_col_indices
|
|
23
|
+
serial_index = np.int32(serial_index)
|
|
24
|
+
grid = generate_modland_grid(h, v, serial_index.shape[0])
|
|
25
|
+
raster = Raster(serial_index, geometry=grid)
|
|
26
|
+
|
|
27
|
+
return raster
|
|
28
|
+
|
|
29
|
+
def calculate_global_modland_serial_indices(tile, tile_size):
|
|
30
|
+
h = int(tile[1:3])
|
|
31
|
+
v = int(tile[4:6])
|
|
32
|
+
indices = calculate_global_modland_serial_indices_hv(h, v, tile_size)
|
|
33
|
+
|
|
34
|
+
return indices
|
|
35
|
+
|
|
36
|
+
def generate_modland_500m(fine_geometry: RasterGeometry, coarse_resolution: int = None) -> Raster:
|
|
37
|
+
logger = logging.getLogger(__name__)
|
|
38
|
+
|
|
39
|
+
FILL_VALUE = -9999
|
|
40
|
+
DEFAULT_COARSE_RESOLUTION = 490
|
|
41
|
+
|
|
42
|
+
if coarse_resolution is None:
|
|
43
|
+
coarse_resolution = DEFAULT_COARSE_RESOLUTION
|
|
44
|
+
|
|
45
|
+
tiles = find_modland_tiles(fine_geometry.boundary_latlon, return_names=True)
|
|
46
|
+
geometry500m = fine_geometry.rescale(coarse_resolution)
|
|
47
|
+
indices500m = np.full(geometry500m.shape, FILL_VALUE, dtype=np.int32)
|
|
48
|
+
|
|
49
|
+
for tile in tiles:
|
|
50
|
+
native_indices = calculate_global_modland_serial_indices(tile, 2400)
|
|
51
|
+
coarse_indices = native_indices.to_geometry(geometry500m, nodata=FILL_VALUE)
|
|
52
|
+
indices500m = rt.where(indices500m == FILL_VALUE, coarse_indices, indices500m)
|
|
53
|
+
|
|
54
|
+
if np.any(indices500m == FILL_VALUE):
|
|
55
|
+
unfilled_proportion = np.count_nonzero(indices500m == FILL_VALUE) / indices500m.size
|
|
56
|
+
logger.warning(f"{round(unfilled_proportion * 100):0.2f} of fine pixels left unfilled")
|
|
57
|
+
|
|
58
|
+
indices500m.nodata = FILL_VALUE
|
|
59
|
+
|
|
60
|
+
return indices500m
|
|
61
|
+
|
|
62
|
+
def generate_modland_1000m(fine_geometry: RasterGeometry, coarse_resolution: int = None) -> Raster:
|
|
63
|
+
logger = logging.getLogger(__name__)
|
|
64
|
+
|
|
65
|
+
FILL_VALUE = -9999
|
|
66
|
+
DEFAULT_COARSE_RESOLUTION = 980
|
|
67
|
+
|
|
68
|
+
if coarse_resolution is None:
|
|
69
|
+
coarse_resolution = DEFAULT_COARSE_RESOLUTION
|
|
70
|
+
|
|
71
|
+
tiles = find_modland_tiles(fine_geometry.boundary_latlon, return_names=True)
|
|
72
|
+
geometry = fine_geometry.rescale(coarse_resolution)
|
|
73
|
+
indices = np.full(geometry.shape, FILL_VALUE, dtype=np.int32)
|
|
74
|
+
|
|
75
|
+
for tile in tiles:
|
|
76
|
+
native_indices = calculate_global_modland_serial_indices(tile, 1200)
|
|
77
|
+
coarse_indices = native_indices.to_geometry(geometry, nodata=FILL_VALUE)
|
|
78
|
+
indices = rt.where(indices == FILL_VALUE, coarse_indices, indices)
|
|
79
|
+
|
|
80
|
+
if np.any(indices == FILL_VALUE):
|
|
81
|
+
unfilled_proportion = np.count_nonzero(indices == FILL_VALUE) / indices.size
|
|
82
|
+
logger.warning(f"{round(unfilled_proportion * 100):0.2f} of fine pixels left unfilled")
|
|
83
|
+
|
|
84
|
+
indices.nodata = FILL_VALUE
|
|
85
|
+
|
|
86
|
+
return indices
|
|
87
|
+
|
|
88
|
+
def calculate_global_modland_columns(spatial_resolution):
|
|
89
|
+
tile_size = MODLAND_TILE_SIZES[spatial_resolution]
|
|
90
|
+
global_modland_columns = tile_size * 36
|
|
91
|
+
|
|
92
|
+
return global_modland_columns
|
|
93
|
+
|
|
94
|
+
def generate_modland_indices(geometry: RasterGeometry, spatial_resolution: float) -> Raster:
|
|
95
|
+
x, y = geometry.get_xy(projection=SINUSOIDAL_PROJECTION)
|
|
96
|
+
global_modland_affine = calculate_global_modland_affine(spatial_resolution)
|
|
97
|
+
global_col_indices, global_row_indices = ~global_modland_affine * (x, y)
|
|
98
|
+
global_col_indices = global_col_indices.astype(np.int32)
|
|
99
|
+
global_row_indices = global_row_indices.astype(np.int32)
|
|
100
|
+
global_modland_columns = calculate_global_modland_columns(spatial_resolution)
|
|
101
|
+
serial_index = global_row_indices * global_modland_columns + global_col_indices
|
|
102
|
+
serial_index = serial_index.astype(np.int32)
|
|
103
|
+
serial_index = Raster(serial_index, geometry=geometry)
|
|
104
|
+
|
|
105
|
+
return serial_index
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from .sinusoidal_to_modland import sinusoidal_to_modland
|
|
2
|
+
from .latlon_to_sinusoidal import latlon_to_sinusoidal
|
|
3
|
+
|
|
4
|
+
# MODIS land target indices for target containing latitude and longitude
|
|
5
|
+
def latlon_to_modland(latitude, longitude):
|
|
6
|
+
if latitude < -90 or latitude > 90:
|
|
7
|
+
raise ValueError('latitude (%f) out of bounds' % latitude)
|
|
8
|
+
|
|
9
|
+
if longitude < -180 or longitude > 180:
|
|
10
|
+
raise ValueError('longitude (%f) out of bounds' % longitude)
|
|
11
|
+
|
|
12
|
+
return sinusoidal_to_modland(*latlon_to_sinusoidal(latitude, longitude))
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from .projection import SINUSOIDAL
|
|
2
|
+
|
|
3
|
+
# transforms WGS84 latitude and longitude to sinusoidal coordinates in meters
|
|
4
|
+
def latlon_to_sinusoidal(latitude, longitude):
|
|
5
|
+
if latitude < -90 or latitude > 90:
|
|
6
|
+
raise ValueError('latitude (%f) out of bounds' % latitude)
|
|
7
|
+
|
|
8
|
+
if longitude < -180 or longitude > 180:
|
|
9
|
+
raise ValueError('longitude (%f) out of bounds' % longitude)
|
|
10
|
+
|
|
11
|
+
return SINUSOIDAL(longitude, latitude)
|
modland/modland.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module handles georeferencing MODIS land tiles.
|
|
3
|
+
"""
|
|
4
|
+
# rotation in affine transformation is not supported
|
|
5
|
+
# latitude and longitude are in WGS84 geographic coordinate system
|
|
6
|
+
import warnings
|
|
7
|
+
import geopandas as gpd
|
|
8
|
+
import numpy as np
|
|
9
|
+
import pyproj
|
|
10
|
+
import rasterio
|
|
11
|
+
from affine import Affine
|
|
12
|
+
from numpy import where, nan, isnan, all
|
|
13
|
+
from pyproj import Proj
|
|
14
|
+
from scipy.spatial.qhull import ConvexHull
|
|
15
|
+
from shapely.geometry import Polygon, Point, MultiPolygon
|
|
16
|
+
from shapely.geometry import mapping
|
|
17
|
+
from shapely.geometry.base import BaseGeometry
|
|
18
|
+
from shapely.geometry.polygon import LinearRing
|
|
19
|
+
|
|
20
|
+
from .constants import *
|
|
21
|
+
from .projection import *
|
|
22
|
+
from .dimensions import *
|
|
23
|
+
from .get_proj4 import get_proj4
|
|
24
|
+
from .transform_shape import *
|
|
25
|
+
from .latlon_to_sinusoidal import latlon_to_sinusoidal
|
|
26
|
+
from .sinusoidal_to_latlon import sinusoidal_to_latlon
|
|
27
|
+
from .sinusoidal_to_modland import sinusoidal_to_modland
|
|
28
|
+
from .latlon_to_modland import latlon_to_modland
|
|
29
|
+
from .MODIS_land_tile import MODISLandTile
|
|
30
|
+
from .modland_pixels import *
|
|
31
|
+
from .outlines import *
|
|
32
|
+
from .find_modland_tiles import find_modland_tiles
|
|
33
|
+
from .modland_affine import *
|
|
34
|
+
from .parsehv import parsehv
|
|
35
|
+
from .indices import *
|
|
36
|
+
|
|
37
|
+
__author__ = 'Gregory H. Halverson'
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from affine import Affine
|
|
2
|
+
|
|
3
|
+
from rasters import RasterGrid
|
|
4
|
+
|
|
5
|
+
from .projection import *
|
|
6
|
+
from .dimensions import MODLAND_TILE_SIZES
|
|
7
|
+
|
|
8
|
+
def calculate_modland_affine(h, v, tile_size):
|
|
9
|
+
# boundaries of sinusodial projection
|
|
10
|
+
UPPER_LEFT_X_METERS = -20015109.355798
|
|
11
|
+
UPPER_LEFT_Y_METERS = 10007554.677899
|
|
12
|
+
LOWER_RIGHT_X_METERS = 20015109.355798
|
|
13
|
+
LOWER_RIGHT_Y_METERS = -10007554.677899
|
|
14
|
+
|
|
15
|
+
# size across (width or height) of any equal-area sinusoidal target
|
|
16
|
+
TILE_SIZE_METERS = 1111950.5197665554
|
|
17
|
+
|
|
18
|
+
# boundaries of MODIS land grid
|
|
19
|
+
TOTAL_ROWS = 18
|
|
20
|
+
TOTAL_COLUMNS = 36
|
|
21
|
+
|
|
22
|
+
y_max = LOWER_RIGHT_Y_METERS + (TOTAL_ROWS - v) * TILE_SIZE_METERS
|
|
23
|
+
x_min = UPPER_LEFT_X_METERS + int(h) * TILE_SIZE_METERS
|
|
24
|
+
|
|
25
|
+
cell_size = TILE_SIZE_METERS / tile_size
|
|
26
|
+
|
|
27
|
+
# width of pixel
|
|
28
|
+
a = cell_size
|
|
29
|
+
# row rotation
|
|
30
|
+
b = 0.0
|
|
31
|
+
# x-coordinate of upper-left corner of upper-left pixel
|
|
32
|
+
c = x_min
|
|
33
|
+
# column rotation
|
|
34
|
+
d = 0.0
|
|
35
|
+
# height of pixel
|
|
36
|
+
e = -1.0 * cell_size
|
|
37
|
+
# y-coordinate of the upper-left corner of upper-left pixel
|
|
38
|
+
f = y_max
|
|
39
|
+
affine = Affine(a, b, c, d, e, f)
|
|
40
|
+
|
|
41
|
+
return affine
|
|
42
|
+
|
|
43
|
+
def calculate_global_modland_affine(spatial_resolution):
|
|
44
|
+
tile_size = MODLAND_TILE_SIZES[spatial_resolution]
|
|
45
|
+
affine = calculate_modland_affine(0, 0, tile_size)
|
|
46
|
+
|
|
47
|
+
return affine
|
|
48
|
+
|
|
49
|
+
def generate_modland_grid(h, v, tile_size):
|
|
50
|
+
affine = calculate_modland_affine(h, v, tile_size)
|
|
51
|
+
grid = RasterGrid.from_affine(affine, tile_size, tile_size, crs=SINUSOIDAL_PROJECTION)
|
|
52
|
+
|
|
53
|
+
return grid
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from .MODIS_land_tile import MODISLandTile
|
|
2
|
+
from .latlon_to_modland import latlon_to_modland
|
|
3
|
+
from .sinusoidal_to_modland import sinusoidal_to_modland
|
|
4
|
+
|
|
5
|
+
# finds target containing a latitude and longitude coordinate
|
|
6
|
+
# then finds pixel within that target nearest to coordinate given size of matrix
|
|
7
|
+
# returns horizontal index, vertical index, row, and column as tuple
|
|
8
|
+
def latlon_to_modland_pixel(latitude, longitude, rows_per_tile, columns_per_tile):
|
|
9
|
+
horizontal_index, vertical_index = latlon_to_modland(latitude, longitude)
|
|
10
|
+
tile = MODISLandTile(horizontal_index, vertical_index, rows_per_tile, columns_per_tile)
|
|
11
|
+
row, column = tile.row_column_from_latlon(latitude, longitude)
|
|
12
|
+
|
|
13
|
+
return horizontal_index, vertical_index, row, column
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# finds target containing a sinusoidal coordinate
|
|
17
|
+
# then finds pixel within that target nearest to coordinate given size of matrix
|
|
18
|
+
# returns horizontal index, vertical index, row, and column as tuple
|
|
19
|
+
def sinusoidal_to_modland_pixel(x_sinusoidal, y_sinusoidal, rows_per_tile, columns_per_tile):
|
|
20
|
+
horizontal_index, vertical_index = sinusoidal_to_modland(x_sinusoidal, y_sinusoidal)
|
|
21
|
+
tile = MODISLandTile(horizontal_index, vertical_index, rows_per_tile, columns_per_tile)
|
|
22
|
+
row, column = tile.row_column_from_sinusoidal(x_sinusoidal, y_sinusoidal)
|
|
23
|
+
|
|
24
|
+
return horizontal_index, vertical_index, row, column
|
modland/outlines.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from .MODIS_land_tile import MODISLandTile
|
|
2
|
+
|
|
3
|
+
# calculate polygon outline of MODIS land target in sinusoidal coordinates
|
|
4
|
+
def outline_sinusoidal(h, v):
|
|
5
|
+
"""
|
|
6
|
+
Calculate polygon outline of MODIS land target in sinusoidal coordinates.
|
|
7
|
+
:param h: horizontal index of MODIS land target
|
|
8
|
+
:param v: vertical index of MODIS land target
|
|
9
|
+
:return: shapely geometry object containing polygon outline of MODIS land target in sinusoidal coordinates
|
|
10
|
+
"""
|
|
11
|
+
return MODISLandTile(h, v, 100, 100).outline_sinusoidal()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# calculate polygon outline of MODIS land target in latitude and longitude
|
|
15
|
+
def outline_latlon(h, v):
|
|
16
|
+
"""
|
|
17
|
+
Calculate polygon outline of MODIS land target in latitude and longitude.
|
|
18
|
+
:param h: horizontal index of MODIS land target
|
|
19
|
+
:param v: vertical index of MODIS land target
|
|
20
|
+
:return: shapely geometry object containing polygon outline of MODIS land target in latitude and longitude
|
|
21
|
+
"""
|
|
22
|
+
return MODISLandTile(h, v, 100, 100).outline_latlon
|
modland/parsehv.py
ADDED
modland/projection.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from pyproj import Proj
|
|
2
|
+
|
|
3
|
+
MODIS_LAND_TILE_PROJECTION_WKT = 'PROJCS["unnamed",GEOGCS["Unknown datum based upon the custom spheroid",DATUM["Not_specified_based_on_custom_spheroid",SPHEROID["Custom spheroid",6371007.181,0]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]],PROJECTION["Sinusoidal"],PARAMETER["longitude_of_center",0],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]]]'
|
|
4
|
+
MODIS_LAND_TILE_PROJECTION_PROJ4 = '+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs'
|
|
5
|
+
MODIS_LAND_TILE_PROJECTION_PCI = ['SIN E700', 'METRE',
|
|
6
|
+
(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)]
|
|
7
|
+
MODIS_LAND_TILE_PROJECTION_MI = 'Earth Projection 16, 104, "m", 0'
|
|
8
|
+
MODIS_LAND_TILE_PROJECTION_EPSG = 6842
|
|
9
|
+
|
|
10
|
+
SINUSOIDAL = Proj(MODIS_LAND_TILE_PROJECTION_PROJ4)
|
|
11
|
+
WGS84 = Proj('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ')
|
|
12
|
+
|
|
13
|
+
SINUSOIDAL_PROJECTION = "+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs"
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from .projection import SINUSOIDAL
|
|
2
|
+
from .dimensions import *
|
|
3
|
+
|
|
4
|
+
# transforms sinusoidal coordinates in meters to WGS84 latitude and longitude
|
|
5
|
+
def sinusoidal_to_latlon(x_sinusoidal, y_sinusoidal):
|
|
6
|
+
if x_sinusoidal < UPPER_LEFT_X_METERS or x_sinusoidal > LOWER_RIGHT_X_METERS:
|
|
7
|
+
raise ValueError('sinusoidal x coordinate (%f) out of bounds' % x_sinusoidal)
|
|
8
|
+
|
|
9
|
+
if y_sinusoidal < LOWER_RIGHT_Y_METERS or y_sinusoidal > UPPER_LEFT_Y_METERS:
|
|
10
|
+
raise ValueError('sinusoidal y (%f) coordinate out of bounds' % y_sinusoidal)
|
|
11
|
+
|
|
12
|
+
longitude, latitude = SINUSOIDAL(x_sinusoidal, y_sinusoidal, inverse=True)
|
|
13
|
+
|
|
14
|
+
if x_sinusoidal < 0 and x_sinusoidal < SINUSOIDAL(-180, latitude)[0]:
|
|
15
|
+
return None, None
|
|
16
|
+
|
|
17
|
+
if x_sinusoidal > 0 and x_sinusoidal > SINUSOIDAL(180, latitude)[0]:
|
|
18
|
+
return None, None
|
|
19
|
+
|
|
20
|
+
return latitude, longitude
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from .dimensions import *
|
|
2
|
+
|
|
3
|
+
# MODIS land target indices for target containing sinusoidal coordinate
|
|
4
|
+
def sinusoidal_to_modland(x_sinusoidal, y_sinusoidal):
|
|
5
|
+
if x_sinusoidal < UPPER_LEFT_X_METERS or x_sinusoidal > LOWER_RIGHT_X_METERS:
|
|
6
|
+
raise ValueError('sinusoidal x coordinate (%f) out of bounds' % x_sinusoidal)
|
|
7
|
+
|
|
8
|
+
if y_sinusoidal < LOWER_RIGHT_Y_METERS or y_sinusoidal > UPPER_LEFT_Y_METERS:
|
|
9
|
+
raise ValueError('sinusoidal y (%f) coordinate out of bounds' % y_sinusoidal)
|
|
10
|
+
|
|
11
|
+
horizontal_index = int((x_sinusoidal - UPPER_LEFT_X_METERS) / TILE_SIZE_METERS)
|
|
12
|
+
vertical_index = int((-1 * (y_sinusoidal + LOWER_RIGHT_Y_METERS)) / TILE_SIZE_METERS)
|
|
13
|
+
|
|
14
|
+
if horizontal_index == TOTAL_COLUMNS:
|
|
15
|
+
horizontal_index -= 1
|
|
16
|
+
|
|
17
|
+
if vertical_index == TOTAL_ROWS:
|
|
18
|
+
vertical_index -= 1
|
|
19
|
+
|
|
20
|
+
return horizontal_index, vertical_index
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
import geopandas as gpd
|
|
3
|
+
from shapely.geometry.base import BaseGeometry
|
|
4
|
+
|
|
5
|
+
from .get_proj4 import get_proj4
|
|
6
|
+
from .projection import *
|
|
7
|
+
|
|
8
|
+
def transform_shape(
|
|
9
|
+
shape: BaseGeometry,
|
|
10
|
+
source_projection: Union[Proj, str],
|
|
11
|
+
target_projection: Union[Proj, str]) -> BaseGeometry:
|
|
12
|
+
if not isinstance(shape, BaseGeometry):
|
|
13
|
+
raise ValueError("invalid shape")
|
|
14
|
+
|
|
15
|
+
# TODO need to stop relying on deprecated proj4 as common projection encoding
|
|
16
|
+
source_projection = get_proj4(source_projection)
|
|
17
|
+
target_projection = get_proj4(target_projection)
|
|
18
|
+
projected_shape = gpd.GeoDataFrame({}, geometry=[shape], crs=source_projection).to_crs(target_projection).geometry[
|
|
19
|
+
0]
|
|
20
|
+
|
|
21
|
+
return projected_shape
|
|
22
|
+
|
|
23
|
+
def transform_sinusoidal_to_latlon(shape: BaseGeometry) -> BaseGeometry:
|
|
24
|
+
return transform_shape(shape, SINUSOIDAL, WGS84)
|
|
25
|
+
|
|
26
|
+
def transform_latlon_to_sinusoidal(shape: BaseGeometry) -> BaseGeometry:
|
|
27
|
+
return transform_shape(shape, WGS84, SINUSOIDAL)
|
modland/version.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
1.0.0
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: modland
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: generates rasters of field capacity and wilting point from the SoilGrids dataset
|
|
5
|
+
Author-email: "Gregory H. Halverson" <gregory.h.halverson@jpl.nasa.gov>
|
|
6
|
+
Classifier: Programming Language :: Python :: 3
|
|
7
|
+
Classifier: Operating System :: OS Independent
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Dist: affine
|
|
12
|
+
Requires-Dist: geopandas
|
|
13
|
+
Requires-Dist: numpy
|
|
14
|
+
Requires-Dist: pyproj
|
|
15
|
+
Requires-Dist: rasterio
|
|
16
|
+
Requires-Dist: rasters
|
|
17
|
+
Requires-Dist: scipy
|
|
18
|
+
Requires-Dist: shapely
|
|
19
|
+
|
|
20
|
+
# `modland` Python Package
|
|
21
|
+
|
|
22
|
+
The `modland` Python package generates georeferencing information for [MODIS/VIIRS Sinusoidal tiles](https://modis-land.gsfc.nasa.gov/MODLAND_grid.html).
|
|
23
|
+
|
|
24
|
+
[Gregory H. Halverson](https://github.com/gregory-halverson-jpl) (they/them)<br>
|
|
25
|
+
[gregory.h.halverson@jpl.nasa.gov](mailto:gregory.h.halverson@jpl.nasa.gov)<br>
|
|
26
|
+
NASA Jet Propulsion Laboratory 329G
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
modland/MODIS_land_tile.py,sha256=471WZ60HoMGmRCzHPWjb4zcrl3nSYngk1D2FAp9CL9o,10962
|
|
2
|
+
modland/__init__.py,sha256=T1zhwNEpMI-vDE9RstcRHmjZO_GkKO2Dd95uOxCamgw,215
|
|
3
|
+
modland/constants.py,sha256=_svpG-KYNMr12pluH_GpQjwTx6mJafABUplJQbiMnRw,40
|
|
4
|
+
modland/dimensions.py,sha256=VZy2uVJAndvhAO4a1lWPj8SRKRVYtF9z6p-FMViW-Ps,2269
|
|
5
|
+
modland/find_modland_tiles.py,sha256=_erawBvX3w624uX_VaDWarmZLPfpzNlj3JfXV4ok49A,2878
|
|
6
|
+
modland/get_proj4.py,sha256=B6dldk8zcehv_QhmxKaP1iirbfxhb0MbH-i2FMliJuE,855
|
|
7
|
+
modland/indices.py,sha256=bNUzhKeh3eG-O0qtes1wJ7PRqBNg0CP0MeT1sYjoseg,4276
|
|
8
|
+
modland/latlon_to_modland.py,sha256=c3SkjEppEu2eKOCjJ28nw9gzoQ6Xo40O7UZ2UfNZJJM,529
|
|
9
|
+
modland/latlon_to_sinusoidal.py,sha256=SCm5QBO20W92fpB0_K6AOaA2YLgAOtO5Kuyut8WI7CE,426
|
|
10
|
+
modland/modland.py,sha256=l00QZcTOSiDbTOXBp4RZoLWirjIAto6bWO_4q6hR040,1219
|
|
11
|
+
modland/modland_affine.py,sha256=bqwRJvBSkG85aAt0BlvflbrPfZ5Ovt4TK629CTdfoa4,1519
|
|
12
|
+
modland/modland_pixels.py,sha256=6clWWR0uMYHR1RxoWKZDNY8kx4jAJxZKRM_02OOtXek,1353
|
|
13
|
+
modland/outlines.py,sha256=4YYipZdPyXlFR_-wPBDyaONPW0ltGLV0R9zK6NHj2KU,984
|
|
14
|
+
modland/parsehv.py,sha256=qUzxDg3vhnc3b69M2aM9NwGqiWM-7C5NHgsaP3psmVg,153
|
|
15
|
+
modland/projection.py,sha256=MOnqw_sA69K2fzbcl-4PGdaS77GsXOAOdFgYVrMgwDo,1076
|
|
16
|
+
modland/sinusoidal_to_latlon.py,sha256=Y_Bf80irSjNBYFgI03b1ZKGpkIaWpYFyAepHjWoz5ZM,844
|
|
17
|
+
modland/sinusoidal_to_modland.py,sha256=tN8APZvdi9h-iqP2dYwF0DkDRiJatWcFofy5gBOx_Hs,850
|
|
18
|
+
modland/transform_shape.py,sha256=Okv0Y1OPp-EVt4ItKSPTcmzJ_LWcH10VqX_1LMhNPws,1002
|
|
19
|
+
modland/version.txt,sha256=klIfw8vZZL3J9YSpkbif3apXVO0cyW1tQkRTOGacEwU,5
|
|
20
|
+
modland-1.0.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
21
|
+
modland-1.0.0.dist-info/METADATA,sha256=4n87-WE0p7Gbz9JqWB-VQs_ueLiCzMw6LRIJnCWK7JY,956
|
|
22
|
+
modland-1.0.0.dist-info/WHEEL,sha256=-oYQCr74JF3a37z2nRlQays_SX2MqOANoqVjBBAP2yE,91
|
|
23
|
+
modland-1.0.0.dist-info/top_level.txt,sha256=k20J5Vjc3fLsWLeZcsEfWZgdE7zUQxLBk3rmMWa999Q,8
|
|
24
|
+
modland-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
modland
|