Python GeoPandas 文本经纬度转换为点要素、线要素

需求目的:将以CSV格式存储的经纬度点坐标文件,转换为shapefile的point类型和line类型的矢量数据,主要依赖geopandas和shapely包。

核心思想:采用shapely里面的Point空间数据模型来构建Point对象,并将其作为参数传入构建GeoPandas的主要类型之一GeoDataFrame的geometry属性中。在线要素的创建中,需要采用shapely的LineString空间数据模型,构建LineString对象传入geodataframe中。完整代码如下。

转换为点要素
# 转换为点要素,经纬度坐标格式为wgslng, wgslat
import os
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point

# CSV转换为shapefile数据
def csv_to_points():
    input_path = "E:\\Data\\csv\\"
    output_path = "E:\\Data\\shp\\"
    for file in os.listdir(input_path):
        df = pd.read_csv(input_path+file, header=0, encoding='gbk')
        geometry = [Point(xy) for xy in zip(df.wgslng, df.wgslat)] # 需要修改为对应的经纬度字段
        gdf = gpd.GeoDataFrame(df, crs="EPSG:4326", geometry=geometry) # 指定坐标系
        gdf.to_file(output_path+file[0:-4]+".shp", encoding='gbk')
        print('finished------' + file)
     print('finished------' )
转换为线要素
import pandas as pd
import geopandas as gpd
from shapely.geometry import LineString

# CSV转换为shapefile线数据,数据格式为lng_oo, lat_oo, lng_dd, lat_dd
def csv_to_lines():
    input_path = "E:\\A Bite of China\\Data\\Flow\\"
    output_path = "E:\\A Bite of China\\Data\\Flow\\shp\\"
    file = "flow.csv"
    df = pd.read_csv(input_path + file, header=0, encoding='gbk')
    geometry = [LineString(xy_list) for xy_list in zip(zip(df.lng_oo, df.lat_oo), zip(df.lng_dd, df.lat_dd))]
    gdf = gpd.GeoDataFrame(df, crs="EPSG:4326", geometry=geometry)
    gdf.to_file(output_path + "flow.shp", encoding='gbk')
    print('finished------' )

参考:

  1. Shapely用户手册:https://www.osgeo.cn/shapely/manual.html
  2. GeoPandas DataFrame构建:https://geopandas.readthedocs.io/en/latest/gallery/create_geopandas_from_pandas.html
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容