【dxf】(3)dxf 的组 Group 读取

A group is just a bunch of DXF entities tied together. All entities of a group has to be on the same layout (modelspace
or any paper layout but not block).

组 Group 是一组捆绑在一起的实体集合(与块 Block 有区别)。
读取思路:
1)遍历 doc 的 groups
2)遍历该 group下的 entities

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, layername):
    doc = ezdxf.readfile(file)
    for group in doc.groups:
    	print("group: ", group)
        for group_entity in group[1]:
            print("group_entity: ", group_entity)
            handles.append(group_entity.dxf.handle)
        	features.append(convert_by_control(group_entity, doc, coord_convert_params, {}, [0, 0], False))
    	feature_collection = {
	        "type": "FeatureCollection",
	        "features": features
	    }
    with open(output, "wt", encoding="utf8") as fp:
        json.dump(feature_collection, fp, indent=2)
    print("output: ", output)


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


def export_geojson(entity, doc):
    try:
        geo_proxy = ezdxf.addons.geo.proxy(entity)
        geometry = geo_proxy.__geo_interface__
        coordinates = transformer(geometry['coordinates'], crs, wgs84)
        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),
            }
        }
        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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值