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