Skip to content
This repository has been archived by the owner on Jun 9, 2018. It is now read-only.

Commit

Permalink
Merge pull request #16 from GitHubRGI/auto-close-gpkg
Browse files Browse the repository at this point in the history
Auto close gpkg
  • Loading branch information
stevendlander committed May 6, 2015
2 parents 1fa44af + ffd4b76 commit 7679abf
Showing 1 changed file with 39 additions and 36 deletions.
75 changes: 39 additions & 36 deletions Packaging/tiles2gpkg_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,10 @@ def matrix_height(self, value):

class Geopackage(object):

def __enter__(self):
"""With-statement caller"""
return self

def __init__(self, file_path, srs):
"""Constructor."""
self.__file_path = file_path
Expand All @@ -491,12 +495,12 @@ def __init__(self, file_path, srs):
self.__projection = ScaledWorldMercator()
else:
self.__projection = Geodetic()
self.db_con = connect(self.__file_path)
self.__db_con = connect(self.__file_path)
self.__create_schema()

def __create_schema(self):
"""Create default geopackage schema on the database."""
with self.db_con as db_con:
with self.__db_con as db_con:
cursor = db_con.cursor()
cursor.execute("""
CREATE TABLE gpkg_contents (
Expand Down Expand Up @@ -684,7 +688,7 @@ def file_path(self):
def update_metadata(self, metadata):
"""Update the metadata of the geopackage database after tile merge."""
# initialize a new projection
with self.db_con as db_con:
with self.__db_con as db_con:
cursor = db_con.cursor()
tile_matrix_stmt = """
INSERT OR REPLACE INTO gpkg_tile_matrix (
Expand Down Expand Up @@ -742,7 +746,7 @@ def update_metadata(self, metadata):

def execute(self, statement, inputs=None):
"""Execute a prepared SQL statement on this geopackage database."""
with self.db_con as db_con:
with self.__db_con as db_con:
cursor = db_con.cursor()
if inputs is not None:
result_cursor = cursor.execute(statement, inputs)
Expand All @@ -754,7 +758,7 @@ def assimilate(self, source):
"""Assimilate .gpkg.part tiles into this geopackage database."""
if not exists(source):
raise IOError
with self.db_con as db_con:
with self.__db_con as db_con:
cursor = db_con.cursor()
cursor.execute("pragma synchronous = off;")
cursor.execute("pragma journal_mode = off;")
Expand All @@ -774,11 +778,9 @@ def assimilate(self, source):
raise
remove(source)

def close(self):
"""
Closes the sqlite3 db handle for this object.
"""
self.db_con.close()
def __exit__(self, type, value, traceback):
"""Resource cleanup on destruction."""
self.__db_con.close()


class TempDB(object):
Expand All @@ -787,6 +789,10 @@ class TempDB(object):
Has a <filename>.gpkg.part file format.
"""

def __enter__(self):
"""With-statement caller."""
return self

def __init__(self, filename):
"""
Constructor.
Expand All @@ -797,8 +803,8 @@ def __init__(self, filename):
uid = uuid4()
self.name = uid.hex + '.gpkg.part'
self.__file_path = join(filename, self.name)
self.db_con = connect(self.__file_path)
with self.db_con as db_con:
self.__db_con = connect(self.__file_path)
with self.__db_con as db_con:
cursor = db_con.cursor()
stmt = """
CREATE TABLE tiles (
Expand All @@ -822,7 +828,7 @@ def __init__(self, filename):
"""

def execute(self, statement, inputs=None):
with self.db_con as db_con:
with self.__db_con as db_con:
cursor = db_con.cursor()
if inputs is not None:
result_cursor = cursor.execute(statement, inputs)
Expand All @@ -841,15 +847,13 @@ def insert_image_blob(self, z, x, y, data):
y -- the column number of the data
data -- the image data containing in a binary array
"""
with self.db_con as db_con:
with self.__db_con as db_con:
cursor = db_con.cursor()
cursor.execute(self.image_blob_stmt, (z, x, y, data))

def close(self):
"""
Closes this sqlite3 database handle.
"""
self.db_con.close()
def __exit__(self, type, value, traceback):
"""Resource cleanup on destruction."""
self.__db_con.close()


def img_to_buf(img, img_type, jpeg_quality=75):
Expand Down Expand Up @@ -1013,18 +1017,18 @@ def sqlite_worker(file_list, extra_args):
the tiles in the TMS directory
"""
temp_db = TempDB(extra_args['root_dir'])
invert_y = None
if extra_args['lower_left']:
if extra_args['srs'] == 3857:
invert_y = Mercator.invert_y
elif extra_args['srs'] == 4326:
invert_y = Geodetic.invert_y
elif extra_args['srs'] == 3395:
invert_y = EllipsoidalMercator.invert_y
elif extra_args['srs'] == 9804:
invert_y = ScaledWorldMercator.invert_y
[worker_map(temp_db, item, extra_args, invert_y) for item in file_list]
temp_db.close()
with TempDB(extra_args['root_dir']) as temp_db:
invert_y = None
if extra_args['lower_left']:
if extra_args['srs'] == 3857:
invert_y = Mercator.invert_y
elif extra_args['srs'] == 4326:
invert_y = Geodetic.invert_y
elif extra_args['srs'] == 3395:
invert_y = EllipsoidalMercator.invert_y
elif extra_args['srs'] == 9804:
invert_y = ScaledWorldMercator.invert_y
[worker_map(temp_db, item, extra_args, invert_y) for item in file_list]


def allocate(cores, pool, file_list, extra_args):
Expand Down Expand Up @@ -1194,7 +1198,6 @@ def main(arg_list):
# Build the tile matrix info object
tile_info = build_lut(files, lower_left, arg_list.srs)
# Initialize the output file
output_geopackage = Geopackage(arg_list.output_file, arg_list.srs)
if arg_list.threading:
# Enable tiling on multiple CPU cores
cores = cpu_count()
Expand Down Expand Up @@ -1237,10 +1240,10 @@ def main(arg_list):
imagery=arg_list.imagery, jpeg_quality=arg_list.q)
sqlite_worker(files, extra_args)
# Combine the individual temp databases into the output file
combine_worker_dbs(output_geopackage)
# Using the data in the output file, create the metadata for it
output_geopackage.update_metadata(tile_info)
output_geopackage.close()
with Geopackage(arg_list.output_file, arg_list.srs) as gpkg:
combine_worker_dbs(gpkg)
# Using the data in the output file, create the metadata for it
gpkg.update_metadata(tile_info)
print("Complete")

if __name__ == '__main__':
Expand Down

0 comments on commit 7679abf

Please sign in to comment.