I am trying to create a basic image viewer using OpenLayers. I have a large image that I have split into image tiles of the Google Maps format where tiles are stored in a set of numbered directories, one for each zoom level. e.g.
Directory structure:
/0
/0
0.jpg
/1
/0
0.jpg
1.jpg
/1
0.jpg
1.jpg
I am using flask and having done some research, I am using the following code to initialise the map for an image with zoom levels 0-4:
// the image height and width values
var imgWidth = x;
var imgHeight = u;
var imgCenter = [imgWidth / 2, -imgHeight / 2];
var zoomMin = 0;
var zoomMax = 4;
var resolutions = [];
for (var i = zoomMax; i >= zoomMin; --i) {
resolutions.push(Math.pow(2, i));
}
var extent = [0, -imgHeight, imgWidth, 0];
var view = new ol.View({
center: ol.extent.getCenter(extent),
zoom: zoomMin,
resolutions: resolutions
})
var source = new ol.source.TileImage({
url: "tiles/{z}/{x}/{y}",
tileGrid: new ol.tilegrid.TileGrid({
resolutions: resolutions,
origin: [0, 0]
})
});
var layer = new ol.layer.Tile({
source: source,
extent: extent
})
var map = new ol.Map({
target: 'map',
layers: [layer],
view: view
});
To serve the image tiles I am using the following flask view which is retrieving the z/x/y image tile from the backend:
@app.route('/tiles')
@app.route('/tiles/<int:z>/<int:x>/<int:y>', methods=['GET'])
def tiles(z,x,y):
file = ....
# retrieve the file with path of z/x/y.jpg
return file
Running this, the first image with tile 0/0/0.jpg is displayed correctly. However, zooming in, the tiles are not in the correct order and I am getting some 404 errors for tiles that do not exist. Is there an issue with the way I am handling the tile URL? Any help would be greatly appreciated!