0

Inside of my .mbtiles SQLite db, there's a column called tile_data holding the information relating to the specific tile that is being queried.

My application is taking a user long/lat as an input and then converting to a tile_row and tile_column equivalent values, then pulling the single blob with the data inside via the SQL query below.

The query (fully functional, just to show the code) is:

string query =
   String.Format("SELECT tile_data from tiles WHERE zoom_level = {0} AND tile_row = {1} AND tile_column = {2}", ZoomLevel, _merc.TileLat, _merc.TileLong);
   SQLiteCommand command = new SQLiteCommand(query, DbConn);
   SQLiteDataReader reader = command.ExecuteReader();

          while (reader.Read())
                {
                    // ...
                }

With the result being a single blob file, querying SELECT * will pull back the single tile with all data.

The blob inside of tile_data is a compressed (gzip) vector file that I need to access and work with, but I can't figure out how to decompress it within my application. Most gzip links that I've read through have been regarding file compression and outputting it to a .txt (or similar), whereas I'm looking to use this data in memory without needing an external file.

How can I decompress the blob and access he data in memory, rather than an external file?

2
  • You should be able to pass the data to a MemoryStream, then pass that to a GZipStream with CompressionMode.Decompress then pass that to a StreamReader. Commented Jul 1, 2016 at 12:10
  • You could use SqlDataReader.GetStream to allow you to pull in the data in chunks instead of loading the entire file into a MemoryStream. Commented Jul 1, 2016 at 12:22

1 Answer 1

2

Try something like this

SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
    using(var file = reader.GetStream(0))
    using(var unzip = new GZipStream(file, CompressionMode.Decompress))
    using(var fileReader = new StreamReader(unzip))
    {
        while(!fileReader.EndOfStream)
        {
            var line = fileReader.ReadLine();
        }
    }
}

Depending on exactly what your file is you might need to substitute a different type of reader.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.