HomeHome ArchiveArchive

Serving Non-mercator data in a Web Mercator grid

The Web Mercator grid as used by OSM, Google, etc. is the de-facto standard for serving web maps. But not many people maintain data in a Web Mercator reference system. People usually use either a locally optimized SRS or store their data in WGS84.

For publishing, data has to be transformed to Web Mercator. If the result is good enough for your purpose, you can take this route. You loose the possibility to use base maps served in your local tile grid, but you can use OSM or other base maps.

If you don’t want to retransform your data to Web Mercator, but still use tools like Mapbox GL JS, which are only supporting Mercator, there is a way to do that. I call this method “Fake Mercator”. You handle the coordinates in your local reference system as if they were Web Mercator coordinates. At a first glance, this looks like a terrible hack, but after using this method in some projects, I think there are legitimate uses of this approach. We’re just using a display grid in an other projection, but as long as we stay in our area of interest, why should we care?

Assuming your data is stored in a PostGIS database, there is simple way to convert them to Web Mercator:

SELECT UpdateGeometrySRID('public', 'mytable', 'wkb_geometry', 3857);

What happens with my country doing that?

mercator-shift

Everything is shifted to the same coordinates but now in the Web Mercator grid. We still have the original projection, only placed on an other part on the Web Mercator world. From then on you can use all tools specialized on Web Mercator with loosing only one thing: you can’t use other base maps served as tiles. What you can do, is using WMS services or raster images with the same shift applied. And you have to take care when displaying WGS84 coordinates that you calculate them from the original projection of the data.

If you don’t want to change the SRS of your data permanently, you can also apply ST_SetSRID on-the-fly. In case of MVT vector tiles, t-rex does that for you by passing the option no-transform=true. It then handles the correct (non-)projection of request BBOX coordinates and calculated extends.

t_rex serve --datasource ch_epsg_2056.gpkg --no-transform true

mercator-shift

You’ll see your data in their original projection on a Web Mercator grid. You’ll only notice that t-rex adds the original coordinates when generating a configuration file:

[[tileset]]
name = "mytable"
# Real extent: [5.96438, 45.81937, 10.55886, 47.77210]
extent = [22.32683, 9.61388, 25.45680, 11.56229]

If you don’t pass no-transform, t-rex will automatically transform your data to Web Mercator. Alternatively you can define a user grid with your data’s reference system, but then you have to use this grid in your map viewer (e.g. OpenLayers) as well.

To sum this post up, there are many possibilities to serve your data in a tile grid and sometimes it makes sense to serve your data without reprojection in a Web Mercator grid.

Pirmin Kalberer (@implgeo)