Skip to content

Commit c07b802

Browse files
easbarkarussell
andauthored
Use profiles in web UI / graphhopper maps (graphhopper#2023)
Co-authored-by: Peter <[email protected]>
1 parent cc24def commit c07b802

File tree

11 files changed

+162
-137
lines changed

11 files changed

+162
-137
lines changed

reader-gtfs/src/main/resources/assets/pt/data/Query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const CreateQuery = (baseUrl, search) => {
1818
}
1919
url.searchParams.set("pt.limit_solutions", search.limitSolutions);
2020
url.searchParams.set("locale", "en-US");
21-
url.searchParams.set("vehicle", "pt");
21+
url.searchParams.set("profile", "pt");
2222
url.searchParams.set("pt.profile", true);
2323
return url.toString();
2424
};

web-bundle/src/main/java/com/graphhopper/http/PtRedirectFilter.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,24 @@ public class PtRedirectFilter implements ContainerRequestFilter {
2929
public void filter(ContainerRequestContext requestContext) {
3030
if (shouldRedirect(requestContext)) {
3131
if (requestContext.getUriInfo().getPath().equals("route")) {
32-
URI forwardURI = requestContext.getUriInfo().getRequestUriBuilder().replacePath("/route-pt").replaceQueryParam("vehicle").build();
32+
URI forwardURI = requestContext.getUriInfo().getRequestUriBuilder().replacePath("/route-pt")
33+
.replaceQueryParam("vehicle")
34+
.replaceQueryParam("profile")
35+
.build();
3336
requestContext.setRequestUri(forwardURI);
3437
} else if (requestContext.getUriInfo().getPath().equals("isochrone")) {
35-
URI forwardURI = requestContext.getUriInfo().getRequestUriBuilder().replacePath("/isochrone-pt").replaceQueryParam("vehicle").build();
38+
URI forwardURI = requestContext.getUriInfo().getRequestUriBuilder().replacePath("/isochrone-pt")
39+
.replaceQueryParam("vehicle")
40+
.replaceQueryParam("profile")
41+
.build();
3642
requestContext.setRequestUri(forwardURI);
3743
}
3844
}
3945
}
4046

4147
private boolean shouldRedirect(ContainerRequestContext requestContext) {
4248
String maybeVehicle = requestContext.getUriInfo().getQueryParameters().getFirst("vehicle");
43-
return maybeVehicle != null && maybeVehicle.equals("pt");
49+
String maybeProfile = requestContext.getUriInfo().getQueryParameters().getFirst("profile");
50+
return "pt".equals(maybeVehicle) || "pt".equals(maybeProfile);
4451
}
4552
}

web-bundle/src/main/java/com/graphhopper/resources/InfoResource.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.graphhopper.GraphHopper;
2121
import com.graphhopper.GraphHopperConfig;
22+
import com.graphhopper.config.ProfileConfig;
2223
import com.graphhopper.routing.profiles.*;
2324
import com.graphhopper.storage.GraphHopperStorage;
2425
import com.graphhopper.util.Constants;
@@ -51,16 +52,25 @@ public InfoResource(GraphHopperConfig config, GraphHopper graphHopper, @Named("h
5152
}
5253

5354
public static class Info {
54-
public static class PerVehicle {
55-
public boolean elevation;
56-
public boolean turn_costs;
55+
public static class ProfileData {
56+
public ProfileData() {
57+
}
58+
59+
public ProfileData(String profileName, String vehicle) {
60+
this.profileName = profileName;
61+
this.vehicle = vehicle;
62+
}
63+
64+
public String profileName;
65+
public String vehicle;
5766
}
5867

5968
public BBox bbox;
69+
public final List<ProfileData> profiles = new ArrayList<>();
6070
public List<String> supported_vehicles;
61-
public Map<String, List<Object>> encoded_values;
62-
public final Map<String, PerVehicle> features = new HashMap<>();
6371
public String version = Constants.VERSION;
72+
public boolean elevation;
73+
public Map<String, List<Object>> encoded_values;
6474
public String import_date;
6575
public String data_date;
6676
public String prepare_ch_date;
@@ -72,20 +82,19 @@ public Info getInfo() {
7282
final Info info = new Info();
7383
// use bbox always without elevation (for backward compatibility)
7484
info.bbox = new BBox(storage.getBounds().minLon, storage.getBounds().maxLon, storage.getBounds().minLat, storage.getBounds().maxLat);
85+
for (ProfileConfig p : config.getProfiles()) {
86+
Info.ProfileData profileData = new Info.ProfileData(p.getName(), p.getVehicle());
87+
info.profiles.add(profileData);
88+
}
89+
if (config.has("gtfs.file"))
90+
info.profiles.add(new Info.ProfileData("pt", "pt"));
91+
92+
info.elevation = hasElevation;
7593
List<String> encoderNames = Arrays.asList(storage.getEncodingManager().toString().split(","));
7694
info.supported_vehicles = new ArrayList<>(encoderNames);
7795
if (config.has("gtfs.file")) {
7896
info.supported_vehicles.add("pt");
7997
}
80-
for (String encoderName : encoderNames) {
81-
Info.PerVehicle perVehicleJson = new Info.PerVehicle();
82-
perVehicleJson.elevation = hasElevation;
83-
perVehicleJson.turn_costs = storage.getEncodingManager().getEncoder(encoderName).supportsTurnCosts();
84-
info.features.put(encoderName, perVehicleJson);
85-
}
86-
if (config.has("gtfs.file")) {
87-
info.features.put("pt", new InfoResource.Info.PerVehicle());
88-
}
8998
info.import_date = storage.getProperties().get("datareader.import.date");
9099
info.data_date = storage.getProperties().get("datareader.data.date");
91100
info.prepare_ch_date = storage.getProperties().get("prepare.ch.date");

web/src/main/resources/assets/css/style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ td img.pic {
299299
vertical-align: middle;
300300
}
301301

302-
.selectvehicle {
302+
.selectprofile {
303303
background-color: #bbb;
304304
-moz-border-radius: 0px;
305305
-webkit-border-radius: 0px;

web/src/main/resources/assets/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
</div>
4747

4848
<div id="options">
49-
<span id="vehicles">
49+
<span id="profiles">
5050

5151
</span>
5252
</div>
@@ -61,7 +61,7 @@
6161
<input id="searchButton" type="submit" value="Search">
6262
</form>
6363
<div id="flex-input-link" class="left" style="cursor: pointer"><small>flex</small></div>
64-
<div id="export-link" class="left"><a href="/maps"><img src='img/link.png'></a></div>
64+
<div id="export-link" class="left"><a href="/maps/"><img src='img/link.png'></a></div>
6565
<div id="gpxExportButton"><a href=""><img alt="gpx" src='img/gpx.png'></a></div>
6666
<div id="flex-input" style="display: none">
6767
<textarea id="flex-input-text" name="Text1" cols="34" rows="10"></textarea>

web/src/main/resources/assets/js/graphhopper/GHRequest.js

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ var GHRequest = function (host, api_key) {
3232
this.route = new GHRoute(new GHInput(), new GHInput());
3333
this.from = this.route.first();
3434
this.to = this.route.last();
35-
this.features = {};
35+
this.profiles = []
3636

3737
this.do_zoom = true;
3838
this.useMiles = false;
3939
this.dataType = "json";
40-
this.api_params = {"locale": "en", "vehicle": "car", "weighting": "fastest", "key": api_key, "pt": {}};
40+
this.api_params = {"locale": "en", "key": api_key, "pt": {}};
4141

4242
// register events
4343
this.route.addListener('route.add', function (evt) {
@@ -78,7 +78,8 @@ GHRequest.prototype.init = function (params) {
7878
if ('use_miles' in params)
7979
this.useMiles = params.use_miles;
8080

81-
this.initVehicle(this.api_params.vehicle);
81+
if (!this.api_params.profile)
82+
this.api_params.profile = this.profiles[0].profile_name
8283

8384
if (params.q) {
8485
var qStr = params.q;
@@ -118,34 +119,56 @@ GHRequest.prototype.getEarliestDepartureTime = function () {
118119
return undefined;
119120
};
120121

121-
GHRequest.prototype.initVehicle = function (vehicle) {
122-
this.api_params.vehicle = vehicle;
123-
if(this.api_params.elevation !== false) {
124-
var featureSet = this.features[vehicle];
125-
this.api_params.elevation = featureSet && featureSet.elevation;
126-
}
127-
this.hasTCSupport();
122+
GHRequest.prototype.setProfile = function (profileName) {
123+
this.api_params.profile = profileName;
124+
};
125+
126+
GHRequest.prototype.getProfile = function () {
127+
return this.api_params.profile;
128128
};
129129

130-
GHRequest.prototype.hasTCSupport = function() {
131-
if(this.api_params.turn_costs !== false) {
132-
var featureSet = this.features[this.api_params.vehicle];
133-
this.api_params.turn_costs = featureSet && featureSet.turn_costs;
134-
}
130+
GHRequest.prototype.setElevation = function (elevation) {
131+
this.api_params.elevation = elevation;
135132
};
136133

137134
GHRequest.prototype.hasElevation = function () {
138135
return this.api_params.elevation === true;
139136
};
140137

141138
GHRequest.prototype.getVehicle = function () {
142-
return this.api_params.vehicle;
139+
var profileName = this.api_params.profile;
140+
var profile = this.profiles.find(function(p) { return p.profile_name === profileName; });
141+
if (!profile)
142+
return "";
143+
else
144+
return profile.vehicle;
143145
};
144146

145147
GHRequest.prototype.isPublicTransit = function () {
146-
return this.getVehicle() === "pt";
148+
// legacy support: we might have set vehicle=pt instead of pt
149+
return this.getProfile() === "pt" || this.getVehicle() === "pt";;
147150
};
148151

152+
GHRequest.prototype.removeProfileParameterIfLegacyRequest = function() {
153+
// we still allow using legacy parameters to support older urls pasted from somewhere, but when they are used
154+
// we may not add the profile parameter to the url
155+
if (
156+
this.api_params["vehicle"] ||
157+
this.api_params["weighting"] ||
158+
this.api_params["turn_costs"] ||
159+
this.api_params["edge_based"]
160+
) {
161+
delete this.api_params.profile;
162+
}
163+
}
164+
165+
GHRequest.prototype.removeLegacyParameters = function() {
166+
delete this.api_params["vehicle"];
167+
delete this.api_params["weighting"];
168+
delete this.api_params["turn_costs"];
169+
delete this.api_params["edge_based"];
170+
}
171+
149172
GHRequest.prototype.createGeocodeURL = function (host, prevIndex) {
150173
var tmpHost = this.host;
151174
if (host)
@@ -232,6 +255,10 @@ GHRequest.prototype.doRequest = function (url, callback) {
232255
$.ajax({
233256
timeout: 30000,
234257
url: url,
258+
beforeSend: function(request) {
259+
request.setRequestHeader("gh-client", "web-ui")
260+
request.setRequestHeader("gh-client-version", "1.0")
261+
},
235262
success: function (json) {
236263
if (json.paths) {
237264
for (var i = 0; i < json.paths.length; i++) {

0 commit comments

Comments
 (0)