Skip to content

Commit

Permalink
Fixed up mapmatching API (#507)
Browse files Browse the repository at this point in the history
* fixed up mapmatching API

* removed unused import

* fixed mapmatching rx

* fixed map matching test
  • Loading branch information
Cameron Mace authored Jul 14, 2017
1 parent c5d2525 commit 791ca02
Show file tree
Hide file tree
Showing 11 changed files with 542 additions and 96 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ directions-fixtures:
-o mapbox/libjava-services/src/test/fixtures/directions_v5_traffic.json

mapmatching-fixtures:
curl "https://api.mapbox.com/matching/v5/mapbox/driving/$(MAP_MATCHING_COORDINATES)?geometries=polyline&access_token=$(MAPBOX_ACCESS_TOKEN)" \
curl "https://api.mapbox.com/matching/v5/mapbox/driving/$(MAP_MATCHING_COORDINATES)?geometries=polyline&language=sv&steps=true&access_token=$(MAPBOX_ACCESS_TOKEN)" \
-o mapbox/libjava-services/src/test/fixtures/mapmatching_v5_polyline.json

optimized-trips-fixtures:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public interface MapMatchingServiceRx {
* along the match geometry. Can be one or all of 'duration', 'distance',
* or 'nodes', each separated by ,. See the response object for more
* details on what it is included with annotations.
* @param language Language of returned turn-by-turn text instructions.
* @return The MapMatchingResponse in an Observable wrapper
* @since 2.0.0
*/
Expand All @@ -60,5 +61,6 @@ Observable<MapMatchingResponse> getObservable(
@Query("steps") Boolean steps,
@Query("overview") String overview,
@Query("timestamps") String timestamps,
@Query("annotations") String annotations);
@Query("annotations") String annotations,
@Query("language") String language);
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public Observable<MapMatchingResponse> getObservable() {
builder.getSteps(),
builder.getOverview(),
builder.getTimestamps(),
builder.getAnnotations()
builder.getAnnotations(),
builder.getLanguage()
);

// Done
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.mapbox.services.api.rx;

import com.google.gson.JsonParser;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.junit.Assert.assertEquals;

public class BaseTest {

protected static final double DELTA = 1E-10;

public void compareJson(String json1, String json2) {
JsonParser parser = new JsonParser();
assertEquals(parser.parse(json1), parser.parse(json2));
}

protected String loadJsonFixture(String filename) throws IOException {
byte[] content = Files.readAllBytes(Paths.get(filename));
return new String(content, Charset.forName("utf-8"));
}

protected String loadJsonFixture(String folder, String filename) throws IOException {
byte[] content = Files.readAllBytes(Paths.get("src/test/fixtures/" + folder + "/" + filename));
return new String(content, Charset.forName("utf-8"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mapbox.services.api.ServicesException;
import com.mapbox.services.api.mapmatching.v5.MapMatchingCriteria;
import com.mapbox.services.api.mapmatching.v5.models.MapMatchingResponse;
import com.mapbox.services.api.rx.BaseTest;
import com.mapbox.services.commons.models.Position;

import org.junit.After;
Expand All @@ -12,9 +13,6 @@
import org.junit.rules.ExpectedException;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import io.reactivex.observers.TestObserver;
Expand All @@ -29,11 +27,9 @@
/**
* Test Rx support on the Mapbox Map Matching API
*/
public class MapboxMapMatchingRxTest extends BaseTest {

public class MapboxMapMatchingRxTest {

public static final String POLYLINE_FIXTURE =
"../libjava-services/src/test/fixtures/mapmatching_v5_polyline.json";
private static final String POLYLINE_FIXTURE = "../libjava-services/src/test/fixtures/mapmatching_v5_polyline.json";

private static final String ACCESS_TOKEN = "pk.XXX";

Expand All @@ -54,7 +50,7 @@ public void setUp() throws IOException {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
try {
String body = new String(Files.readAllBytes(Paths.get(POLYLINE_FIXTURE)), Charset.forName("utf-8"));
String body = loadJsonFixture(POLYLINE_FIXTURE);
return new MockResponse().setBody(body);
} catch (IOException ioException) {
throw new RuntimeException(ioException);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public interface MapMatchingService {
* along the match geometry. Can be one or all of 'duration', 'distance',
* or 'nodes', each separated by ,. See the response object for more
* details on what it is included with annotations.
* @param language Language of returned turn-by-turn text instructions.
* @return The MapMatchingResponse in a Call wrapper
* @since 2.0.0
*/
Expand All @@ -62,5 +63,6 @@ Call<MapMatchingResponse> getCall(
@Query("steps") Boolean steps,
@Query("overview") String overview,
@Query("timestamps") String timestamps,
@Query("annotations") String annotations);
@Query("annotations") String annotations,
@Query("language") String language);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mapbox.services.api.mapmatching.v5;

import com.google.gson.Gson;
import com.mapbox.services.api.MapboxBuilder;
import com.mapbox.services.api.MapboxService;
import com.mapbox.services.api.ServicesException;
Expand Down Expand Up @@ -35,7 +34,6 @@ public class MapboxMapMatching extends MapboxService<MapMatchingResponse> {
protected Builder builder = null;
private MapMatchingService service = null;
private Call<MapMatchingResponse> call = null;
private Gson gson;

protected MapboxMapMatching(Builder builder) {
this.builder = builder;
Expand Down Expand Up @@ -85,7 +83,8 @@ public Call<MapMatchingResponse> getCall() {
builder.getSteps(),
builder.getOverview(),
builder.getTimestamps(),
builder.getAnnotations()
builder.getAnnotations(),
builder.getLanguage()
);

return call;
Expand Down Expand Up @@ -152,6 +151,7 @@ public static class Builder<T extends Builder> extends MapboxBuilder {
private String overview = null;
private String[] timestamps = null;
private String annotations = null;
private String language;

/**
* Constructor
Expand Down Expand Up @@ -378,6 +378,27 @@ public T setBaseUrl(String baseUrl) {
return (T) this;
}

/**
* Optionally set the language of returned turn-by-turn text instructions. The default is {@code en} for English.
*
* @param language The locale in which results should be returned.
* @return Builder
* @see <a href="https://www.mapbox.com/api-documentation/#instructions-languages">Supported languages</a>
* @since 2.2.0
*/
public T setLanguage(String language) {
this.language = language;
return (T) this;
}

/**
* @return The language the turn-by-turn directions will be in.
* @since 2.2.0
*/
public String getLanguage() {
return language;
}

/**
* Builder method
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ public class MapMatchingTracepoint extends DirectionsWaypoint {

@SerializedName("matchings_index")
private int matchingsIndex;

@SerializedName("alternatives_count")
private int alternativesCount;
@SerializedName("waypoint_index")
private int waypointIndex;

public MapMatchingTracepoint() {
}

public MapMatchingTracepoint(int matchingsIndex, int alternativesCount, int waypointIndex) {
this.matchingsIndex = matchingsIndex;
this.alternativesCount = alternativesCount;
this.waypointIndex = waypointIndex;
}

/**
* Index to the match object in matchings the sub-trace was matched to.
*
Expand Down Expand Up @@ -52,4 +59,26 @@ public int getWaypointIndex() {
public void setWaypointIndex(int waypointIndex) {
this.waypointIndex = waypointIndex;
}

/**
* Number of probable alternative matchings for this trace point. A value of zero indicates that this point was
* matched unambiguously. Split the trace at these points for incremental map matching.
*
* @return an integer representing the alternatives count
* @since 2.2.0
*/
public int getAlternativesCount() {
return alternativesCount;
}

/**
* Set the number of probable alternative matchings for this trace point. A value of zero indicates that this point
* was matched unambiguously. Split the trace at these points for incremental map matching.
*
* @param alternativesCount an integer representing the alternatives count
* @since 2.2.0
*/
public void setAlternativesCount(int alternativesCount) {
this.alternativesCount = alternativesCount;
}
}
Loading

0 comments on commit 791ca02

Please sign in to comment.