简体   繁体   English

如何将线串转换为geopandas中的多个经度纬度列

[英]How to convert linestring to multiple longitude latitude column in geopandas

Here's my one row of input这是我的一行输入

Name                    geometry
Jalan W.R. Supratman    LINESTRING (95.317339 5.5603499, 95.3169007 5.5602832, 95.3165735 5.5602391, 95.3161729 5.5602097, 95.3161289 5.5601326, 95.3160873 5.5600178)

Here's my expected output这是我的预期输出

Name                                  Langitude         Longitude
Jalan W.R. Supratman                  95.317339         5.5603499
Jalan W.R. Supratman                  95.3169007        5.5602832
Jalan W.R. Supratman                  95.3165735        5.5602391
Jalan W.R. Supratman                  95.3161729        5.5602097
Jalan W.R. Supratman                  95.3161289        5.5601326
Jalan W.R. Supratman                  95.3160873        5.5600178

Edit:编辑:

Here's what I try这是我尝试的

def utm_to_latlon(coords, zone_number, zone_letter):
    easting = coords[0]
    northing = coords[1]
    return utm.to_latlon(easting, northing, zone_number, zone_letter)

# Using nested list comprehension
df ["lat_lon_tuple"] = [[utm_to_latlon(xy, 44, "N") for xy in tuple(geom.coords)] for geom in df.geometry]

The error message错误信息

<ipython-input-5-4dfd2badb8b4> in <listcomp>(.0)
      5 
      6 # Using nested list comprehension
----> 7 df ["lat_lon_tuple"] = [[utm_to_latlon(xy, 44, "N") for xy in tuple(geom.coords)] for geom in df.geometry]

AttributeError: 'str' object has no attribute 'coords'

You can convert the coordinates defining theLineString with the coords attribute, then convert this to ashapely.geometry.MultiPoint :您可以使用coords属性转换定义LineString的坐标,然后将其转换为shapely.geometry.MultiPoint

df['geometry'] = df['geometry'].apply(
    lambda x: shapely.geometry.MultiPoint(list(x.coords))
)

Once this is done, you can use df.explode :完成后,您可以使用df.explode

In [10]: df.explode(index_parts=False)
Out[10]:
                   geometry
0  POINT (95.31734 5.56035)
0  POINT (95.31690 5.56028)
0  POINT (95.31657 5.56024)
0  POINT (95.31617 5.56021)
0  POINT (95.31613 5.56013)
0  POINT (95.31609 5.56002)

Explode with index and then apply Series :用 index Explode ,然后应用Series

import pandas as pd
import geopandas as gpd
from shapely.geometry import LineString

df = gpd.GeoDataFrame(
    {'Name': ['Jalan W.R. Supratman', 'Other Street'],
     'geometry': [LineString([(95.317339, 5.5603499), (95.3169007,5.5602832), (95.3165735, 5.5602391), (95.3161729,5.5602097), (95.3161289, 5.5601326), (95.3160873, 5.5600178)]),
                  LineString([(95.0, 6.0), (95.01, 6.01)])]})

print(df.set_index('Name').geometry
        .apply(lambda x: list(x.coords))
        .explode(ignore_index=False)
        .apply(pd.Series)
        .reset_index()
        .rename(columns={0: 'Langitude', 1: 'Longitude'}))

Result:结果:

                   Name  Langitude  Longitude
0  Jalan W.R. Supratman  95.317339   5.560350
1  Jalan W.R. Supratman  95.316901   5.560283
2  Jalan W.R. Supratman  95.316574   5.560239
3  Jalan W.R. Supratman  95.316173   5.560210
4  Jalan W.R. Supratman  95.316129   5.560133
5  Jalan W.R. Supratman  95.316087   5.560018
6          Other Street  95.000000   6.000000
7          Other Street  95.010000   6.010000

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

相关问题 如何从 LineString geopandas 数据框中提取纬度和经度对作为列表 - How to extract latitude and longitude pairs as a list from a LineString geopandas dataframe 在 GeoPandas 中将纬度/经度点转换为网格多边形 - Convert Latitude/Longitude points to Grid Polygons in GeoPandas 如何从线串中获取经度/纬度 - How to get the longitude/latitude from a linestring 在 GeoPandas 中翻转经纬度坐标 - Flipping longitude and latitude coordinates in GeoPandas 如何将地理列转换为纬度和经度? - How do I convert a geography column to latitude and longitude? (geopandas)如何在墨卡托投影中正确调整 output 经度/纬度比例? - (geopandas) How to output longitude/latitude scale correctly in Mercator projetion? 从 shapefile 解释 geopandas 纬度/经度数据 - Interpreting geopandas latitude/longitude data from shapefile 从地址地理位置获取纬度和经度 - Get latitude & longitude from address geopandas 如何将HEXEWKB转换为Latitude,Longitude(在python中)? - How to convert HEXEWKB to Latitude, Longitude (in python)? 如何在熊猫数据框中将UTM转换为经度/纬度? - How to convert UTM to Longitude/Latitude in Pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM