前端需要展示CAD绘制的工程图,地图;研究了一下开源,实现转换操作;前端可加载geojson。
import os
from osgeo import osr, ogr, gdal
def cad_to_geojson(cadPath):
basename = os.path.split(cadPath)[0]
filename = os.path.basename(cadPath).split('.')[0]
outfile = "{}.json".format(basename+'\\'+ filename)
ogr.RegisterAll()
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")
gdal.SetConfigOption("SHAPE_ENCODING", "")
gdal.SetConfigOption("DXF_ENCODING", "ASCII") # 设置DXF缺省编码
poDS = ogr.Open(cadPath, False)
poDriver = ogr.GetDriverByName("GeoJson")
# 保存文件
res = poDriver.CopyDataSource(poDS, outfile)
if __name__ == '__main__':
cad_to_geojson('D:/data/cad/001001008子不语.dxf')
记录撸码点滴