mirror of
https://github.com/felt/tippecanoe.git
synced 2026-10-02 08:25:40 +02:00
Work in progress on binning features in overzoom (#258)
* Factoring out tilestats management from GeoJSON file reading * Move code around so overzoom can link against parse_layers * Read the file of bins * Plumb the bins through to overzoom() * Some zip code bins to test with * (Currently non-functional) test of binning * Starting to spell out the bin matching loop * Can't flatten points, so don't flatten bins either * More fleshing out bin traversal * Bounding box of tile-relative mvt geometry * Smallest enclosing tile from bbox * Most of the bin scan * Add point in polygon check. It crashes. * Find the matching bins * GDAL-style bounding boxes have eaten my brain * Make some features to bin into * Increment a count as features are found to be within the bins * Fix longitude wraparound in overzoom bins * Fix the tests * Push off attribute copying until after bin assignment * Carry sum of numeric attributes into the bins * Also add mean, min, and max * Add --calculate-feature-index since I keep needing it for testing * Add an option to accumulate sum/mean/max/min/count of all numeric attrs * Don't bake in tippecanoe:mean, since we redo it from sum and count * Forgot to update this test fixture after removing tiled mean * Update version and changelog
This commit is contained in:
-170
@@ -70,28 +70,6 @@ drawvec decode_geometry(const char **meta, int z, unsigned tx, unsigned ty, long
|
||||
return out;
|
||||
}
|
||||
|
||||
/* pnpoly:
|
||||
Copyright (c) 1970-2003, Wm. Randolph Franklin
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimers.
|
||||
Redistributions in binary form must reproduce the above copyright notice in the documentation and/or other materials provided with the distribution.
|
||||
The name of W. Randolph Franklin may not be used to endorse or promote products derived from this Software without specific prior written permission.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
int pnpoly(const drawvec &vert, size_t start, size_t nvert, long long testx, long long testy) {
|
||||
size_t i, j;
|
||||
bool c = false;
|
||||
for (i = 0, j = nvert - 1; i < nvert; j = i++) {
|
||||
if (((vert[i + start].y > testy) != (vert[j + start].y > testy)) &&
|
||||
(testx < (vert[j + start].x - vert[i + start].x) * (testy - vert[i + start].y) / (double) (vert[j + start].y - vert[i + start].y) + vert[i + start].x))
|
||||
c = !c;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
void check_polygon(drawvec &geom) {
|
||||
geom = remove_noop(geom, VT_POLYGON, 0);
|
||||
|
||||
@@ -381,154 +359,6 @@ drawvec reorder_lines(const drawvec &geom) {
|
||||
return geom;
|
||||
}
|
||||
|
||||
drawvec fix_polygon(const drawvec &geom) {
|
||||
int outer = 1;
|
||||
drawvec out;
|
||||
|
||||
for (size_t i = 0; i < geom.size(); i++) {
|
||||
if (geom[i].op == VT_CLOSEPATH) {
|
||||
outer = 1;
|
||||
} else if (geom[i].op == VT_MOVETO) {
|
||||
// Find the end of the ring
|
||||
|
||||
size_t j;
|
||||
for (j = i + 1; j < geom.size(); j++) {
|
||||
if (geom[j].op != VT_LINETO) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// A polygon ring must contain at least three points
|
||||
// (and really should contain four). If this one does
|
||||
// not have any, avoid a division by zero trying to
|
||||
// calculate the centroid below.
|
||||
if (j - i < 1) {
|
||||
i = j - 1;
|
||||
outer = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Make a temporary copy of the ring.
|
||||
// Close it if it isn't closed.
|
||||
|
||||
drawvec ring;
|
||||
for (size_t a = i; a < j; a++) {
|
||||
ring.push_back(geom[a]);
|
||||
}
|
||||
if (j - i != 0 && (ring[0].x != ring[j - i - 1].x || ring[0].y != ring[j - i - 1].y)) {
|
||||
ring.push_back(ring[0]);
|
||||
}
|
||||
|
||||
// A polygon ring at this point should contain at least four points.
|
||||
// Flesh it out with some vertex copies if it doesn't.
|
||||
|
||||
while (ring.size() < 4) {
|
||||
ring.push_back(ring[0]);
|
||||
}
|
||||
|
||||
// Reverse ring if winding order doesn't match
|
||||
// inner/outer expectation
|
||||
|
||||
bool reverse_ring = false;
|
||||
if (prevent[P_USE_SOURCE_POLYGON_WINDING]) {
|
||||
// GeoJSON winding is reversed from vector winding
|
||||
reverse_ring = true;
|
||||
} else if (prevent[P_REVERSE_SOURCE_POLYGON_WINDING]) {
|
||||
// GeoJSON winding is reversed from vector winding
|
||||
reverse_ring = false;
|
||||
} else {
|
||||
double area = get_area(ring, 0, ring.size());
|
||||
if ((area > 0) != outer) {
|
||||
reverse_ring = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (reverse_ring) {
|
||||
drawvec tmp;
|
||||
for (int a = ring.size() - 1; a >= 0; a--) {
|
||||
tmp.push_back(ring[a]);
|
||||
}
|
||||
ring = tmp;
|
||||
}
|
||||
|
||||
// Now we are rotating the ring to make the first/last point
|
||||
// one that would be unlikely to be simplified away.
|
||||
|
||||
// calculate centroid
|
||||
// a + 1 < size() because point 0 is duplicated at the end
|
||||
long long xtotal = 0;
|
||||
long long ytotal = 0;
|
||||
long long count = 0;
|
||||
for (size_t a = 0; a + 1 < ring.size(); a++) {
|
||||
xtotal += ring[a].x;
|
||||
ytotal += ring[a].y;
|
||||
count++;
|
||||
}
|
||||
xtotal /= count;
|
||||
ytotal /= count;
|
||||
|
||||
// figure out which point is furthest from the centroid
|
||||
long long dist2 = 0;
|
||||
long long furthest = 0;
|
||||
for (size_t a = 0; a + 1 < ring.size(); a++) {
|
||||
// division by 16 because these are z0 coordinates and we need to avoid overflow
|
||||
long long xd = (ring[a].x - xtotal) / 16;
|
||||
long long yd = (ring[a].y - ytotal) / 16;
|
||||
long long d2 = xd * xd + yd * yd;
|
||||
if (d2 > dist2 || (d2 == dist2 && ring[a] < ring[furthest])) {
|
||||
dist2 = d2;
|
||||
furthest = a;
|
||||
}
|
||||
}
|
||||
|
||||
// then figure out which point is furthest from *that*,
|
||||
// which will hopefully be a good origin point since it should be
|
||||
// at a far edge of the shape.
|
||||
long long dist2b = 0;
|
||||
long long furthestb = 0;
|
||||
for (size_t a = 0; a + 1 < ring.size(); a++) {
|
||||
// division by 16 because these are z0 coordinates and we need to avoid overflow
|
||||
long long xd = (ring[a].x - ring[furthest].x) / 16;
|
||||
long long yd = (ring[a].y - ring[furthest].y) / 16;
|
||||
long long d2 = xd * xd + yd * yd;
|
||||
if (d2 > dist2b || (d2 == dist2b && ring[a] < ring[furthestb])) {
|
||||
dist2b = d2;
|
||||
furthestb = a;
|
||||
}
|
||||
}
|
||||
|
||||
// rotate ring so the furthest point is the duplicated one.
|
||||
// the idea is that simplification will then be more efficient,
|
||||
// never wasting the start and end points, which are always retained,
|
||||
// on a point that has little impact on the shape.
|
||||
|
||||
// Copy ring into output, fixing the moveto/lineto ops if necessary because of
|
||||
// reversal or closing
|
||||
|
||||
for (size_t a = 0; a < ring.size(); a++) {
|
||||
size_t a2 = (a + furthestb) % (ring.size() - 1);
|
||||
|
||||
if (a == 0) {
|
||||
out.push_back(draw(VT_MOVETO, ring[a2].x, ring[a2].y));
|
||||
} else {
|
||||
out.push_back(draw(VT_LINETO, ring[a2].x, ring[a2].y));
|
||||
}
|
||||
}
|
||||
|
||||
// Next ring or polygon begins on the non-lineto that ended this one
|
||||
// and is not an outer ring unless there is a terminator first
|
||||
|
||||
i = j - 1;
|
||||
outer = 0;
|
||||
} else {
|
||||
fprintf(stderr, "Internal error: polygon ring begins with %d, not moveto\n", geom[i].op);
|
||||
exit(EXIT_IMPOSSIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
#if 0
|
||||
std::vector<drawvec> chop_polygon(std::vector<drawvec> &geoms) {
|
||||
while (1) {
|
||||
|
||||
Reference in New Issue
Block a user