From 4256473283ce793c543987a07434a14b63f55c55 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Mon, 5 Dec 2016 15:18:27 -0800 Subject: [PATCH] More reorganization to reuse JSON parsing --- Makefile | 2 +- geojson.cpp | 37 +------------------------------------ plugin.cpp | 17 +++-------------- read_json.cpp | 39 +++++++++++++++++++++++++++++++++++++++ read_json.hpp | 13 +++++++++++++ 5 files changed, 57 insertions(+), 51 deletions(-) create mode 100644 read_json.cpp create mode 100644 read_json.hpp diff --git a/Makefile b/Makefile index 03610530..db9f04a0 100644 --- a/Makefile +++ b/Makefile @@ -43,7 +43,7 @@ C = $(wildcard *.c) $(wildcard *.cpp) INCLUDES = -I/usr/local/include -I. LIBS = -L/usr/local/lib -tippecanoe: geojson.o jsonpull/jsonpull.o tile.o pool.o mbtiles.o geometry.o projection.o memfile.o clipper/clipper.o mvt.o serial.o main.o text.o plugin.o write_json.o +tippecanoe: geojson.o jsonpull/jsonpull.o tile.o pool.o mbtiles.o geometry.o projection.o memfile.o clipper/clipper.o mvt.o serial.o main.o text.o plugin.o write_json.o read_json.o $(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread tippecanoe-enumerate: enumerate.o diff --git a/geojson.cpp b/geojson.cpp index c04e2b85..4000713c 100644 --- a/geojson.cpp +++ b/geojson.cpp @@ -38,42 +38,7 @@ extern "C" { #include "options.hpp" #include "serial.hpp" #include "text.hpp" - -#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 */ -#define GEOM_MULTILINESTRING 3 /* array of arrays of arrays of positions */ -#define GEOM_POLYGON 4 /* array of arrays of arrays of positions */ -#define GEOM_MULTIPOLYGON 5 /* array of arrays of arrays of arrays of positions */ -#define GEOM_TYPES 6 - -static const char *geometry_names[GEOM_TYPES] = { - "Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon", -}; - -static int geometry_within[GEOM_TYPES] = { - -1, /* point */ - GEOM_POINT, /* multipoint */ - GEOM_POINT, /* linestring */ - GEOM_LINESTRING, /* multilinestring */ - GEOM_LINESTRING, /* polygon */ - GEOM_POLYGON, /* multipolygon */ -}; - -static int mb_geometry[GEOM_TYPES] = { - VT_POINT, VT_POINT, VT_LINE, VT_LINE, VT_POLYGON, VT_POLYGON, -}; - -void json_context(json_object *j) { - char *s = json_stringify(j); - - if (strlen(s) >= 500) { - sprintf(s + 497, "..."); - } - - fprintf(stderr, "In JSON object %s\n", s); - free(s); // stringify -} +#include "read_json.hpp" long long parse_geometry(int t, json_object *j, long long *bbox, drawvec &out, int op, const char *fname, int line, int *initialized, unsigned *initial_x, unsigned *initial_y, json_object *feature) { long long g = 0; diff --git a/plugin.cpp b/plugin.cpp index c888b9f7..e0370e5b 100644 --- a/plugin.cpp +++ b/plugin.cpp @@ -7,7 +7,6 @@ #include #include "mvt.hpp" #include "plugin.hpp" -#include "write_json.hpp" #include "projection.hpp" #include "geometry.hpp" @@ -15,6 +14,9 @@ extern "C" { #include "jsonpull/jsonpull.h" } +#include "write_json.hpp" +#include "read_json.hpp" + struct writer_arg { int *pipe_orig; mvt_layer *layer; @@ -44,17 +46,6 @@ void *run_writer(void *a) { return NULL; } -static void json_context(json_object *j) { // XXX share with geojson.cpp - char *s = json_stringify(j); - - if (strlen(s) >= 500) { - sprintf(s + 497, "..."); - } - - fprintf(stderr, "In JSON object %s\n", s); - free(s); // stringify -} - mvt_layer parse_layer(int fd, unsigned z, unsigned x, unsigned y) { mvt_layer ret; @@ -124,7 +115,6 @@ mvt_layer parse_layer(int fd, unsigned z, unsigned x, unsigned y) { exit(EXIT_FAILURE); } -#if 0 int t; for (t = 0; t < GEOM_TYPES; t++) { if (strcmp(geometry_type->string, geometry_names[t]) == 0) { @@ -136,7 +126,6 @@ mvt_layer parse_layer(int fd, unsigned z, unsigned x, unsigned y) { json_context(j); exit(EXIT_FAILURE); } -#endif json_free(j); } diff --git a/read_json.cpp b/read_json.cpp new file mode 100644 index 00000000..1e69b406 --- /dev/null +++ b/read_json.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +extern "C" { +#include "jsonpull/jsonpull.h" +} + +#include "read_json.hpp" +#include "geometry.hpp" + +const char *geometry_names[GEOM_TYPES] = { + "Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon", +}; + +int geometry_within[GEOM_TYPES] = { + -1, /* point */ + GEOM_POINT, /* multipoint */ + GEOM_POINT, /* linestring */ + GEOM_LINESTRING, /* multilinestring */ + GEOM_LINESTRING, /* polygon */ + GEOM_POLYGON, /* multipolygon */ +}; + +int mb_geometry[GEOM_TYPES] = { + VT_POINT, VT_POINT, VT_LINE, VT_LINE, VT_POLYGON, VT_POLYGON, +}; + +void json_context(json_object *j) { + char *s = json_stringify(j); + + if (strlen(s) >= 500) { + sprintf(s + 497, "..."); + } + + fprintf(stderr, "In JSON object %s\n", s); + free(s); // stringify +} diff --git a/read_json.hpp b/read_json.hpp new file mode 100644 index 00000000..8da2c3b7 --- /dev/null +++ b/read_json.hpp @@ -0,0 +1,13 @@ +#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 */ +#define GEOM_MULTILINESTRING 3 /* array of arrays of arrays of positions */ +#define GEOM_POLYGON 4 /* array of arrays of arrays of positions */ +#define GEOM_MULTIPOLYGON 5 /* array of arrays of arrays of arrays of positions */ +#define GEOM_TYPES 6 + +extern const char *geometry_names[GEOM_TYPES]; +extern int geometry_within[GEOM_TYPES]; +extern int mb_geometry[GEOM_TYPES]; + +void json_context(json_object *j);