small refactoring
This commit is contained in:
parent
5097b11dfe
commit
f4391b7008
7 changed files with 82 additions and 49 deletions
|
@ -25,6 +25,8 @@ import java.util.LinkedHashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static net.osmand.util.Algorithms.isEmpty;
|
||||
|
||||
public class OnlineRoutingHelper {
|
||||
|
||||
private static final Log LOG = PlatformUtil.getLog(OnlineRoutingHelper.class);
|
||||
|
@ -71,7 +73,15 @@ public class OnlineRoutingHelper {
|
|||
return null;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Nullable
|
||||
public OnlineRoutingResponse calculateRouteOnline(@Nullable String stringKey,
|
||||
@NonNull List<LatLon> path,
|
||||
boolean leftSideNavigation) throws IOException, JSONException {
|
||||
OnlineRoutingEngine engine = getEngineByKey(stringKey);
|
||||
return engine != null ? calculateRouteOnline(engine, path, leftSideNavigation) : null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public OnlineRoutingResponse calculateRouteOnline(@NonNull OnlineRoutingEngine engine,
|
||||
@NonNull List<LatLon> path,
|
||||
boolean leftSideNavigation) throws IOException, JSONException {
|
||||
|
@ -132,7 +142,7 @@ public class OnlineRoutingHelper {
|
|||
@NonNull
|
||||
private String createEngineKeyIfNeeded(@NonNull OnlineRoutingEngine engine) {
|
||||
String key = engine.get(EngineParameter.KEY);
|
||||
if (Algorithms.isEmpty(key)) {
|
||||
if (isEmpty(key)) {
|
||||
key = OnlineRoutingEngine.generateKey();
|
||||
engine.put(EngineParameter.KEY, key);
|
||||
}
|
||||
|
@ -152,7 +162,7 @@ public class OnlineRoutingHelper {
|
|||
private List<OnlineRoutingEngine> readFromSettings() {
|
||||
List<OnlineRoutingEngine> engines = new ArrayList<>();
|
||||
String jsonString = settings.ONLINE_ROUTING_ENGINES.get();
|
||||
if (!Algorithms.isEmpty(jsonString)) {
|
||||
if (!isEmpty(jsonString)) {
|
||||
try {
|
||||
JSONObject json = new JSONObject(jsonString);
|
||||
OnlineRoutingUtils.readFromJson(json, engines);
|
||||
|
@ -164,7 +174,7 @@ public class OnlineRoutingHelper {
|
|||
}
|
||||
|
||||
private void saveCacheToSettings() {
|
||||
if (!Algorithms.isEmpty(cachedEngines)) {
|
||||
if (!isEmpty(cachedEngines)) {
|
||||
try {
|
||||
JSONObject json = new JSONObject();
|
||||
OnlineRoutingUtils.writeToJson(json, getEngines());
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
package net.osmand.plus.onlinerouting;
|
||||
|
||||
import net.osmand.data.LatLon;
|
||||
import net.osmand.Location;
|
||||
import net.osmand.plus.routing.RouteDirectionInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OnlineRoutingResponse {
|
||||
private List<LatLon> route;
|
||||
private List<Location> route;
|
||||
private List<RouteDirectionInfo> directions;
|
||||
|
||||
public OnlineRoutingResponse(List<LatLon> route, List<RouteDirectionInfo> directions) {
|
||||
public OnlineRoutingResponse(List<Location> route, List<RouteDirectionInfo> directions) {
|
||||
this.route = route;
|
||||
this.directions = directions;
|
||||
}
|
||||
|
||||
public List<LatLon> getRoute() {
|
||||
public List<Location> getRoute() {
|
||||
return route;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package net.osmand.plus.onlinerouting.engine;
|
|||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import net.osmand.Location;
|
||||
import net.osmand.data.LatLon;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.onlinerouting.EngineParameter;
|
||||
|
@ -28,8 +29,9 @@ public class GraphhopperEngine extends OnlineRoutingEngine {
|
|||
super(params);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public @NonNull EngineType getType() {
|
||||
public EngineType getType() {
|
||||
return EngineType.GRAPHHOPPER;
|
||||
}
|
||||
|
||||
|
@ -79,7 +81,7 @@ public class GraphhopperEngine extends OnlineRoutingEngine {
|
|||
sb.append('&').append("details=").append("lanes");
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Nullable
|
||||
@Override
|
||||
public OnlineRoutingResponse parseServerResponse(@NonNull String content,
|
||||
boolean leftSideNavigation) throws JSONException {
|
||||
|
@ -87,7 +89,9 @@ public class GraphhopperEngine extends OnlineRoutingEngine {
|
|||
JSONObject root = obj.getJSONArray("paths").getJSONObject(0);
|
||||
|
||||
String encoded = root.getString("points");
|
||||
List<LatLon> route = GeoPolylineParserUtil.parse(encoded, GeoPolylineParserUtil.PRECISION_5);
|
||||
List<LatLon> points = GeoPolylineParserUtil.parse(encoded, GeoPolylineParserUtil.PRECISION_5);
|
||||
if (isEmpty(points)) return null;
|
||||
List<Location> route = convertRouteToLocationsList(points);
|
||||
|
||||
JSONArray instructions = root.getJSONArray("instructions");
|
||||
List<RouteDirectionInfo> directions = new ArrayList<>();
|
||||
|
@ -158,7 +162,7 @@ public class GraphhopperEngine extends OnlineRoutingEngine {
|
|||
} else if (sign == -6) {
|
||||
// not yet used: leave roundabout
|
||||
|
||||
} else if (sign == -3) {
|
||||
} else if (sign == -3) {
|
||||
// turn sharp left
|
||||
id = TurnType.TSHL;
|
||||
|
||||
|
|
|
@ -5,12 +5,15 @@ import android.content.Context;
|
|||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import net.osmand.GPXUtilities.WptPt;
|
||||
import net.osmand.Location;
|
||||
import net.osmand.data.LatLon;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.onlinerouting.EngineParameter;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingFactory;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingResponse;
|
||||
import net.osmand.plus.onlinerouting.VehicleType;
|
||||
import net.osmand.plus.routing.RouteProvider;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
@ -73,15 +76,6 @@ public abstract class OnlineRoutingEngine implements Cloneable {
|
|||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String getBaseUrl() {
|
||||
String customUrl = get(EngineParameter.CUSTOM_URL);
|
||||
if (isEmpty(customUrl)) {
|
||||
return getStandardUrl();
|
||||
}
|
||||
return customUrl;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String getFullUrl(@NonNull List<LatLon> path) {
|
||||
StringBuilder sb = new StringBuilder(getBaseUrl());
|
||||
|
@ -93,11 +87,34 @@ public abstract class OnlineRoutingEngine implements Cloneable {
|
|||
@NonNull List<LatLon> path);
|
||||
|
||||
@NonNull
|
||||
public String getBaseUrl() {
|
||||
String customUrl = get(EngineParameter.CUSTOM_URL);
|
||||
if (isEmpty(customUrl)) {
|
||||
return getStandardUrl();
|
||||
}
|
||||
return customUrl;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public abstract String getStandardUrl();
|
||||
|
||||
@Nullable
|
||||
public abstract OnlineRoutingResponse parseServerResponse(@NonNull String content,
|
||||
boolean leftSideNavigation) throws JSONException;
|
||||
|
||||
@NonNull
|
||||
public abstract String getStandardUrl();
|
||||
protected List<Location> convertRouteToLocationsList(@NonNull List<LatLon> route) {
|
||||
List<Location> result = new ArrayList<>();
|
||||
if (!isEmpty(route)) {
|
||||
for (LatLon pt : route) {
|
||||
WptPt wpt = new WptPt();
|
||||
wpt.lat = pt.getLatitude();
|
||||
wpt.lon = pt.getLongitude();
|
||||
result.add(RouteProvider.createLocation(wpt));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public Map<String, String> getParams() {
|
||||
|
|
|
@ -3,6 +3,7 @@ package net.osmand.plus.onlinerouting.engine;
|
|||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import net.osmand.Location;
|
||||
import net.osmand.data.LatLon;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.onlinerouting.EngineParameter;
|
||||
|
@ -25,8 +26,9 @@ public class OrsEngine extends OnlineRoutingEngine {
|
|||
super(params);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public @NonNull EngineType getType() {
|
||||
public EngineType getType() {
|
||||
return EngineType.ORS;
|
||||
}
|
||||
|
||||
|
@ -76,21 +78,25 @@ public class OrsEngine extends OnlineRoutingEngine {
|
|||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Nullable
|
||||
@Override
|
||||
public OnlineRoutingResponse parseServerResponse(@NonNull String content,
|
||||
boolean leftSideNavigation) throws JSONException {
|
||||
JSONObject obj = new JSONObject(content);
|
||||
JSONArray array = obj.getJSONArray("features").getJSONObject(0)
|
||||
.getJSONObject("geometry").getJSONArray("coordinates");
|
||||
List<LatLon> track = new ArrayList<>();
|
||||
List<LatLon> points = new ArrayList<>();
|
||||
for (int i = 0; i < array.length(); i++) {
|
||||
JSONArray point = array.getJSONArray(i);
|
||||
double lon = Double.parseDouble(point.getString(0));
|
||||
double lat = Double.parseDouble(point.getString(1));
|
||||
track.add(new LatLon(lat, lon));
|
||||
points.add(new LatLon(lat, lon));
|
||||
}
|
||||
return new OnlineRoutingResponse(track, null);
|
||||
if (!isEmpty(points)) {
|
||||
List<Location> route = convertRouteToLocationsList(points);
|
||||
new OnlineRoutingResponse(route, null);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -3,6 +3,7 @@ package net.osmand.plus.onlinerouting.engine;
|
|||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import net.osmand.Location;
|
||||
import net.osmand.data.LatLon;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.onlinerouting.EngineParameter;
|
||||
|
@ -25,7 +26,8 @@ public class OsrmEngine extends OnlineRoutingEngine {
|
|||
}
|
||||
|
||||
@Override
|
||||
public @NonNull EngineType getType() {
|
||||
public @NonNull
|
||||
EngineType getType() {
|
||||
return EngineType.OSRM;
|
||||
}
|
||||
|
||||
|
@ -36,7 +38,8 @@ public class OsrmEngine extends OnlineRoutingEngine {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void collectAllowedParameters() { }
|
||||
protected void collectAllowedParameters() {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void collectAllowedVehicles(@NonNull List<VehicleType> vehicles) {
|
||||
|
@ -64,14 +67,18 @@ public class OsrmEngine extends OnlineRoutingEngine {
|
|||
sb.append('&').append("steps=true");
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Nullable
|
||||
@Override
|
||||
public OnlineRoutingResponse parseServerResponse(@NonNull String content,
|
||||
boolean leftSideNavigation) throws JSONException {
|
||||
JSONObject obj = new JSONObject(content);
|
||||
String encoded = obj.getJSONArray("routes").getJSONObject(0).getString("geometry");
|
||||
List<LatLon> route = GeoPolylineParserUtil.parse(encoded, GeoPolylineParserUtil.PRECISION_5);
|
||||
return new OnlineRoutingResponse(route, null);
|
||||
List<LatLon> points = GeoPolylineParserUtil.parse(encoded, GeoPolylineParserUtil.PRECISION_5);
|
||||
if (!isEmpty(points)) {
|
||||
List<Location> route = convertRouteToLocationsList(points);
|
||||
return new OnlineRoutingResponse(route, null);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -22,7 +22,6 @@ import net.osmand.data.WptLocationPoint;
|
|||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingHelper;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingResponse;
|
||||
import net.osmand.plus.onlinerouting.engine.OnlineRoutingEngine;
|
||||
import net.osmand.plus.settings.backend.OsmandSettings;
|
||||
import net.osmand.plus.settings.backend.CommonPreference;
|
||||
import net.osmand.plus.R;
|
||||
|
@ -327,7 +326,7 @@ public class RouteProvider {
|
|||
}
|
||||
}
|
||||
|
||||
private static Location createLocation(WptPt pt){
|
||||
public static Location createLocation(WptPt pt){
|
||||
Location loc = new Location("OsmandRouteProvider");
|
||||
loc.setLatitude(pt.lat);
|
||||
loc.setLongitude(pt.lon);
|
||||
|
@ -1203,27 +1202,17 @@ public class RouteProvider {
|
|||
private RouteCalculationResult findOnlineRoute(RouteCalculationParams params) throws IOException, JSONException {
|
||||
OnlineRoutingHelper helper = params.ctx.getOnlineRoutingHelper();
|
||||
String stringKey = params.mode.getRoutingProfile();
|
||||
OnlineRoutingEngine engine = helper.getEngineByKey(stringKey);
|
||||
OnlineRoutingResponse response = null;
|
||||
if (engine != null) {
|
||||
response = helper.calculateRouteOnline(engine, getFullPathFromParams(params), params.leftSide);
|
||||
}
|
||||
if (response != null && !Algorithms.isEmpty(response.getRoute())) {
|
||||
List<Location> res = new ArrayList<>();
|
||||
for (LatLon pt : response.getRoute()) {
|
||||
WptPt wpt = new WptPt();
|
||||
wpt.lat = pt.getLatitude();
|
||||
wpt.lon = pt.getLongitude();
|
||||
res.add(createLocation(wpt));
|
||||
}
|
||||
OnlineRoutingResponse response =
|
||||
helper.calculateRouteOnline(stringKey, getPathFromParams(params), params.leftSide);
|
||||
if (response != null) {
|
||||
params.intermediates = null;
|
||||
return new RouteCalculationResult(res, response.getDirections(), params, null, true);
|
||||
return new RouteCalculationResult(response.getRoute(), response.getDirections(), params, null, true);
|
||||
} else {
|
||||
return new RouteCalculationResult("Route is empty");
|
||||
}
|
||||
}
|
||||
|
||||
private static List<LatLon> getFullPathFromParams(RouteCalculationParams params) {
|
||||
private static List<LatLon> getPathFromParams(RouteCalculationParams params) {
|
||||
List<LatLon> points = new ArrayList<>();
|
||||
points.add(new LatLon(params.start.getLatitude(), params.start.getLongitude()));
|
||||
if (Algorithms.isEmpty(params.intermediates)) {
|
||||
|
|
Loading…
Reference in a new issue