Git Product home page Git Product logo

maplibregl-mapbox-request-transformer's Introduction

This library provides a request transforming function enabling the consumption of MapboxGL Styles in MapLibreGL.

By default Mapbox styles are referenced by, and include others to mapbox URLs such as mapbox://styles/mapbox/satellite-streets-v11, however MapLibreGL, since version 2, does not know how to handle these references. This library parsers the mapbox URL and converts into a valid http urls usable by MapLibreGL.

Also compatible with react-map-gl.

Install

npm install maplibregl-mapbox-request-transformer --save

Usage

import { isMapboxURL, transformMapboxUrl } from 'maplibregl-mapbox-request-transformer'

const mapboxKey = 'pk.123'

const transformRequest = (url: string, resourceType: string) => {
  if (isMapboxURL(url)) {
    return transformMapboxUrl(url, resourceType, mapboxKey)
  }
  
  // Do any other transforms you want
  return {url}
}

var map = new maplibregl.Map({
  container: 'map',
  center: [-122.420679, 37.772537],
  zoom: 13,
  style: 'mapbox://styles/mapbox/satellite-streets-v11',
  transformRequest
});

Notes on Mapbox Pricing

When upgrading from MapLibre v1 to >= v2 or react-map-gl, and using this transformer, be advised that Mapbox bills tile requests using their Vector Tiles API requests. A standard Mapbox GL JS application is billed using the Map Loads API, which includes unlimited Vector Tiles API requests (see pricing docs). This billing difference could make hosting an often-visited map more expensive. As of Feb 2023 Mapbox offers 200,000 monthly tile requests on their free tier (see their pricing page for current details).

Acknowledgements

This code was mostly taken and adpted from here.

maplibregl-mapbox-request-transformer's People

Contributors

rowanwins avatar th0rgall avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

maplibregl-mapbox-request-transformer's Issues

`resourceType` can be `undefined`, leading to issues with forwarding `transformRequest` to `transformMapboxUrl`

Where we use this plugin, I've changed the code from the README to this:

      const transformRequest = (url: string, resourceType?: MapLibreResourceType) => {
        if (isMapboxURL(url)) {
          return transformMapboxUrl(url, resourceType ?? MapLibreResourceType.Unknown, accessToken)
        }
        return {url}
      }
  1. resourceType can be typed more strongly (ResourceType is exported by MapLibre). This should be fixed in the README.
  2. resourceType can be undefined because maplibre types it as ?: ResourceType (why?!), but transformMapboxUrl does not allow this. This should be fixed in this project (probably?).

Both of these problems were reported by the TypeScript compiler.

I'm also reporting this with maplibre, the original typing is here:
https://github.com/maplibre/maplibre-gl-js/blame/main/src/util/request_manager.ts#L21

Mapbox maps v2

Some of Mapbox's newer layers, such as their updated Terrain DEM model, require transitioning to their v2 billing system. It seems like, from a technical perspective, this would be a simple thing to implement by reproducing the Mapbox code to create a "sku" token at regular intervals, as is done in Mapbox GL. Is your sense that implementing this in/alongside this library would violate Mapbox's terms?

Error on usage

I receive this error while drawing the map:

Error: layers[35].filter[3][1][0]: Unknown expression "pitch". If you wanted a literal array, use ["literal", [...]].

The first part changes, but it's always highlighting the 'Unknown expression "pitch"'.

install command is incorrect in npmjs page

It's not a issue, I just want to point out that there is an error with the install command

install command in npmjs readme page
npm install maplibregl-mapbox-url-transformer --save

I think it should be like install button
npm i maplibregl-mapbox-request-transformer

How to reference this repository?

Hi Rowan,

I have modified the functions slightly to be used in my opensource project to support legacy mapbox styles with the maplibre 2+ library.

GEOLYTIX/xyz#660

How would you like us to attribute your work?

I have for now added a link in a comment with transform method.

Mapbox maps with a geo-referenced transparent-background image shows with black background

This issue describes a maplibre/mapbox interoperability issue which may be associated with missing functionality in this plugin.

Geo-referencing a GeoTiff image into a mapbox map results in black background in the image transparent areas, instead of showing the map layer under

Note: The tests in this issue use the mapbox style mapbox://styles/mapme1/clbmiaqhu000a14s6w4wqmoij
It is a custom style produced by loading a GeoTiff image in mapbox studio. The image has transparent background.
The flow: load image, create tileset, add as a layer on top of a standard mapbox map.

I created https://jsfiddle.net/tivoni/k60nr18x which loads the style with mapblibregl-js v2.4.0.
Note the black tiles background:

Screenshot 2022-12-16 at 10 55 58 PM

The same style loaded with the older mapblibregl-js v1.15.2 takes into account the image transparent background and shows the map tiles under the image layer:

https://jsfiddle.net/tivoni/k60nr18x/6/
Screenshot 2022-12-16 at 11 04 07 PM

The difference between the jsFiddles is the maplibregl versions and the usage of this plugin (only with maplibre v2.4.0)

Investigation

The requested mapbox style points to this .json resource which includes:

"tiles": [
  "https://a.tiles.mapbox.com/v4/mapme1.9x3arx2f/{z}/{x}/{y}.jpg?access_token=..."

Note the .jpg extension in the url pattern. Since JPEG does not support transparency, the request for such tiles seems incorrect.

To verify this assumption I inspected devtools network panel:

maplibre v1.15.2 requests WEBP images - the tile preview shows transparent background:
Screenshot 2022-12-16 at 11 14 27 PM

In comparison, maplibre v2.4.0 requests JPEG images - the tile preview shows black background!
Screenshot 2022-12-16 at 11 16 10 PM

Possible solutions:

  1. The mapbox json tile url pattern needs to be updated to something like:
    https://a.tiles.mapbox.com/v4/mapme1.9x3arx2f/{z}/{x}/{y}@2x.webp?access_token=...
  • Is this a configurable setting in mapbox studio? (or: is this a question to mapbox support staff?)
  1. Use transformRequest to catch and modify all .jpg tiles requests to *.tiles.mapbox.com:
import {isMapboxURL, transformMapboxUrl} from 'maplibregl-mapbox-request-transformer'

const map = new mapLibreGL.Map({
        ...
        transformRequest: (url, resourceType) => {
            if (isMapboxURL(url)) {
                return transformMapboxUrl(url, resourceType, accessToken)
            } else {
                const regEx =
                    /^https:\/\/[0-9a-zA-Z]+\.tiles\.mapbox\.com\/v[0-9]\/[0-9a-zA-Z]+\.[0-9a-zA-Z]+\/\d+\/\d+\/\d+\.jpg\?/
                if (regEx.test(url)) {
                    url = url.replace('.jpg', '@2x.webp')
                }
                return {url}
            }
        }

Note: the above code is confirmed to: (A) fetch the webp tiles and (B) display the layer under transparent part of the image correctly. Meaning, no black background appears and the map tiles renders under the transparent areas as expected.

  1. Integrate (2) into the maplibregl-mapbox-request-transformer plugin

@rowanwins your insights/advice is appreciated ๐Ÿ™๐Ÿผ

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.