I have a tile-server and a small image in the xyz folders structure, an image that covers only a portion of the world. I have already managed to prevent the loading of Z, of some zoom levels that are not present on that map (Stop loading a tile in OpenLayers 3 tileloadstart event). Now I need to limit the loading for X and Y as well, because many folders and tiles are obviously absent. How can this be done? Could I manage some sort of error thrown on the server side that makes OpenLayers understand not to request those tiles anymore because they are absent? If so, how could this be done?
You can further restrict a tile grid by specifying an extent. You will need to do that using ol.tilegrid.TileGrid
although you can get the default origin and resolutions to use from ol.tilegrid.createXYZ
var defaultTileGrid = ol.tilegrid.createXYZ({
minZoom: 2,
maxZoom: 8
});
var restrictedTileGrid = new ol.tilegrid.TileGrid({
extent: restrictedExtent,
minZoom: defaultTileGrid.getMinZoom(),
origin: defaultTileGrid.getOrigin(0),
resolutions: defaultTileGrid.getResolutions(),
});
You can also specify an extent on a layer (but that must be in the view projection, which can be different to the tilegrid projection).
If you know the minimum and maximum X and Y values at the maximum Z value you could calculate the restricted extent as
var restrictedExtent = ol.extent.extend(
defaultTileGrid.getTileCoordExtent([maxZ, minX, minY]),
defaultTileGrid.getTileCoordExtent([maxZ, maxX, maxY])
);
in OpenLayers 6
or
var restrictedExtent = ol.extent.extend(
defaultTileGrid.getTileCoordExtent([maxZ, minX, -minY - 1]),
defaultTileGrid.getTileCoordExtent([maxZ, maxX, -maxY - 1])
);
in earlier versions