Geopandas用其ID绘制点

2024-09-26 18:06:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在地图上画一个点列表,旁边每个点的id,这就是我正在做的

dfPoints["id"] = dfPoints.index
geomertrySensores2 = [Point(xy) for xy in zip(dfPoints['longitud'], dfPoints['latitud'])]
crs =  {'int':'epsg:4326'}
geoSensores = gpd.GeoDataFrame(dfPoints,crs=crs, geometry = geomertrySensores2)

# creating color map for categories
categories = np.unique(geoSensores["id"])
colors = np.linspace(0, 1, len(categories))
colordict = dict(zip(categories, colors))
geoSensores["Color"] = geoSensores["id"].apply(lambda x: colordict[x])


f, ax = pl.subplots(figsize=(20,15))

geoBase.plot(color = 'grey', alpha=0.4, ax = ax)
geoSensores.plot(ax=ax, markersize=20, color="blue", marker="o", column='id')

但我无法在地图上添加每个点的id 怎么做呢? 我有255个点,所以我希望在它旁边绘制每个点的id,而不是使用调色板


Tags: idfornp地图zipaxcolorcategories
1条回答
网友
1楼 · 发布于 2024-09-26 18:06:39

我使用matplotlib解决了这个问题:

f, ax = pl.subplots(figsize=(120,90))

geoVias.plot(color = 'grey', alpha=0.4, ax = ax)
geoSensores.plot(ax=ax, markersize=20, color="blue", marker="o", column='id')

for  index, row in geoSensores.iterrows():
        x = row.geometry.centroid.x
        y = row.geometry.centroid.y
        pl.text(x, y, row.id, fontsize=10)

但是地图非常饱和,所以我使用了允许缩放的bokeh

p = figure(title="Ubicacion de sensores",
           plot_height = 700 ,
           plot_width = 900, )

def getPointCoords(row, geom, coord_type):
    """Calculates coordinates ('x' or 'y') of a Point geometry"""
    if coord_type == 'x':
        return row[geom].x
    elif coord_type == 'y':
        return row[geom].y
    
ubicacionSensores['x'] = ubicacionSensores.apply(getPointCoords, geom='geometry', coord_type='x', axis=1)
ubicacionSensores['y'] = ubicacionSensores.apply(getPointCoords, geom='geometry', coord_type='y', axis=1)
p_df = geoSensores.drop('geometry', axis=1).copy()   
psource = ColumnDataSource(p_df)   
p.circle('x', 'y', source=psource, color='red', size=7)

my_hover = HoverTool()
my_hover.tooltips = [('Sensor id', '@id')]
p.add_tools(my_hover)
    

def getLineCoords(row, geom, coord_type):
    """Returns a list of coordinates ('x' or 'y') of a LineString geometry"""
    if coord_type == 'x':
        return list( row[geom].coords.xy[0] )
    elif coord_type == 'y':
        return list( row[geom].coords.xy[1] )


geoVias['x'] = geoVias.apply(getLineCoords, geom='geometry', coord_type='x', axis=1)
geoVias['y'] = geoVias.apply(getLineCoords, geom='geometry', coord_type='y', axis=1)
m_df = geoVias.drop('geometry', axis=1).copy()
msource = ColumnDataSource(m_df)
p.multi_line('x', 'y', source=msource, color='grey', alpha=0.4, line_width=1)

show(p)

相关问题 更多 >

    热门问题