This repository has been archived by the owner on May 24, 2024. It is now read-only.
forked from abrensch/brouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerHandler.java
340 lines (295 loc) · 10.3 KB
/
ServerHandler.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package btools.server.request;
import btools.mapaccess.OsmNode;
import btools.router.OsmNodeNamed;
import btools.router.OsmNogoPolygon;
import btools.router.OsmTrack;
import btools.router.RoutingContext;
import btools.server.ServiceContext;
import java.io.BufferedWriter;
import java.io.File;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* URL query parameter handler for web and standalone server. Supports all
* BRouter features without restrictions.
*
* Parameters:
*
* lonlats = lon,lat|... (unlimited list of lon,lat waypoints separated by |)
* nogos = lon,lat,radius|... (optional, radius in meters)
* profile = profile file name without .brf
* alternativeidx = [0|1|2|3] (optional, default 0)
* format = [kml|gpx|geojson] (optional, default gpx)
* trackname = name used for filename and format specific trackname (optional, default brouter)
* exportWaypoints = 1 to export them (optional, default is no export)
* pois = lon,lat,name|... (optional)
*
* Example URLs:
* {@code http://localhost:17777/brouter?lonlats=8.799297,49.565883|8.811764,49.563606&nogos=&profile=trekking&alternativeidx=0&format=gpx}
* {@code http://localhost:17777/brouter?lonlats=1.1,1.2|2.1,2.2|3.1,3.2|4.1,4.2&nogos=-1.1,-1.2,1|-2.1,-2.2,2&profile=shortest&alternativeidx=1&format=kml&trackname=Ride&pois=1.1,2.1,Barner Bar}
*
*/
public class ServerHandler extends RequestHandler {
private RoutingContext rc;
public ServerHandler( ServiceContext serviceContext, HashMap<String, String> params )
{
super( serviceContext, params );
}
@Override
public RoutingContext readRoutingContext()
{
rc = new RoutingContext();
rc.memoryclass = 128;
String profile = params.get( "profile" );
// when custom profile replace prefix with directory path
if ( profile.startsWith( ProfileUploadHandler.CUSTOM_PREFIX ) )
{
String customProfile = profile.substring( ProfileUploadHandler.CUSTOM_PREFIX.length() );
profile = new File( serviceContext.customProfileDir, customProfile ).getPath();
}
else if ( profile.startsWith( ProfileUploadHandler.SHARED_PREFIX ) )
{
String customProfile = profile.substring( ProfileUploadHandler.SHARED_PREFIX.length() );
profile = new File( serviceContext.sharedProfileDir, customProfile ).getPath();
}
rc.localFunction = profile;
rc.setAlternativeIdx(Integer.parseInt(params.get( "alternativeidx" )));
List<OsmNodeNamed> poisList = readPoisList();
rc.poipoints = poisList;
List<OsmNodeNamed> nogoList = readNogoList();
List<OsmNodeNamed> nogoPolygonsList = readNogoPolygons();
if ( nogoList != null )
{
RoutingContext.prepareNogoPoints( nogoList );
rc.nogopoints = nogoList;
}
if (rc.nogopoints == null)
{
rc.nogopoints = nogoPolygonsList;
}
else if ( nogoPolygonsList != null )
{
rc.nogopoints.addAll(nogoPolygonsList);
}
return rc;
}
@Override
public List<OsmNodeNamed> readWayPointList()
{
// lon,lat|...
String lonLats = params.get( "lonlats" );
if (lonLats == null) throw new IllegalArgumentException( "lonlats parameter not set" );
String[] coords = lonLats.split("\\|");
if (coords.length < 2)
throw new IllegalArgumentException( "we need two lat/lon points at least!" );
List<OsmNodeNamed> wplist = new ArrayList<OsmNodeNamed>();
for (int i = 0; i < coords.length; i++)
{
String[] lonLat = coords[i].split(",");
if (lonLat.length < 2)
throw new IllegalArgumentException( "we need two lat/lon points at least!" );
wplist.add( readPosition( lonLat[0], lonLat[1], "via" + i ) );
}
wplist.get(0).name = "from";
wplist.get(wplist.size()-1).name = "to";
return wplist;
}
@Override
public String formatTrack(OsmTrack track)
{
String result;
// optional, may be null
String format = params.get( "format" );
String trackName = getTrackName();
if (trackName != null) {
track.name = trackName;
}
String exportWaypointsStr = params.get( "exportWaypoints" );
if (exportWaypointsStr != null && Integer.parseInt(exportWaypointsStr) != 0) {
track.exportWaypoints = true;
}
if (format == null || "gpx".equals(format))
{
result = track.formatAsGpx();
}
else if ("kml".equals(format))
{
result = track.formatAsKml();
}
else if ("geojson".equals(format))
{
result = track.formatAsGeoJson();
}
else if ("csv".equals(format))
{
try
{
StringWriter sw = new StringWriter();
BufferedWriter bw = new BufferedWriter(sw);
track.writeMessages( bw, rc );
return sw.toString();
}
catch (Exception ex)
{
return "Error: " + ex.getMessage();
}
}
else {
System.out.println("unknown track format '" + format + "', using default");
result = track.formatAsGpx();
}
return result;
}
@Override
public String getMimeType()
{
// default
String result = "text/plain";
// optional, may be null
String format = params.get( "format" );
if ( format != null )
{
if ( "gpx".equals( format ) )
{
result = "application/gpx+xml";
}
else if ( "kml".equals( format ) )
{
result = "application/vnd.google-earth.kml+xml";
}
else if ( "geojson".equals( format ) )
{
result = "application/vnd.geo+json";
}
else if ( "csv".equals( format ) )
{
result = "text/tab-separated-values";
}
}
return result;
}
@Override
public String getFileName()
{
String fileName = null;
String format = params.get( "format" );
String trackName = getTrackName();
if ( format != null )
{
fileName = ( trackName == null ? "brouter" : trackName ) + "." + format;
}
return fileName;
}
private String getTrackName()
{
return params.get( "trackname" ) == null ? null : params.get( "trackname" ).replaceAll("[^a-zA-Z0-9 \\._\\-]+", "");
}
private static OsmNodeNamed readPosition( String vlon, String vlat, String name )
{
if ( vlon == null ) throw new IllegalArgumentException( "lon " + name + " not found in input" );
if ( vlat == null ) throw new IllegalArgumentException( "lat " + name + " not found in input" );
return readPosition(Double.parseDouble( vlon ), Double.parseDouble( vlat ), name);
}
private static OsmNodeNamed readPosition( double lon, double lat, String name )
{
OsmNodeNamed n = new OsmNodeNamed();
n.name = name;
n.ilon = (int)( ( lon + 180. ) *1000000. + 0.5);
n.ilat = (int)( ( lat + 90. ) *1000000. + 0.5);
return n;
}
private List<OsmNodeNamed> readPoisList()
{
// lon,lat,name|...
String pois = params.get( "pois" );
if ( pois == null ) return null;
String[] lonLatNameList = pois.split("\\|");
List<OsmNodeNamed> poisList = new ArrayList<OsmNodeNamed>();
for (int i = 0; i < lonLatNameList.length; i++)
{
String[] lonLatName = lonLatNameList[i].split(",");
OsmNodeNamed n = new OsmNodeNamed();
n.ilon = (int)( ( Double.parseDouble(lonLatName[0]) + 180. ) *1000000. + 0.5);
n.ilat = (int)( ( Double.parseDouble(lonLatName[1]) + 90. ) *1000000. + 0.5);
n.name = lonLatName[2];
poisList.add(n);
}
return poisList;
}
private List<OsmNodeNamed> readNogoList()
{
// lon,lat,radius|...
String nogos = params.get( "nogos" );
if ( nogos == null ) return null;
String[] lonLatRadList = nogos.split("\\|");
List<OsmNodeNamed> nogoList = new ArrayList<OsmNodeNamed>();
for (int i = 0; i < lonLatRadList.length; i++)
{
String[] lonLatRad = lonLatRadList[i].split(",");
String nogoWeight = "NaN";
if (lonLatRad.length > 3) {
nogoWeight = lonLatRad[3];
}
nogoList.add(readNogo(lonLatRad[0], lonLatRad[1], lonLatRad[2], nogoWeight));
}
return nogoList;
}
private static OsmNodeNamed readNogo( String lon, String lat, String radius, String nogoWeight )
{
double weight = "undefined".equals( nogoWeight ) ? Double.NaN : Double.parseDouble( nogoWeight );
return readNogo(Double.parseDouble( lon ), Double.parseDouble( lat ), Integer.parseInt( radius ), weight );
}
private static OsmNodeNamed readNogo( double lon, double lat, int radius, double nogoWeight )
{
OsmNodeNamed n = new OsmNodeNamed();
n.name = "nogo" + radius;
n.ilon = (int)( ( lon + 180. ) *1000000. + 0.5);
n.ilat = (int)( ( lat + 90. ) *1000000. + 0.5);
n.isNogo = true;
n.nogoWeight = nogoWeight;
return n;
}
private List<OsmNodeNamed> readNogoPolygons()
{
List<OsmNodeNamed> result = new ArrayList<OsmNodeNamed>();
parseNogoPolygons( params.get("polylines"), result, false );
parseNogoPolygons( params.get("polygons"), result, true );
return result.size() > 0 ? result : null;
}
private static void parseNogoPolygons(String polygons, List<OsmNodeNamed> result, boolean closed )
{
if ( polygons != null )
{
String[] polygonList = polygons.split("\\|");
for (int i = 0; i < polygonList.length; i++)
{
String[] lonLatList = polygonList[i].split(",");
if ( lonLatList.length > 1 )
{
OsmNogoPolygon polygon = new OsmNogoPolygon(closed);
int j;
for (j = 0; j < 2 * (lonLatList.length / 2) - 1;)
{
String slon = lonLatList[j++];
String slat = lonLatList[j++];
int lon = (int)( ( Double.parseDouble(slon) + 180. ) *1000000. + 0.5);
int lat = (int)( ( Double.parseDouble(slat) + 90. ) *1000000. + 0.5);
polygon.addVertex(lon, lat);
}
String nogoWeight = "NaN";
if (j < lonLatList.length) {
nogoWeight = lonLatList[j];
}
polygon.nogoWeight = Double.parseDouble( nogoWeight );
if ( polygon.points.size() > 0 )
{
polygon.calcBoundingCircle();
result.add(polygon);
}
}
}
}
}
}