python处理矢量数据格式转换,shp转为geojson,geojson转为pbf,pbf转为geojson

矢量数据转geojson/pbf

需求:读取矢量shp面数据,将其转为可以进行geoserver发布使用的pbf/geojson格式
步骤:

  • 读取shp数据
  • 处理矢量数据,重构为geojson格式
  • 输出geojson数据
  • 压缩pbf格式
import geopandas 
import geobuf
# 读取shp数据
shpdata = geopandas.GeoDataFrame.from_file(shpfile)
# 获取box
box = list(shpdata.total_bounds)
# 获取feature信息
for i in range(len(shpdata)):
	geo = shpdata.geometry[0]
	feature = geo.__geo_interface__
	if feature['type'] == 'MultiPolygon':
	    group = feature["coordinates"]
	    feas = []
	    # 多面的处理方式
	    for i in range(len(group)):
	        fea = group[i]
	        feas.append([list(map(list,fea[0]))])
	    print(enname)
	    _feature = {
	    "type":"Feature",
	    "properties":{
	    "id":id[key],
	    "name":enname
	    },
	    "geometry":{
	    "type":'MultiPolygon',
	    "coordinates":feas
	    }
	    }
	elif feature['type'] == 'Polygon':
	    _feature = {
	        "type":"Feature",
	        "properties":{
	        "id":id[key],
	        "name":enname
	        },
	        "geometry":{
	        "type":feature['type'],
	        "coordinates":[list(map(list,feature["coordinates"][0]))]
	        }
	    }
	features.append(_feature)
geojson = {
        "type": "FeatureCollection",
        "box": box,
        "features":features
    }
#写出json
outfile = 'test'.json'
with open(outfile,'w+',encoding='utf-8') as fp:
    fp.write(json.dumps(geojson))
# 将geojson数据压缩为pbf格式
ed = geobuf.encode(geojson)
outpbf = os.path.splitext(outfile)[0]+'.pbf'
with open(outpbf, "wb") as f:
    f.write(ed)
print("pbf write success")

pbf/json转为shp

需求:解码地图数据pbf格式,将其转为明文的json,进一步转为shp
步骤:

  • 读取pbf文件
  • 解析pbf文件
  • 存储为json文件
  • geopands读取json文件
  • 存为shp文件
from osgeo import ogr
from osgeo.gdalconst import GA_ReadOnly
import geopandas
driver = ogr.GetDriverByName("MVT")
pbf_file_list = '/luxl/LSYS/PBF'
for infile in os.listdir(pbf_file_list):
    data_source = driver.Open(os.path.join(pbf_file_list,infile), GA_ReadOnly)  # get data source
    layer_num = data_source.GetLayerCount()
    for i in range(layer_num):
        layer = data_source.GetLayer(i)  # get layers in data source
        layer_name = layer.GetName()  # get layer name 
        for feature in layer:  # get feature in layer
                geometry = feature.ExportToJson()  # get geometry in feature
                print(json.loads(geometry))
                with open(os.path.join(pbf_file_list,infile)+'.json','w+',encoding='utf-8') as fp:
                    fp.write(json.dumps(json.loads(geometry))) # geometry是字符串格式,需要json.loads转为字典格式
# 测试样例
jsondata = geopandas.read_file(os.path.join(pbf_file_list,infile)+'.json') # 读取json文件
# to write shpfile from geojson
localPath = maskname+'.shp'#用于存放生成的文件
jsondata.to_file(localPath, driver='ESRI Shapefile', encoding='utf-8')
print("--保存成功,文件存放位置:"+localPath)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值