首页 > 解决方案 > Python 中的 Arcgis:每次执行时颜色变化。如何设置填充颜色?

问题描述

我想在地图中用特定颜色绘制一个多边形。阅读以下文档后: https ://developers.arcgis.com/python/api-reference/arcgis.geometry.html ,我无法选择正确的填充颜色。事实上,使用相同的代码,每次执行时颜色都会发生变化。

这是一些示例代码:

import arcgis
from arcgis.gis import GIS
from arcgis.geometry import Geometry, Point, Polyline
from arcgis.mapping import create_symbol

gis = GIS()
map1 = gis.map(location=(39.456727, -0.352371), zoomlevel=15)
map1.basemap = "osm"


geom = Geometry({'spatialReference': {'latestWkid': 4326,
    'wkid': 4326},
    'rings': [
    [
            [-0.3524, 39.4566],
            [-0.3524, 39.4568],
            [-0.3522, 39.4568],
            [-0.3522, 39.4566],
            [-0.3524, 39.4566],
        ]
    ]
})
symbol = create_symbol(geometry_type='polygon', colors=[0, 255, 0])  # green

map1.draw(geom, symbol=symbol)
map1.export_to_html("test.html")

如果我执行此操作并test.html使用浏览器打开,我可以看到: 在此处输入图像描述

如果我再次执行相同的代码并打开test.html,颜色会神奇地改变: 在此处输入图像描述

所以,有两个问题:1)为什么每次执行时颜色都会改变?2)如何设置正确的颜色?我将颜色设置为,[0, 255, 0]但它不起作用

标签: pythoncolorsarcgis

解决方案


为了正确设置区域和点的颜色,我发现您可以在colors带有 alpha 通道的参数中添加第四个参数:

symbol = create_symbol(geometry_type='polygon', colors=[0, 255, 0, 255])  # green

根据https://developers.arcgis.com/python/api-reference/arcgis.mapping.html中的文档, alpha 值应该在 0-1 范围内,但这应该是一个错误,因为它只有在你将值置于 0-255 范围内。

如果不添加 alpha 通道,colors则参数不被视为有效,并为其分配一个随机值。这就是每次执行时颜色都会发生变化的原因


推荐阅读