Example #1
0
def esripack_to_mbtiles(directory_path, mbtiles_file, metadata_file=""):
    silent = False  # 安静模式
    con = mbtiles_connect(mbtiles_file, silent)
    cur = con.cursor()
    optimize_connection(cur)
    mbtiles_setup(cur)

    starttime = datetime.datetime.now()  # 计算时间
    # 写入配置参数
    try:
        metadata = json.load(open(metadata_file, 'r'))  # metadata.json
        for name, value in metadata.items():
            cur.execute('insert into metadata (name, value) values (?, ?)',
                        (name, value))
        if not silent:
            logger.info('metadata from metadata.json restored')
    except IOError:
        if not silent:
            logger.warning('metadata.json not found')

    bundle_list = get_bundle_list_from_dir(directory_path)
    for bundle in bundle_list:
        get_tdt_tile_from_bundle(bundle, cur)

    optimize_database(con, silent)
    endtime = datetime.datetime.now()  # 计算时间
    print("Mbtiles数据生成成功!运行时间(s):", (endtime - starttime).seconds)
Example #2
0
 def __init__(self, filepath, create=False, force=False, silent=False):
     self._silent = silent
     if force and os.path.exists(filepath):
         os.remove(filepath)
     self._connnection = mb.mbtiles_connect(filepath, self._silent)
     self._cursor = self._connnection.cursor()
     mb.optimize_connection(self._cursor)
     if create:
         mb.mbtiles_setup(self._cursor)
def patch_mbtiles(source_file, target_file):
    conn = mbutil.mbtiles_connect(target_file)
    conn.executescript("""
        PRAGMA journal_mode=PERSIST;
        PRAGMA page_size=80000;
        PRAGMA synchronous=OFF;
        ATTACH DATABASE '{}' AS source;
        REPLACE INTO map SELECT * FROM source.map;
        REPLACE INTO images SELECT * FROM source.images;
        """.format(source_file))
def patch_mbtiles(source_file, target_file):
    conn = mbutil.mbtiles_connect(target_file)
    conn.executescript(
        """
        PRAGMA journal_mode=PERSIST;
        PRAGMA page_size=80000;
        PRAGMA synchronous=OFF;
        ATTACH DATABASE '{}' AS source;
        REPLACE INTO map SELECT * FROM source.map;
        REPLACE INTO images SELECT * FROM source.images;
        """.format(source_file)
    )
def update_metadata(mbtiles_file, metadata):
    """
    Update metadata key value pairs inside the MBTiles file
    with the provided metadata
    """
    conn = mbutil.mbtiles_connect(mbtiles_file)

    def upsert_entry(key, value):
        conn.execute("DELETE FROM metadata WHERE name='{}'".format(key))
        conn.execute("INSERT INTO metadata VALUES('{}', '{}')".format(key, value))

    for key, value in metadata.items():
        upsert_entry(key, value)
def update_metadata(mbtiles_file, metadata):
    """
    Update metadata key value pairs inside the MBTiles file
    with the provided metadata
    """
    conn = mbutil.mbtiles_connect(mbtiles_file)

    def upsert_entry(key, value):
        conn.execute("DELETE FROM metadata WHERE name='{}'".format(key))
        conn.execute("INSERT INTO metadata VALUES('{}', '{}')".format(
            key, value))

    for key, value in metadata.items():
        upsert_entry(key, value)
Example #7
0
def tile(tif_file, out_dir, remove_blank_tiles=0, verbose=0):

    if bool(verbose):
        print 'tiling image ' + tif_file

    if not known_codec(tif_file):
        if verbose:
            print 'uncompressing image'
        uncompress_tif(tif_file, out_dir)

    vips_file = convert_to_vips(tif_file, out_dir)

    tmp_file = os.path.join(out_dir, 'tmp-file.v')
    tile_dim = 256

    source_x_pix = get_x_dim(vips_file)
    source_y_pix = get_y_dim(vips_file)

    if source_y_pix > source_x_pix:
        if bool(verbose):
            print 'rotating image...'
        cmd = 'vips im_rot90 %s %s' % (vips_file, tmp_file)
        os.system(cmd)
        os.rename(tmp_file, vips_file)
        temp = source_x_pix
        source_x_pix = source_y_pix
        source_y_pix = temp

    source_x_tiles = int(math.ceil(source_x_pix / float(tile_dim)))
    source_y_tiles = int(math.ceil(source_y_pix / float(tile_dim)))

    max_zoom = int(math.ceil(math.log(source_x_tiles, 2)))
    max_x_edge_tile = int(math.pow(2, max_zoom))

    max_y_edge_log = int(math.ceil(math.log(source_y_tiles, 2)))
    max_y_edge_tile = int(math.pow(2, max_y_edge_log))

    delta = max_x_edge_tile - max_y_edge_tile

    max_x_edge_pix = max_x_edge_tile * tile_dim
    max_y_edge_pix = max_y_edge_tile * tile_dim

    x_pad = (max_x_edge_pix - source_x_pix) / 2
    y_pad = (max_y_edge_pix - source_y_pix) / 2

    cmd = (
        'vips im_embed %s %s %s %s %s %s %s' %
        (vips_file, tmp_file, 0, x_pad, y_pad, max_x_edge_pix, max_y_edge_pix))

    resize = subprocess.Popen(cmd, shell=True)
    sts = os.waitpid(resize.pid, 0)
    os.rename(tmp_file, vips_file)

    mbtiles = mbutil.mbtiles_connect("%s.mbtiles" % tif_file)
    cur = mbtiles.cursor()
    mbutil.mbtiles_setup(cur)
    mbtiles.commit()

    if source_x_tiles % 2 != 0:
        source_x_tiles = source_x_tiles + 1
    if source_y_tiles % 2 != 0:
        source_y_tiles = source_y_tiles + 1
    x_offset = (max_x_edge_tile - source_x_tiles) / 2
    y_offset = (max_x_edge_tile - source_y_tiles) / 2
    y_delta = delta / 2

    for zoom in range(max_zoom, -1, -1):
        if bool(verbose):
            print 'Processing zoom level ' + str(zoom)
        tiles = int(math.pow(2, zoom))
        #resize image
        if zoom < max_zoom:
            cmd = 'vips im_shrink ' + vips_file + ' ' + tmp_file + ' 2 2'
            resize = subprocess.Popen(cmd, shell=True)
            sts = os.waitpid(resize.pid, 0)
            os.rename(tmp_file, vips_file)

        x_dim = get_x_dim(vips_file) / tile_dim
        y_dim = get_y_dim(vips_file) / tile_dim

        num_x_tiles = tiles - 2 * x_offset
        num_y_tiles = tiles - 2 * y_offset

        if x_dim != y_dim and y_dim <= num_y_tiles:
            #square up the image
            y_pad = ((x_dim - y_dim) / 2) * tile_dim
            dim = x_dim * tile_dim
            cmd = 'vips im_embed %s %s %s %s %s %s %s' % (
                vips_file, tmp_file, 0, 0, y_pad, dim, dim)
            resize = subprocess.Popen(cmd, shell=True)
            sts = os.waitpid(resize.pid, 0)
            os.rename(tmp_file, vips_file)
            y_delta = 0

        for y in range(y_offset, tiles - y_offset):
            if bool(verbose):
                print 'Processing row ' + str(y)
            for x in range(x_offset, tiles - x_offset):
                if bool(verbose):
                    print 'Processing column ' + str(x)
                left = x * tile_dim
                if y > y_delta:
                    top = (y - y_delta) * tile_dim
                else:
                    top = y * tile_dim
                tile_name = 'tile_%s_%s_%s.jpg' % (zoom, x, y)
                tile_file = os.path.join(out_dir, tile_name)
                cmd = 'vips im_extract_area %s %s %s %s 256 256 2>/dev/null' % (
                    vips_file, tile_file, left, top)
                extract = subprocess.Popen(cmd, shell=True)
                sts = os.waitpid(extract.pid, 0)
                f = open(tile_file, 'rb')
                cur.execute(
                    """insert into tiles (zoom_level,
tile_column, tile_row, tile_data) values
(?, ?, ?, ?);""", (zoom, x, y, sqlite3.Binary(f.read())))
                f.close()
                mbtiles.commit()
                if remove_blank_tiles and is_blank_tile(tile_file):
                    if bool(verbose):
                        print 'Removing tile %s ' % tile_file
                    os.unlink(tile_file)

        x_offset = x_offset / 2
        y_offset = y_offset / 2
        if y_delta > 0:
            y_delta = y_delta / 2
        if x_offset == 1:
            x_offset = 0
        if y_offset == 1:
            y_offset = 0
        if y_delta == 1:
            y_delta = 0
    os.unlink(vips_file)
    mbtiles.close()
    if bool(verbose):
        print 'done'
Example #8
0
def tile(tif_file, out_dir, remove_blank_tiles=0, verbose=0):

    if bool(verbose):
        print 'tiling image ' + tif_file

    if not known_codec(tif_file):
        if verbose:
            print 'uncompressing image'
        uncompress_tif(tif_file, out_dir)

    vips_file = convert_to_vips(tif_file, out_dir)

    tmp_file = os.path.join(out_dir, 'tmp-file.v')
    tile_dim = 256

    source_x_pix = get_x_dim(vips_file)
    source_y_pix = get_y_dim(vips_file)

    if source_y_pix > source_x_pix:
        if bool(verbose):
            print 'rotating image...'
        cmd = 'vips im_rot90 %s %s' % (vips_file, tmp_file)
        os.system(cmd)
        os.rename(tmp_file, vips_file)
        temp = source_x_pix
        source_x_pix = source_y_pix
        source_y_pix = temp

    source_x_tiles = int(math.ceil(source_x_pix / float(tile_dim)))
    source_y_tiles = int(math.ceil(source_y_pix / float(tile_dim)))

    max_zoom = int(math.ceil(math.log(source_x_tiles, 2)))
    max_x_edge_tile = int(math.pow(2, max_zoom))

    max_y_edge_log = int(math.ceil(math.log(source_y_tiles, 2)))
    max_y_edge_tile = int(math.pow(2, max_y_edge_log))

    delta = max_x_edge_tile - max_y_edge_tile

    max_x_edge_pix = max_x_edge_tile * tile_dim
    max_y_edge_pix = max_y_edge_tile * tile_dim

    x_pad = (max_x_edge_pix - source_x_pix) / 2
    y_pad = (max_y_edge_pix - source_y_pix) / 2

    cmd = ('vips im_embed %s %s %s %s %s %s %s'
           % (vips_file, tmp_file, 0, x_pad, y_pad,
              max_x_edge_pix, max_y_edge_pix))

    resize = subprocess.Popen(cmd, shell=True)
    sts = os.waitpid(resize.pid, 0)
    os.rename(tmp_file, vips_file)

    mbtiles = mbutil.mbtiles_connect("%s.mbtiles" % tif_file)
    cur = mbtiles.cursor()
    mbutil.mbtiles_setup(cur)
    mbtiles.commit()

    if source_x_tiles % 2 != 0:
        source_x_tiles = source_x_tiles + 1
    if source_y_tiles % 2 != 0:
        source_y_tiles = source_y_tiles + 1
    x_offset = (max_x_edge_tile - source_x_tiles) / 2
    y_offset = (max_x_edge_tile - source_y_tiles) / 2
    y_delta = delta / 2

    for zoom in range(max_zoom, -1, -1):
        if bool(verbose):
            print 'Processing zoom level ' + str(zoom)
        tiles = int(math.pow(2, zoom))
        #resize image
        if zoom < max_zoom:
            cmd = 'vips im_shrink ' + vips_file + ' ' + tmp_file + ' 2 2'
            resize = subprocess.Popen(cmd, shell=True)
            sts = os.waitpid(resize.pid, 0)
            os.rename(tmp_file, vips_file)

        x_dim = get_x_dim(vips_file) / tile_dim
        y_dim = get_y_dim(vips_file) / tile_dim

        num_x_tiles = tiles - 2 * x_offset
        num_y_tiles = tiles - 2 * y_offset

        if x_dim != y_dim and y_dim <= num_y_tiles:
            #square up the image
            y_pad = ((x_dim - y_dim) / 2) * tile_dim
            dim = x_dim * tile_dim
            cmd = 'vips im_embed %s %s %s %s %s %s %s' % (vips_file, tmp_file, 0, 0, y_pad, dim, dim)
            resize = subprocess.Popen(cmd, shell=True)
            sts = os.waitpid(resize.pid, 0)
            os.rename(tmp_file, vips_file)
            y_delta = 0

        for y in range(y_offset, tiles - y_offset):
            if bool(verbose):
                print 'Processing row ' + str(y)
            for x in range(x_offset, tiles - x_offset):
                if bool(verbose):
                    print 'Processing column ' + str(x)
                left = x * tile_dim
                if y > y_delta:
                    top = (y - y_delta) * tile_dim
                else:
                    top = y * tile_dim
                tile_name = 'tile_%s_%s_%s.jpg' % (zoom, x, y)
                tile_file = os.path.join(out_dir, tile_name)
                cmd = 'vips im_extract_area %s %s %s %s 256 256 2>/dev/null' % (vips_file, tile_file, left, top)
                extract = subprocess.Popen(cmd, shell=True)
                sts = os.waitpid(extract.pid, 0)
		f = open(tile_file, 'rb')
		cur.execute("""insert into tiles (zoom_level,
tile_column, tile_row, tile_data) values
(?, ?, ?, ?);""", (zoom, x, y, sqlite3.Binary(f.read())))
                f.close()
		mbtiles.commit()
                if remove_blank_tiles and is_blank_tile(tile_file):
                    if bool(verbose):
                        print 'Removing tile %s ' % tile_file
                    os.unlink(tile_file)

        x_offset = x_offset / 2
        y_offset = y_offset / 2
        if y_delta > 0:
            y_delta = y_delta / 2
        if x_offset == 1:
            x_offset = 0
        if y_offset == 1:
            y_offset = 0
        if y_delta == 1:
            y_delta = 0
    os.unlink(vips_file)
    mbtiles.close()
    if bool(verbose):
        print 'done'
Example #9
0
 def __init__(self, mbtiles_file, scheme):
     self.conn = mbutil.mbtiles_connect(mbtiles_file)
     self.scheme = scheme
Example #10
0
def tdtzip_to_mbtiles(zipfile_path, mbtiles_file, metadata_file=""):

    silent = False  # 安静模式
    compression = False  # 压缩

    if not silent:
        logger.info("Importing disk to MBTiles")
        logger.debug("%s --> %s" % (zipfile_path, mbtiles_file))

    con = mbtiles_connect(mbtiles_file, silent)
    cur = con.cursor()
    optimize_connection(cur)
    mbtiles_setup(cur)
    #~ image_format = 'png'
    image_format = 'png|jpg|tif'  # 支持的文件格式

    starttime = datetime.datetime.now()  # 计算时间
    # 写入配置参数
    try:
        metadata = json.load(open(metadata_file, 'r'))  # metadata.json
        for name, value in metadata.items():
            cur.execute('insert into metadata (name, value) values (?, ?)',
                        (name, value))
        if not silent:
            logger.info('metadata from metadata.json restored')
    except IOError:
        if not silent:
            logger.warning('metadata.json not found')

    count = 0

    zf = zipfile.ZipFile(zipfile_path, "r")
    for file in zf.namelist():
        import re
        matchObj = re.match(
            r'(?P<img>.*)_(?P<proj>.*)_(?P<time>\d{8})_(?P<x>\d*)'
            r'_(?P<y>\d*)_(?P<z>\d*).(?P<type>.*)', file, re.M | re.I)
        if matchObj:
            x = int(matchObj.groupdict()['x'])
            y = int(matchObj.groupdict()['y'])
            z = int(matchObj.groupdict()['z'])
            image_type = matchObj.groupdict()['type']
            if image_type.lower() in image_format:
                bytes = zf.read(file)
                xyz = file.split('_')
                x = int(xyz[-3])
                y = int(xyz[-2])
                z = int(xyz[-1].split('.')[0])

                if not silent:
                    logger.debug(
                        ' Read tile from Zoom (z): %i\tCol (x): %i\tRow (y): %i'
                        % (z, x, y))
                cur.execute(
                    """insert into tiles (zoom_level,
                    tile_column, tile_row, tile_data) values
                    (?, ?, ?, ?);""", (z, x, y, sqlite3.Binary(bytes)))

    optimize_database(con, silent)
    endtime = datetime.datetime.now()  # 计算时间
    print("Mbtiles数据生成成功!运行时间(s):", (endtime - starttime).seconds)
Example #11
0
def esritiles_to_mbtiles(directory_path, mbtiles_file, metadata_file=""):

    silent = False  # 安静模式
    compression = False  # 压缩

    if not silent:
        logger.info("Importing disk to MBTiles")
        logger.debug("%s --> %s" % (directory_path, mbtiles_file))

    con = mbtiles_connect(mbtiles_file, silent)
    cur = con.cursor()
    optimize_connection(cur)
    mbtiles_setup(cur)
    #~ image_format = 'png'
    image_format = 'png|jpg|tif'  # 支持的文件格式

    starttime = datetime.datetime.now()  # 计算时间
    # 写入配置参数
    try:
        metadata = json.load(open(metadata_file, 'r'))  # metadata.json
        for name, value in metadata.items():
            cur.execute('insert into metadata (name, value) values (?, ?)',
                        (name, value))
        if not silent:
            logger.info('metadata from metadata.json restored')
    except IOError:
        if not silent:
            logger.warning('metadata.json not found')

    count = 0

    for zoom_dir in get_dirs(directory_path):
        if "L" in zoom_dir:
            z = int(zoom_dir[1:])
        for row_dir in get_dirs(os.path.join(directory_path, zoom_dir)):
            x = int(row_dir[1:], 16)
            for current_file in os.listdir(
                    os.path.join(directory_path, zoom_dir, row_dir)):
                file_name, ext = current_file.split('.', 1)
                f = open(
                    os.path.join(directory_path, zoom_dir, row_dir,
                                 current_file), 'rb')
                file_content = f.read()
                f.close()

                tile_size = len(file_content)
                if tile_size == 872:  # png_kong = 876
                    continue
                y = int(file_name[1:], 16)

                if (ext[1].lower() in image_format):
                    if not silent:
                        logger.debug(
                            ' Read tile from Zoom (z): %i\tCol (x): %i\tRow (y): %i'
                            % (z, x, y))
                    cur.execute(
                        """insert into tiles (zoom_level,
                        tile_column, tile_row, tile_data) values
                        (?, ?, ?, ?);""",
                        (z, x, y, sqlite3.Binary(file_content)))

    optimize_database(con, silent)
    endtime = datetime.datetime.now()  # 计算时间
    print("Mbtiles数据生成成功!运行时间(s):", (endtime - starttime).seconds)
Example #12
0
import mbutil

tileFile = r"E:\GHANA_Data\basemap\selected\031113120110_16.mbtiles"
silent = False
con = mbutil.mbtiles_connect(tileFile, silent)
cur = con.cursor()

mbutil.optimize_connection(cur)
mbutil.mbtiles_setup(cur)

mbutil.compression_prepare(cur, silent)
mbutil.compression_do(cur, con, 256, silent)
mbutil.compression_finalize(cur, con, silent)
mbutil.optimize_database(con, silent)