-
Notifications
You must be signed in to change notification settings - Fork 7
/
parallel.py
259 lines (209 loc) · 8.19 KB
/
parallel.py
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"""
* Divide and Conquour script to divide, process in parallel and merge again without having
* to worry about edge effects.
* This version is intended to be used with call gvi.py
"""
from gvi import f
from datetime import datetime
from multiprocessing import Pool
from fiona import open as fi_open
from argparse import ArgumentParser
from subprocess import call, DEVNULL
from rasterio import open as rio_open
from rasterio.transform import rowcol, xy
from rasterio.mask import mask as rio_mask
from rasterio.merge import merge as rio_merge
from shapely.geometry import Polygon, mapping
def coords2Array(a, coords):
"""
* convert between coords and array position
* returns row,col (y,x) as expected by rasterio
"""
x, y = coords
r, c = rowcol(a, x, y)
return int(r), int(c)
def array2Coords(a, index):
"""
* convert between array position and coords
* params are row,col (y,x) as expected by rasterio
* returns coords at the CENTRE of the cell
"""
row, col = index
x, y = xy(a, row, col)
return int(x), int(y)
def extractPolygon(src, poly):
"""
* extract a subset from a raster according to the specified bounds
* returns the dataset (numpy array) and metadata object
"""
# extract the bit of src that intersects poly
out_data, out_transform = rio_mask(src, poly, crop=True, all_touched=True)
# create the metadata for the dataset
out_meta = src.meta
out_meta.update({
"height": out_data.shape[1],
"width": out_data.shape[2],
"transform": out_transform
})
# return the mask and the metadata
return out_data[0], out_meta
def outputFiles(crs, masks):
"""
* Output illustrative GIS (Shapefile and GeoTiff) files of the extracted
* zones and the aoi polygons
"""
# create the file in write mode, with the required CRS (Proj4 string) and an empty schema
with fi_open('./tmp/aoi.shp', 'w',
driver = 'ESRI Shapefile',
crs = crs,
schema = {'geometry': 'Polygon', 'properties': {}}
) as o:
# loop through resulting masks
for m in range(len(masks)):
# write the splitter to shapefile
o.write({'geometry': mapping(masks[m]["aoi"]),'properties': {}})
# create a raster file
with rio_open(f'./tmp/{m}.tif', 'w',
driver = masks[m]["meta"]["driver"],
height = masks[m]["meta"]["height"],
width = masks[m]["meta"]["width"],
count = masks[m]["meta"]["count"],
dtype = masks[m]["meta"]["dtype"],
crs = masks[m]["meta"]["crs"],
transform = masks[m]["meta"]["transform"],
) as dst:
# write the data to the raster
dst.write(masks[m]["dtm"], 1)
# main script
if __name__ == '__main__':
# get settings from args
parser = ArgumentParser(description="Labib's Greenspace Visibility Tool")
parser.add_argument('--dtm', help='DTM layer for analysis', required=True)
parser.add_argument('--dsm', help='DSM layer for analysis', required=True)
parser.add_argument('--green', help='Binary GreenSpace layer for analysis', required=True)
parser.add_argument('--aoi', help='Boundary of Area of Interest', required=True)
parser.add_argument('--padding', help='Required padding for each mask in CRS units (e.g. radius of a viewshed)', required=True)
parser.add_argument('--sections', nargs=2, help='x and y divisions for masking (e.g. 16 divisions would be `4 4`)', required=True)
parser.add_argument('--out', help='Path for result file', required=True)
args = vars(parser.parse_args())
# get args
dtm_path = args['dtm']
dsm_path = args['dsm']
green_path = args['green']
boundary_path = args['aoi']
padding = int(args['padding'])
sections = [ int(x) for x in args['sections'] ]
out_path = args['out']
# log start time and log start
time = datetime.now()
print(f"a {sections[0]}x{sections[1]} grid, {sections[0]*sections[1]} processes, started at {time}.")
# initialise masks array for the results
masks = []
# get aoi bounds from shapefile
with fi_open(boundary_path) as boundary:
bounds = boundary.bounds
# read in raster data
with rio_open(dtm_path) as dtm_input:
with rio_open(dsm_path) as dsm_input:
with rio_open(green_path) as green_input:
# verify raster dimensions and resolution
if (dsm_input.width == dtm_input.width == green_input.width) == False or \
(dsm_input.height == dtm_input.height == green_input.height) == False or \
(dsm_input.res[0] == dtm_input.res[0] == green_input.res[0]) == False:
print("rasters do not match!")
print("width: \t\t", dsm_input.width == dtm_input.width == green_input.width)
print("height: \t", dsm_input.height == dtm_input.height == green_input.height)
print("resolution: \t", dsm_input.res[0] == dtm_input.res[0] == green_input.res[0])
exit()
# store metadata from dtm
meta = dtm_input.meta
# read data bands
dtm_data = dtm_input.read(1)
dsm_data = dsm_input.read(1)
green_data = green_input.read(1)
# adjust bounds to the raster by transforming to image space and back again
minx, miny = array2Coords(dtm_input.transform, coords2Array(dtm_input.transform, [bounds[0], bounds[1]]))
maxx, maxy = array2Coords(dtm_input.transform, coords2Array(dtm_input.transform, [bounds[2] + dtm_input.res[0], bounds[3] + dtm_input.res[0]]))
# get width and height of study area
w, h = (maxx - minx), (maxy - miny)
# get width and height of all aois (absorb offcut into first entry)
aoiWidth = [int(w / sections[0])] * sections[0]
aoiWidth[0] += w - sum(aoiWidth)
aoiHeight = [int(h / sections[1])] * sections[1]
aoiHeight[0] += h - sum(aoiHeight)
# pre-calculate origin location polygon extraction
xOrigin = bounds[0] - padding
yOrigin = bounds[1] - padding
# loop through mask matrix
for x in range(sections[0]):
for y in range(sections[1]):
# construct a Shapely polygon for use in processing
polygon = Polygon([
(bounds[0] + sum(aoiWidth[:x]) - padding, bounds[1] + sum(aoiHeight[:y]) - padding), # bl
(bounds[0] + sum(aoiWidth[:x]) - padding, bounds[1] + sum(aoiHeight[:y+1]) + padding), # tl
(bounds[0] + sum(aoiWidth[:x+2]) + padding, bounds[1] + sum(aoiHeight[:y+1]) + padding), # tr
(bounds[0] + sum(aoiWidth[:x+1]) + padding, bounds[1] + sum(aoiHeight[:y]) - padding) # br
])
# construct a shapely polygon for use in analysis and trimming the results
aoi = Polygon([
(bounds[0] + sum(aoiWidth[:x]), bounds[1] + sum(aoiHeight[:y])), # bl
(bounds[0] + sum(aoiWidth[:x]), bounds[1] + sum(aoiHeight[:y+1])), # tl
(bounds[0] + sum(aoiWidth[:x+1]), bounds[1] + sum(aoiHeight[:y+1])), # tr
(bounds[0] + sum(aoiWidth[:x+1]), bounds[1] + sum(aoiHeight[:y])) # br
])
# extract the polygon and append to masks list
dtm, meta = extractPolygon(dtm_input, [polygon])
dsm, _ = extractPolygon(dsm_input, [polygon])
green, _ = extractPolygon(green_input, [polygon])
# make result object and append to masks list
masks.append({
"dtm": dtm,
"dsm": dsm,
"green": green,
"meta": meta,
"aoi": aoi,
"options": {
"radius": padding, # viewshed radius
"o_height": 1.7, # observer height
"t_height": 0 # target height
}
})
# print(masks[0])
# exit()
# output files for debugging purposes
# outputFiles(dtm_input.crs, masks)
# exit()
# make as many processes as are required and launch them
with Pool(sections[0] * sections[1]) as p:
results = p.map(f, masks)
# open all result files in read mode
files = []
for filepath in results:
files.append(rio_open(filepath, 'r'))
# copy metadata to make a new one
out_meta = files[0].meta.copy()
# merge result files if necessary
if sections[0] > 1 and sections[1] > 1:
# file merge
merged, out_transform = rio_merge(files)
# update metadata
out_meta.update({
"height": merged.shape[1],
"width": merged.shape[2],
"transform": out_transform,
"dtype": 'float64'
})
else:
# otherwise just use it...
merged = files[0]
# create a raster file
with rio_open(out_path, 'w', **out_meta) as dst:
dst.write(merged[0], 1)
# use GDAL binary to calculate histogram and statistics
# call(["gdalinfo", "-stats", out_path], stdout=DEVNULL)
# close all of the files
for file in files:
file.close()
# print how long it took
print(datetime.now() - time)
print("done!")