-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOS_Locator_ETL.py
68 lines (56 loc) · 2.18 KB
/
OS_Locator_ETL.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
#
# Open addresses ETL Common Library
# Open addresses Ordance Survey Locator Opendata ETL tool
#
#
# Version 1.0 (Python) in progress
# Author John Murray
# Licence CC By SA
#
# Purpose Parse, validate and extract elements from UK addresses
#
import ConfigParser
import csv
import glob
import MySQLdb
import string
from bulkinsert import *
# Read database configuration from config file
config = ConfigParser.ConfigParser()
config.read("oa_alpha_etl.cnf")
username = config.get('database', 'username')
password = config.get('database', 'password')
hostname = config.get('database', 'hostname')
database = config.get('database', 'database')
dbConn = MySQLdb.connect(host=hostname,user=username,passwd=password,db=database)
cur = dbConn.cursor()
query = "TRUNCATE TABLE `OS_Locator`;"
cur.execute(query)
fields = ["Name", "Classification", "Centx", "Centy", "Minx", "Maxx", "Miny", "Maxy", "Settlement", "Locality", "Cou_Unit", "Local Authority", "Tile_10k", "Tile_25k", "Source", "MBR25"]
# basequery = "INSERT INTO OS_Locator(`Name`, `Classification`, `Centx`, `Centy`, `Minx`, `Maxx`, `Miny`, `Maxy`, `Settlement`, `Locality`, `Cou_Unit`, `Local Authority`, `Tile_10k`, `Tile_25k`, `Source`) "
bi = BulkInsert(cur,"OS_Locator",fields)
nrecs = 0
for file in glob.glob("OS*.txt"):
print file
csvfile = open(file, 'rb')
reader = csv.reader(csvfile, delimiter=':', quoting=csv.QUOTE_NONE)
for row in reader:
nrecs += 1
# print row
if (nrecs % 10000) == 0:
print "Records read: " + str(nrecs)
minx = int(row[4]) - 25
maxx = int(row[5]) + 25
miny = int(row[6]) - 25
maxy = int(row[7]) + 25
poly = "GeomFromText('Polygon(("+str(minx)+" "+str(miny)+","+str(minx)+" "+str(maxy)+","+str(maxx)+" "+str(maxy)+","+str(maxx)+" "+str(miny)+","+str(minx)+" "+str(miny)+"))')"
# print poly
row.append(poly)
bi.addRow(row)
# query = basequery + "VALUES(" + string.join(["'" + field.replace("'","\\'") + "'" for field in row],",") + ");"
# print query
# cur.execute(query)
print "Records read: " + str(nrecs)
bi.close()
dbConn.commit()
dbConn.close()