Python?shapefile轉(zhuǎn)GeoJson的2種方式實(shí)例
GeoJson的簡要介紹
GeoJson是用json的語法表達(dá)和存儲地理數(shù)據(jù),可以說是json的子集。
GeoJson以鍵值對的形式保存原有對象的信息,具有輕量化、易解析等優(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)文章
Python 分析Nginx訪問日志并保存到MySQL數(shù)據(jù)庫實(shí)例
這篇文章主要介紹了Python 分析Nginx訪問日志并保存到MySQL數(shù)據(jù)庫實(shí)例,需要的朋友可以參考下2014-03-03
python返回多個(gè)值與賦值多個(gè)值的示例代碼
在Python中函數(shù)經(jīng)常會返回多個(gè)值,下面這篇文章主要給大家介紹了關(guān)于python返回多個(gè)值與賦值多個(gè)值的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
Python使用lambda表達(dá)式對字典排序操作示例
這篇文章主要介紹了Python使用lambda表達(dá)式對字典排序操作,結(jié)合實(shí)例形式分析了lambda表達(dá)式實(shí)現(xiàn)字典按鍵排序、按值排序、多條件排序相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
Python腳本按照當(dāng)前日期創(chuàng)建多級目錄
今天小編就為大家分享一篇關(guān)于Python腳本按照當(dāng)前日期創(chuàng)建多級目錄,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
python實(shí)現(xiàn)優(yōu)雅的打印json格式文本
這篇文章主要介紹了python實(shí)現(xiàn)優(yōu)雅的打印json格式文本方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
python調(diào)用xlsxwriter創(chuàng)建xlsx的方法
今天小編就為大家分享一篇python調(diào)用xlsxwriter創(chuàng)建xlsx的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
深度學(xué)習(xí)tensorflow基礎(chǔ)mnist
mnist作為深度學(xué)習(xí)中的HelloWorld,該小工程詳細(xì)描述了如何從零開始深度學(xué)習(xí),代碼詳細(xì),解釋全面,需要的朋友可以參考下2021-04-04

