簡體   English   中英

GeoPandas 標簽多邊形

[英]GeoPandas Label Polygons

鑒於此處可用的形狀文件:我想標記地圖中的每個多邊形(縣)。 GeoPandas 可以做到這一點嗎?

import geopandas as gpd
import matplotlib.pyplot as plt
%matplotlib inline

shpfile=<Path to unzipped .shp file referenced and linked above>
c=gpd.read_file(shpfile)
c=c.loc[c['GEOID'].isin(['26161','26093','26049','26091','26075','26125','26163','26099','26115','26065'])]
c.plot()

提前致謝!

c['geometry']是由shapely.geometry.polygon.Polygon對象組成的系列。 您可以通過檢查來驗證這一點

In [23]: type(c.ix[23, 'geometry'])
Out[23]: shapely.geometry.polygon.Polygon

Shapely docs有一個方法representative_point()

返回保證在幾何對象內的廉價計算點。

聽起來非常適合需要標記多邊形對象的情況! 然后,您可以為您的geopandas dataframe geopandas創建一個新列,像這樣'coords'

c['coords'] = c['geometry'].apply(lambda x: x.representative_point().coords[:])
c['coords'] = [coords[0] for coords in c['coords']]

現在您有一組與每個多邊形對象(每個縣)有關的坐標,您可以通過遍歷數據框來注釋您的繪圖

c.plot()
for idx, row in c.iterrows():
    plt.annotate(s=row['NAME'], xy=row['coords'],
                 horizontalalignment='center')

在此處輸入圖片說明

無需循環,以下是使用 apply 進行注釋的方法:

ax = df.plot()
df.apply(lambda x: ax.annotate(text=x.NAME, xy=x.geometry.centroid.coords[0], ha='center'), axis=1);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM