2018-05-04 10:37:27 -07:00
|
|
|
// for vasprintf() on Linux
|
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif
|
|
|
|
|
2016-12-05 14:12:39 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2018-03-16 16:25:21 -07:00
|
|
|
#include <string.h>
|
2018-08-23 15:05:10 -07:00
|
|
|
#include <math.h>
|
2016-12-05 14:12:39 -08:00
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2018-08-23 17:17:13 -07:00
|
|
|
#include <cmath>
|
2016-12-05 14:12:39 -08:00
|
|
|
#include "projection.hpp"
|
|
|
|
#include "geometry.hpp"
|
|
|
|
#include "mvt.hpp"
|
|
|
|
#include "write_json.hpp"
|
2017-08-21 14:00:40 -07:00
|
|
|
#include "text.hpp"
|
2017-08-29 14:44:34 -07:00
|
|
|
#include "milo/dtoa_milo.h"
|
2016-12-05 14:12:39 -08:00
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
void json_writer::json_adjust() {
|
|
|
|
if (state.size() == 0) {
|
|
|
|
state.push_back(JSON_WRITE_TOP);
|
|
|
|
} else if (state[state.size() - 1] == JSON_WRITE_TOP) {
|
2018-03-16 14:20:52 -07:00
|
|
|
addc('\n');
|
2018-03-16 13:43:21 -07:00
|
|
|
state[state.size() - 1] = JSON_WRITE_TOP;
|
|
|
|
} else if (state[state.size() - 1] == JSON_WRITE_HASH) {
|
|
|
|
if (!nospace) {
|
2018-03-16 14:20:52 -07:00
|
|
|
addc(' ');
|
2018-03-15 16:10:30 -07:00
|
|
|
}
|
2018-03-16 13:43:21 -07:00
|
|
|
nospace = false;
|
|
|
|
state[state.size() - 1] = JSON_WRITE_HASH_KEY;
|
|
|
|
} else if (state[state.size() - 1] == JSON_WRITE_HASH_KEY) {
|
2018-03-16 14:20:52 -07:00
|
|
|
adds(": ");
|
2018-03-16 13:43:21 -07:00
|
|
|
state[state.size() - 1] = JSON_WRITE_HASH_VALUE;
|
|
|
|
} else if (state[state.size() - 1] == JSON_WRITE_HASH_VALUE) {
|
|
|
|
if (wantnl) {
|
2018-03-16 14:20:52 -07:00
|
|
|
adds(",\n");
|
2018-03-16 13:43:21 -07:00
|
|
|
nospace = false;
|
2018-03-16 15:19:23 -07:00
|
|
|
} else if (nospace) {
|
|
|
|
addc(',');
|
|
|
|
nospace = false;
|
2018-03-15 16:10:30 -07:00
|
|
|
} else {
|
2018-03-16 14:20:52 -07:00
|
|
|
adds(", ");
|
2018-03-15 16:10:30 -07:00
|
|
|
}
|
2018-03-16 13:43:21 -07:00
|
|
|
wantnl = false;
|
|
|
|
state[state.size() - 1] = JSON_WRITE_HASH_KEY;
|
|
|
|
} else if (state[state.size() - 1] == JSON_WRITE_ARRAY) {
|
|
|
|
if (!nospace) {
|
2018-03-16 14:20:52 -07:00
|
|
|
addc(' ');
|
2018-03-15 16:10:30 -07:00
|
|
|
}
|
2018-03-16 13:43:21 -07:00
|
|
|
nospace = false;
|
|
|
|
state[state.size() - 1] = JSON_WRITE_ARRAY_ELEMENT;
|
|
|
|
} else if (state[state.size() - 1] == JSON_WRITE_ARRAY_ELEMENT) {
|
|
|
|
if (wantnl) {
|
2018-03-16 14:20:52 -07:00
|
|
|
adds(",\n");
|
2018-03-16 13:43:21 -07:00
|
|
|
nospace = false;
|
2018-03-16 15:19:23 -07:00
|
|
|
} else if (nospace) {
|
|
|
|
addc(',');
|
|
|
|
nospace = false;
|
2018-03-15 16:10:30 -07:00
|
|
|
} else {
|
2018-03-16 14:20:52 -07:00
|
|
|
adds(", ");
|
2018-03-15 16:10:30 -07:00
|
|
|
}
|
2018-03-16 13:43:21 -07:00
|
|
|
wantnl = false;
|
|
|
|
state[state.size() - 1] = JSON_WRITE_ARRAY_ELEMENT;
|
2018-03-15 14:42:53 -07:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Impossible JSON state\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
void json_writer::json_write_array() {
|
|
|
|
json_adjust();
|
2018-03-16 14:20:52 -07:00
|
|
|
addc('[');
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
state.push_back(JSON_WRITE_ARRAY);
|
2018-03-15 14:42:53 -07:00
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
void json_writer::json_end_array() {
|
|
|
|
if (state.size() == 0) {
|
2018-03-15 14:42:53 -07:00
|
|
|
fprintf(stderr, "End JSON array at top level\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
json_write_tok tok = state[state.size() - 1];
|
|
|
|
state.pop_back();
|
2018-03-15 14:42:53 -07:00
|
|
|
|
|
|
|
if (tok == JSON_WRITE_ARRAY || tok == JSON_WRITE_ARRAY_ELEMENT) {
|
2018-03-16 13:43:21 -07:00
|
|
|
if (!nospace) {
|
2018-03-16 14:20:52 -07:00
|
|
|
addc(' ');
|
2018-03-15 16:10:30 -07:00
|
|
|
}
|
2018-03-16 13:43:21 -07:00
|
|
|
nospace = false;
|
2018-03-16 14:20:52 -07:00
|
|
|
addc(']');
|
2018-03-15 14:42:53 -07:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "End JSON array with unexpected state\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
void json_writer::json_write_hash() {
|
|
|
|
json_adjust();
|
2018-03-16 14:20:52 -07:00
|
|
|
addc('{');
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
state.push_back(JSON_WRITE_HASH);
|
2018-03-15 14:42:53 -07:00
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
void json_writer::json_end_hash() {
|
|
|
|
if (state.size() == 0) {
|
2018-03-15 14:42:53 -07:00
|
|
|
fprintf(stderr, "End JSON hash at top level\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
json_write_tok tok = state[state.size() - 1];
|
|
|
|
state.pop_back();
|
2018-03-15 14:42:53 -07:00
|
|
|
|
|
|
|
if (tok == JSON_WRITE_HASH) {
|
2018-03-16 13:43:21 -07:00
|
|
|
if (!nospace) {
|
2018-03-16 14:20:52 -07:00
|
|
|
adds(" "); // Preserve accidental extra space from before
|
2018-03-15 16:10:30 -07:00
|
|
|
}
|
2018-03-16 13:43:21 -07:00
|
|
|
nospace = false;
|
2018-03-16 14:20:52 -07:00
|
|
|
addc('}');
|
2018-03-15 14:42:53 -07:00
|
|
|
} else if (tok == JSON_WRITE_HASH_VALUE) {
|
2018-03-16 13:43:21 -07:00
|
|
|
if (!nospace) {
|
2018-03-16 14:20:52 -07:00
|
|
|
addc(' ');
|
2018-03-15 16:10:30 -07:00
|
|
|
}
|
2018-03-16 13:43:21 -07:00
|
|
|
nospace = false;
|
2018-03-16 14:20:52 -07:00
|
|
|
addc('}');
|
2018-03-15 14:42:53 -07:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "End JSON hash with unexpected state\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 15:19:23 -07:00
|
|
|
void json_writer::json_write_string(std::string const &str) {
|
2018-03-16 13:43:21 -07:00
|
|
|
json_adjust();
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-03-16 14:20:52 -07:00
|
|
|
addc('"');
|
2018-03-16 15:19:23 -07:00
|
|
|
for (size_t i = 0; i < str.size(); i++) {
|
|
|
|
if (str[i] == '\\' || str[i] == '"') {
|
|
|
|
aprintf("\\%c", str[i]);
|
|
|
|
} else if ((unsigned char) str[i] < ' ') {
|
|
|
|
aprintf("\\u%04x", str[i]);
|
2018-03-15 14:42:53 -07:00
|
|
|
} else {
|
2018-03-16 15:19:23 -07:00
|
|
|
addc(str[i]);
|
2018-03-15 14:42:53 -07:00
|
|
|
}
|
|
|
|
}
|
2018-03-16 14:20:52 -07:00
|
|
|
addc('"');
|
2018-03-15 14:42:53 -07:00
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
void json_writer::json_write_number(double d) {
|
|
|
|
json_adjust();
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-03-16 14:20:52 -07:00
|
|
|
adds(milo::dtoa_milo(d).c_str());
|
2018-03-15 14:42:53 -07:00
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
// Just to avoid json_writer:: changing expected output format
|
|
|
|
void json_writer::json_write_float(double d) {
|
|
|
|
json_adjust();
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-03-16 14:20:52 -07:00
|
|
|
aprintf("%f", d);
|
2018-03-15 14:42:53 -07:00
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
void json_writer::json_write_unsigned(unsigned long long v) {
|
|
|
|
json_adjust();
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-03-16 14:20:52 -07:00
|
|
|
aprintf("%llu", v);
|
2018-03-15 14:42:53 -07:00
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
void json_writer::json_write_signed(long long v) {
|
|
|
|
json_adjust();
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-03-16 14:20:52 -07:00
|
|
|
aprintf("%lld", v);
|
2018-03-15 14:42:53 -07:00
|
|
|
}
|
|
|
|
|
2018-03-16 15:19:23 -07:00
|
|
|
void json_writer::json_write_stringified(std::string const &str) {
|
2018-03-16 13:43:21 -07:00
|
|
|
json_adjust();
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-03-16 15:19:23 -07:00
|
|
|
adds(str);
|
2018-03-15 14:42:53 -07:00
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
void json_writer::json_write_bool(bool b) {
|
|
|
|
json_adjust();
|
2018-03-15 14:42:53 -07:00
|
|
|
|
|
|
|
if (b) {
|
2018-03-16 14:20:52 -07:00
|
|
|
adds("true");
|
2018-03-15 14:42:53 -07:00
|
|
|
} else {
|
2018-03-16 14:20:52 -07:00
|
|
|
adds("false");
|
2018-03-15 14:42:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
void json_writer::json_write_null() {
|
|
|
|
json_adjust();
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-03-16 14:20:52 -07:00
|
|
|
adds("null");
|
2018-03-15 14:42:53 -07:00
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
void json_writer::json_write_newline() {
|
2018-03-16 14:20:52 -07:00
|
|
|
addc('\n');
|
2018-03-16 13:43:21 -07:00
|
|
|
nospace = true;
|
2018-03-15 16:10:30 -07:00
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
void json_writer::json_comma_newline() {
|
|
|
|
wantnl = true;
|
2018-03-15 16:10:30 -07:00
|
|
|
}
|
|
|
|
|
2018-03-16 14:20:52 -07:00
|
|
|
void json_writer::aprintf(const char *format, ...) {
|
2018-03-16 15:35:26 -07:00
|
|
|
va_list ap;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
if (vasprintf(&tmp, format, ap) < 0) {
|
|
|
|
fprintf(stderr, "memory allocation failure\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
adds(std::string(tmp, strlen(tmp)));
|
|
|
|
free(tmp);
|
2018-03-16 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void json_writer::addc(char c) {
|
|
|
|
if (f != NULL) {
|
|
|
|
putc(c, f);
|
|
|
|
} else if (s != NULL) {
|
|
|
|
s->push_back(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void json_writer::adds(std::string const &str) {
|
|
|
|
if (f != NULL) {
|
|
|
|
fputs(str.c_str(), f);
|
|
|
|
} else if (s != NULL) {
|
|
|
|
s->append(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 14:12:39 -08:00
|
|
|
struct lonlat {
|
|
|
|
int op;
|
|
|
|
double lon;
|
|
|
|
double lat;
|
2016-12-20 15:07:10 -08:00
|
|
|
long long x;
|
|
|
|
long long y;
|
2018-08-24 15:56:12 -07:00
|
|
|
std::vector<double> elevations;
|
2018-08-28 15:32:43 -07:00
|
|
|
std::string attribute;
|
2016-12-05 14:12:39 -08:00
|
|
|
|
2018-08-28 15:32:43 -07:00
|
|
|
lonlat(int nop, double nlon, double nlat, long long nx, long long ny, std::vector<double> nelevations, std::string nattribute)
|
2017-11-07 15:57:56 -08:00
|
|
|
: op(nop),
|
|
|
|
lon(nlon),
|
|
|
|
lat(nlat),
|
|
|
|
x(nx),
|
2018-09-04 16:07:12 -07:00
|
|
|
y(ny),
|
2018-08-28 15:32:43 -07:00
|
|
|
elevations(nelevations),
|
|
|
|
attribute(nattribute) {
|
2016-12-05 14:12:39 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-20 14:27:22 -07:00
|
|
|
void write_kv(json_writer &state, const char *key, mvt_value const &val) {
|
|
|
|
if (val.type == mvt_string) {
|
|
|
|
state.json_write_string(key);
|
|
|
|
state.json_write_string(val.string_value);
|
|
|
|
} else if (val.type == mvt_int) {
|
|
|
|
state.json_write_string(key);
|
|
|
|
state.json_write_signed(val.numeric_value.int_value);
|
|
|
|
} else if (val.type == mvt_double) {
|
|
|
|
state.json_write_string(key);
|
|
|
|
state.json_write_number(val.numeric_value.double_value);
|
|
|
|
} else if (val.type == mvt_float) {
|
|
|
|
state.json_write_string(key);
|
|
|
|
state.json_write_number(val.numeric_value.float_value);
|
|
|
|
} else if (val.type == mvt_sint) {
|
|
|
|
state.json_write_string(key);
|
|
|
|
state.json_write_signed(val.numeric_value.sint_value);
|
|
|
|
} else if (val.type == mvt_uint) {
|
|
|
|
state.json_write_string(key);
|
|
|
|
state.json_write_unsigned(val.numeric_value.uint_value);
|
|
|
|
} else if (val.type == mvt_bool) {
|
|
|
|
state.json_write_string(key);
|
|
|
|
state.json_write_bool(val.numeric_value.bool_value);
|
|
|
|
} else if (val.type == mvt_null) {
|
|
|
|
state.json_write_string(key);
|
|
|
|
state.json_write_null();
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Internal error: property with unknown type\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 13:26:08 -07:00
|
|
|
static std::string quote(std::string const &s) {
|
|
|
|
std::string buf;
|
2017-08-21 14:00:40 -07:00
|
|
|
buf.push_back('"');
|
2018-10-17 13:26:08 -07:00
|
|
|
|
2017-08-21 14:00:40 -07:00
|
|
|
for (size_t i = 0; i < s.size(); i++) {
|
|
|
|
unsigned char ch = s[i];
|
|
|
|
|
|
|
|
if (ch == '\\' || ch == '\"') {
|
|
|
|
buf.push_back('\\');
|
|
|
|
buf.push_back(ch);
|
|
|
|
} else if (ch < ' ') {
|
|
|
|
char tmp[7];
|
|
|
|
sprintf(tmp, "\\u%04x", ch);
|
|
|
|
buf.append(std::string(tmp));
|
|
|
|
} else {
|
|
|
|
buf.push_back(ch);
|
|
|
|
}
|
|
|
|
}
|
2018-10-17 13:26:08 -07:00
|
|
|
|
2017-08-21 14:00:40 -07:00
|
|
|
buf.push_back('"');
|
2018-10-17 13:26:08 -07:00
|
|
|
return buf;
|
2017-08-21 14:00:40 -07:00
|
|
|
}
|
|
|
|
|
2018-10-17 13:44:12 -07:00
|
|
|
void stringify_val(std::string &out, mvt_value const &val) {
|
2017-08-21 13:06:07 -07:00
|
|
|
if (val.type == mvt_string) {
|
2018-10-17 13:26:08 -07:00
|
|
|
out.append(quote(val.string_value));
|
2018-10-17 13:44:12 -07:00
|
|
|
} else {
|
2018-10-11 16:01:44 -07:00
|
|
|
out.append(val.toString());
|
2017-08-21 13:06:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 13:44:12 -07:00
|
|
|
void print_val(mvt_value const &val, json_writer &state) {
|
|
|
|
std::string s;
|
|
|
|
stringify_val(s, val);
|
|
|
|
state.json_write_stringified(s);
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:56:12 -07:00
|
|
|
void write_coordinates(json_writer &state, lonlat const &p) {
|
|
|
|
state.json_write_array();
|
|
|
|
|
|
|
|
state.json_write_float(p.lon);
|
|
|
|
state.json_write_float(p.lat);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < p.elevations.size(); i++) {
|
2018-10-12 13:40:04 -07:00
|
|
|
state.json_write_number(p.elevations[i]);
|
2018-08-24 15:56:12 -07:00
|
|
|
}
|
|
|
|
|
2018-08-29 12:51:27 -07:00
|
|
|
state.json_end_array();
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_attributes(json_writer &state, lonlat const &p) {
|
2018-08-28 15:32:43 -07:00
|
|
|
if (p.attribute.size() != 0) {
|
|
|
|
state.json_write_stringified(p.attribute);
|
2018-08-29 12:51:27 -07:00
|
|
|
} else {
|
|
|
|
state.json_write_hash();
|
|
|
|
state.json_end_hash();
|
2018-08-28 15:32:43 -07:00
|
|
|
}
|
2018-08-24 15:56:12 -07:00
|
|
|
}
|
|
|
|
|
2018-08-29 12:51:27 -07:00
|
|
|
struct coordinate_writer {
|
|
|
|
std::string tag;
|
|
|
|
void (*function)(json_writer &state, lonlat const &p);
|
|
|
|
};
|
|
|
|
|
2018-10-17 13:26:08 -07:00
|
|
|
std::vector<std::string> decode_node_attributes(mvt_feature const &feature, const mvt_layer &layer) {
|
|
|
|
std::vector<mvt_geometry> const &geom = feature.geometry;
|
|
|
|
std::vector<unsigned long> const &attr = feature.node_attributes;
|
|
|
|
std::vector<std::string> out;
|
|
|
|
|
|
|
|
for (size_t t = 0; t + 1 < attr.size(); t++) {
|
|
|
|
if (attr[t] >= layer.keys.size()) {
|
|
|
|
fprintf(stderr, "Out of bounds attribute reference %lu into %zu\n", attr[t], layer.keys.size());
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string key = layer.keys[attr[t]];
|
|
|
|
|
|
|
|
t++;
|
|
|
|
mvt_value const &val = layer.decode_property(attr, t);
|
|
|
|
|
|
|
|
if (val.type != mvt_list) {
|
|
|
|
fprintf(stderr, "Expected node attribute to be a list\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val.list_value.size() != geom.size()) {
|
|
|
|
fprintf(stderr, "Node attribute list size doesn't match geometry size\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t g = 0; g < geom.size(); g++) {
|
|
|
|
out.push_back(std::string("{") + quote(key) + ":" + val.list_value[g].toString() + "}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
void layer_to_geojson(mvt_layer const &layer, unsigned z, unsigned x, unsigned y, bool comma, bool name, bool zoom, bool dropped, unsigned long long index, long long sequence, long long extent, bool complain, json_writer &state) {
|
2016-12-05 14:12:39 -08:00
|
|
|
for (size_t f = 0; f < layer.features.size(); f++) {
|
2016-12-09 14:14:44 -08:00
|
|
|
mvt_feature const &feat = layer.features[f];
|
2016-12-05 14:12:39 -08:00
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_hash();
|
|
|
|
state.json_write_string("type");
|
|
|
|
state.json_write_string("Feature");
|
2016-12-05 14:12:39 -08:00
|
|
|
|
|
|
|
if (feat.has_id) {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("id");
|
|
|
|
state.json_write_unsigned(feat.id);
|
2016-12-05 14:12:39 -08:00
|
|
|
}
|
2018-09-10 11:25:52 -07:00
|
|
|
if (feat.string_id.size() != 0) {
|
2018-09-07 15:37:46 -07:00
|
|
|
state.json_write_string("id");
|
2018-09-10 11:25:52 -07:00
|
|
|
state.json_write_string(feat.string_id);
|
2018-09-07 15:37:46 -07:00
|
|
|
}
|
2016-12-05 14:12:39 -08:00
|
|
|
|
2017-03-28 16:25:40 -07:00
|
|
|
if (name || zoom || index != 0 || sequence != 0 || extent != 0) {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("tippecanoe");
|
|
|
|
state.json_write_hash();
|
2017-01-13 14:59:11 -08:00
|
|
|
|
|
|
|
if (name) {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("layer");
|
|
|
|
state.json_write_string(layer.name);
|
2017-01-13 14:59:11 -08:00
|
|
|
}
|
|
|
|
|
2017-03-28 16:25:40 -07:00
|
|
|
if (zoom) {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("minzoom");
|
|
|
|
state.json_write_unsigned(z);
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("maxzoom");
|
|
|
|
state.json_write_unsigned(z);
|
2017-03-28 16:25:40 -07:00
|
|
|
}
|
|
|
|
|
2018-02-23 17:06:39 -08:00
|
|
|
if (dropped) {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("dropped");
|
|
|
|
state.json_write_bool(feat.dropped);
|
2018-02-23 17:06:39 -08:00
|
|
|
}
|
|
|
|
|
2017-01-13 14:59:11 -08:00
|
|
|
if (index != 0) {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("index");
|
|
|
|
state.json_write_unsigned(index);
|
2017-01-13 14:59:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sequence != 0) {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("sequence");
|
|
|
|
state.json_write_signed(sequence);
|
2017-01-13 14:59:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (extent != 0) {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("extent");
|
|
|
|
state.json_write_signed(extent);
|
2017-01-13 14:59:11 -08:00
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_end_hash();
|
2016-12-08 15:13:38 -08:00
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("properties");
|
|
|
|
state.json_write_hash();
|
2016-12-05 14:12:39 -08:00
|
|
|
|
|
|
|
for (size_t t = 0; t + 1 < feat.tags.size(); t += 2) {
|
|
|
|
if (feat.tags[t] >= layer.keys.size()) {
|
2017-08-08 13:38:18 -07:00
|
|
|
fprintf(stderr, "Error: out of bounds feature key (%u in %zu)\n", feat.tags[t], layer.keys.size());
|
2016-12-05 14:12:39 -08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (feat.tags[t + 1] >= layer.values.size()) {
|
2017-08-08 13:38:18 -07:00
|
|
|
fprintf(stderr, "Error: out of bounds feature value (%u in %zu)\n", feat.tags[t + 1], layer.values.size());
|
2016-12-05 14:12:39 -08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *key = layer.keys[feat.tags[t]].c_str();
|
|
|
|
mvt_value const &val = layer.values[feat.tags[t + 1]];
|
|
|
|
|
2018-03-19 11:29:20 -07:00
|
|
|
state.json_write_string(key);
|
2018-10-17 13:44:12 -07:00
|
|
|
print_val(val, state);
|
2018-07-20 14:27:22 -07:00
|
|
|
}
|
|
|
|
|
2018-08-14 17:55:02 -07:00
|
|
|
for (size_t t = 0; t + 1 < feat.properties.size(); t++) {
|
2018-09-04 16:07:12 -07:00
|
|
|
if (feat.properties[t] >= layer.keys.size()) {
|
|
|
|
fprintf(stderr, "Out of bounds attribute reference %lu into %zu\n", feat.properties[t], layer.keys.size());
|
2018-08-17 11:21:06 -07:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2018-09-04 16:07:12 -07:00
|
|
|
const char *key = layer.keys[feat.properties[t]].c_str();
|
2018-08-14 17:55:02 -07:00
|
|
|
|
|
|
|
t++;
|
2018-10-11 16:01:44 -07:00
|
|
|
mvt_value const &val = layer.decode_property(feat.properties, t);
|
2018-07-20 14:27:22 -07:00
|
|
|
|
2018-08-14 15:36:44 -07:00
|
|
|
state.json_write_string(key);
|
2018-10-17 13:44:12 -07:00
|
|
|
print_val(val, state);
|
2016-12-05 14:12:39 -08:00
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_end_hash();
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("geometry");
|
|
|
|
state.json_write_hash();
|
2016-12-05 14:12:39 -08:00
|
|
|
|
|
|
|
std::vector<lonlat> ops;
|
2018-08-29 12:51:27 -07:00
|
|
|
bool has_attributes = false;
|
2016-12-05 14:12:39 -08:00
|
|
|
|
2018-10-17 13:26:08 -07:00
|
|
|
std::vector<std::string> attributes = decode_node_attributes(feat, layer);
|
|
|
|
|
2016-12-05 14:12:39 -08:00
|
|
|
for (size_t g = 0; g < feat.geometry.size(); g++) {
|
|
|
|
int op = feat.geometry[g].op;
|
|
|
|
long long px = feat.geometry[g].x;
|
|
|
|
long long py = feat.geometry[g].y;
|
|
|
|
|
|
|
|
if (op == VT_MOVETO || op == VT_LINETO) {
|
|
|
|
long long scale = 1LL << (32 - z);
|
|
|
|
long long wx = scale * x + (scale / layer.extent) * px;
|
|
|
|
long long wy = scale * y + (scale / layer.extent) * py;
|
|
|
|
|
|
|
|
double lat, lon;
|
|
|
|
projection->unproject(wx, wy, 32, &lon, &lat);
|
|
|
|
|
2018-10-17 13:44:12 -07:00
|
|
|
ops.push_back(lonlat(op, lon, lat, px, py, feat.geometry[g].elevations, g >= attributes.size() ? "" : attributes[g]));
|
|
|
|
if (g < attributes.size() && attributes[g].size() != 0) {
|
2018-08-29 12:51:27 -07:00
|
|
|
has_attributes = true;
|
|
|
|
}
|
2016-12-05 14:12:39 -08:00
|
|
|
} else {
|
2018-08-28 15:32:43 -07:00
|
|
|
ops.push_back(lonlat(op, 0, 0, 0, 0, std::vector<double>(), ""));
|
2016-12-05 14:12:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 12:51:27 -07:00
|
|
|
std::vector<coordinate_writer> coordinate_writers;
|
|
|
|
{
|
|
|
|
coordinate_writer coord_writer;
|
|
|
|
coord_writer.tag = "coordinates";
|
|
|
|
coord_writer.function = write_coordinates;
|
|
|
|
coordinate_writers.push_back(coord_writer);
|
|
|
|
|
|
|
|
if (has_attributes) {
|
|
|
|
coordinate_writer attrib_writer;
|
|
|
|
attrib_writer.tag = "attributes";
|
|
|
|
attrib_writer.function = write_attributes;
|
|
|
|
coordinate_writers.push_back(attrib_writer);
|
|
|
|
}
|
2018-10-18 17:06:13 -07:00
|
|
|
|
|
|
|
if (feat.knots.size() != 0) {
|
|
|
|
state.json_write_string("degree");
|
|
|
|
state.json_write_number(feat.spline_degree);
|
|
|
|
|
|
|
|
state.json_write_string("knots");
|
|
|
|
state.json_write_array();
|
|
|
|
|
|
|
|
size_t here = 0;
|
|
|
|
while (here < feat.knots.size()) {
|
|
|
|
mvt_value v = layer.decode_property(feat.knots, here);
|
|
|
|
here++;
|
|
|
|
|
|
|
|
state.json_write_stringified(v.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
state.json_end_array();
|
|
|
|
}
|
2018-08-29 12:51:27 -07:00
|
|
|
}
|
2018-09-04 16:07:12 -07:00
|
|
|
|
2016-12-05 14:12:39 -08:00
|
|
|
if (feat.type == VT_POINT) {
|
|
|
|
if (ops.size() == 1) {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("type");
|
|
|
|
state.json_write_string("Point");
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-08-29 12:51:27 -07:00
|
|
|
for (auto c : coordinate_writers) {
|
|
|
|
state.json_write_string(c.tag);
|
|
|
|
c.function(state, ops[0]);
|
|
|
|
}
|
2016-12-05 14:12:39 -08:00
|
|
|
} else {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("type");
|
|
|
|
state.json_write_string("MultiPoint");
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-08-29 12:51:27 -07:00
|
|
|
for (auto c : coordinate_writers) {
|
|
|
|
state.json_write_string(c.tag);
|
|
|
|
state.json_write_array();
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-08-29 12:51:27 -07:00
|
|
|
for (size_t i = 0; i < ops.size(); i++) {
|
|
|
|
c.function(state, ops[i]);
|
|
|
|
}
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-08-29 12:51:27 -07:00
|
|
|
state.json_end_array();
|
|
|
|
}
|
2016-12-05 14:12:39 -08:00
|
|
|
}
|
2018-10-18 16:15:35 -07:00
|
|
|
} else if (feat.type == VT_LINE || feat.type == VT_SPLINE) {
|
2016-12-05 14:12:39 -08:00
|
|
|
int movetos = 0;
|
|
|
|
for (size_t i = 0; i < ops.size(); i++) {
|
|
|
|
if (ops[i].op == VT_MOVETO) {
|
|
|
|
movetos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (movetos < 2) {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("type");
|
2018-10-18 16:15:35 -07:00
|
|
|
|
|
|
|
if (feat.type == VT_LINE) {
|
|
|
|
state.json_write_string("LineString");
|
|
|
|
} else {
|
|
|
|
state.json_write_string("Spline");
|
|
|
|
}
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-08-29 12:51:27 -07:00
|
|
|
for (auto c : coordinate_writers) {
|
|
|
|
state.json_write_string(c.tag);
|
|
|
|
state.json_write_array();
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-08-29 12:51:27 -07:00
|
|
|
for (size_t i = 0; i < ops.size(); i++) {
|
|
|
|
c.function(state, ops[i]);
|
|
|
|
}
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-08-29 12:51:27 -07:00
|
|
|
state.json_end_array();
|
|
|
|
}
|
2016-12-05 14:12:39 -08:00
|
|
|
} else {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("type");
|
2018-10-18 16:15:35 -07:00
|
|
|
|
|
|
|
if (feat.type == VT_LINE) {
|
|
|
|
state.json_write_string("MultiLineString");
|
|
|
|
} else {
|
|
|
|
state.json_write_string("MultiSpline");
|
|
|
|
}
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-08-29 12:51:27 -07:00
|
|
|
for (auto c : coordinate_writers) {
|
|
|
|
state.json_write_string(c.tag);
|
|
|
|
state.json_write_array();
|
|
|
|
state.json_write_array();
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-08-29 12:51:27 -07:00
|
|
|
int sstate = 0;
|
|
|
|
for (size_t i = 0; i < ops.size(); i++) {
|
|
|
|
if (ops[i].op == VT_MOVETO) {
|
|
|
|
if (sstate == 0) {
|
|
|
|
c.function(state, ops[i]);
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-08-29 12:51:27 -07:00
|
|
|
sstate = 1;
|
|
|
|
} else {
|
|
|
|
state.json_end_array();
|
|
|
|
state.json_write_array();
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-08-29 12:51:27 -07:00
|
|
|
c.function(state, ops[i]);
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-08-29 12:51:27 -07:00
|
|
|
sstate = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.function(state, ops[i]);
|
2016-12-05 14:12:39 -08:00
|
|
|
}
|
|
|
|
}
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-08-29 12:51:27 -07:00
|
|
|
state.json_end_array();
|
|
|
|
state.json_end_array();
|
|
|
|
}
|
2016-12-05 14:12:39 -08:00
|
|
|
}
|
|
|
|
} else if (feat.type == VT_POLYGON) {
|
|
|
|
std::vector<std::vector<lonlat> > rings;
|
|
|
|
std::vector<double> areas;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ops.size(); i++) {
|
|
|
|
if (ops[i].op == VT_MOVETO) {
|
|
|
|
rings.push_back(std::vector<lonlat>());
|
|
|
|
areas.push_back(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int n = rings.size() - 1;
|
|
|
|
if (n >= 0) {
|
|
|
|
if (ops[i].op == VT_CLOSEPATH) {
|
|
|
|
rings[n].push_back(rings[n][0]);
|
|
|
|
} else {
|
|
|
|
rings[n].push_back(ops[i]);
|
|
|
|
}
|
|
|
|
}
|
2017-05-12 17:20:30 -07:00
|
|
|
|
|
|
|
if (i + 1 >= ops.size() || ops[i + 1].op == VT_MOVETO) {
|
|
|
|
if (ops[i].op != VT_CLOSEPATH) {
|
|
|
|
static bool warned = false;
|
|
|
|
|
|
|
|
if (!warned) {
|
|
|
|
fprintf(stderr, "Ring does not end with closepath (ends with %d)\n", ops[i].op);
|
|
|
|
if (complain) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
warned = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-05 14:12:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
int outer = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < rings.size(); i++) {
|
|
|
|
long double area = 0;
|
|
|
|
for (size_t k = 0; k < rings[i].size(); k++) {
|
|
|
|
if (rings[i][k].op != VT_CLOSEPATH) {
|
2016-12-20 15:07:10 -08:00
|
|
|
area += (long double) rings[i][k].x * (long double) rings[i][(k + 1) % rings[i].size()].y;
|
|
|
|
area -= (long double) rings[i][k].y * (long double) rings[i][(k + 1) % rings[i].size()].x;
|
2016-12-05 14:12:39 -08:00
|
|
|
}
|
|
|
|
}
|
2016-12-20 15:07:10 -08:00
|
|
|
area /= 2;
|
2016-12-05 14:12:39 -08:00
|
|
|
|
|
|
|
areas[i] = area;
|
|
|
|
if (areas[i] >= 0 || i == 0) {
|
|
|
|
outer++;
|
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
// fprintf("\"area\": %Lf,", area);
|
2016-12-05 14:12:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (outer > 1) {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("type");
|
|
|
|
state.json_write_string("MultiPolygon");
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("coordinates");
|
|
|
|
state.json_write_array();
|
|
|
|
state.json_write_array();
|
|
|
|
state.json_write_array();
|
2016-12-05 14:12:39 -08:00
|
|
|
} else {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("type");
|
|
|
|
state.json_write_string("Polygon");
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_string("coordinates");
|
|
|
|
state.json_write_array();
|
|
|
|
state.json_write_array();
|
2016-12-05 14:12:39 -08:00
|
|
|
}
|
|
|
|
|
2018-03-15 14:42:53 -07:00
|
|
|
int sstate = 0;
|
2016-12-05 14:12:39 -08:00
|
|
|
for (size_t i = 0; i < rings.size(); i++) {
|
2017-05-12 17:20:30 -07:00
|
|
|
if (i == 0 && areas[i] < 0) {
|
|
|
|
static bool warned = false;
|
|
|
|
|
|
|
|
if (!warned) {
|
|
|
|
fprintf(stderr, "Polygon begins with an inner ring\n");
|
|
|
|
if (complain) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
warned = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 14:12:39 -08:00
|
|
|
if (areas[i] >= 0) {
|
2018-03-15 14:42:53 -07:00
|
|
|
if (sstate != 0) {
|
2016-12-05 14:12:39 -08:00
|
|
|
// new multipolygon
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_end_array();
|
|
|
|
state.json_end_array();
|
2018-03-15 14:42:53 -07:00
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_array();
|
|
|
|
state.json_write_array();
|
2016-12-05 14:12:39 -08:00
|
|
|
}
|
2018-03-15 14:42:53 -07:00
|
|
|
sstate = 1;
|
2016-12-05 14:12:39 -08:00
|
|
|
}
|
|
|
|
|
2018-03-15 14:42:53 -07:00
|
|
|
if (sstate == 2) {
|
2016-12-05 14:12:39 -08:00
|
|
|
// new ring in the same polygon
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_end_array();
|
|
|
|
state.json_write_array();
|
2016-12-05 14:12:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t j = 0; j < rings[i].size(); j++) {
|
|
|
|
if (rings[i][j].op != VT_CLOSEPATH) {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_array();
|
|
|
|
state.json_write_float(rings[i][j].lon);
|
|
|
|
state.json_write_float(rings[i][j].lat);
|
|
|
|
state.json_end_array();
|
2016-12-05 14:12:39 -08:00
|
|
|
} else {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_array();
|
|
|
|
state.json_write_float(rings[i][0].lon);
|
|
|
|
state.json_write_float(rings[i][0].lat);
|
|
|
|
state.json_end_array();
|
2016-12-05 14:12:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 14:42:53 -07:00
|
|
|
sstate = 2;
|
2016-12-05 14:12:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (outer > 1) {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_end_array();
|
|
|
|
state.json_end_array();
|
|
|
|
state.json_end_array();
|
2016-12-05 14:12:39 -08:00
|
|
|
} else {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_end_array();
|
|
|
|
state.json_end_array();
|
2016-12-05 14:12:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_end_hash();
|
|
|
|
state.json_end_hash();
|
2018-03-15 14:42:53 -07:00
|
|
|
|
|
|
|
if (comma) {
|
2018-03-16 13:43:21 -07:00
|
|
|
state.json_write_newline();
|
|
|
|
state.json_comma_newline();
|
2018-03-15 14:42:53 -07:00
|
|
|
}
|
|
|
|
}
|
2016-12-05 14:12:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void fprintq(FILE *fp, const char *s) {
|
|
|
|
fputc('"', fp);
|
|
|
|
for (; *s; s++) {
|
|
|
|
if (*s == '\\' || *s == '"') {
|
|
|
|
fprintf(fp, "\\%c", *s);
|
|
|
|
} else if (*s >= 0 && *s < ' ') {
|
|
|
|
fprintf(fp, "\\u%04x", *s);
|
|
|
|
} else {
|
|
|
|
fputc(*s, fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fputc('"', fp);
|
|
|
|
}
|