-
Notifications
You must be signed in to change notification settings - Fork 12
/
load-census.py
302 lines (237 loc) · 12 KB
/
load-census.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# *********************************************************************************************************************
# load-census.py
# *********************************************************************************************************************
#
# A script for loading Australian Bureau of Statistics Census 2021 data and boundaries
#
# Author: Hugh Saalmans
# GitHub: minus34
# Twitter: @minus34
#
# Copyright:
# - Code is licensed under an Apache License, version 2.0
# - Data is copyright ABS - licensed under Creative Commons (By Attribution) license.
# See http://abs.gov.au for correct attribution
# Process:
# 1. loads census metadata Excel files using Pandas dataframes
# 2. loads all census data CSV files
# 6. party on!
#
# *********************************************************************************************************************
import io
import logging.config
import os
import pandas # module needs to be installed, along with openpyxl (for Excel file load)
import psycopg # module needs to be installed
import settings
import utils
from datetime import datetime
def main():
full_start_time = datetime.now()
# log Python and OS versions
logger.info(f"\t- running Python {settings.python_version} with Psycopg {settings.psycopg_version}")
logger.info(f"\t- on {settings.os_version}")
# get Postgres connection & cursor
pg_conn = psycopg.connect(settings.pg_connect_string)
pg_conn.autocommit = True
pg_cur = pg_conn.cursor()
# add postgis to database (in the public schema) - run this in a try to confirm db user has privileges
try:
pg_cur.execute("SET search_path = public, pg_catalog; CREATE EXTENSION IF NOT EXISTS postgis")
except psycopg.Error:
logger.fatal("Unable to add PostGIS extension\nACTION: Check your Postgres user privileges or PostGIS install")
return False
# test if ST_Subdivide exists (only in PostGIS 2.2+). It's used to split boundaries for faster processing
logger.info(f"\t- using Postgres {settings.pg_version} and PostGIS {settings.postgis_version} "
f"(with GEOS {settings.geos_version})")
# log the user's input parameters
logger.info("")
logger.info("Arguments")
for arg in vars(settings.args):
value = getattr(settings.args, arg)
if value is not None:
if arg != "pgpassword":
logger.info(f"\t- {arg} : {value}")
else:
logger.info(f"\t- {arg} : ************")
# START LOADING DATA
# PART 1 - load census data from CSV files
logger.info(f"")
start_time = datetime.now()
logger.info(f"Start census data load : {start_time}")
create_metadata_tables(pg_cur, settings.metadata_file_prefix, settings.metadata_file_type)
populate_data_tables(settings.data_file_prefix, settings.data_file_type,
settings.table_name_part, settings.bdy_name_part)
logger.info(f"Census data loaded! : {datetime.now() - start_time}")
# close Postgres connection
pg_cur.close()
pg_conn.close()
logger.info("")
logger.info(f"Total time : : {datetime.now() - full_start_time}")
return True
def create_metadata_tables(pg_cur, prefix, suffix):
# Step 1 of 2 : create metadata tables from Census Excel spreadsheets
start_time = datetime.now()
# create schema
if settings.data_schema != "public":
pg_cur.execute(f"CREATE SCHEMA IF NOT EXISTS {settings.data_schema} AUTHORIZATION {settings.pg_user}")
# create metadata tables
sql = f"""DROP TABLE IF EXISTS {settings.data_schema}.metadata_tables CASCADE;
CREATE TABLE {settings.data_schema}.metadata_tables (
table_number text,
table_name text,
table_description text
) WITH (OIDS=FALSE);
ALTER TABLE {settings.data_schema}.metadata_tables OWNER TO {settings.pg_user}"""
pg_cur.execute(sql)
sql = f"""DROP TABLE IF EXISTS {settings.data_schema}.metadata_stats CASCADE;
CREATE TABLE {settings.data_schema}.metadata_stats (
sequential_id text,
short_id text,
long_id text,
table_number text,
profile_table text,
column_heading_description text
)
WITH (OIDS=FALSE);
ALTER TABLE {settings.data_schema}.metadata_stats OWNER TO {settings.pg_user}"""
pg_cur.execute(sql)
# get a list of all files matching the metadata filename prefix
file_list = list()
for root, dirs, files in os.walk(settings.data_directory):
for file_name in files:
if file_name.lower().startswith(prefix.lower()):
# find all XLS and XLSX files (2016 data has a mix!)
if file_name.lower().endswith(suffix.lower()) or file_name.lower().endswith(suffix.lower() + "x"):
file_path = os.path.join(root, file_name)
file_dict = dict()
file_dict["name"] = file_name
file_dict["path"] = file_path
file_list.append(file_dict)
# are there any files to load?
if len(file_list) == 0:
logger.fatal("No Census metadata XLS files found\nACTION: Check your '--census-data-path' value")
logger.fatal("\t- Step 1 of 4 : create metadata tables FAILED!")
else:
# read in excel worksheets into pandas dataframes
for file_dict in file_list:
xl = pandas.ExcelFile(file_dict["path"])
sheets = xl.sheet_names
i = 0
for table_dict in settings.census_metadata_dicts:
df = xl.parse(sheets[i])
# drop unwanted rows at the top
j = 0
first_row = False
while not first_row:
cell = df.iloc[j, 0]
if str(cell).lower() == table_dict["first_row"]:
df_clean = df.drop(df.index[0:j+1])
first_row = True
# drop excess columns in unclean Excel worksheets
if table_dict["table"] == "metadata_stats":
try:
df_clean.drop(df.columns[[6, 7, 8]], axis=1, inplace=True)
except:
pass
# # order what's left by sequential_id field
# df_clean.sort_values(by="Sequential", inplace=True)
# export to in-memory tab delimited text file
tsv_file = io.StringIO()
df_clean.to_csv(tsv_file, sep="\t", index=False, header=False)
tsv_file.seek(0) # move position back to beginning of file before reading
# # output dataframe to test tsv file
# with open(file_dict["name"] + '.tsv', 'w') as fd:
# shutil.copyfileobj(tsv_file, fd)
# tsv_file.seek(0)
# import into Postgres
sql = f"""COPY {settings.data_schema}.{table_dict['table']}
FROM stdin WITH CSV DELIMITER as '\t' NULL as ''"""
with pg_cur.copy(sql) as copy:
while data := tsv_file.read():
copy.write(data)
j += 1
i += 1
logger.info(f"\t\t- imported {file_dict['name']}")
# clean up invalid rows
pg_cur.execute(f"DELETE FROM {settings.data_schema}.metadata_tables WHERE table_number IS NULL")
# add primary keys
pg_cur.execute(f"""ALTER TABLE {settings.data_schema}.metadata_tables
ADD CONSTRAINT metadata_tables_pkey PRIMARY KEY (table_number)""")
pg_cur.execute(f"""ALTER TABLE {settings.data_schema}.metadata_stats
ADD CONSTRAINT metadata_stats_pkey PRIMARY KEY (sequential_id)""")
# cluster tables on primary key (for minor performance improvement)
pg_cur.execute(f"ALTER TABLE {settings.data_schema}.metadata_tables CLUSTER ON metadata_tables_pkey")
pg_cur.execute(f"ALTER TABLE {settings.data_schema}.metadata_stats CLUSTER ON metadata_stats_pkey")
# update stats
pg_cur.execute(f"VACUUM ANALYZE {settings.data_schema}.metadata_tables")
pg_cur.execute(f"VACUUM ANALYZE {settings.data_schema}.metadata_stats")
logger.info(f"\t- Step 1 of 2 : metadata tables created : {datetime.now() - start_time}")
# create stats tables and import data from CSV files using multiprocessing
def populate_data_tables(prefix, suffix, table_name_part, bdy_name_part):
# Step 2 of 2 : create & populate stats tables with CSV files using multiprocessing
start_time = datetime.now()
# get the file list and create sql copy statements
file_list = []
# get a dictionary of all files matching the filename prefix
for root, dirs, files in os.walk(settings.data_directory):
for file_name in files:
if file_name.lower().startswith(prefix.lower()):
if file_name.lower().endswith(suffix.lower()):
file_path = os.path.join(root, file_name)
file_name_components = file_name.lower().split(".")[0].split("_")
table = file_name_components[table_name_part]
# manual fix for the Australia wide data - has a different file name structure
if settings.census_year != '2011':
if "_aus." in file_name.lower():
boundary = "aust"
else:
boundary = file_name_components[bdy_name_part]
else:
boundary = file_name_components[bdy_name_part]
if "." in boundary:
boundary = "aust"
file_dict = {
"path": file_path,
"table": table,
"boundary": boundary,
"name": file_name
}
# if boundary == "ced": # for testing
# print(file_dict)
file_list.append(file_dict)
# are there any files to load?
if len(file_list) == 0:
logger.fatal("No Census data CSV files found\nACTION: Check your '--census-data-path' value")
logger.fatal("\t- Step 2 of 2 : stats table create & populate FAILED!")
else:
# load all files using multiprocessing
utils.multiprocess_csv_import(file_list, settings.max_concurrent_processes, settings.pg_connect_string,
settings.data_schema, settings.pg_user, settings.region_id_field, logger)
logger.info(f"\t- Step 2 of 2 : stats tables created & populated : {datetime.now() - start_time}")
if __name__ == '__main__':
logger = logging.getLogger()
# set logger
log_file = os.path.abspath(__file__).replace(".py", ".log")
logging.basicConfig(filename=log_file, level=logging.DEBUG, format="%(asctime)s %(message)s",
datefmt="%m/%d/%Y %I:%M:%S %p")
# setup logger to write to screen as well as writing to log file
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)
logger.info(f"")
logger.info(f"Start census-loader")
if main():
logger.info(f"Finished successfully!")
else:
logger.fatal("Something bad happened!")
logger.info(f"")
logger.info(f"-------------------------------------------------------------------------------")