如何从线串中获取经度/纬度

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

我有一个带有一列线串的数据框。我想将线串转换为相应的纬度/经度,以便我可以用底图绘制它。我的代码如下:

gdf = gpd.read_file('./call2016.shp') #read the data into a variable

streetsaslinestring = gdf.loc[: , "geometry"] #getting the linestring column

接下来,我想将数据转换为经度/纬度。

streetsinlatlong    = convert_etrs89_to_lonlat(streetsaslinestring)

streetsinlatlong.to_file('./streetslonglat.shp') #store it as .shp in order to plot it with basemap

m.readshapefile('./streetslonglat', 'streets') #read as shape file

几何列看起来像这样:几何列

如何转换长字符串数据?

python matplotlib-basemap geopandas multilinestring
2个回答
2
投票

我认为你可以简单地使用

Lats, Lons = LineStringObject.coords.xy

它将分别返回纬度和经度数组。


0
投票

感谢@Clarence Kuo,这是我在遇到与OP相同的问题时创建的辅助函数:从GeoPandas中的WKT LINESTRING Geometry对象中提取纬度和经度:

import pandas as pd
from shapely import wkt
from typing import Tuple

def expand_max_min_lat_long_from_wkt(df: pd.DataFrame, wkt_col_name: str) -> pd.DataFrame:
    
    def _check_n_correct_wkt(df: pd.DataFrame, wkt_col_name: str) -> Tuple[pd.DataFrame, str]:
        if str(df[wkt_col_name].dtype) != 'geometry':
            new_wkt_col_name = f"{wkt_col_name}__WKT"
            df[new_wkt_col_name] = df[wkt_col_name].apply(wkt.loads)
            wkt_col_name = new_wkt_col_name
        return df, wkt_col_name

    def _expand_max_min_lat_long_from_wkt(wkt_val) -> tuple:
        if not wkt_val:
            return (None, None, None, None)
        lon, lat = wkt_val.coords.xy
        return (
            min(lat),
            max(lat),
            min(lon),
            max(lon),
        )

    df, wkt_col_name = _check_n_correct_wkt(df, wkt_col_name)

    (
        df[f"{wkt_col_name}__LAT_MIN"], 
        df[f"{wkt_col_name}__LAT_MAX"], 
        df[f"{wkt_col_name}__LONG_MIN"], 
        df[f"{wkt_col_name}__LONG_MAX"],
    ) = zip(*df[wkt_col_name].apply(_expand_max_min_lat_long_from_wkt))

    return df

使用:

gdf = expand_max_min_lat_long_from_wkt(gdf, 'geometry')

结果如下: enter image description here

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