首页 > Web开发 > 详细

SHP文件转GeoJson

时间:2020-07-16 17:59:04      阅读:47      评论:0      收藏:0      [点我收藏+]
# -*- coding: utf-8 -*-
from osgeo import ogr
import gdal
import sys
import os

def ChangeToJson(vector, output):
    print("Starting........")
    #打开矢量图层
    gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")
    gdal.SetConfigOption("SHAPE_ENCODING", "GBK")
    shp_ds = ogr.Open(vector)
    shp_lyr = shp_ds.GetLayer(0)

    # 创建结果Geojson
    baseName = os.path.basename(output)
    out_driver = ogr.GetDriverByName(GeoJSON)
    out_ds = out_driver.CreateDataSource(output)
    if out_ds.GetLayer(baseName):
        out_ds.DeleteLayer(baseName)
    out_lyr = out_ds.CreateLayer(baseName, shp_lyr.GetSpatialRef())
    out_lyr.CreateFields(shp_lyr.schema)
    out_feat = ogr.Feature(out_lyr.GetLayerDefn())

    #生成结果文件
    for feature in shp_lyr:
        out_feat.SetGeometry(feature.geometry())
        for j in range(feature.GetFieldCount()):
            out_feat.SetField(j, feature.GetField(j))
        out_lyr.CreateFeature(out_feat)

    del out_ds
    del shp_ds
    print("Success........")

if __name__ == __main__:
    shapefile = D:/daat.shp
    out = D:/daat/123.json
    ChangeToJson(shapefile, out)

 

SHP文件转GeoJson

原文:https://www.cnblogs.com/raorao1994/p/13323424.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!