在GeoDataFrame中具有多个几何列是不正确的做法吗?

问题描述 投票:-1回答:1

我正在尝试创建每行2个邮政编码的GeoDataFrame,我想比较它们之间的距离。我列出了大约220个邮政编码,并对它们进行了itertools组合以获取所有组合,然后将元组解压缩为两列

code_combo = list(itertools.combinations(df_with_all_zip_codes['code'], 2))
df_distance_ctr = pd.DataFrame(code_combo, columns=['first_code','second_code'])

然后,我进行了一些标准的大熊猫合并和列重命名,以将原始地理数据框中的多边形/几何列添加到这个新列中,就在各个邮政编码列旁边。问题是,即使在1.)尝试将数据框转换为地理数据框-AttributeError:尚无几何数据集,2。)应用wkt之后,我似乎也无法将多边形列读取为几何图形.loads到几何列-AttributeError:'MultiPolygon'对象没有属性'encode'。我试图寻找一种将系列转换为地理系列的方法,但是在SO或文档上都找不到。谁能指出我可能会出问题的地方?

gis polygon series geopandas shapely
1个回答
0
投票

[在__init__处查看GeoDataFrame的https://github.com/geopandas/geopandas/blob/master/geopandas/geodataframe.py方法,看起来GDF一次只能有一列。但是,您创建的其他列中仍应包含几何对象。

由于每列中仍然有几何对象,因此可以编写一个使用Shapely的distance方法的方法,如下所示:

import pandas as pd
import geopandas
from shapely.geometry import Point
import matplotlib.pyplot as plt

lats = [-34.58, -15.78, -33.45, 4.60, 10.48]
lons = [-58.66, -47.91, -70.66, -74.08, -66.86]
df = pd.DataFrame(
    {'City': ['Buenos Aires', 'Brasilia', 'Santiago', 'Bogota', 'Caracas'],
     'Country': ['Argentina', 'Brazil', 'Chile', 'Colombia', 'Venezuela'],
     'Latitude': lats,
     'Longitude': lons})

df['Coordinates'] = list(zip(df.Longitude, df.Latitude))
df['Coordinates'] = df['Coordinates'].apply(Point)

df['Coordinates_2'] = list(zip(lons[::-1], lats[::-1]))
df['Coordinates_2'] = df['Coordinates_2'].apply(Point)

gdf = geopandas.GeoDataFrame(df, geometry='Coordinates')


def get_distance(row):
    distance = row.Coordinates.distance(row.Coordinates_2)
    print(distance)
    return distance

gdf['distance'] = gdf.apply(lambda row: get_distance(row), axis=1)

关于AttributeError: 'MultiPolygon' object has no attribute 'encode'MultiPolygon是Shapely几何类。 encode通常是字符串对象的方法,因此您可以删除对wkt.loads的调用。

© www.soinside.com 2019 - 2024. All rights reserved.