python 将带有坐标的pandas数据帧转换为geopandas数据帧。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 将带有坐标的pandas数据帧转换为geopandas数据帧。相关的知识,希望对你有一定的参考价值。

from geopandas import GeoDataFrame
from shapely.geometry import Point


def to_gdf(df, lon, lat, crs={'init': 'epsg:4326'}):
    '''Converts a pandas dataframe with coordinate columns to a geopandas dataframe.
    Inputs:
        df: pandas dataframe
        lon: str; name of longitude column
        lat: str; name of Latitude column
        crs: dict;  projection type
    Returns: 
        gdf: Geopandas dataframe
    '''
    geometry = [Point(xy) for xy in zip(df[f'{lon}'], df[f'{lat}'])]
    df = df.drop([f'{lon}', f'{lat}'], axis=1)
    crs = {'init': 'epsg:4326'}
    gdf = GeoDataFrame(df, crs=crs, geometry=geometry)

    return gdf

以上是关于python 将带有坐标的pandas数据帧转换为geopandas数据帧。的主要内容,如果未能解决你的问题,请参考以下文章