将字典列表转换为地理标准几何图形



我正在努力解决这个问题,已经为一个解决方案工作了好几天,但没有成功。

我有一个GeoPandas GeoDataFrame,它包含一个名为geometry的列。此列由字典列表组成,其中有两个键:值对,表示行字符串中节点的lat/long坐标。例如:

[{'lat': 35.0048032, 'lon': -106.0116078}, {'lat': 35.0048599, 'lon': -106.0120824}, 
{'lat': 35.0048758, 'lon': -106.0122844}, {'lat': 35.0048971, 'lon': -106.0124856}]

我需要将此列识别为geopandas几何列,但需要正确格式化才能做到这一点。我已经尝试创建一个小函数来完成此操作。我在地理数据框架之外的词典示例列表中测试了该函数。功能如下:


geom_list = [{'lat': 35.0048032, 'lon': -106.0116078}, {'lat': 35.0048599, 'lon': -106.0120824}, 
{'lat': 35.0048758, 'lon': -106.0122844}, {'lat': 35.0048971, 'lon': -106.0124856}]
def dict_list_to_tuple_list(input_list):
geom_flat = []
for pair in input_list:
lat_long = (pair['lat'], pair['lon'])
geom_flat.append(lat_long)
return geom_flat
new_geom = dict_list_to_tuple_list(geom_list)
print(new_geom)

输出:

[(35.0048032, -106.0116078), (35.0048599, -106.0120824), 
(35.0048758, -106.0122844), (35.0048971, -106.0124856)]

这似乎已经解决了这个问题,然而,当我试图将其应用于实际的地理数据框架时,我遇到了一个错误。地理数据框架命名为results_gdf,字典列表中的列为geometry。我实现了这样的功能:

# create a new column in the `results_gdf` geodataframe which holds the processed geometry field
results_gdf['geometry_new'] = dict_list_to_tuple_list(input_list=results_gdf['geometry'])

然而,当我运行此程序时,我会得到以下类型错误:

Traceback (most recent call last):
File "C:Usersdanscriptstesting.py", line 76, in <module>
results_gdf['geometry_new'] = dict_list_to_tuple_list(input_list=results_gdf['geometry'])
File "C:Usersdanscriptstesting.py", line 71, in dict_list_to_tuple_list
lat_long = (pair['lat'], pair['lon'])
TypeError: list indices must be integers or slices, not str

然后我尝试了字典.get()的方法,如下所示:

def dict_list_to_tuple_list(input_list):
geom_flat = []
for pair in input_list:
lat_long = (pair.get('lat'), pair.get('lon'))
geom_flat.append(lat_long)
return geom_flat

但是得到了这个错误:AttributeError: 'list' object has no attribute 'get'

这让我相信,出于某种原因,它将pair可迭代性作为列表来阅读。但我不知道为什么会发生这种事。我也试过做dict_pair = dict(pair)。但是得到了一些奇怪的结果。所以我在这里留白。

非常感谢您的帮助!

答案的密钥由@michaelDelgado:提供

# create a dataframe from the geometry column
parsed = pd.DataFrame.from_records(results_gdf['geometry'])
# extract the coordinate pairs from each dictionary
parsed2 = parsed.applymap(lambda x: [x['lat'], x['lon']], na_action='ignore')
# concatenate the extracted coordinate pairs to a new column containing a list of lists of coord pairs
parsed2['geom_new'] = parsed2.apply(lambda row: row.dropna().tolist(), axis=1)
# join the newly converted column to the original gdf
new_geom_col = parsed2['geom_new']
results_gdf = results_gdf.join(new_geom_col)
# convert the new column into a shapely linestring object
results_gdf['linestring'] = results_gdf['geom_new'].apply(lambda x: LineString(x))
# set the GDFs geometry column = to the new linestring column
results_gdf.set_geometry(col='linestring', crs="EPSG:4326", inplace=True)

最新更新