• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Tilewise Processing Abhisek92  May 28th, 2020 (edited) 820 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from itertools import product
2. from math import ceil
3. import numpy as np
4. import rasterio as rio
5. from rasterio import windows
6. from pathlib import Path
7. from functools import partial
8.
9. def overlapping_windows(
10.     src,
11.     win_height,
12.     win_width,
13.     h_overlap=0,
14.     w_overlap=0,
15.     boundless=False,
16.     anchor=None
17. ):
18.
19.     assert isinstance(
21.     ), "Invalid <src>! Expected it to be of type `rasterio.io.DatasetReader`"
22.
23.     assert isinstance(
24.         win_width, int
25.     ), "Invalid <width>, Expected it to be an `int`"
26.
27.     assert isinstance(
28.         win_height, int
29.     ), "Invalid <height>, Expected it to be an `int`"
30.
31.     assert isinstance(
32.         h_overlap, int
33.     ), "Invalid <h_overlap>, Expected it to be an `int`"
34.
35.     assert isinstance(
36.         w_overlap, int
37.     ), "Invalid <w_overlap>, Expected it to be an `int`"
38.
39.     assert isinstance(
40.         boundless, bool
41.     ), "Invalid <boundless>, Expected it to be of type `bool`"
42.
43.     hz = src.meta['height']
44.     wz = src.meta['width']
45.     hz_ = hz - 1
46.     wz_ = wz - 1
47.     dh = win_height - (2 * h_overlap)
48.     dw = win_width - (2 * w_overlap)
49.     kh = ceil(hz_ / dh)
50.     kw = ceil(wz_ / dw)
51.     h0, w0 = 0, 0
52.     h0_cal = hz_ - (dh * kh)
53.     w0_cal = wz_ - (dw * kw)
54.     h0_off = h0 - h_overlap
55.     w0_off = w0 - w_overlap
56.     hz_off = hz - h_overlap
57.     wz_off = wz - w_overlap
58.     h0_eq = h0_cal  // 2
59.     w0_eq = w0_cal  // 2
60.     hz_eq = hz + h0_cal - h0_eq
61.     wz_eq = wz + w0_cal - w0_eq
62.
63.     if anchor is None:
64.         h_start = h0_off
65.         w_start = w0_off
66.         h_end = hz_off
67.         w_end = wz_off
68.     elif anchor == 'topleft':
69.         h_start = h0
70.         w_start = w0
71.         h_end = hz
72.         w_end = wz
73.     elif anchor == 'bottomleft':
74.         h_start = h0_cal
75.         w_start = w0
76.         h_end = hz
77.         w_end = wz_off
78.     elif anchor == 'bottomright':
79.         h_start =  h0_cal
80.         w_start =  w0_cal
81.         h_end = hz
82.         w_end = wz
83.     elif anchor == 'topright':
84.         h_start = h0
85.         w_start = w0_cal
86.         h_end = hz
87.         w_end = wz
88.     elif anchor == 'top':
89.         h_start = h0
90.         w_start = w0_off
91.         h_end = hz
92.         w_end = wz_off
93.     elif anchor == 'left':
94.         h_start = h0_off
95.         w_start = w0
96.         h_end = hz_off
97.         w_end = wz
98.     elif anchor == 'bottom':
99.         h_start = h0_cal
100.         w_start = w0_off
101.         h_end = hz
102.         w_end = wz_off
103.     elif anchor == 'right':
104.         h_start = h0_off
105.         w_start = w0_cal
106.         h_end = hz_off
107.         w_end = wz
108.     elif anchor == 'top-qual':
109.         h_start = h0
110.         w_start = w0_eq
111.         h_end = hz
112.         w_end = wz_eq
113.     elif anchor == 'left-equal':
114.         h_start = h0_eq
115.         w_start = w0
116.         h_end = hz_eq
117.         w_end = wz
118.     elif anchor == 'bottom-equal':
119.         h_start = h0_cal
120.         w_start = w0_eq
121.         h_end = hz
122.         w_end = wz_eq
123.     elif anchor == 'right-equal':
124.         h_start = h0_eq
125.         w_start = w0_cal
126.         h_end = hz_eq
127.         w_end = wz
128.     elif anchor == 'all-equal':
129.         h_start = h0_eq
130.         w_start = w0_eq
131.         h_end = hz_eq
132.         w_end = wz_eq
133.     else:
134.         raise AssertionError("Invalid value for parameter <anchor>!")
135.
136.     w_offsets = range(w_start, w_end, dw)
137.     h_offsets = range(h_start, h_end, dh)
138.
139.     offsets = product(
140.         w_offsets,
141.         h_offsets
142.     )
143.
144.     indices = product(
145.         range(0, len(w_offsets), 1),
146.         range(0, len(h_offsets), 1)
147.     )
148.
149.     for index, (col_off, row_off) in zip(indices, offsets):
150.         window = windows.Window(
151.             col_off=col_off,
152.             row_off=row_off,
153.             width=win_width,
154.             height=win_height
155.         )
156.
157.         if boundless:
158.             yield index, window
159.         else:
160.             yield index, window.intersection(big_window)
161.
162.
163. def get_tiles(
164.     img_path,
165.     win_height,
166.     win_width,
167.     h_overlap=0,
168.     w_overlap=0,
169.     boundless=True,
170.     anchor='topleft',
171. ):
172.     if isinstance(img_path, str):
173.         img_path = Path(img_path)
174.     assert isinstance(img_path, Path), "Invalid type for parameter <img_path>! Expected `str` or `pathlib.Path`"
175.     assert img_path.is_file(), "Image file does not exist or not accessible!"
176.
177.     with rio.open(img_path, 'r') as im_fp:
178.         if base_name is None:
179.             base_name = img_path.stem
180.         else:
181.             assert isinstance(base_name, str)
182.         ext = img_path.suffix
183.         for index, window in overlapping_windows(
184.             src=im_fp,
185.             win_height=win_height,
186.             win_width=win_width,
187.             h_overlap=h_overlap,
188.             w_overlap=w_overlap,
189.             boundless=boundless,
190.             anchor=anchor
191.         ):