Search in sources :

Example 1 with Feature

use of com.mapbox.geojson.Feature 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 Feature

use of com.mapbox.geojson.Feature in project mapbox-plugins-android by mapbox.

the class LocationLayer method onMapClick.

// 
// Map click event
// 
boolean onMapClick(LatLng point) {
    PointF screenLoc = mapboxMap.getProjection().toScreenLocation(point);
    List<Feature> features = mapboxMap.queryRenderedFeatures(screenLoc, BACKGROUND_LAYER, FOREGROUND_LAYER, BEARING_LAYER);
    return !features.isEmpty();
}
Also used : PointF(android.graphics.PointF) Feature(com.mapbox.geojson.Feature)

Example 3 with Feature

use of com.mapbox.geojson.Feature in project mapbox-navigation-android by mapbox.

the class MeasurementUtils method userTrueDistanceFromStep.

/**
 * Calculates the distance between the users current raw {@link android.location.Location} object
 * to the closest {@link Point} in the {@link LegStep}.
 *
 * @param usersRawLocation {@link Point} the raw location where the user is currently located
 * @param step             {@link LegStep} to calculate the closest point on the step to our
 *                         predicted location
 * @return double in distance meters
 * @since 0.2.0
 */
public static double userTrueDistanceFromStep(Point usersRawLocation, LegStep step) {
    // Check that the leg step contains geometry.
    if (TextUtils.isEmpty(step.geometry())) {
        return 0;
    }
    // Get the lineString from the step geometry.
    LineString lineString = LineString.fromPolyline(step.geometry(), Constants.PRECISION_6);
    // the distance is obviously zero, so return 0 to avoid executing additional unnecessary code.
    if (lineString.coordinates().isEmpty() || usersRawLocation.equals(lineString.coordinates().get(0))) {
        return 0;
    }
    if (lineString.coordinates().size() == 1) {
        return TurfMeasurement.distance(usersRawLocation, lineString.coordinates().get(0), UNIT_METERS);
    }
    Feature feature = TurfMisc.nearestPointOnLine(usersRawLocation, lineString.coordinates());
    Point snappedPoint = (Point) feature.geometry();
    if (snappedPoint == null) {
        return 0;
    }
    if (Double.isInfinite(snappedPoint.latitude()) || Double.isInfinite(snappedPoint.longitude())) {
        return TurfMeasurement.distance(usersRawLocation, lineString.coordinates().get(0), UNIT_METERS);
    }
    double distance = TurfMeasurement.distance(usersRawLocation, snappedPoint, UNIT_METERS);
    return Double.isNaN(distance) ? 0d : distance;
}
Also used : LineString(com.mapbox.geojson.LineString) Point(com.mapbox.geojson.Point) Feature(com.mapbox.geojson.Feature)

Example 4 with Feature

use of com.mapbox.geojson.Feature in project mapbox-plugins-android by mapbox.

the class RegionSelectionFragment method getOfflineRegionName.

public String getOfflineRegionName() {
    List<Feature> featureList = mapboxMap.queryRenderedFeatures(boundingBox, LAYER_IDS);
    if (featureList.isEmpty()) {
        Timber.v("Rendered features empty, attempting to query vector source.");
        VectorSource source = mapboxMap.getSourceAs("composite");
        if (source != null) {
            featureList = source.querySourceFeatures(SOURCE_LAYER_IDS, null);
        }
    }
    if (!featureList.isEmpty() && featureList.get(0).properties().has("name")) {
        return featureList.get(0).getStringProperty("name");
    }
    return getString(R.string.mapbox_offline_default_region_name);
}
Also used : VectorSource(com.mapbox.mapboxsdk.style.sources.VectorSource) Feature(com.mapbox.geojson.Feature)

Example 5 with Feature

use of com.mapbox.geojson.Feature in project mapbox-navigation-android by mapbox.

the class NavigationHelper method userSnappedToRoutePosition.

/**
 * 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());
}
Also used : Point(com.mapbox.geojson.Point) Feature(com.mapbox.geojson.Feature)

Aggregations

Feature (com.mapbox.geojson.Feature)6 Point (com.mapbox.geojson.Point)4 LineString (com.mapbox.geojson.LineString)2 PointF (android.graphics.PointF)1 Location (android.location.Location)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 VectorSource (com.mapbox.mapboxsdk.style.sources.VectorSource)1 ArrayList (java.util.ArrayList)1