如何映射数组来为填充分区统计图着色?

发布于 2025-01-11 21:01:27 字数 531 浏览 0 评论 0原文

我正在尝试手动将包含 6 个条目的 geojson 填充层映射到颜色数组。我尝试了以下方法,但它不起作用。

基本上希望颜色是countrypalette[id]

我尝试了countrypalette["get", "id"],但这也不起作用。


const countrypalette = ["#f0d27e", "#789d23", "#c06e51", "#b3e467", "#84241a", "#d9c0c7"]

 map.addLayer(
            {
              id: 'Mekong_countries',
              type: 'fill',
              source: 'Mekong_countries',
              paint: {
                "fill-color": ["at", "id", ["array", countrypalette]]
              }
            }
          )

I am trying to manually map a geojson fill layer with 6 entries to an array of colors. I tried the following but it doesn't work.

Basically was hoping that the colour be countrypalette[id]

I tried countrypalette["get", "id"], but that doesn't work either.


const countrypalette = ["#f0d27e", "#789d23", "#c06e51", "#b3e467", "#84241a", "#d9c0c7"]

 map.addLayer(
            {
              id: 'Mekong_countries',
              type: 'fill',
              source: 'Mekong_countries',
              paint: {
                "fill-color": ["at", "id", ["array", countrypalette]]
              }
            }
          )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小猫一只 2025-01-18 21:01:27

您缺少 id 的属性查找(我猜这是您功能的整数属性)。第二件事有点奇怪,似乎我们需要一个 to-color 转换函数才能完成这项工作:

["to-color", ["at", ["get", "id"], ["literal", COUNTRY_PALETTE]]]

在您的代码中:

const COUNTRY_PALETTE = ["#f0d27e", "#789d23", "#c06e51", "#b3e467", "#84241a", "#d9c0c7"]

map.addLayer({
    id: 'Mekong_countries',
    type: 'fill',
    source: 'Mekong_countries',
    paint: {
        "fill-color": [
              "to-color", ["at", ["get", "id"], ["literal", COUNTRY_PALETTE]]
        ]
    }
});

似乎添加了 to-color code> 是强制性的,否则 Mapbox 会抱怨类型错误。 此问题中描述了该错误。

You are missing a property lookup for your id (I guess it's an integer property of your features). The second thing is a bit odd, it seems like we need to at a to-color conversion function to make this work:

["to-color", ["at", ["get", "id"], ["literal", COUNTRY_PALETTE]]]

In your code:

const COUNTRY_PALETTE = ["#f0d27e", "#789d23", "#c06e51", "#b3e467", "#84241a", "#d9c0c7"]

map.addLayer({
    id: 'Mekong_countries',
    type: 'fill',
    source: 'Mekong_countries',
    paint: {
        "fill-color": [
              "to-color", ["at", ["get", "id"], ["literal", COUNTRY_PALETTE]]
        ]
    }
});

It seems like adding the to-color is mandatory, otherwise mapbox complains about a type error. The bug is described in this issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文