Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support retina/HiDPI raster tiles for non-Mapbox sources #141

Closed
jonasmatthias opened this issue Apr 20, 2021 · 7 comments
Closed

Support retina/HiDPI raster tiles for non-Mapbox sources #141

jonasmatthias opened this issue Apr 20, 2021 · 7 comments

Comments

@jonasmatthias
Copy link

jonasmatthias commented Apr 20, 2021

If the tile server URL is a Maxbox URL (isMapboxURL) and the device has a HiDPI screen (Apple devices: "retina") then the image extension e.g. .png is replaced by @2x.png.

if (tileURL && !isMapboxURL(tileURL)) return tileURL;
const urlObject = parseUrl(tileURL);
const imageExtensionRe = /(\.(png|jpg)\d*)(?=$)/;
const tileURLAPIPrefixRe = /^.+\/v4\//;
// The v4 mapbox tile API supports 512x512 image tiles only when @2x
// is appended to the tile URL. If `tileSize: 512` is specified for
// a Mapbox raster source force the @2x suffix even if a non hidpi device.
const suffix = browser.devicePixelRatio >= 2 || tileSize === 512 ? '@2x' : '';
const extension = webpSupported.supported ? '.webp' : '$1';
urlObject.path = urlObject.path.replace(imageExtensionRe, `${suffix}${extension}`);

Adding @2x before the image extension seems to be the standard approach in Apple SDKs, map libraries and Web Mercator map tile services, see:

It has been discussed to add this feature to the TileJSON spec in mapbox/tilejson-spec#16
However, development on the spec seems to have halted.

I would suggest to allow the above code path for tile servers other than Mapbox either by adding an option to the Map.setStyle() method or by adding a supports2x boolean to the TileJSON.

@kylebarron
Copy link
Contributor

Why not prefix the request URL with @2x yourself and set tileSize: 512? Seems better than Maplibre changing the URL. Some servers might not support @2x in the URL

@jonasmatthias
Copy link
Author

Why not prefix the request URL with @2x yourself

There is a check for the browser.devicePixelRatio >= 2 -- on normal computer screens the normal tiles without @2x will be requested. If I hardcode the @2x all devices will load the 2x tiles.

Seems better than Maplibre changing the URL.

But Maplibre is already changing the URL for a predefined list of domains (currently only Mapbox).

Some servers might not support @2x in the URL

This should not be the default and there should be a configuration option to enable this behaviour.

@HarelM
Copy link
Collaborator

HarelM commented Jun 30, 2021

I suggest to submit a PR to resolve this. I don't think url should be altered.
You can use the transfirmRequest I guess?
In any case, a PR to show what code changes you would like to introduce is the best approach...

@github-actions
Copy link
Contributor

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the stale label Oct 16, 2021
@github-actions
Copy link
Contributor

This issue was closed because it has been stalled for 7 days with no activity.

@xabbu42
Copy link
Contributor

xabbu42 commented Nov 12, 2021

I think this would be a useful feature, moreover it makes sense to me to be as compatible as possible to leafletjs for raster layers, so I'll make a pull request adapting their api, that is an explicit {r} which gets replaced in the tile urls.

@xabbu42 xabbu42 reopened this Nov 12, 2021
xabbu42 added a commit to xabbu42/maplibre-gl-js that referenced this issue Nov 12, 2021
This feature is compatible with leafletjs and useful to fetch hidpi tiles for
layers that support it.
@github-actions github-actions bot removed the stale label Nov 13, 2021
@xabbu42
Copy link
Contributor

xabbu42 commented Nov 23, 2021

For people finding this issue, in the end we settled on {ratio} instead of {r} as that feature already existed in maplibre-gl-native.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants