mirror of
https://github.com/felt/tippecanoe.git
synced 2026-10-02 08:25:40 +02:00
Use the same GeoJSON parsing loop in tippecanoe-json-tool
This commit is contained in:
+3
-6
@@ -99,7 +99,7 @@ void parse_json(json_feature_action *jfa, json_pull *jp) {
|
|||||||
}
|
}
|
||||||
found_geometries++;
|
found_geometries++;
|
||||||
|
|
||||||
jfa->add_feature(j, NULL, NULL, NULL, j);
|
jfa->add_feature(j, false, NULL, NULL, NULL, NULL);
|
||||||
json_free(j);
|
json_free(j);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -140,12 +140,9 @@ void parse_json(json_feature_action *jfa, json_pull *jp) {
|
|||||||
|
|
||||||
json_object *geometries = json_hash_get(geometry, "geometries");
|
json_object *geometries = json_hash_get(geometry, "geometries");
|
||||||
if (geometries != NULL && geometries->type == JSON_ARRAY) {
|
if (geometries != NULL && geometries->type == JSON_ARRAY) {
|
||||||
size_t g;
|
jfa->add_feature(geometries, true, properties, id, tippecanoe, j);
|
||||||
for (g = 0; g < geometries->length; g++) {
|
|
||||||
jfa->add_feature(geometries->array[g], properties, id, tippecanoe, j);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
jfa->add_feature(geometry, properties, id, tippecanoe, j);
|
jfa->add_feature(geometry, false, properties, id, tippecanoe, j);
|
||||||
}
|
}
|
||||||
|
|
||||||
json_free(j);
|
json_free(j);
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@
|
|||||||
struct json_feature_action {
|
struct json_feature_action {
|
||||||
std::string fname;
|
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;
|
virtual void check_crs(json_object *j) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+10
-2
@@ -233,8 +233,16 @@ struct json_serialize_action : json_feature_action {
|
|||||||
int layer;
|
int layer;
|
||||||
std::string layername;
|
std::string layername;
|
||||||
|
|
||||||
int add_feature(json_object *geometry, json_object *properties, json_object *id, json_object *tippecanoe, json_object *feature) {
|
int add_feature(json_object *geometry, bool geometrycollection, json_object *properties, json_object *id, json_object *tippecanoe, json_object *feature) {
|
||||||
return serialize_geojson_feature(sst, geometry, properties, id, layer, tippecanoe, feature, layername);
|
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) {
|
void check_crs(json_object *j) {
|
||||||
|
|||||||
+26
-61
@@ -10,6 +10,7 @@
|
|||||||
#include "jsonpull/jsonpull.h"
|
#include "jsonpull/jsonpull.h"
|
||||||
#include "csv.hpp"
|
#include "csv.hpp"
|
||||||
#include "text.hpp"
|
#include "text.hpp"
|
||||||
|
#include "geojson-loop.hpp"
|
||||||
|
|
||||||
int fail = EXIT_SUCCESS;
|
int fail = EXIT_SUCCESS;
|
||||||
bool wrap = false;
|
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) {
|
void process(FILE *fp, const char *fname) {
|
||||||
json_pull *jp = json_begin_file(fp);
|
json_pull *jp = json_begin_file(fp);
|
||||||
|
|
||||||
while (1) {
|
json_join_action jja;
|
||||||
json_object *j = json_read(jp);
|
parse_json(&jja, 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_end(jp);
|
json_end(jp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user