Search in sources :

Example 1 with LineString

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;
}
Also used : MarkerData(com.mapbox.mapboxsdk.plugins.geojson.model.MarkerData) LatLngBounds(com.mapbox.mapboxsdk.geometry.LatLngBounds) ArrayList(java.util.ArrayList) LineString(com.mapbox.geojson.LineString) Point(com.mapbox.geojson.Point) Feature(com.mapbox.geojson.Feature) Point(com.mapbox.geojson.Point) PolyData(com.mapbox.mapboxsdk.plugins.geojson.model.PolyData) FeatureCollection(com.mapbox.geojson.FeatureCollection) LineString(com.mapbox.geojson.LineString) DataModel(com.mapbox.mapboxsdk.plugins.geojson.model.DataModel) LatLng(com.mapbox.mapboxsdk.geometry.LatLng) Polygon(com.mapbox.geojson.Polygon)

Example 2 with LineString

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);
}
Also used : LineString(com.mapbox.geojson.LineString) Point(com.mapbox.geojson.Point) RouteProgress(com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress) LegStep(com.mapbox.api.directions.v5.models.LegStep) Location(android.location.Location) Test(org.junit.Test) BaseTest(com.mapbox.services.android.navigation.v5.BaseTest)

Example 3 with LineString

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);
}
Also used : LineString(com.mapbox.geojson.LineString) Point(com.mapbox.geojson.Point) RouteProgress(com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress) LegStep(com.mapbox.api.directions.v5.models.LegStep) Location(android.location.Location) Test(org.junit.Test) BaseTest(com.mapbox.services.android.navigation.v5.BaseTest)

Example 4 with LineString

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);
    }
}
Also used : LineString(com.mapbox.geojson.LineString) Point(com.mapbox.geojson.Point) Test(org.junit.Test) BaseTest(com.mapbox.services.android.navigation.v5.BaseTest)

Example 5 with LineString

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);
}
Also used : LineString(com.mapbox.geojson.LineString) Test(org.junit.Test) BaseTest(com.mapbox.services.android.navigation.v5.BaseTest)

Aggregations

LineString (com.mapbox.geojson.LineString)19 Point (com.mapbox.geojson.Point)15 BaseTest (com.mapbox.services.android.navigation.v5.BaseTest)9 Test (org.junit.Test)9 LegStep (com.mapbox.api.directions.v5.models.LegStep)5 RouteProgress (com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress)5 Location (android.location.Location)2 Feature (com.mapbox.geojson.Feature)2 ArrayList (java.util.ArrayList)2 Handler (android.os.Handler)1 NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 FeatureCollection (com.mapbox.geojson.FeatureCollection)1 Polygon (com.mapbox.geojson.Polygon)1 LatLng (com.mapbox.mapboxsdk.geometry.LatLng)1 LatLngBounds (com.mapbox.mapboxsdk.geometry.LatLngBounds)1 DataModel (com.mapbox.mapboxsdk.plugins.geojson.model.DataModel)1 MarkerData (com.mapbox.mapboxsdk.plugins.geojson.model.MarkerData)1 PolyData (com.mapbox.mapboxsdk.plugins.geojson.model.PolyData)1 RouteLegProgress (com.mapbox.services.android.navigation.v5.routeprogress.RouteLegProgress)1