最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python?shapefile轉(zhuǎn)GeoJson的2種方式實(shí)例

 更新時(shí)間:2023年03月08日 10:35:06   作者:小李A(yù)I飛刀^_^  
geojson是地圖可視化系統(tǒng)中最常用的地理數(shù)據(jù)格式,幾乎所有主流地圖可視化庫或框架都支持geojson數(shù)據(jù)的加載,下面這篇文章主要給大家介紹了關(guān)于Python?shapefile轉(zhuǎn)GeoJson的2種方式的相關(guān)資料,需要的朋友可以參考下

GeoJson的簡要介紹

GeoJson是用json的語法表達(dá)和存儲(chǔ)地理數(shù)據(jù),可以說是json的子集。

GeoJson以鍵值對(duì)的形式保存原有對(duì)象的信息,具有輕量化、易解析等優(yōu)點(diǎn)。

GeoJson包括的地理要素有Point(點(diǎn))、 MultiPoint(多點(diǎn))、 LineString(線)、MultiLineString(多線)、 Polygon(面)、 MultiPolygon(多面)、 GeometryCollection(幾何集合)

這些地理要素包括在geometry的type屬性中,并且不同的type具有不同的coordinates值。更多的GeoJson相關(guān)內(nèi)容可參考RFC7946標(biāo)準(zhǔn)。

     {
         "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文件轉(zhuǎn)換為GeoJson的方式

1. 使用geopandas

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

import geopandas as gpd
 
def shp2geojson_gpd(shp_file, geojson_file):
    """
    將shapefile格式的文件轉(zhuǎn)化為geojson
    :param shp_file: 需要轉(zhuǎn)換的shapefile文件名,投影信息可以缺失,也可以指定
    :param geojson_file: 轉(zhuǎn)換輸出的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轉(zhuǎn)換的時(shí)候兩行核心代碼即可搞定,簡單粗暴。但是在實(shí)踐過程中發(fā)現(xiàn),采用geopandas轉(zhuǎn)換后的GeoJson文件并沒有保留shapefile中的屬性properities信息,如area, name等,如下圖所示:

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)
 
    # 創(chuàng)建結(jié)果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())
 
    # 生成結(jié)果文件
    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")

結(jié)果包含原始shapefile文件中的屬性信息:

總結(jié)

到此這篇關(guān)于Python shapefile轉(zhuǎn)GeoJson的2種方式的文章就介紹到這了,更多相關(guān)Python shapefile轉(zhuǎn)GeoJson內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

无为县| 乳山市| 修水县| 深圳市| 四川省| 高唐县| 什邡市| 肇庆市| 上思县| 丹凤县| 郁南县| 石棉县| 蓬莱市| 务川| 西城区| 库尔勒市| 望谟县| 永春县| 都安| 介休市| 威信县| 巢湖市| 柏乡县| 阳东县| 光山县| 东阿县| 三原县| 雅安市| 枣强县| 石泉县| 冀州市| 成武县| 武强县| 杨浦区| 绵竹市| 且末县| 临猗县| 景德镇市| 尖扎县| 霍邱县| 长葛市|