Java Code Examples for com.mapbox.geojson.Point#fromLngLat()

The following examples show how to use com.mapbox.geojson.Point#fromLngLat() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: mapbox-java   File: TurfMeasurement.java    License: MIT License 6 votes vote down vote up
/**
 * Takes a Point and calculates the location of a destination point given a distance in
 * degrees, radians, miles, or kilometers; and bearing in degrees. This uses the Haversine
 * formula to account for global curvature.
 *
 * @param point    starting point used for calculating the destination
 * @param distance distance from the starting point
 * @param bearing  ranging from -180 to 180 in decimal degrees
 * @param units    one of the units found inside {@link TurfConstants.TurfUnitCriteria}
 * @return destination {@link Point} result where you specified
 * @see <a href="http://turfjs.org/docs/#destination">Turf Destination documetation</a>
 * @since 1.2.0
 */
@NonNull
public static Point destination(@NonNull Point point, @FloatRange(from = 0) double distance,
                                @FloatRange(from = -180, to = 180) double bearing,
                                @NonNull @TurfConstants.TurfUnitCriteria String units) {

  double longitude1 = degreesToRadians(point.longitude());
  double latitude1 = degreesToRadians(point.latitude());
  double bearingRad = degreesToRadians(bearing);

  double radians = TurfConversion.lengthToRadians(distance, units);

  double latitude2 = Math.asin(Math.sin(latitude1) * Math.cos(radians)
    + Math.cos(latitude1) * Math.sin(radians) * Math.cos(bearingRad));
  double longitude2 = longitude1 + Math.atan2(Math.sin(bearingRad)
      * Math.sin(radians) * Math.cos(latitude1),
    Math.cos(radians) - Math.sin(latitude1) * Math.sin(latitude2));

  return Point.fromLngLat(
    radiansToDegrees(longitude2), radiansToDegrees(latitude2));
}
 
Example 2
@Nullable
private NavigationRoute.Builder buildRequestFromLocation(Location location, RouteProgress progress) {
  Context context = contextWeakReference.get();
  if (context == null) {
    return null;
  }
  Point origin = Point.fromLngLat(location.getLongitude(), location.getLatitude());
  Double bearing = location.hasBearing() ? Float.valueOf(location.getBearing()).doubleValue() : null;
  RouteOptions options = progress.directionsRoute().routeOptions();
  NavigationRoute.Builder builder = NavigationRoute.builder(context)
    .origin(origin, bearing, BEARING_TOLERANCE)
    .routeOptions(options);

  List<Point> remainingWaypoints = routeUtils.calculateRemainingWaypoints(progress);
  if (remainingWaypoints == null) {
    Timber.e("An error occurred fetching a new route");
    return null;
  }
  addDestination(remainingWaypoints, builder);
  addWaypoints(remainingWaypoints, builder);
  addWaypointNames(progress, builder);
  addApproaches(progress, builder);
  return builder;
}
 
Example 3
Source Project: mapbox-java   File: ShifterTest.java    License: MIT License 5 votes vote down vote up
@Test
public void point_fromJson() throws Exception {

  // set shifter
  CoordinateShifterManager.setCoordinateShifter(new TestCoordinateShifter());

  Point point1 = Point.fromLngLat(1.0, 2.0);
  String jsonString = "{\"type\":\"Point\",\"coordinates\":[1.0, 2.0]}";
  Point point2 = Point.fromJson(jsonString);

  assertEquals(point1, point2);

  CoordinateShifterManager.setCoordinateShifter(null);
}
 
Example 4
@Override
public void onMapClick(@NonNull LatLng point) {
  if (!running || mapboxMap == null) {
    return;
  }

  mapboxMap.addMarker(new MarkerOptions().position(point));
  mapboxMap.setOnMapClickListener(null);

  Point newDestination = Point.fromLngLat(point.getLongitude(), point.getLatitude());
  mockLocationEngine.moveTo(newDestination);
  destination = Point.fromLngLat(point.getLongitude(), point.getLatitude());
  tracking = false;
}
 
Example 5
Source Project: mapbox-java   File: TurfMeasurementTest.java    License: MIT License 5 votes vote down vote up
@Test
public void testMidpointPositionToPoint() throws TurfException {
  Point pt1 = Point.fromLngLat(0, 0);
  Point pt2 = Point.fromLngLat(10, 0);
  Point mid = TurfMeasurement.midpoint(pt1, pt2);

  assertEquals(TurfMeasurement.distance(pt1, mid, TurfConstants.UNIT_MILES),
    TurfMeasurement.distance(pt2, mid, TurfConstants.UNIT_MILES), DELTA);
}
 
Example 6
Source Project: mapbox-java   File: TurfJoinsTest.java    License: MIT License 5 votes vote down vote up
@Test
public void testPolyWithHole() throws TurfException, IOException {
  Point ptInHole = Point.fromLngLat(-86.69208526611328, 36.20373274711739);
  Point ptInPoly = Point.fromLngLat(-86.72229766845702, 36.20258997094334);
  Point ptOutsidePoly = Point.fromLngLat(-86.75079345703125, 36.18527313913089);
  Feature polyHole = Feature.fromJson(loadJsonFixture(POLY_WITH_HOLE_FIXTURE));

  assertFalse(TurfJoins.inside(ptInHole, (Polygon) polyHole.geometry()));
  assertTrue(TurfJoins.inside(ptInPoly, (Polygon) polyHole.geometry()));
  assertFalse(TurfJoins.inside(ptOutsidePoly, (Polygon) polyHole.geometry()));
}
 
Example 7
Source Project: mapbox-java   File: TurfMeasurementTest.java    License: MIT License 5 votes vote down vote up
@Test
public void testMidpointVericalFromEquator() throws TurfException {
  Point pt1 = Point.fromLngLat(0, 0);
  Point pt2 = Point.fromLngLat(0, 10);
  Point mid = TurfMeasurement.midpoint(pt1, pt2);

  assertEquals(TurfMeasurement.distance(pt1, mid, TurfConstants.UNIT_MILES),
    TurfMeasurement.distance(pt2, mid, TurfConstants.UNIT_MILES), DELTA);
}
 
Example 8
@SuppressWarnings( {"MissingPermission"})
private void initLocationEngine() {
  locationEngine = new LocationEngineProvider(this).obtainBestLocationEngineAvailable();
  locationEngine.setPriority(HIGH_ACCURACY);
  locationEngine.setInterval(0);
  locationEngine.setFastestInterval(1000);
  locationEngine.addLocationEngineListener(this);
  locationEngine.activate();

  if (locationEngine.getLastLocation() != null) {
    Location lastLocation = locationEngine.getLastLocation();
    onLocationChanged(lastLocation);
    origin = Point.fromLngLat(lastLocation.getLongitude(), lastLocation.getLatitude());
  }
}
 
Example 9
Source Project: mapbox-java   File: TurfMeasurementTest.java    License: MIT License 5 votes vote down vote up
@Test
public void testDestination() throws TurfException {
  Point pt1 = Point.fromLngLat(-75.0, 39.0);
  double dist = 100;
  double bear = 180;
  assertNotNull(TurfMeasurement.destination(pt1, dist, bear, TurfConstants.UNIT_KILOMETERS));
}
 
Example 10
@Test
public void stepDistanceRemaining_returnsZeroWhenPositionsEqualEachOther() throws Exception {
  DirectionsRoute route = buildMultiLegRoute();
  Point snappedPoint = Point.fromLngLat(-77.062996, 38.798405);
  List<Point> coordinates = PolylineUtils.decode(
    route.legs().get(0).steps().get(1).geometry(), Constants.PRECISION_6
  );

  double distance = NavigationHelper.stepDistanceRemaining(snappedPoint, 0, 1, route, coordinates);

  assertEquals(0.0, distance);
}
 
Example 11
Source Project: mapbox-java   File: ShifterTest.java    License: MIT License 5 votes vote down vote up
@Test
public void bbox_basic_shift() throws Exception {

  Point southwest = Point.fromLngLat(2.0, 2.0);
  Point northeast = Point.fromLngLat(4.0, 4.0);

  CoordinateShifter shifter = new TestCoordinateShifter();

  // Manually shifted
  List<Double> shifted = shifter.shiftLonLat(southwest.longitude(), southwest.latitude());
  Point southwestManualShifted = Point.fromLngLat(shifted.get(0), shifted.get(1));
  shifted = shifter.shiftLonLat(northeast.longitude(), northeast.latitude());
  Point northeastManualShifted = Point.fromLngLat(shifted.get(0), shifted.get(1));

  CoordinateShifterManager.setCoordinateShifter(shifter);

  BoundingBox boundingBoxFromDouble = BoundingBox.fromLngLats(2.0, 2.0, 4.0, 4.0);

  BoundingBox boundingBoxFromPoints =
          BoundingBox.fromPoints(Point.fromLngLat(2.0, 2.0),
                                 Point.fromLngLat(4.0, 4.0));


  assertEquals(boundingBoxFromDouble, boundingBoxFromPoints);
  assertEquals(southwestManualShifted, boundingBoxFromPoints.southwest());
  assertEquals(northeastManualShifted, boundingBoxFromPoints.northeast());

  CoordinateShifterManager.setCoordinateShifter(null);
}
 
Example 12
private Point retrieveRouteDestination(DirectionsRoute route) {
  RouteLeg lastLeg = route.legs().get(route.legs().size() - 1);
  LegStep lastStep = lastLeg.steps().get(lastLeg.steps().size() - 1);
  StepManeuver finalManuever = lastStep.maneuver();
  if (finalManuever.location() != null) {
    return finalManuever.location();
  }
  return Point.fromLngLat(0d, 0d);
}
 
Example 13
Source Project: mapbox-java   File: ShifterTest.java    License: MIT License 5 votes vote down vote up
@Test
public void point_toJson() throws Exception {

  // set shifter
  CoordinateShifterManager.setCoordinateShifter(new TestCoordinateShifter());
  Point point1 = Point.fromLngLat(2.0, 3.0);
  String point1JsonString = point1.toJson();
  Point point2 = Point.fromJson(point1JsonString);

  assertEquals(point1, point2);

  CoordinateShifterManager.setCoordinateShifter(null);
}
 
Example 14
private void fetchRoute(Context context) {
  Point origin = Point.fromLngLat(-77.033987, 38.900123);
  Point destination = Point.fromLngLat(-77.044818, 38.848942);
  NavigationRoute.builder(context)
    .accessToken(Mapbox.getAccessToken())
    .origin(origin)
    .destination(destination)
    .build().getRoute(this);
}
 
Example 15
/**
 * Takes in a raw location, converts it to a point, and snaps it to the closest point along the
 * route. This is isolated as separate logic from the snap logic provided because we will always
 * need to snap to the route in order to get the most accurate information.
 */
static Point userSnappedToRoutePosition(Location location, List<Point> coordinates) {
  if (coordinates.size() < 2) {
    return Point.fromLngLat(location.getLongitude(), location.getLatitude());
  }

  Point locationToPoint = Point.fromLngLat(location.getLongitude(), location.getLatitude());

  // Uses Turf's pointOnLine, which takes a Point and a LineString to calculate the closest
  // Point on the LineString.
  Feature feature = TurfMisc.nearestPointOnLine(locationToPoint, coordinates);
  return ((Point) feature.geometry());
}
 
Example 16
/**
 * Method to check if the user has passed either the set (in {@link MapboxNavigationOptions})
 * minimum amount of seconds or minimum amount of meters since the last reroute.
 * <p>
 * If the user is above both thresholds, then the off-route can proceed.  Otherwise, ignore.
 *
 * @param location current location from engine
 * @param options  for second (default 3) / distance (default 50m) minimums
 * @return true if valid, false if not
 */
private boolean validOffRoute(Location location, MapboxNavigationOptions options) {
  // Check if minimum amount of distance has been passed since last reroute
  Point currentPoint = Point.fromLngLat(location.getLongitude(), location.getLatitude());
  double distanceFromLastReroute = 0d;
  if (lastReroutePoint != null) {
    distanceFromLastReroute = TurfMeasurement.distance(lastReroutePoint,
      currentPoint, TurfConstants.UNIT_METERS);
  } else {
    // If null, this is our first update - set the last reroute point to the given location
    updateLastReroutePoint(location);
  }
  return distanceFromLastReroute > options.minimumDistanceBeforeRerouting();
}
 
Example 17
Source Project: mapbox-java   File: TurfMiscTest.java    License: MIT License 5 votes vote down vote up
@Test
public void testTurfPointOnLineFirstPoint() throws TurfException {
  List<Point> line = new ArrayList<>();
  line.add(Point.fromLngLat(-122.45717525482178, 37.72003306385638));
  line.add(Point.fromLngLat(-122.45717525482178, 37.718242366859215));

  Point pt = Point.fromLngLat(-122.45717525482178, 37.72003306385638);

  Feature snappedFeature = TurfMisc.nearestPointOnLine(pt, line);
  Point snapped = (Point) snappedFeature.geometry();
  // pt on start does not move
  assertEquals(pt, snapped);
}
 
Example 18
Source Project: mapbox-java   File: StepManeuver.java    License: MIT License 2 votes vote down vote up
/**
 * A {@link Point} representing this intersection location.
 *
 * @return GeoJson Point representing this intersection location
 * @since 3.0.0
 */
@NonNull
public Point location() {
  return Point.fromLngLat(rawLocation()[0], rawLocation()[1]);
}
 
Example 19
Source Project: mapbox-java   File: OptimizationWaypoint.java    License: MIT License 2 votes vote down vote up
/**
 * A {@link Point} representing this waypoint location.
 *
 * @return GeoJson Point representing this waypoint location
 * @since 3.0.0
 */
@Nullable
public Point location() {
  return Point.fromLngLat(rawLocation()[0], rawLocation()[1]);
}
 
Example 20
Source Project: mapbox-java   File: DirectionsWaypoint.java    License: MIT License 2 votes vote down vote up
/**
 * A {@link Point} representing this waypoint location.
 *
 * @return GeoJson Point representing this waypoint location
 * @since 3.0.0
 */
@Nullable
public Point location() {
  return Point.fromLngLat(rawLocation()[0], rawLocation()[1]);
}