使用 Bokeh 或 ArcPy 标注地图

Using Bokeh or ArcPy for labeling maps

我使用 Bokeh 制作等值线图并创建其他地理图形,因为 Python 生成它的速度比我用其他方式创建它的速度快一百万倍。

现在我正在做类似的事情:

from bokeh.io import show, output_file, export_png, output_notebook, output_file, save
from bokeh.models import ColumnDataSource, HoverTool, LabelSet
from bokeh.plotting import figure
from bokeh.sampledata.us_counties import data as counties
import pandas as pd

desired_counties=['Franklin County, Ohio','Fairfield County, Ohio']
counties2 = {code: county for code, county in counties.items() if county["detailed name"] in desired_counties}

unemploymentRate = {'Location': desired_counties, 'rate': [100, 100]} #placeholders
df = pd.DataFrame(data=unemploymentRate)

county_xs = [county["lons"] for county in counties2.values()]
county_ys = [county["lats"] for county in counties2.values()]
county_names = [county['name'] for county in counties2.values()]
unemployment = [df.loc[index,'rate'] for county in counties2.values() for index, row in df.iterrows() if df.loc[index,'Location']==county['detailed name']]

source = ColumnDataSource(data=dict(x=county_xs,y=county_ys,name=county_names,rate=unemployment))

TOOLS="hover,save"
p = figure(x_axis_location=None, y_axis_location=None, plot_width=350,plot_height=250,tools=TOOLS)
p.grid.grid_line_color = None
p.outline_line_color = None
p.patches('x', 'y', source=source,
          fill_color='#007a33',
          fill_alpha=1, line_color="black", line_width=3.5)
hover = p.select_one(HoverTool)
hover.point_policy = "follow_mouse"
hover.tooltips = [
    ("Name", "@name"),
    ("Rate","@rate{0.0}%")
]
labels = LabelSet(x='x', y='y', text='name', level='glyph',
          x_offset=0, y_offset=0, source=source, render_mode='css')
p.add_layout(labels)
p.toolbar.logo = None
p.toolbar_location = None
show(p)

给出:

当我将鼠标悬停在图像上时,我看到了我想要的数据,但我更希望得到的是图像上注释的数据,而不是打印的可能性。使用 Bokeh 在其文档中提供的 LabelSet class 似乎很完美,除了它适用于 xy 图,因此当尝试在此处使用它时,它只是将标签堆叠在角落里。

问题:

  1. 有谁知道如何在不使用外部图形软件的情况下在散景等值图中添加标签?
  2. 我有 ArcGIS Pro,它显然可以创建这种类型的地图,但我正在寻找可以使用编程来创建的东西。我现在使用 Bokeh,因为我基本上可以告诉它我想要的中心县,并且我编写了代码,使我能够突出显示我想要的县及其周边县的州地图,创建 county/surrounding 县,并在这些县的失业率插图上给出了悬停数据(上面的 picture/code 是其简化版本)。有没有办法改用 AcrPy/ArcGIS 来解决这个问题?

您的标签放错位置的原因是因为您告诉他们使用数据源中的字段 'x' 和 'y',但数据源没有这些列。您可以计算每个形状的 "center",并将它们作为 x 和 y 列添加到 source 中,然后内容将按照您的需要显示。