From 3bdcc3ca908c5b70a2bd8666a2963a9c39fee3f3 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Wed, 15 Oct 2014 17:03:30 -0700 Subject: [PATCH] Heavy-handedly fix clipping errors at the edges of z0 and z1 tiles --- geojson.c | 27 +++++++++++++++++++++++++-- tile.cc | 12 ++++++------ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/geojson.c b/geojson.c index 0664f42..827e7b0 100644 --- a/geojson.c +++ b/geojson.c @@ -61,8 +61,31 @@ void latlon2tile(double lat, double lon, int zoom, unsigned int *x, unsigned int double lat_rad = lat * M_PI / 180; unsigned long long n = 1LL << zoom; - *x = n * ((lon + 180) / 360); - *y = n * (1 - (log(tan(lat_rad) + 1/cos(lat_rad)) / M_PI)) / 2; + long long llx = n * ((lon + 180) / 360); + long long lly = n * (1 - (log(tan(lat_rad) + 1/cos(lat_rad)) / M_PI)) / 2; + + if (lat >= 85.0511) { + lly = 0; + } + if (lat <= -85.0511) { + lly = n - 1; + } + + if (llx < 0) { + llx = 0; + } + if (lly < 0) { + lly = 0; + } + if (llx >= n) { + llx = n - 1; + } + if (lly >= n) { + lly = n - 1; + } + + *x = llx; + *y = lly; } // http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames diff --git a/tile.cc b/tile.cc index 85bebf1..e281d67 100644 --- a/tile.cc +++ b/tile.cc @@ -373,8 +373,8 @@ void douglas_peucker(drawvec &geom, int start, int n, double e) { } } -static bool inside(draw d, int edge, int area) { - int clip_buffer = area / 64; +static bool inside(draw d, int edge, long long area) { + long long clip_buffer = area / 64; switch (edge) { case 0: // top @@ -408,8 +408,8 @@ static draw get_line_intersection(draw p0, draw p1, draw p2, draw p3) { return draw(VT_LINETO, p0.x + (t * s1_x), p0.y + (t * s1_y)); } -static draw intersect(draw a, draw b, int edge, int area) { - int clip_buffer = area / 64; +static draw intersect(draw a, draw b, int edge, long long area) { + long long clip_buffer = area / 64; switch (edge) { case 0: // top @@ -437,9 +437,9 @@ static draw intersect(draw a, draw b, int edge, int area) { static drawvec clip_poly1(drawvec &geom, int z, int detail) { drawvec out = geom; - unsigned area = 0xFFFFFFFF; + long long area = 0xFFFFFFFF; if (z != 0) { - area = 1 << (32 - z); + area = 1LL << (32 - z); } for (int edge = 0; edge < 4; edge++) {