GeoPandas标签多边形 [英] GeoPandas Label Polygons

查看:192
本文介绍了GeoPandas标签多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出可用的形状文件此处:我会喜欢在地图上标记每个多边形(县). GeoPandas可以做到这一点吗?

Given the shape file available here: I'd like to label each polygon (county) in the map. Is this possible with 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个对象组成的系列.您可以通过检查来验证

c['geometry'] is a series comprised of shapely.geometry.polygon.Polygon objects. You can verify this by checking

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

整形文档中,有一种方法

From the Shapely docs there is a method representative_point() that

返回一个廉价计算的点,该点保证在 几何对象.

Returns a cheaply computed point that is guaranteed to be within the geometric object.

最适合需要标记多边形对象的情况的声音!然后,您可以像这样为geopandas dataframe'coords'创建新列

Sounds ideal for a situation in which you need to label the polygon objects! You can then create a new column for your geopandas dataframe, 'coords' like so

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

现在,您拥有与每个多边形对象(每个县)相关的一组坐标,您可以通过遍历数据框来注释绘图

Now that you have a set of coordinates pertaining to each polygon object (each county), you can annotate your plot by iterating through your dataframe

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

这篇关于GeoPandas标签多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆