python list转geojson

本篇介绍如何将列表转化为geojson,并在qgis上进行可视化。

 代码:

import pandas as pd
from shapely.wkt import dumps, loads
import geopandas as gpd
from shapely.geometry import LineString


# geoDataFrame对象写入geojson文件
def geoDataframe2geojson(data, geojson_path):
    data.to_file(geojson_path, driver="GeoJSON")


def list2geojson(write_geojson_path,lst,*others):
    """
    :param write_geojson_path:结果保存成geojson的路径
    :param lst:待转化的列表
    :param others:附加的其它信息
    :return: 空
    """
    #lst与其它附加的info转成dataframe
    pdd = pd.DataFrame()
    pdd["geom"] = lst
    for i in range(len(others)):
        pdd["others_{}".format(i)] = others[i]
    #dataframe转成geodataframe
    gdata = gpd.GeoDataFrame(pdd)

    #geom列即lst保存的是geometry类型时:
    gdata["geometry"] = list(gdata["geom"].values)
    #geom列即lst保存的是geometry的str类型时:
    # gdata["geometry"] = list(map(loads, gdata["geom"].values))

    gdata.drop(["geom"], axis=1, inplace=True)
    geoDataframe2geojson(gdata, write_geojson_path)
    return

list2geojson("tmp.geojson",
[LineString([[1,2],[3,4]]),LineString([[5,6],[7,8]])],        
["linestring1","linestring2"],
["信息1","信息2"])

 

qgis可视化geojson结果图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值