Skip to content

Commit

Permalink
Fix clipping bug when the clip region doesn't intersect the tile (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f authored Dec 11, 2024
1 parent d70e332 commit 7165ae6
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.73.0

* Correctly clip features down to nothing when the clip region doesn't intersect the tile at all

# 2.72.0

* Add --clip-polygon-file and --feature-filter-file options to tippecanoe-overzoom
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,10 @@ overzoom-test: tippecanoe-overzoom
./tippecanoe-decode tests/pbf/countries-8-135-86-bigclip.pbf 8 135 86 > tests/pbf/countries-8-135-86-bigclip.json.check
cmp tests/pbf/countries-8-135-86-bigclip.json.check tests/pbf/countries-8-135-86-bigclip.json
rm tests/pbf/countries-8-135-86-bigclip.pbf tests/pbf/countries-8-135-86-bigclip.json.check
# Clip region that does not intersect with the tile
./tippecanoe-overzoom -o tests/pbf/squirrels-13-2413-3077-clip.pbf --clip-polygon-file tests/pbf/squirrels-clip.json tests/pbf/squirrels-13-2413-3077.pbf 13/2413/3077 13/2413/3077
cmp tests/pbf/squirrels-13-2413-3077-clip.pbf /dev/null # clipped away
rm tests/pbf/squirrels-13-2413-3077-clip.pbf

join-test: tippecanoe tippecanoe-decode tile-join
./tippecanoe -q -f -z12 -o tests/join-population/tabblock_06001420.mbtiles -YALAND10:'Land area' -L'{"file": "tests/join-population/tabblock_06001420.json", "description": "population"}'
Expand Down
52 changes: 31 additions & 21 deletions overzoom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ int main(int argc, char **argv) {
// clip the clip polygons, if any, to the tile bounds,
// to reduce their complexity

bool clipped_to_nothing = false;
if (clipbboxes.size() > 0) {
long long wx1 = (nx - buffer / 256.0) * (1LL << (32 - nz));
long long wy1 = (ny - buffer / 256.0) * (1LL << (32 - nz));
Expand All @@ -315,38 +316,47 @@ int main(int argc, char **argv) {

if (c.dv.size() > 0) {
c.dv = clip_poly_poly(c.dv, tile_bounds);

if (c.dv.size() == 0) {
clipped_to_nothing = true;
break;
}
}
}
}

json_object *json_filter = NULL;
if (filter.size() > 0) {
json_filter = parse_filter(filter.c_str());
}

for (auto const &s : sources) {
std::string tile;
char buf[1000];
int len;
std::string out;

FILE *f = fopen(s.tile.c_str(), "rb");
if (f == NULL) {
perror(s.tile.c_str());
exit(EXIT_FAILURE);
if (!clipped_to_nothing) {
json_object *json_filter = NULL;
if (filter.size() > 0) {
json_filter = parse_filter(filter.c_str());
}

while ((len = fread(buf, sizeof(char), 1000, f)) > 0) {
tile.append(std::string(buf, len));
for (auto const &s : sources) {
std::string tile;
char buf[1000];
int len;

FILE *f = fopen(s.tile.c_str(), "rb");
if (f == NULL) {
perror(s.tile.c_str());
exit(EXIT_FAILURE);
}

while ((len = fread(buf, sizeof(char), 1000, f)) > 0) {
tile.append(std::string(buf, len));
}
fclose(f);

input_tile t = s;
t.tile = std::move(tile);
its.push_back(std::move(t));
}
fclose(f);

input_tile t = s;
t.tile = std::move(tile);
its.push_back(std::move(t));
out = overzoom(its, nz, nx, ny, detail, buffer, keep, exclude, exclude_prefix, do_compress, NULL, demultiply, json_filter, preserve_input_order, attribute_accum, unidecode_data, simplification, tiny_polygon_size, bins, bin_by_id_list, accumulate_numeric, SIZE_MAX, clipbboxes);
}

std::string out = overzoom(its, nz, nx, ny, detail, buffer, keep, exclude, exclude_prefix, do_compress, NULL, demultiply, json_filter, preserve_input_order, attribute_accum, unidecode_data, simplification, tiny_polygon_size, bins, bin_by_id_list, accumulate_numeric, SIZE_MAX, clipbboxes);

FILE *f = fopen(outfile, "wb");
if (f == NULL) {
perror(outfile);
Expand Down
6 changes: 4 additions & 2 deletions platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ size_t calc_memsize();

size_t get_max_open_files();

constexpr const char *get_null_device() { return "/dev/null"; }
constexpr const char *get_null_device() {
return "/dev/null";
}

#endif // PLATFORM_HPP
#endif // PLATFORM_HPP
Binary file added tests/pbf/squirrels-13-2413-3077.pbf
Binary file not shown.
1 change: 1 addition & 0 deletions tests/pbf/squirrels-clip.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"coordinates":[[[-73.9722549,40.7904659],[-73.9755817,40.7674848],[-73.938783,40.767022],[-73.9457082,40.7791562],[-73.9628855,40.7760201],[-73.9629534,40.7914425],[-73.9722549,40.7904659]]],"crs":{"properties":{"name":"EPSG:4326"},"type":"name"},"type":"Polygon"}
2 changes: 1 addition & 1 deletion version.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef VERSION_HPP
#define VERSION_HPP

#define VERSION "v2.72.0"
#define VERSION "v2.73.0"

#endif

0 comments on commit 7165ae6

Please sign in to comment.