Skip to content

Commit

Permalink
Fix bug calculating image source tile coordinates
Browse files Browse the repository at this point in the history
Closes #4550

Using `Math.round` on the `centerCoord` value here causes a coordinate
like `{zoom: 2, column: 1.5, row: 1.5}` to become
`{zoom: 2, column: 2, row: 2}`, which is out of bounds for a z2 tile;
the correct tile for containing those coordinates is `{zoom: 2, column: 1, row: 1}`
  • Loading branch information
Anand Thakker committed Apr 6, 2017
1 parent ec8e4ee commit 20b2958
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/source/image_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,28 @@ class ImageSource extends Evented {
// may be outside the tile, because raster tiles aren't clipped when rendering.

const map = this.map;

// transform the geo coordinates into (zoom 0) tile space coordinates
const cornerZ0Coords = coordinates.map((coord) => {
return map.transform.locationCoordinate(LngLat.convert(coord)).zoomTo(0);
});

// Compute the coordinates of the tile we'll use to hold this image's
// render data
const centerCoord = this.centerCoord = util.getCoordinatesCenter(cornerZ0Coords);
centerCoord.column = Math.round(centerCoord.column);
centerCoord.row = Math.round(centerCoord.row);
// `column` and `row` may be fractional; round them down so that they
// represent integer tile coordinates
centerCoord.column = Math.floor(centerCoord.column);
centerCoord.row = Math.floor(centerCoord.row);
this.coord = new TileCoord(centerCoord.zoom, centerCoord.column, centerCoord.row);

// Constrain min/max zoom to our tile's zoom level in order to force
// SourceCache to request this tile (no matter what the map's zoom
// level)
this.minzoom = this.maxzoom = centerCoord.zoom;
this.coord = new TileCoord(centerCoord.zoom, centerCoord.column, centerCoord.row);

// Transform the corner coordinates into the coordinate space of our
// tile.
this._tileCoords = cornerZ0Coords.map((coord) => {
const zoomedCoord = coord.zoomTo(centerCoord.zoom);
return new Point(
Expand Down

0 comments on commit 20b2958

Please sign in to comment.