-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_OpenMap.py
130 lines (115 loc) · 3.61 KB
/
load_OpenMap.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
#
# Open addresses Spatial Research
# OS Open Map ETL Script
#
#
# Version 1.0 (Python) in progress
# Author John Murray
# Licence MIT
#
# Purpose Load OS Open Map into database
#
import shapefile
import datetime
import glob
import sys
import collections
from pprint import pprint
from extract_shape import *
from bulkinsert import *
import os.path
import fnmatch
import os
if len(sys.argv) > 2:
print "Invalid arguments. Usage is 'python shapetest.py [directory]'"
sys.exit()
elif len(sys.argv) == 2:
folder = sys.argv[1]
else:
folder = "."
# Read database configuration from config file
username = "****"
password = "*******"
hostname = "*******"
database = "****"
tables = {}
shape_bi = {}
shape_fields = {}
dbConn = MySQLdb.connect(host=hostname,user=username,passwd=password,db=database)
cur = dbConn.cursor()
dbConn.set_character_set('utf8')
cur.execute('SET NAMES utf8;')
cur.execute('SET CHARACTER SET utf8;')
cur.execute('SET character_set_connection=utf8;')
nfiles = 0
nrecs = 0
nupdate = 0
matches = []
for root, dirnames, filenames in os.walk('./'):
for filename in fnmatch.filter(filenames, '*.shp'):
matches.append(os.path.join(root, filename))
# print "root="+root
# print "filename="+filename
# for file in glob.glob(folder+"/data/*.shp"):
for file in sorted(matches):
base = os.path.basename(file).split('.')[0].lower()
prefix = base.find('_')
if prefix > -1:
base = base[prefix+1:]
base = "oml_" + base
print base
print "File: "+file+" Base: "+base
sf = shapeExtract(file)
if base not in tables:
query = "DROP TABLE IF EXISTS `"+base+"`;"
cur.execute(query)
query = sf.getTableSQL(base)
cur.execute(query)
shape_fields[base] = sf.getFieldNames()
# print shape_fields
shape_bi[base] = BulkInsert(cur,base,shape_fields[base],max=100)
tables[base] = 0
tables[base] += 1
for shape in sf.getShapes():
params = []
for i in range(0,len(shape_fields[base])-1):
params.append(shape[2][shape_fields[base][i]])
# print shape[4]
if shape[0] == 1:
# print shape
geom = "GeomFromText('POINT("+str(shape[1][0][0]) + " " + str(shape[1][0][1])+")')"
else:
if shape[0] == 3:
geom = "GeomFromText('LINESTRING("
for i in range(0,len(shape[1])):
if i > 0:
geom += ","
geom += str(shape[1][i][0]) + " " + str(shape[1][i][1])
geom += ")')"
else:
geom = "GeomFromText('POLYGON("
shape[4].extend([len(shape[1])])
for i in range(1,len(shape[4])):
if i > 1:
geom += ","
geom += "("
for j in range(shape[4][i-1],shape[4][i]):
if j > shape[4][i-1]:
geom += ","
geom += str(shape[1][j][0]) + " " + str(shape[1][j][1])
geom += ")"
geom += ")')"
# print geom
params.append(geom)
#print params
shape_bi[base].addRow(params)
nrecs += 1
if nrecs % 1000 == 0:
print "Records read: " + str(nrecs)
dbConn.commit()
print "Records read: " + str(nrecs)
if nfiles > 0:
for table in shape_bi:
shape_bi[table].close()
dbConn.commit()
dbConn.close()