Use the same GeoJSON parsing loop in tippecanoe-json-tool

This commit is contained in:
Eric Fischer 2018-08-08 13:11:25 -07:00
parent 79363e397b
commit ba8966a4ae
4 changed files with 40 additions and 70 deletions

View File

@ -99,7 +99,7 @@ void parse_json(json_feature_action *jfa, json_pull *jp) {
}
found_geometries++;
jfa->add_feature(j, NULL, NULL, NULL, j);
jfa->add_feature(j, false, NULL, NULL, NULL, NULL);
json_free(j);
continue;
}
@ -140,12 +140,9 @@ void parse_json(json_feature_action *jfa, json_pull *jp) {
json_object *geometries = json_hash_get(geometry, "geometries");
if (geometries != NULL && geometries->type == JSON_ARRAY) {
size_t g;
for (g = 0; g < geometries->length; g++) {
jfa->add_feature(geometries->array[g], properties, id, tippecanoe, j);
}
jfa->add_feature(geometries, true, properties, id, tippecanoe, j);
} else {
jfa->add_feature(geometry, properties, id, tippecanoe, j);
jfa->add_feature(geometry, false, properties, id, tippecanoe, j);
}
json_free(j);

View File

@ -4,7 +4,7 @@
struct json_feature_action {
std::string fname;
virtual int add_feature(json_object *geometry, json_object *properties, json_object *id, json_object *tippecanoe, json_object *feature) = 0;
virtual int add_feature(json_object *geometry, bool geometrycollection, json_object *properties, json_object *id, json_object *tippecanoe, json_object *feature) = 0;
virtual void check_crs(json_object *j) = 0;
};

View File

@ -233,8 +233,16 @@ struct json_serialize_action : json_feature_action {
int layer;
std::string layername;
int add_feature(json_object *geometry, json_object *properties, json_object *id, json_object *tippecanoe, json_object *feature) {
return serialize_geojson_feature(sst, geometry, properties, id, layer, tippecanoe, feature, layername);
int add_feature(json_object *geometry, bool geometrycollection, json_object *properties, json_object *id, json_object *tippecanoe, json_object *feature) {
if (geometrycollection) {
int ret = 1;
for (size_t g = 0; g < geometry->length; g++) {
ret &= serialize_geojson_feature(sst, geometry->array[g], properties, id, layer, tippecanoe, feature, layername);
}
return ret;
} else {
return serialize_geojson_feature(sst, geometry, properties, id, layer, tippecanoe, feature, layername);
}
}
void check_crs(json_object *j) {

View File

@ -10,6 +10,7 @@
#include "jsonpull/jsonpull.h"
#include "csv.hpp"
#include "text.hpp"
#include "geojson-loop.hpp"
int fail = EXIT_SUCCESS;
bool wrap = false;
@ -362,70 +363,34 @@ void join_csv(json_object *j) {
}
}
struct json_join_action : json_feature_action {
int add_feature(json_object *geometry, bool geometrycollection, json_object *properties, json_object *id, json_object *tippecanoe, json_object *feature) {
if (feature != NULL) {
if (csvfile != NULL) {
join_csv(feature);
}
char *s = json_stringify(feature);
out(s, 1, json_hash_get(feature, "properties"));
free(s);
} else {
char *s = json_stringify(geometry);
out(s, 2, NULL);
free(s);
}
return 1;
}
void check_crs(json_object *j) {
}
};
void process(FILE *fp, const char *fname) {
json_pull *jp = json_begin_file(fp);
while (1) {
json_object *j = json_read(jp);
if (j == NULL) {
if (jp->error != NULL) {
fprintf(stderr, "%s:%d: %s\n", fname, jp->line, jp->error);
}
json_free(jp->root);
break;
}
json_object *type = json_hash_get(j, "type");
if (type == NULL || type->type != JSON_STRING) {
continue;
}
if (strcmp(type->string, "Feature") == 0) {
if (csvfile != NULL) {
join_csv(j);
}
char *s = json_stringify(j);
out(s, 1, json_hash_get(j, "properties"));
free(s);
json_free(j);
} else if (strcmp(type->string, "Point") == 0 ||
strcmp(type->string, "MultiPoint") == 0 ||
strcmp(type->string, "LineString") == 0 ||
strcmp(type->string, "MultiLineString") == 0 ||
strcmp(type->string, "MultiPolygon") == 0) {
int is_geometry = 1;
if (j->parent != NULL) {
if (j->parent->type == JSON_ARRAY && j->parent->parent != NULL) {
if (j->parent->parent->type == JSON_HASH) {
json_object *geometries = json_hash_get(j->parent->parent, "geometries");
if (geometries != NULL) {
// Parent of Parent must be a GeometryCollection
is_geometry = 0;
}
}
} else if (j->parent->type == JSON_HASH) {
json_object *geometry = json_hash_get(j->parent, "geometry");
if (geometry != NULL) {
// Parent must be a Feature
is_geometry = 0;
}
}
}
if (is_geometry) {
char *s = json_stringify(j);
out(s, 2, NULL);
free(s);
json_free(j);
}
} else if (strcmp(type->string, "FeatureCollection") == 0) {
json_free(j);
}
}
json_join_action jja;
parse_json(&jja, jp);
json_end(jp);
}