-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetAM.m
116 lines (100 loc) · 3.39 KB
/
getAM.m
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
function [nodes, HAM, DAM, OAM] = getAM(place,simple)
% Outputs highway class & distance adjacency sparse matrix and node list
%
% DETAIL:
% Converts the results from the database and converts it into
% appropriate adjacency matrix containing highway class information,
% another adjacency matrix with distance between the nodes and a
% separate list of reference nodes.
% INPUT:
% place (String)
% OUTPUT:
% HAM(i,j) (Sparse) - Adjacency matrix containing the highway
% class between nodes i and j
% DAM(i,j) (Sparse) - Adjacency matrix containing the distance
% information between nodes i and j
% nodes (Double x 2) - Node longitude and latitude of the array
% index for reference by HAM & DAM
% EXAMPLE:
% [nodes, HAM, DAM, OAM] = getAM('Bristol')
load('global');
if ~exist('simple','var')
simple = false;
end
fp = ['./cache/highway/' DBase '/' place '/'];
fNodes = [fp 'nodes.mat'];
fHAM = [fp 'HAM.mat'];
fDAM = [fp 'DAM.mat'];
fOAM = [fp 'OAM.mat'];
if ~exist(fp,'file')
mkdir(fp);
end
if exist(fNodes,'file')&&exist(fHAM,'file')&&exist(fDAM,'file')&&exist(fOAM,'file')
load(fNodes,'nodes');
load(fHAM,'HAM');
load(fDAM,'DAM');
load(fOAM,'OAM');
else
tic;
highwayResult = getHighway(place);
% removes all duplicate coordinates and makes them individual nodes
nodes = unique(highwayResult(:,1:2),'rows');
m = length(nodes);
n = 2;
thisNode = 0;
thatNode = 0;
% process adjacency matrix
HAM = sparse(m,m);
DAM = sparse(m,m);
OAM = sparse(m,m);
nR = length(highwayResult);
step = 'Converting result to AM...';
for i = 1:nR
% If the index of the line segment is 1, start a new road
if (highwayResult(i,3) == 1)
thisNode = 0;
else
thisNode = thatNode;
end
thatNode = find(any(all(bsxfun(@eq,reshape(highwayResult(i,1:2).',1,n,[]),nodes),2),3)); % 41.381587 seconds
if (thisNode && thatNode)
HAM(thisNode,thatNode)=highwayResult(i,4);
DAM(thisNode,thatNode)=haversine([nodes(thisNode,1) nodes(thatNode,1)],[nodes(thisNode,2) nodes(thatNode,2)]);
if ~highwayResult(i,5) % If not one-way
HAM(thatNode,thisNode) = HAM(thisNode,thatNode);
DAM(thatNode,thisNode) = DAM(thisNode,thatNode);
OAM(thisNode,thatNode) = 2;
else
OAM(thisNode,thatNode) = 1;
end
end
if mod(i,100) == 0 || mod(i,nR) == 0
completed = i/nR;
progress = [num2str(i) ' of ' num2str(nR) ' complete.'];
disp([place ': ' step progress]);
end
end
save(fNodes,'nodes');
save(fHAM,'HAM');
save(fDAM,'DAM');
save(fOAM,'OAM');
toc;
end
if (simple)
fNodes = [fp 'snodes.mat'];
fHAM = [fp 'sHAM.mat'];
fDAM = [fp 'sDAM.mat'];
fOAM = [fp 'sOAM.mat'];
if exist(fNodes,'file')&&exist(fHAM,'file')&&exist(fDAM,'file')&&exist(fOAM,'file')
load(fNodes,'nodes');
load(fHAM,'HAM');
load(fDAM,'DAM');
load(fOAM,'OAM');
else
[nodes,HAM,DAM,OAM]=simplifyAM(nodes,HAM,DAM,OAM,place);
save(fNodes,'nodes');
save(fHAM,'HAM');
save(fDAM,'DAM');
save(fOAM,'OAM');
end
end