-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathharris2gv.py
149 lines (130 loc) · 5.07 KB
/
harris2gv.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Ark
A QGIS plugin
QGIS Plugin for ARK, the Archaeological Recording Kit
-------------------
begin : 2015-03-02
git sha : $Format:%H$
copyright : (C) 2015 by L - P: Heritage LLP
copyright : (C) 2015 by John Layt
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from sys import argv
source =''
destination = ''
dataset = ''
nodes = []
edges = []
def parseValueList(input, tag):
return input[len(tag):].strip().replace(',', ' ').split()
def getNode(id):
for node in nodes:
if node['id'] == id:
return node
def setNode(node, unit, above, below):
if node is None or node == '':
return
nodes.append({'id': node, 'unit': unit, 'above': above, 'below': below})
def setEdges(parent, children):
for child in children:
edges.append({'from': parent, 'to': child, 'weight': 1})
def childEdgesForNode(id):
children = []
for edge in edges:
if edge['from'] == id:
children.append(edge)
return children
def parentEdgesForNode(id):
parents = []
for edge in edges:
if edge['to'] == id:
parents.append(edge)
return parents
def readFile():
with open(source) as file:
#print 'Opened file : ' + source
line = file.readline().strip()
if (line is None or not line.startswith('Stratigraphic Dataset')):
print 'Invalid Header Line'
return
dataset = line.strip()[len('Stratigraphic Dataset'):].strip()
#print 'Reading dataset: ' + dataset
line = file.readline().strip()
if (line is None or line != ''):
print 'Missing Blank Line'
return
line = file.readline().strip()
if (line is None or line != 'Name'):
print 'Invalid Name Line'
return
context = ''
above = []
contemporary = []
equal = []
below = []
unit = ''
for line in file:
if (line == ''):
pass
elif (not line.startswith(' ')):
setNode(context, unit, len(above), len(below))
setEdges(context, below)
context = line.strip()
above = []
contemporary = []
equal = []
below = []
unit = ''
else:
attribute = line.strip()
if (attribute.lower().startswith('above:')):
above.extend(parseValueList(attribute, 'above:'))
elif (attribute.lower().startswith('contemporary with:')):
contemporary.extend(parseValueList(attribute, 'contemporary with:'))
elif (attribute.lower().startswith('equal to:')):
equal.extend(parseValueList(attribute, 'equal to:'))
elif (attribute.lower().startswith('below:')):
below.extend(parseValueList(attribute, 'below:'))
elif (attribute.lower().startswith('unit class:')):
unit = attribute[len('unit class:'):].strip()
setNode(context, unit, len(above), len(below))
setEdges(context, below)
def weightEdges():
for edge in edges:
if getNode(edge['from'])['below'] == 1:
edge['weight'] = edge['weight'] + 2
if getNode(edge['to'])['above'] == 1:
edge['weight'] = edge['weight'] + 2
if len(argv) == 2:
cmd, source = argv
else:
cmd, source, destination = argv
readFile()
weightEdges()
print 'strict digraph' + dataset.replace(' ', '_') + '{'
print ' splines=polyline' # Should be ortho but ports support not implemented
print ' concentrate=true'
print ' remincross=true'
print ' ranksep="1.0 equally"'
print ' nodesep="2.0 equally"'
print ' node [shape=box]'
print ' edge [arrowhead=none]' # headport=n tailport=s
#print 'Nodes:'
#for node in nodes:
# print ' "' + node['id'] + '" [label="' + node['id'] + '", xlabel="' + node['id'] + '", pos="0,0"]'
# print node
#print 'Edges'
for edge in edges:
print ' ' + '"' + edge['from'] + '"' + ' -> ' + '"' + edge['to'] + '";'# [weight=' + str(edge['weight']) + '];'
print '}'