-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconvert.py
executable file
·55 lines (38 loc) · 1.4 KB
/
convert.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
#!/usr/bin/env python3
import sys
import glob
from csv import DictReader, DictWriter
from pyproj import Transformer
from lib.convert import normalize_postcode, check_postcode
##
## CSV Input
##
in_filenames = sorted(glob.glob('codepo_gb/Data/CSV/*.csv'))
# 10 columns but we only need the first 4
in_fieldnames = ['postcode', 'positional_quality_indicator', 'eastings', 'northings']
##
## Transform
##
# We convert OSGB 1936 (https://epsg.io/27700) to WGS 84 (https://epsg.io/4326)
transformer = Transformer.from_crs('EPSG:27700', 'EPSG:4326')
##
## CSV Output
##
out_filenames = ['postcode', 'lat', 'lon']
csv_writer = DictWriter(sys.stdout, fieldnames=out_filenames, lineterminator="\n")
csv_writer.writeheader()
for fieldname in in_filenames:
with open(fieldname) as file:
csv_reader = DictReader(file, fieldnames=in_fieldnames)
for row in csv_reader:
# Starting Proj version 6 the order of the coordinates changed
latitude, longitude = transformer.transform(row['eastings'], row['northings'])
postcode = normalize_postcode(row['postcode'])
if not check_postcode(postcode):
print("invalid postcode '%s'" % postcode, file=sys.stderr)
continue
csv_writer.writerow({
'postcode': postcode,
'lat': '%0.5f' % latitude,
'lon': '%0.5f' % longitude
})