-
Notifications
You must be signed in to change notification settings - Fork 1
/
module_F.py
133 lines (98 loc) · 4.69 KB
/
module_F.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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 3 13:22:45 2017
@author: isaac
"""
__author__ = 'isaac'
# Merge Single Nodes and Intersection Nodes
#
import gpxpy
import numpy as np
from geopy.distance import vincenty
def joinSNIN(job,option):
sortFileID = job.fileStore.importFile('file:///Users/isaac/IdeaProjects/Network_Workflow/SNnodes.csv')
with job.fileStore.readGlobalFileStream(sortFileID) as fH:
coordsSN = np.loadtxt(fH,delimiter=",",usecols=(0,1,2))
sortFileID2 = job.fileStore.importFile('file:///Users/isaac/IdeaProjects/Network_Workflow/INnodes.csv')
with job.fileStore.readGlobalFileStream(sortFileID2) as fH2:
coordsIN = np.loadtxt(fH2,delimiter=",",usecols=(0,1,2,3))
routes = np.unique(coordsIN[:,2]).astype(int)
tolerance = 100.0
sort_routes = []
for route in routes:
# print "SN Route: %i" %route
routeCoordsSN = coordsSN[coordsSN[:,2]==route]
routeCoordsIn = coordsIN[coordsIN[:,2]==route]
routeCoordsIn2 = coordsIN[coordsIN[:,3]==route]
# assert len(routeCoordsSN)<2
sort_coords = []
sort_coords.append(routeCoordsSN[0][0:3].tolist())
coords = np.concatenate((routeCoordsSN[1:-1],routeCoordsIn[:,0:3],routeCoordsIn2[:,0:3]))
if len(coords) == 1:
sort_coords.append(coords[0][0:3])
else:
f = open(pathFile+str(route)+".gpx", 'r')
p = gpxpy.parse(f)
p.simplify()
p.reduce_points(500)
points = p.tracks[0].segments[0].points
for point in points:
pA = (point.longitude,point.latitude)
for idx,pB in enumerate(coords):
dis = vincenty(pA, pB[0:2].tolist()).meters
if dis<=tolerance:
sort_coords.append([pB[0],pB[1],route])
coords = np.delete(coords,idx,0)
break
if len(coords)==0:
break
# if len(coords)>0:
# print "\tHan quedado - puntos entre si cercanos: %i" %len(coords)
sort_coords.append(routeCoordsSN[-1][0:3].tolist())
sort_routes.append(sort_coords)
print "-"*50
#==============================================================================
# Rutas from bloques derechos de IN - can be improved...
#==============================================================================
routesB = np.unique(coordsIN[:,3]).astype(int)
toleranceB = 100.0
for route in routesB:
if route in routes:
continue
routeCoordsSN = coordsSN[coordsSN[:,2]==route]
routeCoordsIn = coordsIN[coordsIN[:,3]==route]
# assert len(routeCoordsSN)<2
sort_coords = []
sort_coords.append(routeCoordsSN[0][0:3].tolist())
coords = np.concatenate((routeCoordsSN[1:-1],routeCoordsIn[:,0:3]))
if len(coords) == 1:
sort_coords.append(coords[0][0:3])
else:
with open(pathFile+str(route)+".gpx", 'r') as f:
p = gpxpy.parse(f)
p.simplify()
p.reduce_points(500)
points = p.tracks[0].segments[0].points
for point in points:
pA = (point.longitude,point.latitude)
for idx,pB in enumerate(coords):
dis = vincenty(pA, pB[0:2].tolist()).meters
if dis<=toleranceB:
sort_coords.append([pB[0],pB[1],route])
coords = np.delete(coords,idx,0)
break
if len(coords)==0:
break
# if len(coords)>0:
# print "Han quedado - puntos entre si cercanos: IN"
sort_coords.append(routeCoordsSN[-1][0:3].tolist())
sort_routes.append(sort_coords)
routes = np.unique(coordsIN[:,2:4]).astype(int)
result = []
for sortR in sort_routes:
for point in sortR:
result.append(point)
result = np.array(result)
with job.fileStore.writeGlobalFileStream() as (fileHandle, outComputeSNID):
np.save(fileHandle,result)
job.fileStore.exportFile(outComputeSNID, 'file:///Users/isaac/IdeaProjects/Network_Workflow/FNodes.npy')