-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_parallel.py
210 lines (185 loc) · 7.49 KB
/
main_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
"""THIS SCRIPT IS TO RUN CODE ON LARGE EXTENTS USING MULTIPLE CPUS. ALTHOUGH IT WORKS, IT SEEMS LIKE IT COULD BE MADE MORE EFFICIIENT AS IT SEEMS A LITTLE SLOW"""
from pathlib import Path
import geopandas as gpd
import fiona
import pandas as pd
from concurrent.futures import ProcessPoolExecutor, as_completed
import multiprocessing
import os
from datetime import datetime
import gridgran
NUM_WORKERS = os.cpu_count()
# BASE = Path(r'D:\DATA\grids_dummy_data').resolve()
BASE = Path(r'Q:\Census_grids_data_DO_NOT_DELETE').resolve()
GRID_1km = BASE.joinpath('EWGRID_1km.gpkg')
GRID_125M = Path(r'R:\HeatherPorter\CensusGrids\Nested '
r'Grids\NestedGridData\UKGrids').joinpath('UKGrid_125m.gpkg')
POINTS = BASE.joinpath('DUMMY_POINTS_GLOBAL.gpkg')
BFC_ALL = BASE.joinpath('BFC/CTRY_DEC_2021_GB_BFC.shp') # To make water
# mask
# OUTPATH = BASE.joinpath('brighton_parallel/TEST_brighton.gpkg')
# OUTPATH_TMP = OUTPATH.parent.joinpath('tmp/TEST_brighton.gpkg')
OUTPATH = BASE.joinpath('ew_parallel/EW.gpkg')
OUTPATH_TMP = OUTPATH.parent.joinpath('tmp/EW.gpkg')
classification_dict = {
'p_1': 10,
'p_2': 40,
'p_3': 49,
'h_1': 5,
'h_2': 20,
'h_3': 24,
}
CLASSIFICATION_SETTINGS = {
"classification_dict": classification_dict,
"cls_2_threshold_1000m": False, # These should remain false
"cls_2_threshold_500m": False, # These should remain false
"cls_2_threshold_250m": False, # These should remain false
"cls_2_threshold_125m": False, # These should remain false
}
touching_points_list = []
def main_process_serial(layer=None):
m = multiprocessing.Manager()
l_pt = m.Lock()
l_125 = m.Lock()
if layer:
gdf_1km = gpd.read_file(GRID_1km, layer=layer)
else:
gdf_1km = gpd.read_file(GRID_1km)
for grid_cell in gdf_1km.itertuples():
process(grid_cell, l_pt, l_125)
print(touching_points_list)
def main_process_parallel(layer=None):
m = multiprocessing.Manager()
l_pt = m.Lock()
l_125 = m.Lock()
l_water = m.Lock() # lock to read water mask file
if layer:
gdf_1km = gpd.read_file(GRID_1km, layer=layer)
else:
gdf_1km = gpd.read_file(GRID_1km)
gdf_water_all = gpd.read_file(BFC_ALL)
CELLS_1km = [x for index, x in gdf_1km.iterrows()]
number_of_rows = len(CELLS_1km)
rows_10_perc = round(number_of_rows/10)
number_of_rows_left = len(CELLS_1km)
counter = 0
GRIDS = []
WATERS = []
DFS = []
DFS_NON_EMPTY = []
with ProcessPoolExecutor(max_workers=NUM_WORKERS) as executor:
future_to_grids = {executor.submit(process, cell, l_pt, l_125,
l_water, gdf_water_all): cell
for
cell in CELLS_1km}
for future in as_completed(future_to_grids):
processed_grid = future_to_grids[future]
try:
grid, water, df, df_non_empty = future.result()
if isinstance(grid, gpd.GeoDataFrame):
GRIDS.append(grid)
WATERS.append(water)
DFS.append(df)
DFS_NON_EMPTY.append(df_non_empty)
if number_of_rows_left % rows_10_perc == 0:
print('ROWS LEFT', number_of_rows_left)
number_of_rows_left -= 1
counter += 1
if counter % 500 == 0:
print(f'Processed {counter} cells')
except Exception as e:
print(processed_grid, e)
grid_final = gpd.GeoDataFrame(pd.concat(GRIDS)).set_crs(27700)
grid_final.to_file(
OUTPATH,
layer='grids',
driver='GPKG',
index=False)
df_final = pd.concat(DFS)
df_final.to_csv(OUTPATH.parent.joinpath('grids.csv'), index=False)
df_non_empty_final = pd.concat(DFS_NON_EMPTY)
df_non_empty_final.to_csv(OUTPATH.parent.joinpath(
'points_in_non_empty_grids.csv'), index=False)
df_water = gpd.GeoDataFrame(pd.concat(WATERS)).set_crs(27700)
df_water['diss'] = 1
df_water = df_water.dissolve(by='diss')
df_water.to_file(OUTPATH, layer='watermask', index=False)
def process(grid_cell, l_pt, l_125, l_water, gdf_water_all):
outlayer = grid_cell.GridID1km
bbox = grid_cell.geometry.bounds
points = gpd.read_file(POINTS, bbox=bbox)
if not points.empty:
gdf_125 = gpd.read_file(GRID_125M, bbox=bbox)
gdf_125 = gdf_125[
gdf_125.GridID125m.str[:-3] + '000' == grid_cell.GridID1km]
touching_points = points[points.geometry.touches(grid_cell.geometry)]
if not touching_points.empty:
for pt in touching_points.uprn.tolist():
if pt in touching_points_list:
points = points[points.uprn != pt]
else:
touching_points_list.append(pt)
water_gdf = gpd.clip(gdf_water_all, gdf_125)
x = gridgran.GridGranulatorSingleCell(
grid_cell,
gdf_125,
points,
water_gdf,
OUTPATH_TMP,
outlayer,
OUTPATH_TMP.parent.joinpath(f'{outlayer}_grids.csv'),
CLASSIFICATION_SETTINGS,
class_2_threshold_prp=0.05,
fill_values_below_threshold_with='minimum'
)
grid, water, df, df_non_empty = x.iterate_and_process()
return grid, water, df, df_non_empty
return None, None, None, None
def make_final_csv(csv_list, outname, delete_files=False):
df_list = [pd.read_csv(x) for x in csv_list]
df_final = pd.concat(df_list)
df_final.to_csv(outname, index=False)
if delete_files:
[x.unlink() for x in csv_list]
def concatenate_gpkg_layers(gpkg_tmp, out_gpkg, delete_tmp=False):
# https://gis.stackexchange.com/questions/300630/how-to-use-merge-vector-layers-in-qgis-using-geopackages-as-output-with-fids-d
layers = fiona.listlayers(gpkg_tmp)
gdf_grids_list = []
gdf_water_list = []
for layer in layers:
gdf = gpd.read_file(gpkg_tmp, layer=layer)
if layer.endswith('water_mask'):
gdf_water_list.append(gdf)
else:
gdf_grids_list.append(gdf)
gdf_grids = gpd.GeoDataFrame(pd.concat(gdf_grids_list)).set_crs(27700)
gdf_water = gpd.GeoDataFrame(pd.concat(gdf_water_list)).set_crs(27700)
gdf_grids.to_file(out_gpkg, layer='grids', index=False)
gdf_water['diss'] = 1
gdf_water = gdf_water.dissolve(by='diss').reset_index()
gdf_water.to_file(out_gpkg, layer='watermask', index=False)
if delete_tmp:
gpkg_tmp.unlink()
if __name__ == "__main__":
# main_process_serial(layer='1000m')
start = datetime.now()
if not OUTPATH_TMP.parent.exists():
OUTPATH_TMP.parent.mkdir(parents=True)
# main_process_parallel(layer='1000m')
main_process_parallel(layer='1km2')
# GRIDS_CSVS = [x for x in OUTPATH_TMP.parent.iterdir() if x.name.endswith(
# '0_grids.csv')]
# POINTS_CSVS = [x for x in OUTPATH_TMP.parent.iterdir() if
# x.name.endswith(
# 'empty_grids.csv')]
# make_final_csv(GRIDS_CSVS, OUTPATH.parent.joinpath('EW.csv'),
# delete_files=True)
# make_final_csv(POINTS_CSVS, OUTPATH.parent.joinpath(
# 'EW_points_without_empty_grids.csv'), delete_files=True)
# concatenate_gpkg_layers(OUTPATH_TMP, OUTPATH, delete_tmp=True)
# OUTPATH_TMP.parent.rmdir()
print('FIND TOUCHING POINTS')
print('APPEND CSVS AND LAYERS')
print('DISSOLVE WATER')
finish = datetime.now()
print(f'parallel {finish - start} seconds')