我已经创建了两个geodataframe,我试图根据答案here找到一个geodataframe中每个点到另一个geodataframe中任意点之间的最短距离。在
然而,即使我认为我已经创建了一个几何列,我也不能访问这些行。我得到这个错误:
AttributeError: ("'Series' object has no attribute 'geometry'", u'occurred at index 0')
在GeoDataFrame.head的名称()退货:
^{pr2}$这是我非常不雅的剧本。在
f = h5py.File("temp_bin2x_outer_tagged.hdf", "r")
data = f["MDF/images/0/image"]
my_test = data[171, :, :]
val = filter.threshold_otsu(my_test)
binary = np.where(my_test > val, 1, 0)
outskel = skeletonize(binary)
x, y = np.where(outskel>0)
y_coord = y.tolist()
x_coord = x.tolist()
index = list(range(0,(len(x_coord))))
df = pd.DataFrame({"y_coord": y_coord, "x_coord": x_coord, "node": index})
df['Coordinates1'] = list(zip(df.x_coord, df.y_coord))
df['Coordinates1'] = df['Coordinates1'].apply(Point)
outer = geopandas.GeoDataFrame(df, geometry='Coordinates1')
f2 = h5py.File("temp_bin2x_inner_tagged.hdf", "r")
data2 = f2["MDF/images/0/image"]
my_test2 = data2[211, :, :]
val2 = filter.threshold_otsu(my_test2)
binary2 = np.where(my_test2 > val2, 1, 0)
binary2 = np.where(my_test2 > val2, 1, 0)
inskel = skeletonize(binary2)
x2, y2 = np.where(inskel>0)
y_coord2 = y2.tolist()
x_coord2 = x2.tolist()
index2 = list(range(0,(len(x_coord2))))
df2 = pd.DataFrame({"y_coord2": y_coord2, "x_coord2": x_coord2, "node": index2})
df2['Coordinates'] = list(zip(df2.x_coord2, df2.y_coord2))
df2['Coordinates'] = df2['Coordinates'].apply(Point)
inner = geopandas.GeoDataFrame(df2, geometry='Coordinates')
from shapely.ops import nearest_points
pts3 = inner.geometry.unary_union
def near(point, pts=pts3):
nearest = inner.geometry == nearest_points(point, pts)[1]
return inner[nearest].node.get_values()[0]
outer['Nearest'] = outer.apply(lambda row: near(row.geometry), axis=1)
我是否误解了地理数据框是如何构造的?在
非常感谢,任何帮助都会很棒!在
您将列的新名称与名称geometry混合使用,这是错误的原因:(名称转换并不总是完成)
输出:
^{pr2}$之后,如果您有大量的要点,我建议您使用cKDTree:
输出:
以防有人在为一个古老的geopandas版本而挣扎。解决方法如下:
相关问题 更多 >
编程相关推荐