From 528e0575e22cd41c76b8ad6a62046bc6b4eff3c1 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Thu, 25 Sep 2014 16:34:17 -0700 Subject: [PATCH] Actually write tiles directly to the .mbtiles file! --- geojson.c | 6 +++--- tile.cc | 29 +++++++++++++++-------------- tile.h | 2 +- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/geojson.c b/geojson.c index e1bbf21..0245691 100644 --- a/geojson.c +++ b/geojson.c @@ -323,7 +323,7 @@ void range_search(struct index *ix, long long n, unsigned long long start, unsig } } -void check(struct index *ix, long long n, char *metabase, unsigned *file_bbox, struct pool *file_keys, unsigned *midx, unsigned *midy, char *layername, int maxzoom, int minzoom, char *outdir) { +void check(struct index *ix, long long n, char *metabase, unsigned *file_bbox, struct pool *file_keys, unsigned *midx, unsigned *midy, char *layername, int maxzoom, int minzoom, sqlite3 *outdb) { fprintf(stderr, "\n"); long long most = 0; @@ -359,7 +359,7 @@ void check(struct index *ix, long long n, char *metabase, unsigned *file_bbox, s fprintf(stderr, " %3.1f%% %d/%u/%u %x %x %lld to %lld \r", (((i - ix) + (j - ix)) / 2.0 / n + (maxzoom - z)) / (maxzoom - minzoom + 1) * 100, z, tx, ty, wx, wy, (long long)(i - ix), (long long)(j - ix)); - long long len = write_tile(i, j, metabase, file_bbox, z, tx, ty, z == maxzoom ? 12 : 10, maxzoom, file_keys, layername, outdir); + long long len = write_tile(i, j, metabase, file_bbox, z, tx, ty, z == maxzoom ? 12 : 10, maxzoom, file_keys, layername, outdb); if (z == maxzoom && len > most) { *midx = tx; @@ -622,7 +622,7 @@ next_feature: } qsort(index, indexst.st_size / sizeof(struct index), sizeof(struct index), indexcmp); - check(index, indexst.st_size / sizeof(struct index), meta, file_bbox, &file_keys, &midx, &midy, layername, maxzoom, minzoom, outdir); + check(index, indexst.st_size / sizeof(struct index), meta, file_bbox, &file_keys, &midx, &midy, layername, maxzoom, minzoom, outdb); munmap(index, indexst.st_size); munmap(meta, metast.st_size); diff --git a/tile.cc b/tile.cc index 4b3cd67..ac5971f 100644 --- a/tile.cc +++ b/tile.cc @@ -8,6 +8,7 @@ #include #include #include +#include #include "vector_tile.pb.h" extern "C" { @@ -380,7 +381,7 @@ int simplify_lines(struct draw *geom, int n, int z, int detail) { return out; } -long long write_tile(struct index *start, struct index *end, char *metabase, unsigned *file_bbox, int z, unsigned tx, unsigned ty, int detail, int basezoom, struct pool *file_keys, char *layername, char *outdir) { +long long write_tile(struct index *start, struct index *end, char *metabase, unsigned *file_bbox, int z, unsigned tx, unsigned ty, int detail, int basezoom, struct pool *file_keys, char *layername, sqlite3 *outdb) { GOOGLE_PROTOBUF_VERIFY_VERSION; mapnik::vector::tile tile; @@ -526,21 +527,21 @@ long long write_tile(struct index *start, struct index *end, char *metabase, uns exit(EXIT_FAILURE); } - const char *prefix = outdir; - char path[strlen(prefix) + 200]; + sqlite3_stmt *stmt; + const char *query = "insert into tiles (zoom_level, tile_column, tile_row, tile_data) values (?, ?, ?, ?)"; + if (sqlite3_prepare_v2(outdb, query, -1, &stmt, NULL) != SQLITE_OK) { + fprintf(stderr, "sqlite3 insert prep failed\n"); + exit(EXIT_FAILURE); + } - mkdir(prefix, 0777); + sqlite3_bind_int(stmt, 1, z); + sqlite3_bind_int(stmt, 2, tx); + sqlite3_bind_int(stmt, 3, (1 << z) - 1 - ty); + sqlite3_bind_blob(stmt, 4, compressed.data(), compressed.size(), NULL); - sprintf(path, "%s/%d", prefix, z); - mkdir(path, 0777); - - sprintf(path, "%s/%d/%u", prefix, z, tx); - mkdir(path, 0777); - - sprintf(path, "%s/%d/%u/%u.pbf", prefix, z, tx, ty); - FILE *f = fopen(path, "wb"); - fwrite(compressed.data(), 1, compressed.size(), f); - fclose(f); + if (sqlite3_step(stmt) != SQLITE_DONE) { + fprintf(stderr, "sqlite3 insert failed: %s\n", sqlite3_errmsg(outdb)); + } return count; } diff --git a/tile.h b/tile.h index ec00928..fbfddb4 100644 --- a/tile.h +++ b/tile.h @@ -49,4 +49,4 @@ struct index { }; -long long write_tile(struct index *start, struct index *end, char *metabase, unsigned *file_bbox, int z, unsigned x, unsigned y, int detail, int basezoom, struct pool *file_keys, char *layername, char *outdir); +long long write_tile(struct index *start, struct index *end, char *metabase, unsigned *file_bbox, int z, unsigned x, unsigned y, int detail, int basezoom, struct pool *file_keys, char *layername, sqlite3 *outdb);