|
1 | 1 | import time |
| 2 | +from itertools import product |
| 3 | + |
2 | 4 | import numpy as np |
3 | 5 | from affine import Affine |
4 | 6 | from scipy.spatial import KDTree |
5 | | -from .utils import export_raster, convert_index_to_coords, calc_haversine_distance |
| 7 | +from tqdm import tqdm |
| 8 | + |
| 9 | +from .utils import (calc_haversine_distance, convert_index_to_coords, |
| 10 | + export_raster) |
6 | 11 |
|
7 | 12 |
|
8 | 13 | class DistanceRaster(object): |
9 | 14 |
|
10 | | - def __init__(self, raster_array, affine=None, conditional=None, output_path=None): |
| 15 | + def __init__(self, raster_array, affine=None, conditional=None, output_path=None, progress_bar=False): |
11 | 16 | """build distance array from raster array |
12 | 17 |
|
13 | 18 | Args |
@@ -46,6 +51,7 @@ def __init__(self, raster_array, affine=None, conditional=None, output_path=None |
46 | 51 | self.raster_array = raster_array |
47 | 52 | self.pixel_size = pixel_size |
48 | 53 | self.affine = affine |
| 54 | + self.progress_bar = progress_bar |
49 | 55 |
|
50 | 56 | self.tree = None |
51 | 57 | self.dist_array = None |
@@ -77,34 +83,33 @@ def _calculate_distance(self): |
77 | 83 |
|
78 | 84 | t_start = time.time() |
79 | 85 |
|
80 | | - for r in range(nrows): |
81 | | - |
82 | | - for c in range(ncols): |
| 86 | + row_col_iterable = product(range(nrows), range(ncols)) |
83 | 87 |
|
84 | | - cur_index = (r, c) |
85 | | - min_dist, min_index = self.tree.query([cur_index]) |
86 | | - min_dist = min_dist[0] |
87 | | - min_index = self.tree.data[min_index[0]] |
| 88 | + for r, c in tqdm(row_col_iterable, total=nrows*ncols, disable=(not self.progress_bar)): |
| 89 | + cur_index = (r, c) |
| 90 | + min_dist, min_index = self.tree.query([cur_index]) |
| 91 | + min_dist = min_dist[0] |
| 92 | + min_index = self.tree.data[min_index[0]] |
88 | 93 |
|
89 | | - if self.affine is not None: |
90 | | - if cur_index[1] == min_index[1]: |
91 | | - # columns are same meaning nearest is either vertical or self. |
92 | | - # no correction needed, just convert to km |
93 | | - dd_min_dist = min_dist * self.pixel_size |
94 | | - km_min_dist = dd_min_dist * 111.321 |
| 94 | + if self.affine is not None: |
| 95 | + if cur_index[1] == min_index[1]: |
| 96 | + # columns are same meaning nearest is either vertical or self. |
| 97 | + # no correction needed, just convert to km |
| 98 | + dd_min_dist = min_dist * self.pixel_size |
| 99 | + km_min_dist = dd_min_dist * 111.321 |
95 | 100 |
|
96 | | - else: |
97 | | - km_min_dist = calc_haversine_distance( |
98 | | - convert_index_to_coords(cur_index, self.affine), |
99 | | - convert_index_to_coords(min_index, self.affine), |
100 | | - ) |
| 101 | + else: |
| 102 | + km_min_dist = calc_haversine_distance( |
| 103 | + convert_index_to_coords(cur_index, self.affine), |
| 104 | + convert_index_to_coords(min_index, self.affine), |
| 105 | + ) |
101 | 106 |
|
102 | | - val = km_min_dist * 1000 |
| 107 | + val = km_min_dist * 1000 |
103 | 108 |
|
104 | | - else: |
105 | | - val = min_dist |
| 109 | + else: |
| 110 | + val = min_dist |
106 | 111 |
|
107 | | - self.dist_array[r][c] = val |
| 112 | + self.dist_array[r][c] = val |
108 | 113 |
|
109 | 114 | print("Distance calc run time: {0} seconds".format(round(time.time() - t_start, 4))) |
110 | 115 |
|
|
0 commit comments