- if ( dlon >= 0 ) {
- lon = (int)( (int)(lon / span) * span);
- } else {
- // cout << " lon = " << lon
- // << " tmp = " << (int)((lon-1) / span) << endl;
- lon = (int)( (int)((lon + 1) / span) * span - span);
- if ( lon < -180 ) {
- lon = -180;
- }
- }
+ /* We have one or more degrees per tile,
+ * so we need to find the base longitude
+ * of that tile.
+ *
+ * First we calculate the integral base longitude
+ * (e.g. -85.5 => -86) and then find the greatest
+ * multiple of span that is less than or equal to
+ * that longitude.
+ *
+ * That way, the Greenwich Meridian is always
+ * a tile border.
+ *
+ * This gets us into trouble with the polar caps,
+ * which have width 360 and thus either span
+ * the range from 0 to 360 or from -360 to 0
+ * degrees, depending on whether lon is positive
+ * or negative!
+ *
+ * We also get into trouble with the 8 degree tiles
+ * north of 88N and south of 88S, because the west-
+ * and east-most tiles in that range will cover 184W
+ * to 176W and 176E to 184E respectively, with their
+ * center at 180E/W!
+ */
+ lon=(int)floor(floor((lon+SG_EPSILON)/span)*span);
+ /* Correct the polar cap issue */
+ if ( lon < -180 ) {
+ lon = -180;
+ }