Java Code Examples for com.mapbox.geojson.Feature#fromGeometry()

The following examples show how to use com.mapbox.geojson.Feature#fromGeometry() . 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 File: PolygonContainer.java    From AirMapSDK-Android with Apache License 2.0 6 votes vote down vote up
public boolean checkForIntersections() {
    List<LatLng> points = PointMath.findIntersections(path);
    if (points.isEmpty()) {
        return false;
    }

    List<Point> intersections = latLngsToPositions(points);
    if (map.getStyle().getLayer(INTERSECTION_LAYER) == null) {
        Source intersectionSource = new GeoJsonSource(INTERSECTION_SOURCE, Feature.fromGeometry(MultiPoint.fromLngLats(intersections)));
        map.getStyle().addSource(intersectionSource);
        Layer intersectionLayer = new SymbolLayer(INTERSECTION_LAYER, INTERSECTION_SOURCE)
                .withProperties(PropertyFactory.iconImage(INTERSECTION_IMAGE));
        map.getStyle().addLayer(intersectionLayer);
    } else {
        GeoJsonSource intersectionsSource = map.getStyle().getSourceAs(INTERSECTION_SOURCE);
        intersectionsSource.setGeoJson(Feature.fromGeometry(MultiPoint.fromLngLats(intersections)));
    }

    return true;
}
 
Example 2
Source File: NavigationMapRoute.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
private Feature getPointFromLineString(RouteLeg leg, int index) {
  Feature feature = Feature.fromGeometry(Point.fromLngLat(
    leg.steps().get(index).maneuver().location().longitude(),
    leg.steps().get(index).maneuver().location().latitude()
  ));
  feature.addStringProperty(SOURCE_KEY, WAYPOINT_SOURCE_ID);
  feature.addStringProperty("waypoint",
    index == 0 ? "origin" : "destination"
  );
  return feature;
}
 
Example 3
Source File: TurfMeasurement.java    From mapbox-java with MIT License 5 votes vote down vote up
/**
 * Takes a {@link BoundingBox} and uses its coordinates to create a {@link Polygon}
 * geometry.
 *
 * @param boundingBox a {@link BoundingBox} object to calculate with
 * @param properties a {@link JsonObject} containing the feature properties
 * @param id  common identifier of this feature
 * @return a {@link Feature} object
 * @see <a href="http://turfjs.org/docs/#bboxPolygon">Turf BoundingBox Polygon documentation</a>
 * @since 4.9.0
 */
public static Feature bboxPolygon(@NonNull BoundingBox boundingBox,
                                  @Nullable JsonObject properties,
                                  @Nullable String id) {
  return Feature.fromGeometry(Polygon.fromLngLats(
    Collections.singletonList(
      Arrays.asList(
        Point.fromLngLat(boundingBox.west(), boundingBox.south()),
        Point.fromLngLat(boundingBox.east(), boundingBox.south()),
        Point.fromLngLat(boundingBox.east(), boundingBox.north()),
        Point.fromLngLat(boundingBox.west(), boundingBox.north()),
        Point.fromLngLat(boundingBox.west(), boundingBox.south())))), properties, id);
}
 
Example 4
Source File: TurfMeasurement.java    From mapbox-java with MIT License 5 votes vote down vote up
/**
 * Takes a bbox and uses its coordinates to create a {@link Polygon} geometry.
 *
 * @param bbox a double[] object to calculate with
 * @param properties a {@link JsonObject} containing the feature properties
 * @param id  common identifier of this feature
 * @return a {@link Feature} object
 * @see <a href="http://turfjs.org/docs/#bboxPolygon">Turf BoundingBox Polygon documentation</a>
 * @since 4.9.0
 */
public static Feature bboxPolygon(@NonNull double[] bbox,
                                  @Nullable JsonObject properties,
                                  @Nullable String id) {
  return Feature.fromGeometry(Polygon.fromLngLats(
    Collections.singletonList(
      Arrays.asList(
        Point.fromLngLat(bbox[0], bbox[1]),
        Point.fromLngLat(bbox[2], bbox[1]),
        Point.fromLngLat(bbox[2], bbox[3]),
        Point.fromLngLat(bbox[0], bbox[3]),
        Point.fromLngLat(bbox[0], bbox[1])))), properties, id);
}
 
Example 5
Source File: TurfConversion.java    From mapbox-java with MIT License 5 votes vote down vote up
@Nullable
private static Feature coordsToLine(@NonNull List<List<Point>> coordinates,
                                    @Nullable JsonObject properties) {
  if (coordinates.size() > 1) {
    return Feature.fromGeometry(MultiLineString.fromLngLats(coordinates), properties);
  } else if (coordinates.size() == 1) {
    LineString lineString = LineString.fromLngLats(coordinates.get(0));
    return Feature.fromGeometry(lineString, properties);
  }
  return null;
}
 
Example 6
Source File: NavigationMapRoute.java    From graphhopper-navigation-android with MIT License 4 votes vote down vote up
private void updateArrowShaftWith(List<Point> points) {
  LineString shaft = LineString.fromLngLats(points);
  arrowShaftGeoJsonFeature = Feature.fromGeometry(shaft);
  arrowShaftGeoJsonSource.setGeoJson(arrowShaftGeoJsonFeature);
}
 
Example 7
Source File: NavigationMapRoute.java    From graphhopper-navigation-android with MIT License 4 votes vote down vote up
private void updateArrowHeadWith(List<Point> points) {
  double azimuth = TurfMeasurement.bearing(points.get(points.size() - 2), points.get(points.size() - 1));
  arrowHeadGeoJsonFeature = Feature.fromGeometry(points.get(points.size() - 1));
  arrowHeadGeoJsonFeature.addNumberProperty(ARROW_BEARING, (float) MathUtils.wrap(azimuth, 0, MAX_DEGREES));
  arrowHeadGeoJsonSource.setGeoJson(arrowHeadGeoJsonFeature);
}
 
Example 8
Source File: NavigationMapRoute.java    From graphhopper-navigation-android with MIT License 4 votes vote down vote up
private void buildRouteFeatureFromGeometry(int index, List<Feature> features, LineString originalGeometry) {
  Feature feat = Feature.fromGeometry(originalGeometry);
  feat.addStringProperty(SOURCE_KEY, String.format(Locale.US, ID_FORMAT, GENERIC_ROUTE_SOURCE_ID, index));
  feat.addNumberProperty(INDEX_KEY, index);
  features.add(feat);
}
 
Example 9
Source File: TurfMisc.java    From mapbox-java with MIT License 4 votes vote down vote up
/**
 * Takes a {@link Point} and a {@link LineString} and calculates the closest Point on the
 * LineString.
 *
 * @param pt point to snap from
 * @param coords line to snap to
 * @param units one of the units found inside {@link TurfConstants.TurfUnitCriteria}
 *              can be degrees, radians, miles, or kilometers
 * @return closest point on the line to point
 * @since 4.9.0
 */
@NonNull
public static Feature nearestPointOnLine(@NonNull Point pt, @NonNull List<Point> coords,
                                         @Nullable @TurfConstants.TurfUnitCriteria String units) {

  if (coords.size() < 2) {
    throw new TurfException("Turf nearestPointOnLine requires a List of Points "
        + "made up of at least 2 coordinates.");
  }

  if (units == null) {
    units = TurfConstants.UNIT_KILOMETERS;
  }

  Feature closestPt = Feature.fromGeometry(
      Point.fromLngLat(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY));
  closestPt.addNumberProperty("dist", Double.POSITIVE_INFINITY);

  for (int i = 0; i < coords.size() - 1; i++) {
    Feature start = Feature.fromGeometry(coords.get(i));
    Feature stop = Feature.fromGeometry(coords.get(i + 1));
    //start
    start.addNumberProperty("dist", TurfMeasurement.distance(
      pt, (Point) start.geometry(), units));
    //stop
    stop.addNumberProperty("dist", TurfMeasurement.distance(
      pt, (Point) stop.geometry(), units));
    //perpendicular
    double heightDistance = Math.max(
      start.properties().get("dist").getAsDouble(),
      stop.properties().get("dist").getAsDouble()
    );
    double direction = TurfMeasurement.bearing((Point) start.geometry(),
      (Point) stop.geometry());
    Feature perpendicularPt1 = Feature.fromGeometry(
      TurfMeasurement.destination(pt, heightDistance, direction + 90, units));
    Feature perpendicularPt2 = Feature.fromGeometry(
      TurfMeasurement.destination(pt, heightDistance, direction - 90, units));
    LineIntersectsResult intersect = lineIntersects(
      ((Point) perpendicularPt1.geometry()).longitude(),
      ((Point) perpendicularPt1.geometry()).latitude(),
      ((Point) perpendicularPt2.geometry()).longitude(),
      ((Point) perpendicularPt2.geometry()).latitude(),
      ((Point) start.geometry()).longitude(),
      ((Point) start.geometry()).latitude(),
      ((Point) stop.geometry()).longitude(),
      ((Point) stop.geometry()).latitude()
    );

    Feature intersectPt = null;
    if (intersect != null) {
      intersectPt = Feature.fromGeometry(
        Point.fromLngLat(intersect.horizontalIntersection(), intersect.verticalIntersection()));
      intersectPt.addNumberProperty("dist", TurfMeasurement.distance(pt,
        (Point) intersectPt.geometry(), units));
    }

    if ((double) start.getNumberProperty("dist")
      < (double) closestPt.getNumberProperty("dist")) {
      closestPt = start;
      closestPt.addNumberProperty(INDEX_KEY, i);
    }
    if ((double) stop.getNumberProperty("dist")
      < (double) closestPt.getNumberProperty("dist")) {
      closestPt = stop;
      closestPt.addNumberProperty(INDEX_KEY, i);
    }
    if (intersectPt != null
      && (double) intersectPt.getNumberProperty("dist")
      < (double) closestPt.getNumberProperty("dist")) {
      closestPt = intersectPt;
      closestPt.addNumberProperty(INDEX_KEY, i);
    }
  }

  return closestPt;
}
 
Example 10
Source File: TurfMeasurementTest.java    From mapbox-java with MIT License 4 votes vote down vote up
@Test
public void centerFeature() {
  Feature expectedFeature = Feature.fromGeometry(Point.fromLngLat(133.5, -27.0));
  Feature inputFeature = Feature.fromJson(loadJsonFixture(TURF_AREA_POLYGON_GEOJSON));
  assertEquals(expectedFeature, TurfMeasurement.center(inputFeature, null, null));
}
 
Example 11
Source File: DataCollector.java    From deltachat-android with GNU General Public License v3.0 4 votes vote down vote up
public void updateSource(int chatId,
                          int contactId,
                          long startTimestamp,
                          long endTimestamp) {
    DcArray locations = dcContext.getLocations(chatId, contactId, startTimestamp, endTimestamp);

    MapSource contactMapMetadata = contactMapSources.get(contactId);
    if (contactMapMetadata == null) {
        contactMapMetadata = addContactMapSource(contactMapSources, contactId);
    }
    int count = locations.getCnt();

    LinkedList<Feature> sortedPointFeatures = featureCollections.get(contactMapMetadata.getMarkerFeatureCollection());
    if (sortedPointFeatures != null && sortedPointFeatures.size() == count) {
        return;
    } else {
        sortedPointFeatures = new LinkedList<>();
    }
    LinkedList<Feature> sortedLineFeatures = new LinkedList<>();

    for (int i = count - 1; i >= 0; i--) {
        Point point = Point.fromLngLat(locations.getLongitude(i), locations.getLatitude(i));

        String codepointChar =
                locations.getMarker(i) != null ?
                        locations.getMarker(i) :
                        "";
        boolean isPoi = locations.isIndependent(i);
        int messageId = locations.getMsgId(i);

        Feature pointFeature = Feature.fromGeometry(point, new JsonObject(), String.valueOf(locations.getLocationId(i)));
        pointFeature.addBooleanProperty(MARKER_SELECTED, false);
        pointFeature.addBooleanProperty(LAST_LOCATION, false);
        pointFeature.addNumberProperty(CONTACT_ID, contactId);
        pointFeature.addNumberProperty(TIMESTAMP, locations.getTimestamp(i));
        pointFeature.addNumberProperty(MESSAGE_ID, messageId);
        pointFeature.addNumberProperty(ACCURACY, locations.getAccuracy(i));
        pointFeature.addStringProperty(MARKER_CHAR, codepointChar);
        pointFeature.addStringProperty(MARKER_ICON, isPoi ?
                contactMapMetadata.getMarkerPoi() :
                contactMapMetadata.getMarkerIcon());
        pointFeature.addBooleanProperty(IS_POI, isPoi);
        if (isPoi && codepointChar.length() == 0 && messageId != 0) {
            //has a long poi label
            DcMsg poiMsg = dcContext.getMsg(messageId);
            String poiLongDescription = poiMsg.getText();
            pointFeature.addStringProperty(POI_LONG_DESCRIPTION, poiLongDescription);
        }

        sortedPointFeatures.addFirst(pointFeature);

        if (!locations.isIndependent(i) && sortedPointFeatures.size() > 1) {
            Point lastPoint = (Point) sortedPointFeatures.get(1).geometry();
            ArrayList<Point> lineSegmentPoints = new ArrayList<>(3);
            lineSegmentPoints.add(lastPoint);
            lineSegmentPoints.add(point);
            LineString l = LineString.fromLngLats(lineSegmentPoints);
            Feature lineFeature = Feature.fromGeometry(l, new JsonObject(), "l_" + pointFeature.id());
            lineFeature.addNumberProperty(TIMESTAMP, pointFeature.getNumberProperty(TIMESTAMP));
            sortedLineFeatures.addFirst(lineFeature);
        }

        if (boundingBuilder != null) {
            boundingBuilder.include(new LatLng(locations.getLatitude(i), locations.getLongitude(i)));
        }
    }

    if (sortedPointFeatures.size() > 0) {
        for (Feature position : sortedPointFeatures) {
            if (!position.getBooleanProperty(IS_POI)) {
                position.addStringProperty(LAST_POSITION_ICON, contactMapMetadata.getMarkerLastPositon());
                position.addStringProperty(LAST_POSITION_LABEL, contactMapMetadata.getDisplayName());
                position.removeProperty(MARKER_ICON);
                position.addBooleanProperty(LAST_LOCATION, true);
                lastPositions.put(contactId, position);
                break;
            }
        }
    }

    featureCollections.put(contactMapMetadata.getMarkerFeatureCollection(), sortedPointFeatures);
    featureCollections.put(contactMapMetadata.getLineFeatureCollection(), sortedLineFeatures);
}
 
Example 12
Source File: TurfMeasurement.java    From mapbox-java with MIT License 3 votes vote down vote up
/**
 * Takes {@link FeatureCollection} and returns the absolute center
 * of the {@link Feature}s in the {@link FeatureCollection}.
 *
 * @param featureCollection the single {@link FeatureCollection} to find the center of.
 * @param properties a optional {@link JsonObject} containing the properties that should be
 *                   placed in the returned {@link Feature}.
 * @param id  an optional common identifier that should be placed in the returned {@link Feature}.
 * @return a {@link Feature} with a {@link Point} geometry type.
 * @since 5.3.0
 */
public static Feature center(FeatureCollection featureCollection,
                             @Nullable JsonObject properties,
                             @Nullable String id) {
  double[] ext = bbox(featureCollection);
  double finalCenterLongitude = (ext[0] + ext[2]) / 2;
  double finalCenterLatitude = (ext[1] + ext[3]) / 2;
  return Feature.fromGeometry(Point.fromLngLat(finalCenterLongitude, finalCenterLatitude),
      properties, id);
}
 
Example 13
Source File: BasicFeatureCollection.java    From mapbox-java with MIT License 2 votes vote down vote up
public static void main(String[] args) {

    Feature feature = Feature.fromGeometry(Point.fromLngLat(1.0, 2.0));
    System.out.println(feature.toJson());

  }