多個geojson經(jīng)過坐標系轉(zhuǎn)換后如何合并為一個shp數(shù)據(jù)
更新時間:2023年10月27日 09:26:20 作者:ylfmsn
這篇文章主要介紹了多個geojson經(jīng)過坐標系轉(zhuǎn)換后如何合并為一個shp數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
多個geojson經(jīng)過坐標系轉(zhuǎn)換后合并為一個shp數(shù)據(jù)
利用geopandas和pyproj將多個geojson數(shù)據(jù)經(jīng)過坐標轉(zhuǎn)換后合并成一個shp數(shù)據(jù):
import geopandas as gpd
from pyproj import CRS
import pandas as pd
BASE_DIR = 'D:/demo'
# 輸入的WGS84 GeoJSON文件列表
geojson_files = [f"{BASE_DIR}/file1.geojson", f"{BASE_DIR}/file2.geojson", f"{BASE_DIR}/file3.geojson"]
# 目標坐標系的EPSG代碼,使用一個西安80高斯投影舉例
target_epsg = 2363
# 創(chuàng)建一個空的GeoDataFrame
merged_gdf = gpd.GeoDataFrame()
# 定義WGS84和目標坐標系之間的坐標轉(zhuǎn)換
wgs84_crs = CRS.from_epsg(4326)
target_crs = CRS.from_epsg(target_epsg)
# 逐個讀取、轉(zhuǎn)換和合并GeoJSON文件
for geojson_file in geojson_files:
gdf = gpd.read_file(geojson_file, crs=wgs84_crs)
# 為每個geodataframe新添加字符類型的字段name,并將文件名賦值給字段name
gdf['name'] = geojson_file[-13:-8]
gdf = gdf.to_crs(target_crs)
merged_gdf = pd.concat([merged_gdf, gdf], ignore_index=True)
# 過濾掉面積小于100的要素
gdf_filtered = merged_gdf[merged_gdf["area"] >= 100]
# 保存為shp文件
gdf_filtered.to_file(output_shp, driver='ESRI Shapefile', encoding='utf-8')
- geopandas版本:0.12.0
- pandas版本:1.5.3
- pyproj版本:3.4.0
python里面GeoJson和shp文件互轉(zhuǎn)
# shp to GeoJson
import geopandas as gpd
data = gpd.read_file('中國省級區(qū)域20200720.shp')
data.to_file("中國省級區(qū)域20200720.json", driver='GeoJSON', encoding="utf-8")
# GeoJson to shp
data = gpd.read_file('中國省級區(qū)域20200720.json')
data.to_file('中國省級區(qū)域20200720', driver='ESRI Shapefile', encoding='utf-8')
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python 中關(guān)于pycharm選擇運行環(huán)境的問題
這篇文章主要介紹了python 中關(guān)于pycharm選擇運行環(huán)境的相關(guān)知識,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
python更新數(shù)據(jù)庫中某個字段的數(shù)據(jù)(方法詳解)
這篇文章主要介紹了python更新數(shù)據(jù)庫中某個字段的數(shù)據(jù)方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11

