Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat tiny polygon holes as dust again, but accumulated separately now #134

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void check_polygon(drawvec &geom) {
}
}

drawvec reduce_tiny_poly(drawvec &geom, int z, int detail, bool *reduced, double *accum_area, serial_feature *this_feature, serial_feature *tiny_feature) {
drawvec reduce_tiny_poly(drawvec &geom, int z, int detail, bool *reduced, double *accum_area, double *accum_hole, serial_feature *this_feature, serial_feature *tiny_feature) {
drawvec out;
const double pixel = (1LL << (32 - detail - z)) * (double) tiny_polygon_size;
bool includes_real = false;
Expand Down Expand Up @@ -221,7 +221,9 @@ drawvec reduce_tiny_poly(drawvec &geom, int z, int detail, bool *reduced, double
// in a tiny polygon rather than letting it represent itself,
// OR it is an inner ring and we haven't output an outer ring for it to be
// cut out of, so we are just subtracting its area from the tiny polygon
// rather than trying to deal with it geometrically
// rather than trying to deal with it geometrically (even if it is too big
// to really be considered a tiny polygon, which shouldn't happen because
// it should be smaller than its outer ring, which was considered tiny).
if ((area > 0 && area <= pixel * pixel) || (area < 0 && !included_last_outer)) {
// printf("area is only %f vs %lld so using square\n", area, pixel * pixel);

Expand All @@ -243,10 +245,32 @@ drawvec reduce_tiny_poly(drawvec &geom, int z, int detail, bool *reduced, double
included_last_outer = false;
}
}

// this is a tiny hole, and we have a real outer ring that we can cut it out of,
// so accumulate some hole, and if we have enough to be worth cutting, cut it out.
//
// these hole areas are accumulated separately rather than just decrementing
// the accumulated area, because otherwise it is possible to accumulate enough
// negative area to lose entire outer rings in the process of getting back up to 0.
else if (area < 0 && -area <= pixel * pixel && included_last_outer) {
*accum_hole -= area;
if (*accum_hole > pixel * pixel) {
// XXX use centroid;

// winding is backward from above because this is a hole
out.push_back(draw(VT_MOVETO, geom[i].x - pixel / 2, geom[i].y - pixel / 2));
out.push_back(draw(VT_LINETO, geom[i].x - pixel / 2, geom[i].y - pixel / 2 + pixel));
out.push_back(draw(VT_LINETO, geom[i].x - pixel / 2 + pixel, geom[i].y - pixel / 2 + pixel));
out.push_back(draw(VT_LINETO, geom[i].x - pixel / 2 + pixel, geom[i].y - pixel / 2));
out.push_back(draw(VT_LINETO, geom[i].x - pixel / 2, geom[i].y - pixel / 2));

includes_dust = true;

*accum_hole -= pixel * pixel;
}
}

// i.e., this ring is large enough that it gets to represent itself
// or it is a tiny hole out of a real polygon, which we are still treating
// as a real geometry because otherwise we can accumulate enough tiny holes
// that we will drop the next several outer rings getting back up to 0.
else {
// printf("area is %f so keeping instead of %lld\n", area, pixel * pixel);

Expand Down
2 changes: 1 addition & 1 deletion geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ drawvec clip_point(drawvec &geom, int z, long long buffer);
drawvec clean_or_clip_poly(drawvec &geom, int z, int buffer, bool clip);
drawvec simple_clip_poly(drawvec &geom, int z, int buffer);
drawvec close_poly(drawvec &geom);
drawvec reduce_tiny_poly(drawvec &geom, int z, int detail, bool *reduced, double *accum_area, serial_feature *this_feature, serial_feature *tiny_feature);
drawvec reduce_tiny_poly(drawvec &geom, int z, int detail, bool *reduced, double *accum_area, double *accum_hole, serial_feature *this_feature, serial_feature *tiny_feature);
int clip(double *x0, double *y0, double *x1, double *y1, double xmin, double ymin, double xmax, double ymax);
drawvec clip_lines(drawvec &geom, int z, long long buffer);
drawvec stairstep(drawvec &geom, int z, int detail);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
,
{ "type": "Feature", "properties": { "scalerank": 1, "featurecla": "Admin-0 country", "labelrank": 3, "sovereignt": "Mozambique", "sov_a3": "MOZ", "adm0_dif": 0, "level": 2, "type": "Sovereign country", "admin": "Mozambique", "adm0_a3": "MOZ", "geou_dif": 0, "geounit": "Mozambique", "gu_a3": "MOZ", "su_dif": 0, "subunit": "Mozambique", "su_a3": "MOZ", "brk_diff": 0, "name": "Mozambique", "name_long": "Mozambique", "brk_a3": "MOZ", "brk_name": "Mozambique", "abbrev": "Moz.", "postal": "MZ", "formal_en": "Republic of Mozambique", "name_sort": "Mozambique", "mapcolor7": 4, "mapcolor8": 2, "mapcolor9": 1, "mapcolor13": 4, "pop_est": 21669278, "gdp_md_est": 18940, "pop_year": -99, "lastcensus": 2007, "gdp_year": -99, "economy": "7. Least developed region", "income_grp": "5. Low income", "wikipedia": -99, "iso_a2": "MZ", "iso_a3": "MOZ", "iso_n3": "508", "un_a3": "508", "wb_a2": "MZ", "wb_a3": "MOZ", "woe_id": -99, "adm0_a3_is": "MOZ", "adm0_a3_us": "MOZ", "adm0_a3_un": -99, "adm0_a3_wb": -99, "continent": "Africa", "region_un": "Africa", "subregion": "Eastern Africa", "region_wb": "Sub-Saharan Africa", "name_len": 10, "long_len": 10, "abbrev_len": 4, "tiny": -99, "homepart": 1 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 44.736328, -5.965754 ], [ 44.736328, -14.604847 ], [ 35.947266, -14.604847 ], [ 35.947266, -5.965754 ], [ 44.736328, -5.965754 ] ] ] } }
,
{ "type": "Feature", "properties": { "scalerank": 1, "featurecla": "Admin-0 country", "labelrank": 2, "sovereignt": "South Africa", "sov_a3": "ZAF", "adm0_dif": 0, "level": 2, "type": "Sovereign country", "admin": "South Africa", "adm0_a3": "ZAF", "geou_dif": 0, "geounit": "South Africa", "gu_a3": "ZAF", "su_dif": 0, "subunit": "South Africa", "su_a3": "ZAF", "brk_diff": 0, "name": "South Africa", "name_long": "South Africa", "brk_a3": "ZAF", "brk_name": "South Africa", "abbrev": "S.Af.", "postal": "ZA", "formal_en": "Republic of South Africa", "name_sort": "South Africa", "mapcolor7": 2, "mapcolor8": 3, "mapcolor9": 4, "mapcolor13": 2, "pop_est": 49052489, "gdp_md_est": 491000, "pop_year": -99, "lastcensus": 2001, "gdp_year": -99, "economy": "5. Emerging region: G20", "income_grp": "3. Upper middle income", "wikipedia": -99, "iso_a2": "ZA", "iso_a3": "ZAF", "iso_n3": "710", "un_a3": "710", "wb_a2": "ZA", "wb_a3": "ZAF", "woe_id": -99, "adm0_a3_is": "ZAF", "adm0_a3_us": "ZAF", "adm0_a3_un": -99, "adm0_a3_wb": -99, "continent": "Africa", "region_un": "Africa", "subregion": "Southern Africa", "region_wb": "Sub-Saharan Africa", "name_len": 12, "long_len": 12, "abbrev_len": 5, "tiny": -99, "homepart": 1 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 29.443359, -22.105999 ], [ 31.201172, -22.268764 ], [ 31.904297, -24.367114 ], [ 31.728516, -25.482951 ], [ 31.816406, -25.878994 ], [ 31.376953, -25.641526 ], [ 31.025391, -25.720735 ], [ 30.673828, -26.431228 ], [ 30.673828, -26.745610 ], [ 31.289062, -27.293689 ], [ 31.904297, -27.215556 ], [ 32.080078, -26.745610 ], [ 32.871094, -26.745610 ], [ 32.431641, -28.304381 ], [ 32.167969, -28.767659 ], [ 31.289062, -29.382175 ], [ 30.058594, -31.128199 ], [ 28.916016, -32.175612 ], [ 28.212891, -32.768800 ], [ 26.455078, -33.651208 ], [ 25.927734, -33.651208 ], [ 25.751953, -33.943360 ], [ 25.136719, -33.797409 ], [ 24.697266, -34.016242 ], [ 23.554688, -33.797409 ], [ 23.027344, -33.943360 ], [ 22.587891, -33.870416 ], [ 20.654297, -34.452218 ], [ 20.039062, -34.813803 ], [ 19.599609, -34.813803 ], [ 19.160156, -34.452218 ], [ 18.896484, -34.452218 ], [ 18.457031, -34.016242 ], [ 18.369141, -34.161818 ], [ 18.281250, -33.284620 ], [ 17.929688, -32.620870 ], [ 18.281250, -32.398516 ], [ 18.193359, -31.653381 ], [ 16.347656, -28.613459 ], [ 16.787109, -28.071980 ], [ 17.226562, -28.381735 ], [ 17.402344, -28.767659 ], [ 18.457031, -29.075375 ], [ 18.984375, -28.998532 ], [ 19.863281, -28.459033 ], [ 19.863281, -24.766785 ], [ 20.126953, -24.926295 ], [ 20.742188, -25.878994 ], [ 20.654297, -26.509905 ], [ 20.917969, -26.824071 ], [ 21.621094, -26.745610 ], [ 22.587891, -25.958045 ], [ 22.851562, -25.482951 ], [ 23.291016, -25.244696 ], [ 24.169922, -25.641526 ], [ 25.048828, -25.720735 ], [ 25.664062, -25.482951 ], [ 25.927734, -24.686952 ], [ 26.455078, -24.607069 ], [ 27.158203, -23.563987 ], [ 28.037109, -22.836946 ], [ 29.443359, -22.105999 ] ], [ [ 28.564453, -28.613459 ], [ 27.509766, -29.228890 ], [ 26.982422, -29.840644 ], [ 27.773438, -30.675715 ], [ 28.125000, -30.524413 ], [ 28.300781, -30.221102 ], [ 28.828125, -30.069094 ], [ 29.355469, -29.228890 ], [ 28.564453, -28.613459 ] ] ] } }
{ "type": "Feature", "properties": { "scalerank": 1, "featurecla": "Admin-0 country", "labelrank": 2, "sovereignt": "South Africa", "sov_a3": "ZAF", "adm0_dif": 0, "level": 2, "type": "Sovereign country", "admin": "South Africa", "adm0_a3": "ZAF", "geou_dif": 0, "geounit": "South Africa", "gu_a3": "ZAF", "su_dif": 0, "subunit": "South Africa", "su_a3": "ZAF", "brk_diff": 0, "name": "South Africa", "name_long": "South Africa", "brk_a3": "ZAF", "brk_name": "South Africa", "abbrev": "S.Af.", "postal": "ZA", "formal_en": "Republic of South Africa", "name_sort": "South Africa", "mapcolor7": 2, "mapcolor8": 3, "mapcolor9": 4, "mapcolor13": 2, "pop_est": 49052489, "gdp_md_est": 491000, "pop_year": -99, "lastcensus": 2001, "gdp_year": -99, "economy": "5. Emerging region: G20", "income_grp": "3. Upper middle income", "wikipedia": -99, "iso_a2": "ZA", "iso_a3": "ZAF", "iso_n3": "710", "un_a3": "710", "wb_a2": "ZA", "wb_a3": "ZAF", "woe_id": -99, "adm0_a3_is": "ZAF", "adm0_a3_us": "ZAF", "adm0_a3_un": -99, "adm0_a3_wb": -99, "continent": "Africa", "region_un": "Africa", "subregion": "Southern Africa", "region_wb": "Sub-Saharan Africa", "name_len": 12, "long_len": 12, "abbrev_len": 5, "tiny": -99, "homepart": 1 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 29.443359, -22.105999 ], [ 31.201172, -22.268764 ], [ 31.904297, -24.367114 ], [ 31.728516, -25.482951 ], [ 31.816406, -25.878994 ], [ 31.376953, -25.641526 ], [ 31.025391, -25.720735 ], [ 30.673828, -26.431228 ], [ 30.673828, -26.745610 ], [ 31.289062, -27.293689 ], [ 31.904297, -27.215556 ], [ 32.080078, -26.745610 ], [ 32.871094, -26.745610 ], [ 32.431641, -28.304381 ], [ 32.167969, -28.767659 ], [ 31.289062, -29.382175 ], [ 30.058594, -31.128199 ], [ 28.916016, -32.175612 ], [ 28.212891, -32.768800 ], [ 26.455078, -33.651208 ], [ 25.927734, -33.651208 ], [ 25.751953, -33.943360 ], [ 25.136719, -33.797409 ], [ 24.697266, -34.016242 ], [ 23.554688, -33.797409 ], [ 23.027344, -33.943360 ], [ 22.587891, -33.870416 ], [ 20.654297, -34.452218 ], [ 20.039062, -34.813803 ], [ 19.599609, -34.813803 ], [ 19.160156, -34.452218 ], [ 18.896484, -34.452218 ], [ 18.457031, -34.016242 ], [ 18.369141, -34.161818 ], [ 18.281250, -33.284620 ], [ 17.929688, -32.620870 ], [ 18.281250, -32.398516 ], [ 18.193359, -31.653381 ], [ 16.347656, -28.613459 ], [ 16.787109, -28.071980 ], [ 17.226562, -28.381735 ], [ 17.402344, -28.767659 ], [ 18.457031, -29.075375 ], [ 18.984375, -28.998532 ], [ 19.863281, -28.459033 ], [ 19.863281, -24.766785 ], [ 20.126953, -24.926295 ], [ 20.742188, -25.878994 ], [ 20.654297, -26.509905 ], [ 20.917969, -26.824071 ], [ 21.621094, -26.745610 ], [ 22.587891, -25.958045 ], [ 22.851562, -25.482951 ], [ 23.291016, -25.244696 ], [ 24.169922, -25.641526 ], [ 25.048828, -25.720735 ], [ 25.664062, -25.482951 ], [ 25.927734, -24.686952 ], [ 26.455078, -24.607069 ], [ 27.158203, -23.563987 ], [ 28.037109, -22.836946 ], [ 29.443359, -22.105999 ] ] ] } }
,
{ "type": "Feature", "properties": { "scalerank": 1, "featurecla": "Admin-0 country", "labelrank": 3, "sovereignt": "Madagascar", "sov_a3": "MDG", "adm0_dif": 0, "level": 2, "type": "Sovereign country", "admin": "Madagascar", "adm0_a3": "MDG", "geou_dif": 0, "geounit": "Madagascar", "gu_a3": "MDG", "su_dif": 0, "subunit": "Madagascar", "su_a3": "MDG", "brk_diff": 0, "name": "Madagascar", "name_long": "Madagascar", "brk_a3": "MDG", "brk_name": "Madagascar", "abbrev": "Mad.", "postal": "MG", "formal_en": "Republic of Madagascar", "name_sort": "Madagascar", "mapcolor7": 6, "mapcolor8": 5, "mapcolor9": 2, "mapcolor13": 3, "pop_est": 20653556, "gdp_md_est": 20130, "pop_year": -99, "lastcensus": 1993, "gdp_year": -99, "economy": "7. Least developed region", "income_grp": "5. Low income", "wikipedia": -99, "iso_a2": "MG", "iso_a3": "MDG", "iso_n3": "450", "un_a3": "450", "wb_a2": "MG", "wb_a3": "MDG", "woe_id": -99, "adm0_a3_is": "MDG", "adm0_a3_us": "MDG", "adm0_a3_un": -99, "adm0_a3_wb": -99, "continent": "Africa", "region_un": "Africa", "subregion": "Eastern Africa", "region_wb": "Sub-Saharan Africa", "name_len": 10, "long_len": 10, "abbrev_len": 4, "tiny": -99, "homepart": 1 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 53.613281, -7.710992 ], [ 53.613281, -16.299051 ], [ 44.824219, -16.299051 ], [ 44.824219, -7.710992 ], [ 53.613281, -7.710992 ] ] ] } }
,
Expand Down
Loading