From 8c5681a58249639ec050b5431d393d8ed9565abe Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Fri, 9 Oct 2015 16:45:34 -0700 Subject: [PATCH] Still not quite a round trip from GeoJSON to GeoJSON, but getting closer --- decode.cc | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/decode.cc b/decode.cc index f082f57..692a4be 100644 --- a/decode.cc +++ b/decode.cc @@ -173,7 +173,7 @@ void handle(std::string message, int z, unsigned x, unsigned y) { ops.push_back(draw(op, lon, lat)); } } else { - ops.push_back(draw(op, 0, 27)); + ops.push_back(draw(op, 0, 0)); } } @@ -227,7 +227,79 @@ void handle(std::string message, int z, unsigned x, unsigned y) { printf(" ] ]"); } } else if (feat.type() == VT_POLYGON) { + std::vector > rings; + std::vector areas; + for (unsigned i = 0; i < ops.size(); i++) { + if (ops[i].op == VT_MOVETO) { + rings.push_back(std::vector()); + areas.push_back(0); + } + + int n = rings.size() - 1; + if (n >= 0) { + rings[n].push_back(ops[i]); + } + } + + int outer = 0; + + for (unsigned i = 0; i < rings.size(); i++) { + double area = 0; + for (unsigned k = 0; k < rings[i].size(); k++) { + if (rings[i][k].op != VT_CLOSEPATH) { + area += rings[i][k].lon * rings[i][(k + 1) % rings[i].size()].lat; + area -= rings[i][k].lat * rings[i][(k + 1) % rings[i].size()].lon; + } + } + + areas[i] = area; + if (areas[i] <= 0) { + outer++; + } + + // printf("area %f\n", area / .00000274 / .00000274); + } + + if (outer > 1) { + printf("\"type\": \"MultiPolygon\", \"coordinates\": [ [ [ "); + } else { + printf("\"type\": \"Polygon\", \"coordinates\": [ [ "); + } + + int state = 0; + for (unsigned i = 0; i < rings.size(); i++) { + if (areas[i] <= 0) { + if (state != 0) { + // new multipolygon + printf(" ] ], [ [ "); + } + state = 1; + } + + if (state == 2) { + // new ring in the same polygon + printf(" ], [ "); + } + + for (unsigned j = 0; j < rings[i].size(); j++) { + if (rings[i][j].op != VT_CLOSEPATH) { + if (j != 0) { + printf(", "); + } + + printf("[ %f, %f ]", rings[i][j].lon, rings[i][j].lat); + } + } + + state = 2; + } + + if (outer > 1) { + printf(" ] ] ]"); + } else { + printf(" ] ]"); + } } printf(" } }\n");