【发布时间】:2020-12-10 15:05:41
【问题描述】:
我想使用 geopandas GeoSeries.distance(self, other) 函数计算从马尼拉到菲律宾城市的距离。
步骤:
# So I start with the dataset, which should produce a geopandas dataframe consisting basically of cities and a polygon of its boundaries in latlong.
url = 'https://raw.githubusercontent.com/macoymejia/geojsonph/master/MuniCities/MuniCities.minimal.json'
df1 = gpd.read_file(url)
# then I define a centroid column
df1['Centroid'] = df1.geometry.centroid
# then I define Manila location as a shapely point geometry, which produces a DataFrame with point geometry and address as columns
manila_loc = gpd.tools.geocode('Manila')
# then I try to calculate the distance
df1.Centroid.distance(manila_loc.geometry)
但我收到此错误:
AttributeError Traceback (most recent call last)
<ipython-input-30-76585915942f> in <module>
----> 1 df1.Centroid.distance(manila_loc.geometry)
~/opt/anaconda3/envs/Coursera/lib/python3.8/site-packages/pandas/core/generic.py in __getattr__(self, name)
5137 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5138 return self[name]
-> 5139 return object.__getattribute__(self, name)
5140
5141 def __setattr__(self, name: str, value) -> None:
AttributeError: 'Series' object has no attribute 'distance'
我是 GeoPandas 的新手,但我从文档中认为距离方法可以作用于 GeoSeries,并且 df1.Centroid 和 manila.geometry 是有效的形状几何对象。所以我不知道我错过了什么。请帮忙。
【问题讨论】:
-
您需要创建一个代表马尼拉的
Point对象。并且不要忘记确保您在适当的 CRS 中工作
标签: geopandas