/
gpkg-pg_loadpkg.py
executable file
·320 lines (299 loc) · 12.5 KB
/
gpkg-pg_loadpkg.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python
#------------------------------------------------------------------------------
#
# Project: PostgreSQL-GeoPackage
# Authors: Stephan Meissl <stephan.meissl@eox.at>
#
#------------------------------------------------------------------------------
# Copyright (c) 2016 EOX IT Services GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#------------------------------------------------------------------------------
#
# Description:
#
# This script loads a SQLite GeoPackage into a PostgreSQL-GeoPackage
# database.
#
# gpkg-pg_loadpkg.py takes a provided SQLite GeoPackage containing raster
# tile data only and loads it into the given PostgreSQL-GeoPackage database.
# This script might get a switch to make a selection of the data to be
# loaded, for example based on a spatial bounding box.
#
# Ideas for future:
#
# * Add a progress indicator
#
#------------------------------------------------------------------------------
import sys
import os
import sqlite3
import datetime
import psycopg2
def record_to_string(record):
type_str = type(u'str')
type_datetime = type(datetime.datetime.now())
type_buffer = type(buffer(""))
type_None = type(None)
type_float = type(0.0)
values = []
for item in record:
if type(item) == type_None:
values.append('NULL')
elif type(item) == type_str:
values.append("'" + item.replace("'", "''") + "'")
elif type(item) == type_datetime:
values.append('"' + str(item) + '"')
elif type(item) == type_buffer:
values.append(psycopg2.Binary(item).getquoted())
elif type(item) == type_float:
values.append("%.19f" % item)
else:
values.append(str(item))
return ','.join(values)
def copy_table(conn_in, conn_out, table_name, constraint=None):
cursor_in = conn_in.cursor()
#Check that table exists
cursor_in.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name='%s';"
% table_name
)
if cursor_in.fetchone():
cursor_in.execute(
"SELECT * FROM \"%s\"%s;" % (table_name, "" if constraint is None
else " WHERE " + constraint)
)
with conn_out.cursor() as cursor_out:
for record in cursor_in:
values = record_to_string(record)
try:
cursor_out.execute(
"INSERT INTO \"%s\" VALUES (%s);" %
(table_name, values)
)
except psycopg2.IntegrityError as e:
conn_out.rollback()
if e.pgcode == '23505':
sys.stderr.write(
"ERROR: GeoPackage seems to be already imported. "
"Error message was: '%s'.\n" % e.message
)
sys.exit(1)
except Exception as e:
conn_out.rollback()
sys.stderr.write(
"ERROR: Input doesn't seem to be a valid GeoPackage. "
"Error message was: '%s'.\n" % e.message
)
sys.exit(1)
def create_tiles_table(conn_in, conn_out, cursor_out, table_name):
#Create GeoPackage tiles table
cursor_out.execute(
"CREATE TABLE \"%s\" ("
" id BIGSERIAL PRIMARY KEY,"
" zoom_level BIGINT NOT NULL,"
" tile_column BIGINT NOT NULL,"
" tile_row BIGINT NOT NULL,"
" tile_data BYTEA NOT NULL,"
" UNIQUE (zoom_level, tile_column, tile_row)"
");" % table_name
)
#Create triggers for new table
cursor_out.execute(
"CREATE FUNCTION \"%s_tile_column_insert\"() RETURNS trigger AS $$"
" BEGIN"
" IF NEW.tile_column < 0 THEN"
" RAISE EXCEPTION 'insert on table ''%s'' violates "
"constraint: tile_column cannot be < 0';"
" END IF;"
" IF NOT (NEW.tile_column < (SELECT matrix_width FROM "
"gpkg_tile_matrix WHERE table_name = '%s' AND zoom_level = "
"NEW.zoom_level)) THEN"
" RAISE EXCEPTION 'insert on table ''%s'' violates "
"constraint: tile_column must by < matrix_width specified for table "
"and zoom level in gpkg_tile_matrix';"
" END IF;"
" RETURN NEW;"
" END;"
"$$ LANGUAGE plpgsql;"
"CREATE TRIGGER \"%s_tile_column_insert\""
"BEFORE INSERT ON \"%s\""
"FOR EACH ROW EXECUTE PROCEDURE \"%s_tile_column_insert\"();"
% ((table_name,)*7)
)
cursor_out.execute(
"CREATE FUNCTION \"%s_tile_column_update\"() RETURNS trigger AS $$"
" BEGIN"
" IF NEW.tile_column < 0 THEN"
" RAISE EXCEPTION 'update on table ''%s'' violates "
"constraint: tile_column cannot be < 0';"
" END IF;"
" IF NOT (NEW.tile_column < (SELECT matrix_width FROM "
"gpkg_tile_matrix WHERE table_name = '%s' AND zoom_level = "
"NEW.zoom_level)) THEN"
" RAISE EXCEPTION 'update on table ''%s'' violates "
"constraint: tile_column must by < matrix_width specified for table "
"and zoom level in gpkg_tile_matrix';"
" END IF;"
" RETURN NEW;"
" END;"
"$$ LANGUAGE plpgsql;"
"CREATE TRIGGER \"%s_tile_column_update\""
"BEFORE UPDATE ON \"%s\""
"FOR EACH ROW EXECUTE PROCEDURE \"%s_tile_column_update\"();"
% ((table_name,)*7)
)
cursor_out.execute(
"CREATE FUNCTION \"%s_tile_row_insert\"() RETURNS trigger AS $$"
" BEGIN"
" IF NEW.tile_row < 0 THEN"
" RAISE EXCEPTION 'insert on table ''%s'' violates "
"constraint: tile_row cannot be < 0';"
" END IF;"
" IF NOT (NEW.tile_row < (SELECT matrix_height FROM "
"gpkg_tile_matrix WHERE table_name = '%s' AND zoom_level = "
"NEW.zoom_level)) THEN"
" RAISE EXCEPTION 'insert on table ''%s'' violates "
"constraint: tile_row must by < matrix_height specified for table and "
"zoom level in gpkg_tile_matrix';"
" END IF;"
" RETURN NEW;"
" END;"
"$$ LANGUAGE plpgsql;"
"CREATE TRIGGER \"%s_tile_row_insert\""
"BEFORE INSERT ON \"%s\""
"FOR EACH ROW EXECUTE PROCEDURE \"%s_tile_row_insert\"();"
% ((table_name,)*7)
)
cursor_out.execute(
"CREATE FUNCTION \"%s_tile_row_update\"() RETURNS trigger AS $$"
" BEGIN"
" IF NEW.tile_row < 0 THEN"
" RAISE EXCEPTION 'update on table ''%s'' violates "
"constraint: tile_row cannot be < 0';"
" END IF;"
" IF NOT (NEW.tile_row < (SELECT matrix_height FROM "
"gpkg_tile_matrix WHERE table_name = '%s' AND zoom_level = "
"NEW.zoom_level)) THEN"
" RAISE EXCEPTION 'update on table ''%s'' violates "
"constraint: tile_row must by < matrix_height specified for table and "
"zoom level in gpkg_tile_matrix';"
" END IF;"
" RETURN NEW;"
" END;"
"$$ LANGUAGE plpgsql;"
"CREATE TRIGGER \"%s_tile_row_update\""
"BEFORE UPDATE ON \"%s\""
"FOR EACH ROW EXECUTE PROCEDURE \"%s_tile_row_update\"();"
% ((table_name,)*7)
)
cursor_out.execute(
"CREATE FUNCTION \"%s_zoom_insert\"() RETURNS trigger AS $$"
" BEGIN"
" IF NOT (NEW.zoom_level IN (SELECT zoom_level FROM "
"gpkg_tile_matrix WHERE table_name = '%s')) THEN"
" RAISE EXCEPTION 'insert on table ''%s'' violates "
"constraint: zoom_level not specified for table in gpkg_tile_matrix';"
" END IF;"
" RETURN NEW;"
" END;"
"$$ LANGUAGE plpgsql;"
"CREATE TRIGGER \"%s_zoom_insert\""
"BEFORE INSERT ON \"%s\""
"FOR EACH ROW EXECUTE PROCEDURE \"%s_zoom_insert\"();"
% ((table_name,)*6)
)
cursor_out.execute(
"CREATE FUNCTION \"%s_zoom_update\"() RETURNS trigger AS $$"
" BEGIN"
" IF NOT (NEW.zoom_level IN (SELECT zoom_level FROM "
"gpkg_tile_matrix WHERE table_name = '%s')) THEN"
" RAISE EXCEPTION 'update on table ''%s'' violates "
"constraint: zoom_level not specified for table in gpkg_tile_matrix';"
" END IF;"
" RETURN NEW;"
" END;"
"$$ LANGUAGE plpgsql;"
"CREATE TRIGGER \"%s_zoom_update\""
"BEFORE UPDATE ON \"%s\""
"FOR EACH ROW EXECUTE PROCEDURE \"%s_zoom_update\"();"
% ((table_name,)*6)
)
#Copy content of new table
copy_table(conn_in, conn_out, table_name)
#Adjust serial fro future inserts
cursor_out.execute(
"SELECT setval(pg_get_serial_sequence('\"%s\"', 'id'), "
"coalesce(max(id),0) + 1, false) FROM \"%s\";" % ((table_name,)*2)
)
def read_gpkg(gpkg_filename, pg_connection_string):
if not os.path.exists(gpkg_filename):
sys.stderr.write("ERROR: GeoPackage '%s' not found\n" % gpkg_filename)
sys.exit(1)
with sqlite3.connect(gpkg_filename) as conn_in:
with psycopg2.connect(pg_connection_string) as conn_out:
copy_table(conn_in, conn_out, "gpkg_spatial_ref_sys",
"srs_id NOT IN ('-1','0','4326')")
copy_table(conn_in, conn_out, "gpkg_contents",
"data_type = 'tiles'")
copy_table(conn_in, conn_out, "gpkg_tile_matrix_set")
copy_table(conn_in, conn_out, "gpkg_tile_matrix")
copy_table(conn_in, conn_out, "gpkg_metadata")
copy_table(conn_in, conn_out, "gpkg_metadata_reference")
cursor_in = conn_in.cursor()
cursor_in.execute(
"SELECT table_name FROM gpkg_contents "
"WHERE data_type = 'tiles';"
)
with conn_out.cursor() as cursor_out:
for table_name in cursor_in:
try:
create_tiles_table(
conn_in, conn_out, cursor_out, table_name[0]
)
except psycopg2.IntegrityError as e:
conn_out.rollback()
if e.pgcode == '23505':
sys.stderr.write(
"ERROR: GeoPackage seems to be already "
"imported. Error message was: '%s'.\n"
% e.message
)
sys.exit(1)
except Exception as e:
sys.stderr.write(
"ERROR: Input doesn't seem to be a valid "
"GeoPackage. Error message was: '%s'.\n"
% e.message
)
sys.exit(1)
if __name__ == "__main__":
if len(sys.argv) <= 2:
sys.stderr.write(
"ERROR: Please provide filename of GeoPackage as well as "
"connection string for PostgreSQL.\n"
)
sys.exit(1)
gpkg_filename = sys.argv[1]
pg_connection_string = sys.argv[2]
read_gpkg(gpkg_filename, pg_connection_string)
sys.stdout.write(
"GeoPackage '%s' successfully imported\n" % gpkg_filename
)
sys.exit(0)