Skip to content

Instantly share code, notes, and snippets.

@dmarion
Created March 2, 2021 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmarion/da55bc8838d946b91b8cf545d2f2955b to your computer and use it in GitHub Desktop.
Save dmarion/da55bc8838d946b91b8cf545d2f2955b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys, argparse, ezdxf, os
from xml.dom import minidom
from pprint import pprint
types = "POINT,LINE,POLYLINE,LWPOLYLINE"
p = argparse.ArgumentParser(description='dxf2gml')
p.add_argument('-i', '--input', action='store', help='input filename (DXF)', required = True)
p.add_argument('-o', '--output', action='store', help='output filename (GML)', required = True)
p.add_argument('-l', '--layers', action='store', help='coma separatted list of layers')
p.add_argument('-t', '--type', action='store', help='coma separatted list of object types [{}]'.format(types))
r = p.parse_args()
layers = None
types = types.split(',')
if r.layers:
layers = r.layers.split(",")
if r.type:
types = r.type.split(",")
if "POINT" in types:
print (types)
exit
xml = minidom.Document()
wfs = xml.createElement('wfs:FeatureCollection')
wfs.setAttribute("xmlns:wfs", "http://www.opengis.net/wfs")
wfs.setAttribute("xsi:schemaLocation", "http://www.opengis.net/gml http://schemas.opengis.net/gml/2.1.2/feature.xsd")
wfs.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")
doc = ezdxf.readfile(r.input)
msp = doc.modelspace()
def createCoordinates(coords):
text = ""
for c in coords:
if text != "":
text += " "
x, y, z = c
if z != 0:
text += '{:.2f},{:.2f},{:.2f}'.format(x, y, z)
else:
text += '{:.2f},{:.2f}'.format(x, y)
co = xml.createElement("gml:coordinates")
co.setAttribute("decimal", ".")
co.setAttribute("cs", ",")
co.setAttribute("ts", " ")
co.appendChild(xml.createTextNode(text))
return co
for e in msp:
if not layers or e.dxf.layer in layers:
coords=""
if e.dxftype() == "POINT" and "POINT" in types:
co = createCoordinates([e.dxf.location])
geometry = xml.createElement('gml:Point')
geometry.setAttribute("srsName", "EPSG:3765")
geometry.appendChild(co)
elif e.dxftype() == "LINE" and "LINE" in types:
co = createCoordinates([e.dxf.start, e.dxf.end])
geometry = xml.createElement('gml:LineString')
geometry.setAttribute("srsName", "EPSG:3765")
geometry.appendChild(co)
elif e.dxftype() == "POLYLINE" and "POLYLINE" in types:
pprint(e)
continue
elif e.dxftype() == "LWPOLYLINE" and "LWPOLYLINE" in types:
coords = []
for x, y in e.vertices():
coords.append((x,y, e.dxf.elevation))
co = createCoordinates(coords)
lr = xml.createElement('gml:LinearRing')
lr.appendChild(co)
outb = xml.createElement('gml:outerBoundaryIs')
outb.appendChild(lr)
geometry = xml.createElement('gml:Polygon')
geometry.setAttribute("srsName", "EPSG:3765")
geometry.appendChild(outb)
else:
print("Skipping {} in layer '{}' ...".format(e.dxftype(), e.dxf.layer))
continue
feature = xml.createElement('feature:geometry')
feature.appendChild(geometry)
features = xml.createElement('feature:features')
features.appendChild(feature)
features.setAttribute("xmlns:feature", "http://mapserver.gis.umn.edu/mapserver")
gml_fm = xml.createElement('gml:featureMember')
gml_fm.appendChild (features)
gml_fm.setAttribute("xmlns:gml", "http://www.opengis.net/gml")
wfs.appendChild (gml_fm)
print(wfs.toprettyxml(indent=" "), file=open(r.output, "w"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment