-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2dmGraphML.py
More file actions
97 lines (85 loc) · 3.69 KB
/
Copy path2dmGraphML.py
File metadata and controls
97 lines (85 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import numpy as np
#set workspace
myworkspace = "D:/temp"
#set the name of the .2dm file (mesh-file for BASEMENT-ETH flood simulation model)
meshfilename = "testgraphml.2dm"
#set the name of the output file
outputfilename = "out.graphml"
outputfilefullname = myworkspace+"/"+outputfilename
#parse the mesh and fill the nodes-array with x,y,z of nodes and triangles-list
mesh = open(myworkspace+"/"+meshfilename, "r")
countnodes = 0
counttriangles = 0
countedges = 0
nodeslist = []
triangleslist = []
edgeslist = []
templist = []
temptrianglelist = []
print "importing the mesh "+str(meshfilename)+" ..."
#loop through all nodes and triangles in the .2dm mesh file
for line in mesh:
tokens = line.strip().split()
if tokens[0] == 'ND':
nodeslist.append(tokens[1:5])
countnodes += 1
if tokens[0] == 'E3T':
triangleslist.append(tokens[1:5])
counttriangles += 1
print "mesh file "+str(meshfilename)+" imported ..."
print "number of imported nodes in mesh: "+str(countnodes)
print "number of imported triangles in mesh: "+str(counttriangles)
mesh.close()
#create an array for the nodes and their attributes (id, x, y, z)
nodesarray = np.zeros((countnodes, 4), dtype=np.float32)
linenumber=0
for node in nodeslist:
nodesarray[linenumber,0]=float(node[0])
nodesarray[linenumber,1]=float(node[1])
nodesarray[linenumber,2]=float(node[2])
nodesarray[linenumber,3]=float(node[3])
linenumber += 1
nodeslist = []
#create an array for the triangles
trianglesarray = np.zeros((counttriangles, 4), dtype=np.int)
linenumber=0
for triangle in triangleslist:
trianglesarray[linenumber,0]=int(triangle[0])
trianglesarray[linenumber,1]=int(triangle[1])
trianglesarray[linenumber,2]=int(triangle[2])
trianglesarray[linenumber,3]=int(triangle[3])
linenumber += 1
#create and open the output file
outputfile = open(outputfilefullname, "w")
#write the BMG header file
outputfile.write('<?xml version="1.0" encoding="UTF-8"?>' + '\n')
outputfile.write('<graphml xmlns="http://graphml.graphdrawing.org/xmlns"'+ '\n')
outputfile.write(' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'+ '\n')
outputfile.write(' xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns'+ '\n')
outputfile.write(' http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">'+ '\n')
outputfile.write('<key id="x" attr.name="x" attr.type="double" for="node" />'+ '\n')
outputfile.write('<key id="y" attr.name="y" attr.type="double" for="node" />'+ '\n')
#outputfile.write('<key id="z" for="node" attr.name="z" attr.type="double" /key>'+ '\n')
outputfile.write('<graph edgedefault="undirected">'+ '\n')
#loop through the nodes-array and write the attributes to the graphml file
i = 0
while i < countnodes:
outputfile.write('<node id="' + str(int(nodesarray[i,0])) + '">' + '\n')
outputfile.write(' <data key="x">' + str(nodesarray[i,1]) + '</data>' + '\n')
outputfile.write(' <data key="y">' + str(nodesarray[i,2]) + '</data>' + '\n')
outputfile.write('</node>'+ '\n')
i += 1
#loop through the triangles-array and write the edges
j = 0
while j < counttriangles:
outputfile.write('<edge source="' + str(int(trianglesarray[j,1])) + '" target="'+str(int(trianglesarray[j,2])) +'">' + '\n')
outputfile.write('</edge>'+ '\n')
outputfile.write('<edge source="' + str(int(trianglesarray[j,1])) + '" target="'+str(int(trianglesarray[j,3])) +'">' + '\n')
outputfile.write('</edge>'+ '\n')
outputfile.write('<edge source="' + str(int(trianglesarray[j,2])) + '" target="'+str(int(trianglesarray[j,3])) +'">' + '\n')
outputfile.write('</edge>'+ '\n')
j += 1
outputfile.write('</graph>' + '\n')
outputfile.write('</graphml>' + '\n')
outputfile.close()
print "output written .."