Actually write tiles directly to the .mbtiles file!

This commit is contained in:
Eric Fischer 2014-09-25 16:34:17 -07:00
parent ce485148fe
commit 528e0575e2
3 changed files with 19 additions and 18 deletions

View File

@ -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);

29
tile.cc
View File

@ -8,6 +8,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <math.h>
#include <sqlite3.h>
#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;
}

2
tile.h
View File

@ -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);