Skip to content

Fix pointToTile for points just west or north of a tile edge (#45) - #54

Open
cpruijsen wants to merge 1 commit into
mapbox:mainfrom
cpruijsen:fix/issue-45
Open

cpruijsen wants to merge 1 commit into
mapbox:mainfrom
cpruijsen:fix/issue-45

Conversation

@cpruijsen

Copy link
Copy Markdown

pointToTile returns the wrong tile for points sitting on or very near a tile boundary. The
coordinates come back from pointToTileFraction as floats, and flooring them is exact only when the
division was: a longitude a hair west of an edge can compute to a fraction a hair above the integer,
so the point lands in the tile east of the one it belongs to. The reverse happens on the north edge.

This re-checks the floored tile against the edge it claims, in src/index.ts. If the longitude is
west of tile2lon for the chosen column, the column is decremented; the same for latitude against
tile2lat for the row. The comparison is done in degrees against the same functions that define the
edges, rather than by nudging the fraction, so the tile the point ends in is the one whose bounds
actually contain it.

The convention is preserved: an edge belongs to the tile east and south of it. The longitude guard is
bounded to [-180, 180) and the latitude guard to rows above 0, so the antimeridian and the top row
are left alone rather than wrapping to a tile that does not exist.

Tests cover a point just west of an edge, a point exactly on one, and the reported case.

Fixes #45

Rounding in pointToTileFraction can land a point that is just past a
tile edge exactly on the edge, so Math.floor assigns the tile on the
wrong side. Verify the floored tile actually contains the point and
step back one tile when it does not.
@ox-security

ox-security Bot commented Sep 13, 2026

Copy link
Copy Markdown

OX Security Logo

OX Security reviewed this pull request — nothing to fix.

No issues found

Branch fix/issue-45main

View scan in OX Security →

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong tile is returned for edge cases

1 participant