Skip to content

Instantly share code, notes, and snippets.

@dmarion
Created March 2, 2021 16:15
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/5a02bb7ef24f49c368aebc4bb79520c6 to your computer and use it in GitHub Desktop.
Save dmarion/5a02bb7ef24f49c368aebc4bb79520c6 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
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 [POINT, LWPOLYLINE]')
r = p.parse_args()
layers = None
types = "POINT,LINE,LWPOLYLINE".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 xyz2str(c):
x, y, z = c;
if z != 0:
return '{:.2f},{:.2f},{:.2f}'.format(x, y, z)
else:
return '{:.2f},{:.2f}'.format(x, y)
def createCoordinates(text):
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(xyz2str(e.dxf.location))
geometry = xml.createElement('gml:Point')
geometry.setAttribute("srsName", "EPSG:3765")
geometry.appendChild(co)
elif e.dxftype() == "LINE" and "LINE" in types:
coord = xyz2str(e.dxf.start) + " " + xyz2str(e.dxf.end)
co = createCoordinates(coord)
geometry = xml.createElement('gml:LineString')
geometry.setAttribute("srsName", "EPSG:3765")
geometry.appendChild(co)
elif e.dxftype() == "LWPOLYLINE" and "LWPOLYLINE" in types:
for x, y in e.vertices():
coords += '{:.2f},{:.2f} '.format(x, y)
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