Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapbox.services.commons.models;

import com.mapbox.services.Constants;
import com.mapbox.services.commons.utils.TextUtils;

import java.util.logging.Logger;

Expand All @@ -27,24 +28,25 @@ public class Position {
* @since 1.0.0
*/
private Position(double longitude, double latitude, double altitude) {

this.longitude = longitude;
this.latitude = latitude;
this.altitude = altitude;

if (latitude < -90 || latitude > 90) {
// Checks the latitude value is within range or provide a warning otherwise
logger.warning(String.format(Constants.DEFAULT_LOCALE,
"Latitude value seems to be out of range (found: %f, expected: [-90, 90]). "
"Latitude value seems to be out of range (found: %s, expected: [-90, 90]). "
+ "Did you accidentally reverse the longitude/latitude order?",
latitude));
TextUtils.formatCoordinate(latitude)));
}

if (longitude < -180 || longitude > 180) {
// Checks the longitude value is within range or provide a warning otherwise
logger.warning(String.format(Constants.DEFAULT_LOCALE,
"Longitude value seems to be out of range (found: %f, expected: [-180, 180]). "
+ "Did you accidentally reverse the longitude/latitude order?",
longitude));
"Longitude value seems to be out of range (found: %s, expected: [-180, 180]). "
+ "Did you accidentally reverse the longitude/latitude order?",
TextUtils.formatCoordinate(longitude)));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.mapbox.services.commons.utils;

import com.mapbox.services.Constants;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

/**
* We avoid including a full library like org.apache.commons:commons-lang3 to avoid an unnecessary
* large number of methods, which is inconvenient to Android devs.
Expand Down Expand Up @@ -47,4 +53,16 @@ public static String join(CharSequence delimiter, Object[] tokens) {
return sb.toString();
}

/**
* Useful to remove any trailing zeros and prevent a coordinate being over 7 significant figures.
*
* @param coordinate a double value representing a coordinate.
* @return a formatted string.
* @since 2.0.1
*/
public static String formatCoordinate(double coordinate) {
DecimalFormat decimalFormat = new DecimalFormat("0.######", new DecimalFormatSymbols(Locale.US));
return String.format(Constants.DEFAULT_LOCALE, "%s",
decimalFormat.format(coordinate));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@ public void toJson() throws IOException {
FeatureCollection geo = FeatureCollection.fromJson(geojson);
compareJson(geojson, geo.toJson());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.mapbox.services.api.MapboxBuilder;
import com.mapbox.services.api.MapboxService;
import com.mapbox.services.api.ServicesException;
import com.mapbox.services.api.directions.v5.models.DirectionsResponse;
import com.mapbox.services.commons.models.Position;
import com.mapbox.services.commons.utils.TextUtils;
import com.mapbox.services.api.directions.v5.models.DirectionsResponse;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -398,22 +398,22 @@ public String getCoordinates() {
List<String> coordinatesFormatted = new ArrayList<>();
// Insert origin at beginning of list if one is provided.
if (origin != null) {
coordinatesFormatted.add(String.format(Locale.US, "%f,%f",
origin.getLongitude(),
origin.getLatitude()));
coordinatesFormatted.add(String.format(Locale.US, "%s,%s",
TextUtils.formatCoordinate(origin.getLongitude()),
TextUtils.formatCoordinate(origin.getLatitude())));
}
if (coordinates != null) {
for (Position coordinate : coordinates) {
coordinatesFormatted.add(String.format(Locale.US, "%f,%f",
coordinate.getLongitude(),
coordinate.getLatitude()));
coordinatesFormatted.add(String.format(Locale.US, "%s,%s",
TextUtils.formatCoordinate(coordinate.getLongitude()),
TextUtils.formatCoordinate(coordinate.getLatitude())));
}
}
// Insert destination at end of list if one is provided.
if (destination != null) {
coordinatesFormatted.add(String.format(Locale.US, "%f,%f",
destination.getLongitude(),
destination.getLatitude()));
coordinatesFormatted.add(String.format(Locale.US, "%s,%s",
TextUtils.formatCoordinate(destination.getLongitude()),
TextUtils.formatCoordinate(destination.getLatitude())));
}

return TextUtils.join(";", coordinatesFormatted.toArray());
Expand Down Expand Up @@ -468,7 +468,9 @@ public String getBearings() {
if (bearings[i].length == 0) {
bearingFormatted[i] = "";
} else {
bearingFormatted[i] = String.format(Locale.US, "%f,%f", bearings[i][0], bearings[i][1]);
bearingFormatted[i] = String.format(Locale.US, "%s,%s",
TextUtils.formatCoordinate(bearings[i][0]),
TextUtils.formatCoordinate(bearings[i][1]));
}
}
return TextUtils.join(";", bearingFormatted);
Expand Down Expand Up @@ -499,7 +501,7 @@ public String getRadiuses() {
if (radiuses[i] == Double.POSITIVE_INFINITY) {
radiusesFormatted[i] = "unlimited";
} else {
radiusesFormatted[i] = String.format(Locale.US, "%f", radiuses[i]);
radiusesFormatted[i] = String.format(Locale.US, "%s", TextUtils.formatCoordinate(radiuses[i]));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,10 @@ public T setCoordinates(Position position) {
if (position == null) {
return (T) this;
}
query = String.format(Locale.US, "%f,%f",
position.getLongitude(),
position.getLatitude());

query = String.format(Locale.US, "%s,%s",
TextUtils.formatCoordinate(position.getLongitude()),
TextUtils.formatCoordinate(position.getLatitude()));
return (T) this;
}

Expand Down Expand Up @@ -347,9 +348,9 @@ public T setProximity(Position position) {
if (position == null) {
return (T) this;
}
proximity = String.format(Locale.US, "%f,%f",
position.getLongitude(),
position.getLatitude());
proximity = String.format(Locale.US, "%s,%s",
TextUtils.formatCoordinate(position.getLongitude()),
TextUtils.formatCoordinate(position.getLatitude()));
return (T) this;
}

Expand Down Expand Up @@ -421,7 +422,11 @@ public T setBbox(double minX, double minY, double maxX, double maxY) throws Serv
throw new ServicesException("You provided an empty bounding box");
}

this.bbox = String.format(Locale.US, "%f,%f,%f,%f", minX, minY, maxX, maxY);
this.bbox = String.format(Locale.US, "%s,%s,%s,%s",
TextUtils.formatCoordinate(minX),
TextUtils.formatCoordinate(minY),
TextUtils.formatCoordinate(maxX),
TextUtils.formatCoordinate(maxY));
return (T) this;
}

Expand Down Expand Up @@ -529,16 +534,16 @@ public String getLanguage() {

/**
* The locale in which results should be returned.
*
* <p>
* This property affects the language of returned results; generally speaking,
* it does not determine which results are found. If the Geocoding API does not
* recognize the language code, it may fall back to another language or the default
* language. Components other than the language code, such as the country and
* script codes, are ignored.
*
* <p>
* By default, this property is set to `null`, causing results to be in the default
* language.
*
* <p>
* This option is experimental.
*
* @param language The locale in which results should be returned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ public T setAnnotations(String annotations) {
public String getCoordinates() {
List<String> coordinatesFormatted = new ArrayList<>();
for (Position coordinate : coordinates) {
coordinatesFormatted.add(String.format(Locale.US, "%f,%f",
coordinate.getLongitude(),
coordinate.getLatitude()));
coordinatesFormatted.add(String.format(Locale.US, "%s,%s",
TextUtils.formatCoordinate(coordinate.getLongitude()),
TextUtils.formatCoordinate(coordinate.getLatitude())));
}

return TextUtils.join(";", coordinatesFormatted.toArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

public class MapboxDirectionsTest {


private static final String DIRECTIONS_V5_FIXTURE = "src/test/fixtures/directions_v5.json";
private static final String DIRECTIONS_V5_PRECISION6_FIXTURE = "src/test/fixtures/directions_v5_precision_6.json";
private static final String DIRECTIONS_TRAFFIC_FIXTURE = "src/test/fixtures/directions_v5_traffic.json";
Expand Down Expand Up @@ -222,7 +221,7 @@ public void testRadius() throws ServicesException, IOException {
.build();

assertTrue(client.executeCall().raw().request().url().toString()
.contains("radiuses=100.000000;100.000000;100.000000"));
.contains("radiuses=100;100;100"));
}

@Test
Expand All @@ -242,7 +241,7 @@ public void testRadiusWithUnlimitedDistance() throws ServicesException, IOExcept
.build();

assertTrue(client.executeCall().raw().request().url().toString()
.contains("radiuses=100.000000;unlimited;100.000000"));
.contains("radiuses=100;unlimited;100"));
}

@Test
Expand Down Expand Up @@ -295,7 +294,7 @@ public void testBearing() throws ServicesException, IOException {
.build();

assertTrue(client.executeCall().raw().request().url().toString()
.contains("bearings=60.000000,45.000000;;45.000000,45.000000"));
.contains("bearings=60,45;;45,45"));
}

@Test
Expand Down Expand Up @@ -554,7 +553,7 @@ public void testSetCoordinates() {
String coordinates = new MapboxDirections.Builder()
.setCoordinates(test)
.getCoordinates();
assertEquals(coordinates, "2.100000,2.200000;3.100000,3.200000");
assertEquals(coordinates, "2.1,2.2;3.1,3.2");
}

@Test
Expand All @@ -563,7 +562,7 @@ public void setOriginDestination() {
.setOrigin(Position.fromCoordinates(2.1, 2.2))
.setDestination(Position.fromCoordinates(3.1, 3.2))
.getCoordinates();
assertEquals(coordinates, "2.100000,2.200000;3.100000,3.200000");
assertEquals(coordinates, "2.1,2.2;3.1,3.2");
}

@Test
Expand All @@ -578,7 +577,7 @@ public void testSetCoordinatesMixed() {
.setOrigin(Position.fromCoordinates(1.1, 1.2))
.setDestination(Position.fromCoordinates(4.1, 4.2))
.getCoordinates();
assertEquals(coordinates, "1.100000,1.200000;2.100000,2.200000;3.100000,3.200000;4.100000,4.200000");
assertEquals(coordinates, "1.1,1.2;2.1,2.2;3.1,3.2;4.1,4.2");
}

@Test
Expand All @@ -594,7 +593,7 @@ public void testLocale() {
.setDestination(Position.fromCoordinates(4.1, 4.2))
.setCoordinates(test)
.getCoordinates();
assertEquals(coordinates, "1.100000,1.200000;2.100000,2.200000;3.100000,3.200000;4.100000,4.200000");
assertEquals(coordinates, "1.1,1.2;2.1,2.2;3.1,3.2;4.1,4.2");
}

@Test
Expand Down Expand Up @@ -622,7 +621,7 @@ public void originDestinationCoordinatesListCorrectOrder() throws ServicesExcept

String callUrl = client.executeCall().raw().request().url().toString();
assertTrue(
callUrl.contains("-122.431300,37.778900;-122.416667,37.783333;-121.900000,37.333333;-121.800100,37.227500")
callUrl.contains("-122.4313,37.7789;-122.416667,37.783333;-121.9,37.333333;-121.8001,37.2275")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.mapbox.services.api.geocoding.v5.models.CarmenFeature;
import com.mapbox.services.api.geocoding.v5.models.GeocodingResponse;
import com.mapbox.services.commons.geojson.Point;
import com.mapbox.services.commons.models.Position;

import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -199,6 +200,28 @@ public void testCountryNotSupported() throws ServicesException, IOException {
assertEquals(0, response.body().getFeatures().size());
}

@Test
public void testBbox() throws IOException, ServicesException {
MapboxGeocoding clientNeSw = new MapboxGeocoding.Builder()
.setAccessToken(ACCESS_TOKEN)
.setLocation("1600 pennsylvania ave nw")
.setBbox(Position.fromCoordinates(-77.0035, 38.9115), Position.fromCoordinates(-77.0702, 38.8561))
.setBaseUrl(mockUrl.toString())
.build();

MapboxGeocoding clientMinMax = new MapboxGeocoding.Builder()
.setAccessToken(ACCESS_TOKEN)
.setLocation("1600 pennsylvania ave nw")
.setBbox(-77.0035, 38.9115, -77.0702, 38.8561)
.setBaseUrl(mockUrl.toString())
.build();

assertTrue(
clientNeSw.executeCall().raw().request().url().toString().contains("bbox=-77.0702,38.8561,-77.0035,38.9115"));
assertTrue(
clientMinMax.executeCall().raw().request().url().toString().contains("bbox=-77.0035,38.9115,-77.0702,38.8561"));
}

@Test
public void testLanguage() throws IOException, ServicesException {
MapboxGeocoding clientNoLanguage = new MapboxGeocoding.Builder()
Expand Down