-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremove_loops.py
More file actions
49 lines (41 loc) · 1.53 KB
/
remove_loops.py
File metadata and controls
49 lines (41 loc) · 1.53 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
#!/usr/bin/env python
#
# Script to remove loops from rivers
#
# Example:
# /---------\
# ----+ \--+-------- => ----+ +--------
# \------------/ \------------/
def remove_loops(river):
loops = []
for i in range(0,len(river['paths'])):
for j in range(0,len(river['paths'])):
if i!=j:
j_has_start_of_i = False
j_has_end_of_i = False
for k in range(0,len(river['paths'][j])):
if river['paths'][i][0]==river['paths'][j][k]:
j_has_start_of_i = True
if river['paths'][i][-1]==river['paths'][j][k]:
j_has_end_of_i = True
if j_has_start_of_i and j_has_end_of_i:
loops.append(i)
print 'path %d is a loop of %d' % (i,j)
newpaths = []
for i in range(0,len(river['paths'])):
if i not in loops:
newpaths.append(river['paths'][i])
#print '%s: path: %d->%d points: %d->%d' % (river['name'].encode('latin1'),len(river['paths']),len(newpaths),sum(map(len,river['paths'])),sum(map(len,newpaths)))
river['paths'] = newpaths
def main():
input_filename = 'rivers-merged.json'
output_filename = 'rivers-merged-cleaned.json'
import json
with open(input_filename,'r') as f:
rivers = json.load(f)
for river in rivers:
remove_loops(river)
with open(output_filename,'w') as f:
json.dump(rivers,f)
if __name__=='__main__':
main()