【dxf】(1)dxf 转 geojson

import json
import os

import ezdxf
import pyproj
from ezdxf.addons import geo

# XX请改成自己的数据
crs = pyproj.CRS('EPSG:XX')
wgs84 = pyproj.CRS('EPSG:4326')


def transformer(data, crs, wgs84):
    result = []
    if type(data) == tuple:
        project = pyproj.Transformer.from_crs(crs, wgs84, always_xy=True)
        wgs84_pts = project.transform(data[0], data[1])
        result.append(wgs84_pts)
    else:
        for points in data:
            if type(points) == list:
                points = transformer(points, crs, wgs84)
                result.append(points)
            else:
                project = pyproj.Transformer.from_crs(crs, wgs84, always_xy=True)
                wgs84_pts = project.transform(points[0], points[1])
                result.append(wgs84_pts)
    return result


def printEzdxf(file, output, layernames):
    doc = ezdxf.readfile(file)
    msp = doc.modelspace()
    features = []
    for layername in layernames:
        tracks = msp.query('*[layer=="' + layername + '"]')
        n = 0
        for track in tracks:
            print("dxftype: ", track.DXFTYPE)
            features.append(export_geojson(track, doc))
            n = n + 1
            print('percent: ', n / len(tracks) * 100)
    geo = {
        "type": "FeatureCollection",
        "features": features
    }
    with open(output, "wt", encoding="utf8") as fp:
        json.dump(geo, fp, indent=2)

    print("output: ", output)
 
 
def export_geojson(entity, doc):
    try:
        geo_proxy = ezdxf.addons.geo.proxy(entity)
        geometry = geo_proxy.__geo_interface__
        coordinates = transformer(geometry['coordinates'], pyproj.CRS('EPSG:3414'), pyproj.CRS('EPSG:4326'))
        print('geometry-after: ', coordinates)
        layer = doc.layers.get(str(entity.dxf.layer).lower())
        geo = {
            "type": "Feature",
            "geometry": {
                "type": geometry['type'],
                "coordinates": coordinates
            },
            "properties": {
                "handle": str(entity.dxf.handle),
                "type": str(entity.DXFTYPE),
                "layer": str(entity.dxf.layer),
                "color": str(layer.color),
            }
        }
        if entity.DXFTYPE == 'LINE':
            geo['properties']['linetype'] = str(layer.dxf.linetype)
            geo['properties']['lineweight'] = str(layer.dxf.lineweight)
        if entity.DXFTYPE == 'LWPOLYLINE':
            geo['properties']['linetype'] = str(layer.dxf.linetype)
            geo['properties']['lineweight'] = str(layer.dxf.lineweight)
        if entity.DXFTYPE == 'POLYLINE':
            geo['properties']['linetype'] = str(layer.dxf.linetype)
            geo['properties']['lineweight'] = str(layer.dxf.lineweight)
        if entity.DXFTYPE == 'CIRCLE':
            geo['properties']['center'] = str(entity.dxf.center)
            geo['properties']['radius'] = str(entity.dxf.radius)
        if entity.DXFTYPE == 'ARC':
            geo['properties']['center'] = str(entity.dxf.center)
            geo['properties']['radius'] = str(entity.dxf.radius)
            geo['properties']['start_angle'] = str(entity.dxf.start_angle)
            geo['properties']['end_angle'] = str(entity.dxf.end_angle)
        return geo
    except Exception as e:
        print("!!! Error found in export_geojson(): ", e)
        pass


if __name__ == "__main__":
    try:
        path = 'XX'
        file = 'XX.dxf'
        layername = 'XX'
        print('file-start: ', file)
        printEzdxf(os.path.join(path, file), file.split("\\")[-1].split(".")[0] + ".json", layername)
        print('file-done: ', file)
        print("### main(): done")
    except Exception as e:
        print("!!! Error found: ", e)
        pass

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值