use of com.mapbox.geojson.LineString in project mapbox-plugins-android by mapbox.
the class GeoJsonPlugin method parseGeoJsonString.
/**
* @param geoJson String of the GeoJSON file
* @return DataModel list of polylines, polygons, and point with bounded
*/
private DataModel parseGeoJsonString(String geoJson) {
int pointCount = 0;
DataModel dataModel = new DataModel();
LatLngBounds.Builder builder = new LatLngBounds.Builder();
FeatureCollection featureCollection = FeatureCollection.fromJson(geoJson);
List<Feature> listFeature = featureCollection.features();
for (Feature feature : listFeature) {
String featureType = feature.geometry().type();
if (!TextUtils.isEmpty(featureType)) {
if (featureType.equalsIgnoreCase("LineString")) {
List<LatLng> latLngs = new ArrayList<>();
LineString lineString = (LineString) feature.geometry();
List<Point> coordinates = lineString.coordinates();
for (Point position : coordinates) {
LatLng latLng = new LatLng(position.latitude(), position.longitude());
latLngs.add(latLng);
pointCount++;
builder.include(latLng);
}
PolyData polylinePolyData = new PolyData();
polylinePolyData.setPoints(latLngs);
polylinePolyData.setType(featureType);
dataModel.addPolyline(polylinePolyData);
} else if (featureType.equalsIgnoreCase("Point")) {
Point point = (Point) feature.geometry();
if (point != null) {
LatLng latLng = new LatLng(point.latitude(), point.longitude());
MarkerData markerData = new MarkerData();
markerData.setPoint(latLng);
markerData.setProperties(feature.properties());
dataModel.addMarker(markerData);
pointCount++;
builder.include(latLng);
}
} else if (featureType.equalsIgnoreCase("Polygon")) {
List<LatLng> latLngs = new ArrayList<>();
Polygon polygon = (Polygon) feature.geometry();
List<Point> listPosition = polygon.coordinates().get(0);
for (Point position : listPosition) {
LatLng latLng = new LatLng(position.latitude(), position.longitude());
latLngs.add(latLng);
pointCount++;
builder.include(latLng);
}
PolyData polygonPolyData = new PolyData();
polygonPolyData.setPoints(latLngs);
polygonPolyData.setType(featureType);
dataModel.addPolygon(polygonPolyData);
} else {
// TODO
}
}
}
if (pointCount > 1) {
dataModel.setBounds(builder.build());
}
return dataModel;
}
use of com.mapbox.geojson.LineString in project mapbox-navigation-android by mapbox.
the class OffRouteDetectorTest method isUserOffRoute_AssertFalseTwoUpdatesAwayFromManeuverThenOneTowards.
@Test
public void isUserOffRoute_AssertFalseTwoUpdatesAwayFromManeuverThenOneTowards() throws Exception {
RouteProgress routeProgress = buildDefaultRouteProgress();
LegStep currentStep = routeProgress.currentLegProgress().currentStep();
LineString lineString = LineString.fromPolyline(currentStep.geometry(), Constants.PRECISION_6);
List<Point> coordinates = lineString.coordinates();
Location firstLocationUpdate = buildDefaultLocationUpdate(-77.0339782574523, 38.89993519985637);
offRouteDetector.isUserOffRoute(firstLocationUpdate, routeProgress, options);
Point lastPointInCurrentStep = coordinates.remove(coordinates.size() - 1);
Location secondLocationUpdate = buildDefaultLocationUpdate(lastPointInCurrentStep.longitude(), lastPointInCurrentStep.latitude());
boolean isUserOffRouteFirstTry = offRouteDetector.isUserOffRoute(secondLocationUpdate, routeProgress, options);
assertFalse(isUserOffRouteFirstTry);
Point secondLastPointInCurrentStep = coordinates.remove(coordinates.size() - 1);
Location thirdLocationUpdate = buildDefaultLocationUpdate(secondLastPointInCurrentStep.longitude(), secondLastPointInCurrentStep.latitude());
boolean isUserOffRouteSecondTry = offRouteDetector.isUserOffRoute(thirdLocationUpdate, routeProgress, options);
assertFalse(isUserOffRouteSecondTry);
Location fourthLocationUpdate = buildDefaultLocationUpdate(lastPointInCurrentStep.longitude(), lastPointInCurrentStep.latitude());
boolean isUserOffRouteThirdTry = offRouteDetector.isUserOffRoute(fourthLocationUpdate, routeProgress, options);
assertFalse(isUserOffRouteThirdTry);
}
use of com.mapbox.geojson.LineString in project mapbox-navigation-android by mapbox.
the class OffRouteDetectorTest method isUserOffRoute_AssertTrueWhenOnRouteButMovingAwayFromManeuver.
@Test
public void isUserOffRoute_AssertTrueWhenOnRouteButMovingAwayFromManeuver() throws Exception {
RouteProgress routeProgress = buildDefaultRouteProgress();
LegStep currentStep = routeProgress.currentLegProgress().currentStep();
LineString lineString = LineString.fromPolyline(currentStep.geometry(), Constants.PRECISION_6);
List<Point> coordinates = lineString.coordinates();
Location firstLocationUpdate = buildDefaultLocationUpdate(-77.0339782574523, 38.89993519985637);
offRouteDetector.isUserOffRoute(firstLocationUpdate, routeProgress, options);
Point lastPointInCurrentStep = coordinates.remove(coordinates.size() - 1);
Location secondLocationUpdate = buildDefaultLocationUpdate(lastPointInCurrentStep.longitude(), lastPointInCurrentStep.latitude());
boolean isUserOffRouteFirstTry = offRouteDetector.isUserOffRoute(secondLocationUpdate, routeProgress, options);
assertFalse(isUserOffRouteFirstTry);
Point secondLastPointInCurrentStep = coordinates.remove(coordinates.size() - 1);
Location thirdLocationUpdate = buildDefaultLocationUpdate(secondLastPointInCurrentStep.longitude(), secondLastPointInCurrentStep.latitude());
boolean isUserOffRouteSecondTry = offRouteDetector.isUserOffRoute(thirdLocationUpdate, routeProgress, options);
assertFalse(isUserOffRouteSecondTry);
Point thirdLastPointInCurrentStep = coordinates.remove(coordinates.size() - 1);
Location fourthLocationUpdate = buildDefaultLocationUpdate(thirdLastPointInCurrentStep.longitude(), thirdLastPointInCurrentStep.latitude());
boolean isUserOffRouteThirdTry = offRouteDetector.isUserOffRoute(fourthLocationUpdate, routeProgress, options);
assertFalse(isUserOffRouteThirdTry);
Point fourthLastPointInCurrentStep = coordinates.remove(coordinates.size() - 1);
Location fifthLocationUpdate = buildDefaultLocationUpdate(fourthLastPointInCurrentStep.longitude(), fourthLastPointInCurrentStep.latitude());
boolean isUserOffRouteFourthTry = offRouteDetector.isUserOffRoute(fifthLocationUpdate, routeProgress, options);
assertFalse(isUserOffRouteFourthTry);
Point fifthLastPointInCurrentStep = coordinates.remove(coordinates.size() - 1);
Location sixthLocationUpdate = buildDefaultLocationUpdate(fifthLastPointInCurrentStep.longitude(), fifthLastPointInCurrentStep.latitude());
boolean isUserOffRouteFifthTry = offRouteDetector.isUserOffRoute(sixthLocationUpdate, routeProgress, options);
assertTrue(isUserOffRouteFifthTry);
}
use of com.mapbox.geojson.LineString in project mapbox-navigation-android by mapbox.
the class RouteStepProgressTest method fractionTraveled_equalsCorrectValueAtIntervals.
@Test
public void fractionTraveled_equalsCorrectValueAtIntervals() {
LineString lineString = LineString.fromPolyline(firstStep.geometry(), Constants.PRECISION_6);
// meters
double stepSegments = 5;
// Chop the line in small pieces
for (double i = 0; i < firstStep.distance(); i += stepSegments) {
Point point = TurfMeasurement.along(lineString, i, TurfConstants.UNIT_METERS);
LineString slicedLine = TurfMisc.lineSlice(point, route.legs().get(0).steps().get(1).maneuver().location(), lineString);
double distance = TurfMeasurement.length(slicedLine, TurfConstants.UNIT_METERS);
RouteProgress routeProgress = RouteProgress.builder().stepDistanceRemaining(distance).legDistanceRemaining(firstLeg.distance()).distanceRemaining(route.distance()).directionsRoute(route).stepIndex(0).legIndex(0).build();
RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();
float fractionRemaining = (float) ((firstStep.distance() - distance) / firstStep.distance());
if (fractionRemaining < 0) {
fractionRemaining = 0;
}
assertEquals(fractionRemaining, routeStepProgress.fractionTraveled(), DELTA);
}
}
use of com.mapbox.geojson.LineString in project mapbox-navigation-android by mapbox.
the class RouteStepProgressTest method distanceRemaining_equalsStepDistanceAtBeginning.
@Test
public void distanceRemaining_equalsStepDistanceAtBeginning() {
LineString lineString = LineString.fromPolyline(firstLeg.steps().get(5).geometry(), Constants.PRECISION_6);
double stepDistance = TurfMeasurement.length(lineString, TurfConstants.UNIT_METERS);
RouteProgress routeProgress = RouteProgress.builder().stepDistanceRemaining(firstLeg.steps().get(5).distance()).legDistanceRemaining(firstLeg.distance()).distanceRemaining(route.distance()).directionsRoute(route).stepIndex(4).legIndex(0).build();
RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();
assertEquals(stepDistance, routeStepProgress.distanceRemaining(), BaseTest.LARGE_DELTA);
}
Aggregations