Converting from WGS84 to EPSG:27700 raster tiles without drawing a map

1k views Asked by At

Using this example from OS Data Hub - https://labs.os.uk/public/os-data-hub-examples/os-maps-api/zxy-27700-basic-map

I can get a list of tiles displayed on the map, I would like to get the coordinates of the tile without drawing the map.

Starting from a single point in WGS84 (lat/long) I can convert this to EPSG:27700 using Proj4js

var source = new proj4.Proj('EPSG:4326');

proj4.defs("EPSG:27700","+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs");

var dest = new proj4.Proj('EPSG:27700');

var coords=proj4.transform(source, dest, [X,Y]);

I then need to translate this into coordinates for the raster tile, which is done in the leaflet example with this code:

var crs = new L.Proj.CRS('EPSG:27700', '+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs', {
    resolutions: [ 896.0, 448.0, 224.0, 112.0, 56.0, 28.0, 14.0, 7.0, 3.5, 1.75 ],
    origin: [ -238375.0, 1376256.0 ]
});

How can i replicate this step to produce the tile coordinates, without having to draw the leaflet map?

I ultimately want to use the coordinates to grab & save a single tile from the OS Data Hub with this format:

https://api.os.uk/maps/raster/v1/zxy/layer/%7Bz%7D/%7Bx%7D/%7By%7D.png?key=

1

There are 1 answers

0
Mike On

Using the EPSG:27700 coords calculated using proj4, and the zoom level resolutions (which are meters per pixel) and tile grid origin coordinates used in the definition you can calculate the {x} and {y} values in https://api.os.uk/maps/raster/v1/zxy/layer/{z}/{x}/{y}.png?key= for any zoom level {z} based on the standard tile size of 256 pixels as

x = Math.floor((coords[0] - origin[0]) / (resolutions[z] * 256));

y = Math.floor((origin[1] - coords[1]) / (resolutions[z] * 256));