mirror of
https://github.com/mapbox/tippecanoe.git
synced 2025-02-08 12:00:26 +00:00
Stop using malloc for layer names
This commit is contained in:
parent
68c3bafab0
commit
271ec3d154
48
main.cpp
48
main.cpp
@ -1177,14 +1177,10 @@ int read_input(std::vector<source> &sources, char *fname, const char *layername,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char *layernames[nlayers];
|
std::vector<std::string> layernames;
|
||||||
for (i = 0; i < nlayers; i++) {
|
for (i = 0; i < nlayers; i++) {
|
||||||
if (layername != NULL) {
|
if (layername != NULL) {
|
||||||
layernames[i] = strdup(layername);
|
layernames.push_back(std::string(layername));
|
||||||
if (layernames[i] == NULL) {
|
|
||||||
perror("Out of memory");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
const char *src;
|
const char *src;
|
||||||
if (sources.size() < 1) {
|
if (sources.size() < 1) {
|
||||||
@ -1195,39 +1191,37 @@ int read_input(std::vector<source> &sources, char *fname, const char *layername,
|
|||||||
src = sources[i].file.c_str();
|
src = sources[i].file.c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
char *trunc = layernames[i] = (char *) malloc(strlen(src) + 1);
|
// Find the last component of the pathname
|
||||||
if (trunc == NULL) {
|
|
||||||
perror("Out of memory");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *ocp, *use = src;
|
const char *ocp, *use = src;
|
||||||
for (ocp = src; *ocp; ocp++) {
|
for (ocp = src; *ocp; ocp++) {
|
||||||
if (*ocp == '/' && ocp[1] != '\0') {
|
if (*ocp == '/' && ocp[1] != '\0') {
|
||||||
use = ocp + 1;
|
use = ocp + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
strcpy(trunc, use);
|
std::string trunc = std::string(use);
|
||||||
|
|
||||||
char *cp = strstr(trunc, ".json");
|
// Trim .json or .mbtiles from the name
|
||||||
if (cp != NULL) {
|
ssize_t cp;
|
||||||
*cp = '\0';
|
cp = trunc.find(".json");
|
||||||
|
if (cp >= 0) {
|
||||||
|
trunc = trunc.substr(0, cp);
|
||||||
}
|
}
|
||||||
cp = strstr(trunc, ".mbtiles");
|
cp = trunc.find(".mbtiles");
|
||||||
if (cp != NULL) {
|
if (cp >= 0) {
|
||||||
*cp = '\0';
|
trunc = trunc.substr(0, cp);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *out = trunc;
|
// Trim out characters that can't be part of selector
|
||||||
for (cp = trunc; *cp; cp++) {
|
std::string out;
|
||||||
if (isalpha(*cp) || isdigit(*cp) || *cp == '_') {
|
for (cp = 0; cp < trunc.size(); cp++) {
|
||||||
*out++ = *cp;
|
if (isalpha(trunc[cp]) || isdigit(trunc[cp]) || trunc[cp] == '_') {
|
||||||
|
out.append(trunc, cp, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*out = '\0';
|
layernames.push_back(out);
|
||||||
|
|
||||||
if (!quiet) {
|
if (!quiet) {
|
||||||
fprintf(stderr, "For layer %d, using name \"%s\"\n", i, trunc);
|
fprintf(stderr, "For layer %d, using name \"%s\"\n", i, out.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1692,10 +1686,6 @@ int read_input(std::vector<source> &sources, char *fname, const char *layername,
|
|||||||
|
|
||||||
mbtiles_write_metadata(outdb, fname, layernames, minzoom, maxzoom, minlat, minlon, maxlat, maxlon, midlat, midlon, file_keys, nlayers, forcetable, attribution);
|
mbtiles_write_metadata(outdb, fname, layernames, minzoom, maxzoom, minlat, minlon, maxlat, maxlon, midlat, midlon, file_keys, nlayers, forcetable, attribution);
|
||||||
|
|
||||||
for (i = 0; i < nlayers; i++) {
|
|
||||||
free(layernames[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,7 +132,7 @@ bool type_and_string::operator<(const type_and_string &o) const {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, char **layername, int minzoom, int maxzoom, double minlat, double minlon, double maxlat, double maxlon, double midlat, double midlon, std::vector<std::set<type_and_string> > &file_keys, int nlayers, int forcetable, const char *attribution) {
|
void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, std::vector<std::string> &layername, int minzoom, int maxzoom, double minlat, double minlon, double maxlat, double maxlon, double midlat, double midlon, std::vector<std::set<type_and_string> > &file_keys, int nlayers, int forcetable, const char *attribution) {
|
||||||
char *sql, *err;
|
char *sql, *err;
|
||||||
|
|
||||||
sql = sqlite3_mprintf("INSERT INTO metadata (name, value) VALUES ('name', %Q);", fname);
|
sql = sqlite3_mprintf("INSERT INTO metadata (name, value) VALUES ('name', %Q);", fname);
|
||||||
@ -237,7 +237,7 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, char **layername,
|
|||||||
}
|
}
|
||||||
|
|
||||||
aprintf(&buf, "{ \"id\": \"");
|
aprintf(&buf, "{ \"id\": \"");
|
||||||
quote(&buf, layername[i]);
|
quote(&buf, layername[i].c_str());
|
||||||
aprintf(&buf, "\", \"description\": \"\", \"minzoom\": %d, \"maxzoom\": %d, \"fields\": {", minzoom, maxzoom);
|
aprintf(&buf, "\", \"description\": \"\", \"minzoom\": %d, \"maxzoom\": %d, \"fields\": {", minzoom, maxzoom);
|
||||||
|
|
||||||
std::set<type_and_string>::iterator j;
|
std::set<type_and_string>::iterator j;
|
||||||
|
@ -9,6 +9,6 @@ sqlite3 *mbtiles_open(char *dbname, char **argv, int forcetable);
|
|||||||
|
|
||||||
void mbtiles_write_tile(sqlite3 *outdb, int z, int tx, int ty, const char *data, int size);
|
void mbtiles_write_tile(sqlite3 *outdb, int z, int tx, int ty, const char *data, int size);
|
||||||
|
|
||||||
void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, char **layername, int minzoom, int maxzoom, double minlat, double minlon, double maxlat, double maxlon, double midlat, double midlon, std::vector<std::set<type_and_string> > &file_keys, int nlayers, int forcetable, const char *attribution);
|
void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, std::vector<std::string> &layername, int minzoom, int maxzoom, double minlat, double minlon, double maxlat, double maxlon, double midlat, double midlon, std::vector<std::set<type_and_string> > &file_keys, int nlayers, int forcetable, const char *attribution);
|
||||||
|
|
||||||
void mbtiles_close(sqlite3 *outdb, char **argv);
|
void mbtiles_close(sqlite3 *outdb, char **argv);
|
||||||
|
@ -25,7 +25,7 @@ struct stats {
|
|||||||
double minlat, minlon, maxlat, maxlon;
|
double minlat, minlon, maxlat, maxlon;
|
||||||
};
|
};
|
||||||
|
|
||||||
void handle(std::string message, int z, unsigned x, unsigned y, std::vector<std::set<type_and_string> > &file_keys, char ***layernames, int *nlayers, sqlite3 *outdb, std::vector<std::string> &header, std::map<std::string, std::vector<std::string> > &mapping, std::set<std::string> &exclude, int ifmatched) {
|
void handle(std::string message, int z, unsigned x, unsigned y, std::vector<std::set<type_and_string> > &file_keys, std::vector<std::string> &layernames, int *nlayers, sqlite3 *outdb, std::vector<std::string> &header, std::map<std::string, std::vector<std::string> > &mapping, std::set<std::string> &exclude, int ifmatched) {
|
||||||
mvt_tile tile;
|
mvt_tile tile;
|
||||||
mvt_tile outtile;
|
mvt_tile outtile;
|
||||||
int features_added = 0;
|
int features_added = 0;
|
||||||
@ -47,24 +47,13 @@ void handle(std::string message, int z, unsigned x, unsigned y, std::vector<std:
|
|||||||
|
|
||||||
int ll;
|
int ll;
|
||||||
for (ll = 0; ll < *nlayers; ll++) {
|
for (ll = 0; ll < *nlayers; ll++) {
|
||||||
if (strcmp((*layernames)[ll], ln) == 0) {
|
if (strcmp(layernames[ll].c_str(), ln) == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ll == *nlayers) {
|
if (ll == *nlayers) {
|
||||||
file_keys.push_back(std::set<type_and_string>());
|
file_keys.push_back(std::set<type_and_string>());
|
||||||
*layernames = (char **) realloc(*layernames, (ll + 1) * sizeof(char *));
|
layernames.push_back(std::string(ln));
|
||||||
|
|
||||||
if (*layernames == NULL) {
|
|
||||||
perror("realloc layernames");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
(*layernames)[ll] = strdup(ln);
|
|
||||||
if ((*layernames)[ll] == NULL) {
|
|
||||||
perror("Out of memory");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
*nlayers = ll + 1;
|
*nlayers = ll + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,7 +204,7 @@ double max(double a, double b) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void decode(char *fname, char *map, std::vector<std::set<type_and_string> > &file_keys, char ***layernames, int *nlayers, sqlite3 *outdb, struct stats *st, std::vector<std::string> &header, std::map<std::string, std::vector<std::string> > &mapping, std::set<std::string> &exclude, int ifmatched, char **attribution) {
|
void decode(char *fname, char *map, std::vector<std::set<type_and_string> > &file_keys, std::vector<std::string> &layernames, int *nlayers, sqlite3 *outdb, struct stats *st, std::vector<std::string> &header, std::map<std::string, std::vector<std::string> > &mapping, std::set<std::string> &exclude, int ifmatched, char **attribution) {
|
||||||
sqlite3 *db;
|
sqlite3 *db;
|
||||||
|
|
||||||
if (sqlite3_open(fname, &db) != SQLITE_OK) {
|
if (sqlite3_open(fname, &db) != SQLITE_OK) {
|
||||||
@ -434,12 +423,12 @@ int main(int argc, char **argv) {
|
|||||||
st.maxzoom = st.maxlat = st.maxlon = INT_MIN;
|
st.maxzoom = st.maxlat = st.maxlon = INT_MIN;
|
||||||
|
|
||||||
std::vector<std::set<type_and_string> > file_keys;
|
std::vector<std::set<type_and_string> > file_keys;
|
||||||
char **layernames = NULL;
|
std::vector<std::string> layernames;
|
||||||
int nlayers = 0;
|
int nlayers = 0;
|
||||||
char *attribution = NULL;
|
char *attribution = NULL;
|
||||||
|
|
||||||
for (i = optind; i < argc; i++) {
|
for (i = optind; i < argc; i++) {
|
||||||
decode(argv[i], csv, file_keys, &layernames, &nlayers, outdb, &st, header, mapping, exclude, ifmatched, &attribution);
|
decode(argv[i], csv, file_keys, layernames, &nlayers, outdb, &st, header, mapping, exclude, ifmatched, &attribution);
|
||||||
}
|
}
|
||||||
|
|
||||||
mbtiles_write_metadata(outdb, outfile, layernames, st.minzoom, st.maxzoom, st.minlat, st.minlon, st.maxlat, st.maxlon, st.midlat, st.midlon, file_keys, nlayers, 0, attribution);
|
mbtiles_write_metadata(outdb, outfile, layernames, st.minzoom, st.maxzoom, st.minlat, st.minlon, st.maxlat, st.maxlon, st.midlat, st.midlon, file_keys, nlayers, 0, attribution);
|
||||||
|
17
tile.cpp
17
tile.cpp
@ -506,7 +506,7 @@ int manage_gap(unsigned long long index, unsigned long long *previndex, double s
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
long long write_tile(FILE *geoms, long long *geompos_in, char *metabase, char *stringpool, int z, unsigned tx, unsigned ty, int detail, int min_detail, int basezoom, char **layernames, sqlite3 *outdb, double droprate, int buffer, const char *fname, FILE **geomfile, int minzoom, int maxzoom, double todo, volatile long long *along, double gamma, int nlayers, int *prevent, int *additional, int child_shards, long long *meta_off, long long *pool_off, unsigned *initial_x, unsigned *initial_y, volatile int *running) {
|
long long write_tile(FILE *geoms, long long *geompos_in, char *metabase, char *stringpool, int z, unsigned tx, unsigned ty, int detail, int min_detail, int basezoom, std::vector<std::string> *layernames, sqlite3 *outdb, double droprate, int buffer, const char *fname, FILE **geomfile, int minzoom, int maxzoom, double todo, volatile long long *along, double gamma, int nlayers, int *prevent, int *additional, int child_shards, long long *meta_off, long long *pool_off, unsigned *initial_x, unsigned *initial_y, volatile int *running) {
|
||||||
int line_detail;
|
int line_detail;
|
||||||
double fraction = 1;
|
double fraction = 1;
|
||||||
|
|
||||||
@ -901,7 +901,7 @@ long long write_tile(FILE *geoms, long long *geompos_in, char *metabase, char *s
|
|||||||
for (size_t j = 0; j < features.size(); j++) {
|
for (size_t j = 0; j < features.size(); j++) {
|
||||||
mvt_layer layer;
|
mvt_layer layer;
|
||||||
|
|
||||||
layer.name = layernames[j];
|
layer.name = (*layernames)[j];
|
||||||
layer.version = 2;
|
layer.version = 2;
|
||||||
layer.extent = 1 << line_detail;
|
layer.extent = 1 << line_detail;
|
||||||
|
|
||||||
@ -994,7 +994,7 @@ struct write_tile_args {
|
|||||||
char *stringpool;
|
char *stringpool;
|
||||||
int min_detail;
|
int min_detail;
|
||||||
int basezoom;
|
int basezoom;
|
||||||
char **layernames;
|
std::vector<std::string> *layernames;
|
||||||
sqlite3 *outdb;
|
sqlite3 *outdb;
|
||||||
double droprate;
|
double droprate;
|
||||||
int buffer;
|
int buffer;
|
||||||
@ -1021,6 +1021,7 @@ struct write_tile_args {
|
|||||||
unsigned *initial_x;
|
unsigned *initial_x;
|
||||||
unsigned *initial_y;
|
unsigned *initial_y;
|
||||||
volatile int *running;
|
volatile int *running;
|
||||||
|
int err;
|
||||||
};
|
};
|
||||||
|
|
||||||
void *run_thread(void *vargs) {
|
void *run_thread(void *vargs) {
|
||||||
@ -1064,11 +1065,7 @@ void *run_thread(void *vargs) {
|
|||||||
long long len = write_tile(geom, &geompos, arg->metabase, arg->stringpool, z, x, y, z == arg->maxzoom ? arg->full_detail : arg->low_detail, arg->min_detail, arg->basezoom, arg->layernames, arg->outdb, arg->droprate, arg->buffer, arg->fname, arg->geomfile, arg->minzoom, arg->maxzoom, arg->todo, arg->along, arg->gamma, arg->nlayers, arg->prevent, arg->additional, arg->child_shards, arg->meta_off, arg->pool_off, arg->initial_x, arg->initial_y, arg->running);
|
long long len = write_tile(geom, &geompos, arg->metabase, arg->stringpool, z, x, y, z == arg->maxzoom ? arg->full_detail : arg->low_detail, arg->min_detail, arg->basezoom, arg->layernames, arg->outdb, arg->droprate, arg->buffer, arg->fname, arg->geomfile, arg->minzoom, arg->maxzoom, arg->todo, arg->along, arg->gamma, arg->nlayers, arg->prevent, arg->additional, arg->child_shards, arg->meta_off, arg->pool_off, arg->initial_x, arg->initial_y, arg->running);
|
||||||
|
|
||||||
if (len < 0) {
|
if (len < 0) {
|
||||||
int *err = (int *) malloc(sizeof(int));
|
int *err = &arg->err;
|
||||||
if (err == NULL) {
|
|
||||||
perror("Out of memory");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
*err = z - 1;
|
*err = z - 1;
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@ -1116,7 +1113,7 @@ void *run_thread(void *vargs) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int traverse_zooms(int *geomfd, off_t *geom_size, char *metabase, char *stringpool, unsigned *midx, unsigned *midy, char **layernames, int maxzoom, int minzoom, int basezoom, sqlite3 *outdb, double droprate, int buffer, const char *fname, const char *tmpdir, double gamma, int nlayers, int *prevent, int *additional, int full_detail, int low_detail, int min_detail, long long *meta_off, long long *pool_off, unsigned *initial_x, unsigned *initial_y) {
|
int traverse_zooms(int *geomfd, off_t *geom_size, char *metabase, char *stringpool, unsigned *midx, unsigned *midy, std::vector<std::string> &layernames, int maxzoom, int minzoom, int basezoom, sqlite3 *outdb, double droprate, int buffer, const char *fname, const char *tmpdir, double gamma, int nlayers, int *prevent, int *additional, int full_detail, int low_detail, int min_detail, long long *meta_off, long long *pool_off, unsigned *initial_x, unsigned *initial_y) {
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i <= maxzoom; i++) {
|
for (i = 0; i <= maxzoom; i++) {
|
||||||
long long most = 0;
|
long long most = 0;
|
||||||
@ -1226,7 +1223,7 @@ int traverse_zooms(int *geomfd, off_t *geom_size, char *metabase, char *stringpo
|
|||||||
args[thread].stringpool = stringpool;
|
args[thread].stringpool = stringpool;
|
||||||
args[thread].min_detail = min_detail;
|
args[thread].min_detail = min_detail;
|
||||||
args[thread].basezoom = basezoom;
|
args[thread].basezoom = basezoom;
|
||||||
args[thread].layernames = layernames;
|
args[thread].layernames = &layernames;
|
||||||
args[thread].outdb = outdb; // locked with db_lock
|
args[thread].outdb = outdb; // locked with db_lock
|
||||||
args[thread].droprate = droprate;
|
args[thread].droprate = droprate;
|
||||||
args[thread].buffer = buffer;
|
args[thread].buffer = buffer;
|
||||||
|
2
tile.hpp
2
tile.hpp
@ -1,5 +1,5 @@
|
|||||||
long long write_tile(char **geom, char *metabase, char *stringpool, unsigned *file_bbox, int z, unsigned x, unsigned y, int detail, int min_detail, int basezoom, char **layernames, sqlite3 *outdb, double droprate, int buffer, const char *fname, FILE **geomfile, int file_minzoom, int file_maxzoom, double todo, char *geomstart, long long along, double gamma, int nlayers, int *prevent, int *additional);
|
long long write_tile(char **geom, char *metabase, char *stringpool, unsigned *file_bbox, int z, unsigned x, unsigned y, int detail, int min_detail, int basezoom, char **layernames, sqlite3 *outdb, double droprate, int buffer, const char *fname, FILE **geomfile, int file_minzoom, int file_maxzoom, double todo, char *geomstart, long long along, double gamma, int nlayers, int *prevent, int *additional);
|
||||||
|
|
||||||
int traverse_zooms(int *geomfd, off_t *geom_size, char *metabase, char *stringpool, unsigned *midx, unsigned *midy, char **layernames, int maxzoom, int minzoom, int basezoom, sqlite3 *outdb, double droprate, int buffer, const char *fname, const char *tmpdir, double gamma, int nlayers, int *prevent, int *additional, int full_detail, int low_detail, int min_detail, long long *meta_off, long long *pool_off, unsigned *initial_x, unsigned *initial_y);
|
int traverse_zooms(int *geomfd, off_t *geom_size, char *metabase, char *stringpool, unsigned *midx, unsigned *midy, std::vector<std::string> &layernames, int maxzoom, int minzoom, int basezoom, sqlite3 *outdb, double droprate, int buffer, const char *fname, const char *tmpdir, double gamma, int nlayers, int *prevent, int *additional, int full_detail, int low_detail, int min_detail, long long *meta_off, long long *pool_off, unsigned *initial_x, unsigned *initial_y);
|
||||||
|
|
||||||
int manage_gap(unsigned long long index, unsigned long long *previndex, double scale, double gamma, double *gap);
|
int manage_gap(unsigned long long index, unsigned long long *previndex, double scale, double gamma, double *gap);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user