简体   繁体   English

如何从 LineString geopandas 数据框中提取纬度和经度对作为列表

[英]How to extract latitude and longitude pairs as a list from a LineString geopandas dataframe

I have a geopandas dataframe that looks like this:我有一个看起来像这样的 geopandas 数据框:

    shape_id    geometry
    1000252     LINESTRING (4.91790 52.34725, 4.91797 52.34715...
    1000254     LINESTRING (4.80382 52.34495, 4.80413 52.34500...
    1000255     LINESTRING (4.89922 52.37811, 4.89923 52.37807...

With Python, I would like to extract the coordinates in the geometry column for each shape_id row individually as a list.使用 Python,我想将每个shape_id行的几何列中的坐标单独提取为列表。 For example, the output for shape_id = 1000252 should be as follows:例如, shape_id = 1000252的输出应如下所示:

[[52.34725, 4.91790],
 [52.34715, 4.91797],
 [52.34742, 4.91723],
 [52.34752, 4.91713]]

What is the most efficient way to achieve this?实现这一目标的最有效方法是什么?

Each shapelyLineString object has a coords attribute which give the points defining the linestring, and you can access the xy attr to convert the MultiPoint to a tuple of numpy arrays.每个 shapelyLineString对象都有一个coords属性,它给出定义线串的点,您可以访问xy attr 以将 MultiPoint 转换为 numpy 数组的元组。 A bit of extra numpy will get you to a stacked list of lists:一点额外的 numpy 将使您获得列表的堆叠列表:

# e.g. for position 40...
In [3]: np.vstack(gdf.iloc[40].geometry.coords.xy).T.tolist()
Out[3]:
[[0.7741171421283728, 1.715569328873729],
 [0.5852143769680165, 1.4516089839272017],
 [0.378452363108969, 1.2226445706965148],
 [0.43147551026039477, 0.7940308770193946],
 [0.3105453476502247, 0.770655256832471],
 [0.13440130471131118, 0.2957373776736154],
 [0.6793980801823408, 1.4291149753156192],
 [0.25803877234174954, 0.5296081932347322],
 [0.12773596566152468, 0.6238335508304359],
 [0.1575172393070674, 0.44929138014961945],
 [0.2222528104586241, 0.8623618596533595],
 [0.8185687868071416, 1.5897595726257494]]

See the shapely docs on coordinate sequences for more info.有关更多信息,请参阅坐标序列上的匀称文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM