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

Bug: literal expression cannot parse ["get", ..] #545

Open
IKondor opened this issue Feb 19, 2024 · 2 comments
Open

Bug: literal expression cannot parse ["get", ..] #545

IKondor opened this issue Feb 19, 2024 · 2 comments
Labels
bug Something isn't working PR is more than welcome

Comments

@IKondor
Copy link

IKondor commented Feb 19, 2024

Describe the bug

Trying to move text like this style and have error with this expression

"text-offset": [
  "array",
  "number",
  2,
  [
    "literal",
    [
      ["number", ["get", "label_x"]],
      ["number", ["get", "label_y"]]
    ]
  ]
]

There is html example
https://github.com/IKondor/temp

To Reproduce

  • download https://github.com/IKondor/temp/blob/main/temp.html > temp.html
  • uncomment one of example which started at line 94
  • run temp.html via browser
  • have console Error: layers[2].layout.text-offset: Expected array<number, 2> but found array<value, 2> instead. and no map loaded

Expected behavior
No console errors

Additional context
So I looked at the sources and discovered the intended location which have some errors

src/expression/definitions/assertion.ts line 88, checkSubtype call && typeOf call
checkSubtype from src/expression/types.ts
typeOf from src/expression/values.ts

The bug is

typeOf([
      ["number", ["get", "label_x"]],
      ["number", ["get", "label_y"]]
    ])

have itemType array<value, 2> type, but I have never way to cast it to <number, 2>

Even if I add the numbers cast as in the example above, its won't work

May be there is another way?

Screenshots
Снимок экрана от 2024-02-19 19-00-54
Снимок экрана от 2024-02-19 19-01-03
Снимок экрана от 2024-02-19 19-03-24
Снимок экрана от 2024-02-19 19-03-32

Desktop (please complete the following information):

  • OS: Ubuntu 23.10, core 6.5.0
  • Browser: Chome
  • Version 120
@IKondor IKondor added the bug Something isn't working label Feb 19, 2024
@HarelM
Copy link
Collaborator

HarelM commented Feb 20, 2024

I think this is the same as:
mapbox/mapbox-gl-js#6155
It's probably just not implemented, or not implemented correctly.

The following is working, but according to the linked thread I think it only works for geojson and not vector tiles, but you can try and test that out:
The difference is that I created a propery to hold an array of two points instead of two different properties.
This is also somewhat relevant:
https://stackoverflow.com/questions/52715411/mapbox-gl-data-driven-styling-for-text-offset

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Test</title>
    <meta property="og:description" content="Use extrusions to display buildings' height in 3D." />
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/maplibre-gl.css" />
    <script src="https://unpkg.com/[email protected]/dist/maplibre-gl.js"></script>
    <style>
      body {
        margin: 0;
        padding: 0;
      }

      #map {
        position: absolute;
        height: 100vh;
        width: 100%;
        background: #202020;
      }
    </style>
  </head>

  <body>
    <div id="map"></div>
    <script>
      const map = new maplibregl.Map({
        style: {
          version: 8,
          name: "Raster Tiles",
          glyphs:
            "https://smellman.github.io/creating_tiles_with_global_map_support_files/2015/mapbox_vector_tile_demo/demosites/fonts/{fontstack}/{range}.pbf",
          sprite:
            "https://smellman.github.io/creating_tiles_with_global_map_support_files/2015/mapbox_vector_tile_demo/demosites/maki-sprites/sprite",
          sources: {
            osm: {
              type: "raster",
              tiles: [
                "https://a.tile.openstreetmap.org/{z}/{x}/{y}.png",
                "https://b.tile.openstreetmap.org/{z}/{x}/{y}.png",
                "https://c.tile.openstreetmap.org/{z}/{x}/{y}.png"
              ],
              tileSize: 256
            },
            power_stations: {
              type: "geojson",
              data: {
                type: "FeatureCollection",
                features: [
                  {
                    type: "Feature",
                    geometry: {
                      type: "Point",
                      coordinates: [20, 35]
                    },
                    properties: {
                      label_x: 5,
                      label_y: 5,
                      description: "test",
                      label_x_y: [5, 5]
                    }
                  }
                ]
              }
            }
          },
          layers: [
            {
              id: "osm",
              type: "raster",
              source: "osm",
              paint: {
                "raster-fade-duration": 100
              }
            },
            {
              id: "point",
              type: "circle",
              source: "power_stations",
              paint: {
                "circle-radius": 10,
                "circle-color": "#3887be"
              }
            },
            {
              id: "poi-labels",
              type: "symbol",
              source: "power_stations",
              layout: {
                "text-offset": ["get", "label_x_y"],
                "text-field": ["concat", "x=", ["get", "label_x"], ", ", "y=", ["get", "label_y"]]
              }
            }
          ]
        },
        zoom: 5,
        center: [20, 35],
        container: "map",
        antialias: true,
        hash: true
      });

      map.on("load", () => {
        console.log(map);
      });
    </script>
  </body>
</html>

@IKondor
Copy link
Author

IKondor commented Feb 21, 2024

Thanks! That solved my issue

Maybe I misunderstand this line in specs

If an expression accepts an array argument and the user supplies an array literal, that array must be wrapped in a literal expression

https://maplibre.org/maplibre-style-spec/expressions/#type-system

Also this expression is also incorrectly parsed, I think in same reason

[
  "literal",
  [
    ["+", 10, 10],
    ["+", 10, 10]
  ]
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working PR is more than welcome
Projects
None yet
Development

No branches or pull requests

2 participants