使用 GeometryCollection 列加载/绘制 geopandas 数据框

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

我有这样的数据集:

您可以重现此数据框加载以下字典:

{'entity_ts': {0: '2022-11-01T00:00:56.000Z', 1: '2022-11-01T00:00:56.000Z'},  'entity_id': {0: 'WAZE.jams.1172133072', 1: 'WAZE.jams.1284082818'},  'street': {0: 'Ac. Monsanto, Benfica', 1: nan},  'position': {0: {'type': 'GeometryCollection',    'geometries': [{'coordinates': [[[-9.168816, 38.741779],        [-9.169618, 38.741353],        [-9.16976, 38.741289]]],      'type': 'MultiLineString'}]},   1: {'type': 'GeometryCollection',    'geometries': [{'coordinates': [[[-9.16116, 38.774899],        [-9.16083, 38.774697]]],      'type': 'MultiLineString'}]}},  'level': {0: 5, 1: 5},  'length': {0: 99, 1: 36},  'delay': {0: -1, 1: -1},  'speed': {0: 0.0, 1: 0.0}}

我的问题是:

我无法正确使用 geopandas 加载此数据。在 geopandas 上,指定几何列很重要,但这种“位置”列格式对我来说是全新的。

1 - 我尝试使用 pandas dataframe 加载

data=`pd.read_csv(data.csv, converters={'position': json.loads})`

2 - 然后我转换为 GeoDataFrame:

import geopandas as gpd
import contextily as ctx
crs={'init':'epsg:4326'}

gdf = gpd.GeoDataFrame(
data, geometry=data['position'], crs=crs)

但是我得到了这个错误:

TypeError: Input must be valid geometry objects: {'type': 'GeometryCollection', 'geometries': [{'coordinates': [[[-9.168816, 38.741779], [-9.169618, 38.741353], [-9.16976, 38.741289]]], 'type': 'MultiLineString'}]}
python gis geopandas shapely
1个回答
0
投票

一个选择是使用

str
获取几何信息然后要求
shape
/
geoms

from shapely.geometry import shape
​
gdf = gpd.GeoDataFrame(data, geometry=data.pop("position").str["geometries"]
                                 .explode().apply(lambda x: shape(x).geoms[0]), crs=crs)
​

输出:

print(gdf)

                  entity_ts             entity_id                 street  level  length  delay  speed                                                              geometry
0  2022-11-01T00:00:56.000Z  WAZE.jams.1172133072  Ac. Monsanto, Benfica      5      99     -1    0.0  LINESTRING (-9.16882 38.74178, -9.16962 38.74135, -9.16976 38.74129)
1  2022-11-01T00:00:56.000Z  WAZE.jams.1284082818                    NaN      5      36     -1    0.0                     LINESTRING (-9.16116 38.77490, -9.16083 38.77470)

# <class 'geopandas.geodataframe.GeoDataFrame'>
© www.soinside.com 2019 - 2024. All rights reserved.