1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python处理矢量数据格式转换 shp转为geojson geojson转为pbf pbf转为geojson

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

时间:2020-09-05 17:51:49

相关推荐

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)# 获取boxbox = 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}#写出jsonoutfile = '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 ogrfrom osgeo.gdalconst import GA_ReadOnlyimport geopandasdriver = 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 sourcelayer_num = data_source.GetLayerCount()for i in range(layer_num):layer = data_source.GetLayer(i) # get layers in data sourcelayer_name = layer.GetName() # get layer name for feature in layer: # get feature in layergeometry = feature.ExportToJson() # get geometry in featureprint(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 geojsonlocalPath = maskname+'.shp'#用于存放生成的文件jsondata.to_file(localPath, driver='ESRI Shapefile', encoding='utf-8')print("--保存成功,文件存放位置:"+localPath)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。