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
Expand Up @@ -16,6 +16,8 @@ public class TurfConstants {
public static final String UNIT_INCHES = "inches";
public static final String UNIT_YARDS = "yards";
public static final String UNIT_METERS = "meters";
public static final String UNIT_CENTIMETERS = "centimeters";
public static final String UNIT_FEET = "feet";

public static final String UNIT_DEFAULT = UNIT_KILOMETERS;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ public class TurfHelpers {
factors.put(TurfConstants.UNIT_INCHES, 250905600d);
factors.put(TurfConstants.UNIT_YARDS, 6969600d);
factors.put(TurfConstants.UNIT_METERS, 6373000d);
factors.put(TurfConstants.UNIT_CENTIMETERS, 6.373e+8d);
factors.put(TurfConstants.UNIT_KILOMETERS, 6373d);
factors.put(TurfConstants.UNIT_FEET, 20908792.65d);

// Also supported
factors.put("centimetres", 6.373e+8d);
factors.put("metres", 6373000d);
factors.put("kilometres", 6373d);
}
Expand Down Expand Up @@ -88,4 +91,41 @@ public static double distanceToRadians(double distance, String units) throws Tur
}
return distance / factor;
}

/**
* Converts a distance to the requested unit.
* Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
*
* @param distance the distance to be converted
* @param originalUnit of the distance
* @return the converted distance
* @since 2.2.0
*/
public static double convertDistance(double distance, String originalUnit) {
return convertDistance(distance, originalUnit, TurfConstants.UNIT_DEFAULT);
}

/**
* Converts a distance to the requested unit.
* Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
*
* @param distance the distance to be converted
* @param originalUnit of the distance
* @param finalUnit returned unit, {@link TurfConstants#UNIT_DEFAULT} if not specified.
* @return the converted distance
* @since 2.2.0
*/
public static double convertDistance(double distance, String originalUnit, String finalUnit) {
Double factor = factors.get(originalUnit);
if (factor == null) {
throw new TurfException("Invalid unit.");
} else if (!(distance >= 0)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case doesn't seem to be tested (negative distance), only invalid units are.

throw new TurfException("Distance must be a positive number.");
}
if (finalUnit == null) {
finalUnit = TurfConstants.UNIT_DEFAULT;
}

return radiansToDistance(distanceToRadians(distance, originalUnit), finalUnit);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.mapbox.services.api.turf;

import com.mapbox.services.api.BaseTest;
import com.mapbox.services.api.utils.turf.TurfConstants;
import com.mapbox.services.api.utils.turf.TurfException;
import com.mapbox.services.api.utils.turf.TurfHelpers;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.hamcrest.Matchers.startsWith;

public class TurfHelpersTest extends BaseTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void convertDistance_throwNegativeDistanceException() throws TurfException {
thrown.expect(TurfException.class);
thrown.expectMessage(startsWith("Distance must be a"));
TurfHelpers.convertDistance(-1, TurfConstants.UNIT_MILES);
}

@Test
public void convertDistance_throwInvalidUnitException() throws TurfException {
thrown.expect(TurfException.class);
thrown.expectMessage(startsWith("Invalid unit."));
TurfHelpers.convertDistance(1, "foo");
}

@Test
public void convertDistance() throws TurfException {
Assert.assertEquals(1,
TurfHelpers.convertDistance(1000, TurfConstants.UNIT_METERS), DELTA);
Assert.assertEquals(0.6213714106386318,
TurfHelpers.convertDistance(1, TurfConstants.UNIT_KILOMETERS, TurfConstants.UNIT_MILES), DELTA);
Assert.assertEquals(1.6093434343434343,
TurfHelpers.convertDistance(1, TurfConstants.UNIT_MILES, TurfConstants.UNIT_KILOMETERS), DELTA);
Assert.assertEquals(1.851999843075488,
TurfHelpers.convertDistance(1, TurfConstants.UNIT_NAUTICAL_MILES), DELTA);
Assert.assertEquals(100,
TurfHelpers.convertDistance(1, TurfConstants.UNIT_METERS, TurfConstants.UNIT_CENTIMETERS), DELTA);
}
}