-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathutils.py
More file actions
222 lines (174 loc) · 5.85 KB
/
utils.py
File metadata and controls
222 lines (174 loc) · 5.85 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
import sys
import numpy as np
from rasterio import features
from affine import Affine
from numpy import min_scalar_type
from shapely.geometry import box, MultiPolygon
from .io import window_bounds
DEFAULT_STATS = ['count', 'min', 'max', 'mean']
VALID_STATS = DEFAULT_STATS + \
['sum', 'std', 'median', 'majority', 'minority', 'unique', 'range', 'nodata', 'nan']
# also percentile_{q} but that is handled as special case
def get_percentile(stat):
if not stat.startswith('percentile_'):
raise ValueError("must start with 'percentile_'")
qstr = stat.replace("percentile_", '')
q = float(qstr)
if q > 100.0:
raise ValueError('percentiles must be <= 100')
if q < 0.0:
raise ValueError('percentiles must be >= 0')
return q
def rasterize_geom(geom, like, all_touched=False):
"""
Parameters
----------
geom: GeoJSON geometry
like: raster object with desired shape and transform
all_touched: rasterization strategy
Returns
-------
ndarray: boolean
"""
geoms = [(geom, 1)]
rv_array = features.rasterize(
geoms,
out_shape=like.shape,
transform=like.affine,
fill=0,
dtype='uint8',
all_touched=all_touched)
return rv_array.astype(bool)
# https://stackoverflow.com/questions/8090229/
# resize-with-averaging-or-rebin-a-numpy-2d-array/8090605#8090605
def rebin_sum(a, shape, dtype):
sh = shape[0],a.shape[0]//shape[0],shape[1],a.shape[1]//shape[1]
return a.reshape(sh).sum(-1, dtype=dtype).sum(1, dtype=dtype)
class objectview(object):
def __init__(self, d):
self.__dict__ = d
def rasterize_pctcover_geom(geom, like, scale=None, all_touched=False):
"""
Parameters
----------
geom: GeoJSON geometry
like: raster object with desired shape and transform
scale: scale at which to generate percent cover estimate
Returns
-------
ndarray: float32
"""
scale = scale if scale is not None else 10
min_dtype = min_scalar_type(scale**2)
pixel_size_lon = like.affine[0]/scale
pixel_size_lat = like.affine[4]/scale
topleftlon = like.affine[2]
topleftlat = like.affine[5]
new_affine = Affine(pixel_size_lon, 0, topleftlon,
0, pixel_size_lat, topleftlat)
new_shape = (like.shape[0]*scale, like.shape[1]*scale)
new_like = objectview({'shape': new_shape, 'affine': new_affine})
rv_array = rasterize_geom(geom, new_like, all_touched=all_touched)
rv_array = rebin_sum(rv_array, like.shape, min_dtype)
return rv_array.astype('float32') / (scale**2)
def stats_to_csv(stats):
if sys.version_info[0] >= 3:
from io import StringIO as IO # pragma: no cover
else:
from cStringIO import StringIO as IO # pragma: no cover
import csv
csv_fh = IO()
keys = set()
for stat in stats:
for key in list(stat.keys()):
keys.add(key)
fieldnames = sorted(list(keys), key=str)
csvwriter = csv.DictWriter(csv_fh, delimiter=str(","), fieldnames=fieldnames)
csvwriter.writerow(dict((fn, fn) for fn in fieldnames))
for row in stats:
csvwriter.writerow(row)
contents = csv_fh.getvalue()
csv_fh.close()
return contents
def check_stats(stats, categorical):
if not stats:
if not categorical:
stats = DEFAULT_STATS
else:
stats = []
else:
if isinstance(stats, str):
if stats in ['*', 'ALL']:
stats = VALID_STATS
else:
stats = stats.split()
for x in stats:
if x.startswith("percentile_"):
get_percentile(x)
elif x not in VALID_STATS:
raise ValueError(
"Stat `%s` not valid; "
"must be one of \n %r" % (x, VALID_STATS))
run_count = False
if categorical or 'majority' in stats or 'minority' in stats or 'unique' in stats:
# run the counter once, only if needed
run_count = True
return stats, run_count
def remap_categories(category_map, stats):
def lookup(m, k):
""" Dict lookup but returns original key if not found
"""
try:
return m[k]
except KeyError:
return k
return {lookup(category_map, k): v
for k, v in stats.items()}
def key_assoc_val(d, func, exclude=None):
"""return the key associated with the value returned by func
"""
vs = list(d.values())
ks = list(d.keys())
key = ks[vs.index(func(vs))]
return key
def boxify_points(geom, rast):
"""
Point and MultiPoint don't play well with GDALRasterize
convert them into box polygons 99% cellsize, centered on the raster cell
"""
if 'Point' not in geom.type:
raise ValueError("Points or multipoints only")
buff = -0.01 * abs(min(rast.affine.a, rast.affine.e))
if geom.type == 'Point':
pts = [geom]
elif geom.type == "MultiPoint":
pts = geom.geoms
geoms = []
for pt in pts:
row, col = rast.index(pt.x, pt.y)
win = ((row, row + 1), (col, col + 1))
geoms.append(box(*window_bounds(win, rast.affine)).buffer(buff))
return MultiPolygon(geoms)
def rs_mean(masked, cover_weights=None):
if cover_weights is not None:
val = float(
np.sum(masked * cover_weights) /
np.sum(~masked.mask * cover_weights))
else:
val = float(masked.mean())
return val
def rs_count(masked, cover_weights=None):
if cover_weights is not None:
val = float(np.sum(~masked.mask * cover_weights))
else:
val = int(masked.count())
return val
def rs_sum(masked, cover_weights=None):
if cover_weights is not None:
val = float(np.sum(masked * cover_weights))
else:
val = float(masked.sum())
return val