This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utilities.py
79 lines (65 loc) · 2.12 KB
/
utilities.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
# -*- coding: UTF-8 -*-
import time
import os
from math import log, exp as exp_
from collections import defaultdict
def complete_filename(name):
return os.path.join(os.path.dirname(__file__), name)
def format_(min_, max_):
format_ = "%.2e"
if max_ < 10000 and abs(min_) >= 0.1:
format_ = "%.2f"
return format_
def multiplier(value):
"""return a couple of multiplier and text representing it that are appropiate for
the specified range"""
multiplyers = {1e-9:u" x 10⁻⁹", 1e-6:u" x 10⁻⁶", 1e-3:u" x 10⁻³", 1.0:u"", 1e3:u" x 10³", 1e6:u" x 10⁶", 1e9:u" x 10⁹"}
mult = 1e-9
for x in sorted(multiplyers.keys()):
if x <= abs(value):
mult = x
return mult, multiplyers[mult]
def linemerge(lines):
"""Returns a (set of) LineString(s) formed by sewing together a multilinestring."""
graph = defaultdict(set)
# first build a bidirectional graph
for line in lines:
b = tuple(line[0])
e = tuple(line[-1])
graph[b].add(e)
graph[e].add(b)
for k, v in graph.iteritems():
assert(len(v) in (1,2))
# now consume the graph
if not len(graph):
return []
def depth_first_append(graph, node):
connected=[node]
direction = "first"
neigbors = graph[node]
del graph[node]
for n in neigbors:
if n in graph:
if direction == "first":
connected += depth_first_append(graph, n)
#else:
# connected = list(reversed(depth_first_append(graph, n))) + connected
direction = "second"
return connected
out = []
while len(graph):
nxt = graph.iterkeys().next()
out.append(depth_first_append(graph, nxt))
return out
# run as script for testing
if __name__ == "__main__":
#@todo: unit test multiplier
#@todo: unit test linemerge
pass
class Timer(object):
def __init__(self):
self.start = time.time()
def reset(self, text=""):
s = self.start
self.start = time.time()
return "%30s % 8.4f sec"%(text, (self.start - s))