-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuse_functions.py
More file actions
49 lines (43 loc) · 1.42 KB
/
Copy pathuse_functions.py
File metadata and controls
49 lines (43 loc) · 1.42 KB
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
# Useful functions
import numpy as np
import rasterio
from rasterio.coords import BoundingBox
def reverse_coordinates(pol):
"""
Reverse the coordinates in pol
Receives list of coordinates: [[x1,y1],[x2,y2],...,[xN,yN]]
Returns [[y1,x1],[y2,x2],...,[yN,xN]]
"""
return [list(f[-1::-1]) for f in pol]
def to_index(wind_):
"""
Generates a list of index (row,col): [[row1,col1],[row2,col2],[row3,col3],[row4,col4],[row1,col1]]
"""
return [[wind_.row_off,wind_.col_off],
[wind_.row_off,wind_.col_off+wind_.width],
[wind_.row_off+wind_.height,wind_.col_off+wind_.width],
[wind_.row_off+wind_.height,wind_.col_off],
[wind_.row_off,wind_.col_off]]
def generate_polygon(bbox):
"""
Generates a list of coordinates: [[x1,y1],[x2,y2],[x3,y3],[x4,y4],[x1,y1]]
"""
return [[bbox[0],bbox[1]],
[bbox[2],bbox[1]],
[bbox[2],bbox[3]],
[bbox[0],bbox[3]],
[bbox[0],bbox[1]]]
def pol_to_np(pol):
"""
Receives list of coordinates: [[x1,y1],[x2,y2],...,[xN,yN]]
"""
return np.array([list(l) for l in pol])
def pol_to_bounding_box(pol):
"""
Receives list of coordinates: [[x1,y1],[x2,y2],...,[xN,yN]]
"""
arr = pol_to_np(pol)
return BoundingBox(np.min(arr[:,0]),
np.min(arr[:,1]),
np.max(arr[:,0]),
np.max(arr[:,1]))