Skip to content

Commit f46109a

Browse files
authored
Merge pull request #25 from jacobwhall/progress-bar
Add option to display progress bar
2 parents 661b8b4 + 69642f4 commit f46109a

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ install_requires =
2626
affine
2727
scipy>=1.6.0
2828
rasterstats
29+
tqdm
2930

3031
[options.packages.find]
3132
where = src

src/distancerasters/main.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import time
2+
from itertools import product
3+
24
import numpy as np
35
from affine import Affine
46
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)
611

712

813
class DistanceRaster(object):
914

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):
1116
"""build distance array from raster array
1217
1318
Args
@@ -46,6 +51,7 @@ def __init__(self, raster_array, affine=None, conditional=None, output_path=None
4651
self.raster_array = raster_array
4752
self.pixel_size = pixel_size
4853
self.affine = affine
54+
self.progress_bar = progress_bar
4955

5056
self.tree = None
5157
self.dist_array = None
@@ -77,34 +83,33 @@ def _calculate_distance(self):
7783

7884
t_start = time.time()
7985

80-
for r in range(nrows):
81-
82-
for c in range(ncols):
86+
row_col_iterable = product(range(nrows), range(ncols))
8387

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]]
8893

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
95100

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+
)
101106

102-
val = km_min_dist * 1000
107+
val = km_min_dist * 1000
103108

104-
else:
105-
val = min_dist
109+
else:
110+
val = min_dist
106111

107-
self.dist_array[r][c] = val
112+
self.dist_array[r][c] = val
108113

109114
print("Distance calc run time: {0} seconds".format(round(time.time() - t_start, 4)))
110115

0 commit comments

Comments
 (0)