【dxf】(2)dxf 的块 Block 读取

该代码段读取DXF文件,过滤指定图层的INSERT类型实体(块),遍历块中的点、线、多段线等几何对象。然后使用pyproj库将这些几何对象从源坐标系统转换到WGS84坐标系,生成GeoJSON格式的数据输出。
摘要由CSDN通过智能技术生成

A block is just another kind of entity space, which can be inserted multiple times into other layouts and blocks by the INSERT entity also called block references, this is a very powerful and important concept of the DXF format.

块 Block 是一组可以被多次使用的实体集合,DXFTYPE 为 INSERT。
读取思路:
1)先过滤该图层的 INSERT 类型的 entity
2)通过名称寻找 Block
3)遍历该 Block 下的 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)
    msp = doc.modelspace()
    features = []
    entities = msp.query('*[layer=="' + layername + '"]')
    n = 0
    for entity in entities:
        print("dxftype: ", entity.DXFTYPE)
        if entity.DXFTYPE == 'INSERT':
        	block = doc.blocks.get(track.dxf.name)
        	for block_entity in block.entity_space.entities:
	   			if block_entity.DXFTYPE == 'POINT' \
                                or block_entity.DXFTYPE == 'LWPOLYLINE' or block_entity.DXFTYPE == 'LINE' or block_entity.DXFTYPE == 'POLYLINE' \
                                or block_entity.DXFTYPE == 'CIRCLE' \
                                or block_entity.DXFTYPE == 'ARC' \
                                or block_entity.DXFTYPE == 'SOLID' \
                                or block_entity.DXFTYPE == 'HATCH':
                            properties = {
                                "handle": str(entity.dxf.handle),
                                "type": str(entity.DXFTYPE),
                                "layer": str(entity.dxf.layer),
                                "color": str(entity.dxf.color),
                                "edge": 'block',
                                "block_name": str(track.dxf.name),
                            }
                            for attr in track.attribs:
                                properties[attr.dxf.tag] = attr.dxf.text
                                # print('attr: ', attr.dxf.tag)
                            features.append(
                                convert_by_control(block_entity, doc, coord_convert_params, properties,
                                                   entity.dxf.insert,
                                                   True))
        n = n + 1
        print('percent: ', n / len(entities) * 100)
    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 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值