1
1
package org .heigit .ors .routing .graphhopper .extensions .userspeed ;
2
2
3
+ import org .heigit .ors .exceptions .*;
4
+ import org .heigit .ors .routing .RoutingErrorCodes ;
3
5
import org .json .JSONObject ;
4
6
7
+ import java .util .Arrays ;
8
+ import java .util .List ;
9
+
5
10
public class RoadPropertySpeedParser {
6
- public RoadPropertySpeedMap parse (String input ) {
11
+ public RoadPropertySpeedMap parse (String input ) throws StatusCodeException {
7
12
return parse (new JSONObject (input ));
8
13
}
9
14
10
- public RoadPropertySpeedMap parse (org .json .simple .JSONObject input ) {
15
+ public RoadPropertySpeedMap parse (org .json .simple .JSONObject input ) throws StatusCodeException {
11
16
return parse (input .toJSONString ());
12
17
}
13
18
14
- public RoadPropertySpeedMap parse (JSONObject json ) {
19
+ public RoadPropertySpeedMap parse (JSONObject json ) throws StatusCodeException {
15
20
RoadPropertySpeedMap rsm = new RoadPropertySpeedMap ();
16
21
22
+ // test that only valid keys are present:
23
+ List <String > validKeys = Arrays .asList ("unit" , "roadSpeeds" , "surfaceSpeeds" );
24
+ for (String key : json .keySet ()) {
25
+ if (!validKeys .contains (key )) {
26
+ throw new UnknownParameterException (RoutingErrorCodes .UNKNOWN_PARAMETER , key );
27
+ }
28
+ }
29
+
17
30
// parse units
18
31
String unit = "kmh" ;
19
32
if (json .has ("unit" )) {
20
33
unit = json .getString ("unit" );
21
34
}
22
- double unitFactor = 1.0 ;
23
- if (unit .equals ("mph" )) {
35
+
36
+ double unitFactor ;
37
+ if (unit .equals ("kmh" )) {
38
+ unitFactor = 1.0 ;
39
+ } else if (unit .equals ("mph" )) {
24
40
unitFactor = 1.60934 ;
41
+ } else {
42
+ throw new ParameterValueException (RoutingErrorCodes .INVALID_PARAMETER_VALUE , "unit" , unit );
25
43
}
26
44
27
45
// parse road speeds
28
46
JSONObject roadSpeeds ;
29
47
if (json .has ("roadSpeeds" )) {
30
48
roadSpeeds = json .getJSONObject ("roadSpeeds" );
31
49
for (String roadType : roadSpeeds .keySet ()) {
32
- rsm .addRoadPropertySpeed (roadType , roadSpeeds .getDouble (roadType )*unitFactor );
50
+ try {
51
+ rsm .addRoadPropertySpeed (roadType , roadSpeeds .getDouble (roadType ) * unitFactor );
52
+ } catch (IllegalArgumentException e ) {
53
+ if (e .getMessage ().contains ("must be" )) {
54
+ throw new ParameterOutOfRangeException (RoutingErrorCodes .INVALID_PARAMETER_VALUE , "speed" );
55
+ } else {
56
+ throw new UnknownParameterException (RoutingErrorCodes .UNKNOWN_PARAMETER , e .getMessage ().substring (23 ));
57
+ }
58
+ }
33
59
}
34
60
}
35
61
@@ -38,7 +64,15 @@ public RoadPropertySpeedMap parse(JSONObject json) {
38
64
if (json .has ("surfaceSpeeds" )) {
39
65
surfaceSpeeds = json .getJSONObject ("surfaceSpeeds" );
40
66
for (String surfaceType : surfaceSpeeds .keySet ()) {
41
- rsm .addRoadPropertySpeed (surfaceType , surfaceSpeeds .getDouble (surfaceType )*unitFactor );
67
+ try {
68
+ rsm .addRoadPropertySpeed (surfaceType , surfaceSpeeds .getDouble (surfaceType ) * unitFactor );
69
+ } catch (IllegalArgumentException e ) {
70
+ if (e .getMessage ().contains ("must be" )) {
71
+ throw new ParameterOutOfRangeException (RoutingErrorCodes .INVALID_PARAMETER_VALUE , "speed" );
72
+ } else {
73
+ throw new UnknownParameterException (RoutingErrorCodes .UNKNOWN_PARAMETER , e .getMessage ().substring (23 ));
74
+ }
75
+ }
42
76
}
43
77
}
44
78
0 commit comments