diff --git a/geojson.c b/geojson.c index d9ff6a5..1863c5a 100644 --- a/geojson.c +++ b/geojson.c @@ -25,6 +25,9 @@ int low_detail = 10; int full_detail = -1; int min_detail = 7; +unsigned initial_x = 0, initial_y = 0; +int initialized = 0; + #define GEOM_POINT 0 /* array of positions */ #define GEOM_MULTIPOINT 1 /* array of arrays of positions */ #define GEOM_LINESTRING 2 /* array of arrays of positions */ @@ -100,7 +103,7 @@ void serialize_string(FILE *out, const char *s, long long *fpos, const char *fna *fpos += len + 1; } -void parse_geometry(int t, json_object *j, unsigned *bbox, long long *fpos, FILE *out, int op, const char *fname, json_pull *source, long long *wx, long long *wy) { +void parse_geometry(int t, json_object *j, unsigned *bbox, long long *fpos, FILE *out, int op, const char *fname, json_pull *source, long long *wx, long long *wy, int *initialized) { if (j == NULL || j->type != JSON_ARRAY) { fprintf(stderr, "%s:%d: expected array for type %d\n", fname, source->line, t); return; @@ -119,7 +122,7 @@ void parse_geometry(int t, json_object *j, unsigned *bbox, long long *fpos, FILE } } - parse_geometry(within, j->array[i], bbox, fpos, out, op, fname, source, wx, wy); + parse_geometry(within, j->array[i], bbox, fpos, out, op, fname, source, wx, wy, initialized); } } else { if (j->length >= 2 && j->array[0]->type == JSON_NUMBER && j->array[1]->type == JSON_NUMBER) { @@ -152,6 +155,14 @@ void parse_geometry(int t, json_object *j, unsigned *bbox, long long *fpos, FILE } } + if (!*initialized) { + initial_x = x; + initial_y = y; + *wx = x; + *wy = y; + *initialized = 1; + } + serialize_byte(out, op, fpos, fname); serialize_long_long(out, x - *wx, fpos, fname); serialize_long_long(out, y - *wy, fpos, fname); @@ -654,8 +665,8 @@ int read_json(int argc, char **argv, char *fname, const char *layername, int max serialize_byte(geomfile, mb_geometry[t], &geompos, fname); serialize_byte(geomfile, n, &geompos, fname); serialize_long_long(geomfile, metastart, &geompos, fname); - long long wx = 0, wy = 0; - parse_geometry(t, coordinates, bbox, &geompos, geomfile, VT_MOVETO, fname, jp, &wx, &wy); + long long wx = initial_x, wy = initial_y; + parse_geometry(t, coordinates, bbox, &geompos, geomfile, VT_MOVETO, fname, jp, &wx, &wy, &initialized); serialize_byte(geomfile, VT_END, &geompos, fname); /* diff --git a/geometry.cc b/geometry.cc index 6a79dcf..14d489d 100644 --- a/geometry.cc +++ b/geometry.cc @@ -25,7 +25,7 @@ drawvec decode_geometry(char **meta, int z, unsigned tx, unsigned ty, int detail bbox[2] = LONG_LONG_MIN; bbox[3] = LONG_LONG_MIN; - long long wx = 0, wy = 0; + long long wx = initial_x, wy = initial_y; while (1) { draw d; diff --git a/tile.cc b/tile.cc index 7c05564..12791c3 100644 --- a/tile.cc +++ b/tile.cc @@ -506,7 +506,7 @@ long long write_tile(char **geoms, char *metabase, char *stringpool, unsigned *f serialize_byte(geomfile[j], t, &geompos[j], fname); serialize_byte(geomfile[j], layer, &geompos[j], fname); serialize_long_long(geomfile[j], metastart, &geompos[j], fname); - long long wx = 0, wy = 0; + long long wx = initial_x, wy = initial_y; for (unsigned u = 0; u < geom.size(); u++) { serialize_byte(geomfile[j], geom[u].op, &geompos[j], fname); diff --git a/tile.h b/tile.h index 5494c20..bccc145 100644 --- a/tile.h +++ b/tile.h @@ -26,3 +26,5 @@ void deserialize_byte(char **f, signed char *n); struct pool_val *deserialize_string(char **f, struct pool *p, int type); 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, struct pool **file_keys, char **layernames, sqlite3 *outdb, double droprate, int buffer, const char *fname, FILE *geomfile[4], int file_minzoom, int file_maxzoom, double todo, char *geomstart, long long along, double gamma, int nlayers, char *prevent); + +extern unsigned initial_x, initial_y;