forked from ONSgeo/gridgranulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
127 lines (111 loc) · 4.73 KB
/
main.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
"""Main function to prepare points from a global dataset at extent of local
authority and process at this extent. Although this will work for multuple
LAs, it may run out of memory for very large areas, and in these cases Local
Authorities should be run separately.
NOTE - THIS SCRIPT IS POINTING TO DATA IN THE PSMA DRIVE WHICH IS CONFIGURED AS 'Q' IN THIS SCRIPT. THIS MAY NEED TO BE CHANGED (DATA_DIR) IN THE USER'S CONFIGURATION
"""
from pathlib import Path
import geopandas as gpd
import gridgran
BASE_DIR = Path(__file__).resolve().parent
# DATA_DIR = Path(r'D:\DATA\grids_dummy_data').resolve()
DATA_DIR = Path(r'Q:\Census_grids_data_DO_NOT_DELETE').resolve()
BFC_ALL = DATA_DIR.joinpath('BFC/CTRY_DEC_2021_GB_BFC.shp') # FOR WATER MASK
OA_SHP = DATA_DIR.joinpath(
'OA_2021/Output_Areas_(December_2021)_Boundaries_Full_Clipped_EW_('
'BFC)/OA_2021_EW_BFC_V7.shp')
LA_SHP = DATA_DIR.joinpath('Local_Authority_Districts_('
'December_2021)_GB_BFC/LAD_DEC_2021_GB_BFC.shp')
GLOBAL_POINTS = DATA_DIR.joinpath('DUMMY_POINTS_GLOBAL.gpkg')
GLOBAL_POINTS_LAYER = 'part-0'
# GLOBAL_POINTS = DATA_DIR.joinpath('BOA/BOA.gpkg')
# GLOBAL_POINTS_LAYER = 'points'
GLOBAL_GRID_1km = DATA_DIR.joinpath("EWGRID_1km.gpkg")
GLOBAL_GRID_125m = Path(r'R:\HeatherPorter\CensusGrids\Nested '
r'Grids\NestedGridData\UKGrids\UKGrid_125m.gpkg'
).resolve()
########################## SET OUT DIRECTORY ################################
OUT_DIR = BASE_DIR.parent.joinpath('GRIDS') # THIS SHOULD BE SET AS
# APPROPRIATE
########################## SET OUT DIRECTORY ################################
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
}
def main(GPKG, la_ids, la_col, include_oas=False):
#If include_oas is False, the oa layer will not be included in the
# output. This is just for purposes of comparison, but slows down the
# script if data is on the network
BFC_CLIP = GPKG.parent.joinpath('BFC_clip.shp')
if not GPKG.parent.exists():
GPKG.parent.mkdir()
if not GPKG.exists():
gridgran.make_points_geopackage(
LA_SHP,
la_ids,
la_col,
GLOBAL_POINTS,
GPKG,
GLOBAL_GRID_1km,
GLOBAL_GRID_125m,
out_layer='points',
la_layer=None,
pt_layer=GLOBAL_POINTS_LAYER,
pt_pop_col='people',
layer_1km='1km2',
layer_125m=None
)
gridgran.clip_water(BFC_ALL, BFC_CLIP, GPKG, layer='1000m')
if include_oas:
add_oas_to_gpkg(GPKG)
gridgran.GridGranulatorGPKG(GPKG,
GPKG,
GPKG.parent.name,
GPKG.parent.joinpath(
f'{GPKG.parent.name}.csv'),
CLASSIFICATION_SETTINGS,
path_to_waterline=BFC_CLIP,
class_2_threshold_prp=0.05,
fill_values_below_threshold_with='minimum'
)
def add_oas_to_gpkg(GPKG):
"""Aggregates points to OA shapefile in gpkg"""
pt_df = gpd.read_file(GPKG, layer='points')
bbox = tuple(list(pt_df.total_bounds))
oa_df = gpd.read_file(OA_SHP, bbox=bbox)
oa_join = oa_df.sjoin(pt_df, how='inner', predicate='intersects')
oa_agg = oa_join[['OA21CD', 'people']].groupby('OA21CD').sum()
oa_final = oa_df.set_index('OA21CD').join(oa_agg).dropna()
oa_final['pop_density'] = oa_final.people / oa_final.geometry.area
oa_final.to_file(GPKG, layer='OA', driver='GPKG')
if __name__ == "__main__":
from datetime import datetime # Timing script
start = datetime.now() # timing script
LA_IDS = {
'Soton': ['Southampton']
}
la_col = 'LAD21NM' # Could also use LAD21CD
for la, la_ids in LA_IDS.items():
print(f'starting {la}')
print(la)
print(la_ids)
GPKG = OUT_DIR.joinpath(f'{la}/{la}.gpkg') # Save to here
if not GPKG.parent.exists():
GPKG.parent.mkdir(parents=True, exist_ok=True)
try:
main(GPKG, la_ids, la_col, include_oas=False)
except Exception as e:
print(f'Could not do {la} because of {e}')
finish = datetime.now() # timing script
print(f'{la} took {finish - start}') # timing script