forked from abrensch/brouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BRouter.java
143 lines (127 loc) · 4.99 KB
/
BRouter.java
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
package btools.server;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.io.File;
import btools.router.OsmNodeNamed;
import btools.router.RoutingContext;
import btools.router.RoutingEngine;
import btools.router.SearchBoundary;
public class BRouter {
public static void main(String[] args) throws Exception {
if (args.length == 2) // cgi-input-mode
{
try {
String queryString = args[1];
int sepIdx = queryString.indexOf('=');
if (sepIdx >= 0) queryString = queryString.substring(sepIdx + 1);
queryString = URLDecoder.decode(queryString, "ISO-8859-1");
int ntokens = 1;
for (int ic = 0; ic < queryString.length(); ic++) {
if (queryString.charAt(ic) == '_') ntokens++;
}
String[] a2 = new String[ntokens + 1];
int idx = 1;
int pos = 0;
for (; ; ) {
int p = queryString.indexOf('_', pos);
if (p < 0) {
a2[idx++] = queryString.substring(pos);
break;
}
a2[idx++] = queryString.substring(pos, p);
pos = p + 1;
}
// cgi-header
System.out.println("Content-type: text/plain");
System.out.println();
OsmNodeNamed from = readPosition(a2, 1, "from");
OsmNodeNamed to = readPosition(a2, 3, "to");
int airDistance = from.calcDistance(to);
String airDistanceLimit = System.getProperty("airDistanceLimit");
if (airDistanceLimit != null) {
int maxKm = Integer.parseInt(airDistanceLimit);
if (airDistance > maxKm * 1000) {
System.out.println("airDistance " + (airDistance / 1000) + "km exceeds limit for online router (" + maxKm + "km)");
return;
}
}
long maxRunningTime = 60000; // the cgi gets a 1 Minute timeout
String sMaxRunningTime = System.getProperty("maxRunningTime");
if (sMaxRunningTime != null) {
maxRunningTime = Integer.parseInt(sMaxRunningTime) * 1000;
}
List<OsmNodeNamed> wplist = new ArrayList<OsmNodeNamed>();
wplist.add(from);
wplist.add(to);
RoutingEngine re = new RoutingEngine(null, null, new File(args[0]), wplist, readRoutingContext(a2));
re.doRun(maxRunningTime);
if (re.getErrorMessage() != null) {
System.out.println(re.getErrorMessage());
}
} catch (Throwable e) {
System.out.println("unexpected exception: " + e);
}
System.exit(0);
}
System.out.println("BRouter " + BRouter.class.getPackage().getImplementationVersion());
if (args.length < 6) {
System.out.println("Find routes in an OSM map");
System.out.println("usage: java -jar brouter.jar <segmentdir> <lon-from> <lat-from> <lon-to> <lat-to> <profile>");
return;
}
List<OsmNodeNamed> wplist = new ArrayList<OsmNodeNamed>();
wplist.add(readPosition(args, 1, "from"));
RoutingEngine re = null;
if ("seed".equals(args[3])) {
int searchRadius = Integer.parseInt(args[4]); // if = 0 search a 5x5 square
String filename = SearchBoundary.getFileName(wplist.get(0));
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("traffic/" + filename)));
for (int direction = 0; direction < 8; direction++) {
RoutingContext rc = readRoutingContext(args);
SearchBoundary boundary = new SearchBoundary(wplist.get(0), searchRadius, direction / 2);
rc.trafficOutputStream = dos;
rc.inverseDirection = (direction & 1) != 0;
re = new RoutingEngine("mytrack", "mylog", new File(args[0]), wplist, rc);
re.boundary = boundary;
re.airDistanceCostFactor = rc.trafficDirectionFactor;
rc.countTraffic = true;
re.doSearch();
if (re.getErrorMessage() != null) {
break;
}
}
dos.close();
} else {
wplist.add(readPosition(args, 3, "to"));
RoutingContext rc = readRoutingContext(args);
re = new RoutingEngine("mytrack", "mylog", new File(args[0]), wplist, rc);
re.doRun(0);
}
if (re.getErrorMessage() != null) {
System.out.println(re.getErrorMessage());
}
}
private static OsmNodeNamed readPosition(String[] args, int idx, String name) {
OsmNodeNamed n = new OsmNodeNamed();
n.name = name;
n.ilon = (int) ((Double.parseDouble(args[idx]) + 180.) * 1000000. + 0.5);
n.ilat = (int) ((Double.parseDouble(args[idx + 1]) + 90.) * 1000000. + 0.5);
return n;
}
private static RoutingContext readRoutingContext(String[] args) {
RoutingContext c = new RoutingContext();
if (args.length > 5) {
c.localFunction = args[5];
if (args.length > 6) {
c.setAlternativeIdx(Integer.parseInt(args[6]));
}
}
c.memoryclass = (int) (Runtime.getRuntime().maxMemory() / 1024 / 1024);
// c.startDirection= Integer.valueOf( 150 );
return c;
}
}