网络编程
位置:首页>> 网络编程>> Python编程>> Python shapefile转GeoJson的2种方式实例

Python shapefile转GeoJson的2种方式实例

作者:小李AI飞刀^_^  发布时间:2023-02-20 01:07:52 

标签:python,shapefile,geojson

GeoJson的简要介绍

GeoJson是用json的语法表达和存储地理数据,可以说是json的子集。

GeoJson以键值对的形式保存原有对象的信息,具有轻量化、易解析等优点。

GeoJson包括的地理要素有Point(点)、 MultiPoint(多点)、 LineString(线)、MultiLineString(多线)、 Polygon(面)、 MultiPolygon(多面)、 GeometryCollection(几何集合)

这些地理要素包括在geometry的type属性中,并且不同的type具有不同的coordinates值。更多的GeoJson相关内容可参考RFC7946标准。

{
        "type": "MultiPoint",
        "coordinates": [
            [100.0, 0.0],
            [101.0, 1.0]
        ]
    }

{
        "type": "MultiPolygon",
        "coordinates": [
            [
                [
                    [102.0, 2.0],
                    [103.0, 2.0],
                    [103.0, 3.0],
                    [102.0, 3.0],
                    [102.0, 2.0]
                ]
            ],
            [
                [
                    [100.0, 0.0],
                    [101.0, 0.0],
                    [101.0, 1.0],
                    [100.0, 1.0],
                    [100.0, 0.0]
                ],
                [
                    [100.2, 0.2],
                    [100.2, 0.8],
                    [100.8, 0.8],
                    [100.8, 0.2],
                    [100.2, 0.2]
                ]
            ]
        ]
    }

两种将shapefile文件转换为GeoJson的方式

1. 使用geopandas

核心代码:geopandas.GeoSeries 和out_data.to_file

import geopandas as gpd

def shp2geojson_gpd(shp_file, geojson_file):
   """
   将shapefile格式的文件转化为geojson
   :param shp_file: 需要转换的shapefile文件名,投影信息可以缺失,也可以指定
   :param geojson_file: 转换输出的geojson文件名
   """

if os.path.exists(geojson_file):
       os.remove(geojson_file)

out_data = gpd.read_file(shp_file)
   crs = out_data.crs
   out_data = gpd.GeoSeries(out_data.geometry, crs=crs)
   out_data.to_file(geojson_file, driver='GeoJSON', encoding="utf-8")
   print("successfully convert shapefile to geojson")

使用geopandas转换的时候两行核心代码即可搞定,简单粗暴。但是在实践过程中发现,采用geopandas转换后的GeoJson文件并没有保留shapefile中的属性properities信息,如area, name等,如下图所示:

Python shapefile转GeoJson的2种方式实例

2. 使用gdal

import gdal
import ogr
import os

def shp2geojson_gdal(shp_file, geojson_file):
   gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")
   gdal.SetConfigOption("SHAPE_ENCODING", "GBK")
   src_ds = ogr.Open(shp_file)
   src_layer = src_ds.GetLayer(0)

# 创建结果Geojson
   baseName = os.path.basename(geojson_file)
   dst_driver = ogr.GetDriverByName('GeoJSON')
   dst_ds = dst_driver.CreateDataSource(geojson_file)
   if dst_ds.GetLayer(baseName):
       dst_ds.DeleteLayer(baseName)
   dst_layer = dst_ds.CreateLayer(baseName, src_layer.GetSpatialRef())
   dst_layer.CreateFields(src_layer.schema)
   dst_feat = ogr.Feature(dst_layer.GetLayerDefn())

# 生成结果文件
   for feature in src_layer:
       dst_feat.SetGeometry(feature.geometry())
       for j in range(feature.GetFieldCount()):
           dst_feat.SetField(j, feature.GetField(j))
       dst_layer.CreateFeature(dst_feat)

del dst_ds
   del src_ds
   print("successfully convert shapefile to geojson")

结果包含原始shapefile文件中的属性信息:

Python shapefile转GeoJson的2种方式实例

来源:https://blog.csdn.net/MLH7M/article/details/121024141

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com