com.mapbox.geojson.Geometry Java Examples

The following examples show how to use com.mapbox.geojson.Geometry. 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: TurfMeta.java    From mapbox-java with MIT License 6 votes vote down vote up
/**
 * Get all coordinates from a {@link FeatureCollection} object, returning a
 * {@code List} of {@link Point} objects.
 *
 * @param pointList        the {@code List} of {@link Point}s.
 * @param geometry         the {@link Geometry} object to extract the {@link Point}s from
 * @param excludeWrapCoord whether or not to include the final coordinate of LinearRings that
 *                         wraps the ring in its iteration. Used if the {@link Feature}
 *                         passed through the method is a {@link Polygon} or {@link MultiPolygon}
 *                         geometry.
 * @return a {@code List} made up of {@link Point}s
 * @since 4.8.0
 */
@NonNull
private static List<Point> coordAllFromSingleGeometry(@NonNull List<Point> pointList,
                                                      @NonNull Geometry geometry,
                                                      @NonNull boolean excludeWrapCoord) {
  if (geometry instanceof Point) {
    pointList.add((Point) geometry);
  } else if (geometry instanceof MultiPoint) {
    pointList.addAll(((MultiPoint) geometry).coordinates());
  } else if (geometry instanceof LineString) {
    pointList.addAll(((LineString) geometry).coordinates());
  } else if (geometry instanceof MultiLineString) {
    coordAll(pointList, (MultiLineString) geometry);
  } else if (geometry instanceof Polygon) {
    coordAll(pointList, (Polygon) geometry, excludeWrapCoord);
  } else if (geometry instanceof MultiPolygon) {
    coordAll(pointList, (MultiPolygon) geometry, excludeWrapCoord);
  } else if (geometry instanceof GeometryCollection) {
    // recursive
    for (Geometry singleGeometry : ((GeometryCollection) geometry).geometries()) {
      coordAllFromSingleGeometry(pointList, singleGeometry, excludeWrapCoord);
    }
  }
  return pointList;
}
 
Example #2
Source File: TurfMeasurement.java    From mapbox-java with MIT License 6 votes vote down vote up
/**
 * Takes a set of features, calculates the bbox of all input features, and returns a bounding box.
 *
 * @param geoJson a {@link GeoJson} object
 * @return a double array defining the bounding box in this order {@code [minX, minY, maxX, maxY]}
 * @since 4.8.0
 */
public static double[] bbox(GeoJson geoJson) {
  BoundingBox boundingBox = geoJson.bbox();
  if (boundingBox != null) {
    return new double[] {
      boundingBox.west(),
      boundingBox.south(),
      boundingBox.east(),
      boundingBox.north()
    };
  }

  if (geoJson instanceof Geometry) {
    return bbox((Geometry) geoJson);
  } else if (geoJson instanceof FeatureCollection) {
    return bbox((FeatureCollection) geoJson);
  } else if (geoJson instanceof Feature) {
    return bbox((Feature) geoJson);
  } else {
    throw new UnsupportedOperationException("bbox type not supported for GeoJson instance");
  }
}
 
Example #3
Source File: TurfMetaTest.java    From mapbox-java with MIT License 6 votes vote down vote up
@Test
public void coordAllGeometryCollection() throws TurfException {
  List<Point> points = new ArrayList<>();
  points.add(Point.fromLngLat(1.0, 2.0));
  points.add(Point.fromLngLat(2.0, 3.0));
  LineString lineString = LineString.fromLngLats(points);
  List<Geometry> geometries = new ArrayList<>();
  geometries.add(points.get(0));
  geometries.add(lineString);

  BoundingBox bbox = BoundingBox.fromLngLats(1.0, 2.0, 3.0, 4.0);
  GeometryCollection geometryCollection = GeometryCollection.fromGeometries(geometries, bbox);

  FeatureCollection featureCollection = FeatureCollection.fromFeature(
    Feature.fromGeometry(geometryCollection)
  );

  assertNotNull(featureCollection);
  assertNotNull(TurfMeta.coordAll(featureCollection,true));
  assertEquals(3, TurfMeta.coordAll(featureCollection,true).size());
  assertEquals(1.0, TurfMeta.coordAll(featureCollection,true).get(0).longitude(), DELTA);
  assertEquals(2.0, TurfMeta.coordAll(featureCollection,true).get(0).latitude(), DELTA);
}
 
Example #4
Source File: TurfMeasurementTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void bboxFromGeometry() throws IOException, TurfException {
  Geometry geometry = MultiPolygon.fromJson(loadJsonFixture(TURF_BBOX_MULTIPOLYGON));
  double[] bbox = TurfMeasurement.bbox(geometry);

  assertEquals(4, bbox.length);
  assertEquals(100, bbox[0], DELTA);
  assertEquals(0, bbox[1], DELTA);
  assertEquals(103, bbox[2], DELTA);
  assertEquals(3, bbox[3], DELTA);
}
 
Example #5
Source File: GeometryGeoJson.java    From mapbox-java with MIT License 5 votes vote down vote up
/**
 * Create a new instance of Geometry class by passing in a formatted valid JSON String.
 *
 * @param json a formatted valid JSON string defining a GeoJson Geometry
 * @return a new instance of Geometry class defined by the values passed inside
 *   this static factory method
 * @since 4.0.0
 */
public static Geometry fromJson(@NonNull String json) {

  GsonBuilder gson = new GsonBuilder();
  gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create());
  gson.registerTypeAdapterFactory(GeometryAdapterFactory.create());

  return gson.create().fromJson(json, Geometry.class);
}
 
Example #6
Source File: AirMapMapView.java    From AirMapSDK-Android with Apache License 2.0 5 votes vote down vote up
private void zoomToFeatureIfNecessary(Feature featureClicked) {
    LatLngBounds cameraBounds = getMap().getProjection().getVisibleRegion().latLngBounds;
    LatLngBounds.Builder advisoryLatLngsBuilder = new LatLngBounds.Builder();
    boolean zoom = false;

    Geometry geometry = featureClicked.geometry();
    List<Point> points = new ArrayList<>();
    if (geometry instanceof Polygon) {
        points.addAll(((Polygon) geometry).outer().coordinates());
    } else if (geometry instanceof MultiPolygon) {
        List<Polygon> polygons = ((MultiPolygon) geometry).polygons();
        for (Polygon polygon : polygons) {
            points.addAll(polygon.outer().coordinates());
        }
    }

    for (Point position : points) {
        LatLng latLng = new LatLng(position.latitude(), position.longitude());
        advisoryLatLngsBuilder.include(latLng);
        if (!cameraBounds.contains(latLng)) {
            Timber.d("Camera position doesn't contain point");
            zoom = true;
        }
    }

    if (zoom) {
        int padding = Utils.dpToPixels(getContext(), 72).intValue();
        getMap().moveCamera(CameraUpdateFactory.newLatLngBounds(advisoryLatLngsBuilder.build(), padding));
    }
}
 
Example #7
Source File: Annotation.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Nullable
abstract Geometry getOffsetGeometry(@NonNull Projection projection, @NonNull MoveDistancesObject moveDistancesObject,
                                    float touchAreaShiftX, float touchAreaShiftY);
 
Example #8
Source File: DraggableAnnotationController.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
boolean onMove(MoveGestureDetector detector) {
  if (draggedAnnotation != null && (detector.getPointersCount() > 1 || !draggedAnnotation.isDraggable())) {
    // Stopping the drag when we don't work with a simple, on-pointer move anymore
    stopDragging(draggedAnnotation);
    return true;
  }

  // Updating symbol's position
  if (draggedAnnotation != null) {
    MoveDistancesObject moveObject = detector.getMoveObject(0);

    float x = moveObject.getCurrentX() - touchAreaShiftX;
    float y = moveObject.getCurrentY() - touchAreaShiftY;

    PointF pointF = new PointF(x, y);

    if (pointF.x < 0 || pointF.y < 0 || pointF.x > touchAreaMaxX || pointF.y > touchAreaMaxY) {
      stopDragging(draggedAnnotation);
      return true;
    }

    Geometry shiftedGeometry = draggedAnnotation.getOffsetGeometry(
      mapboxMap.getProjection(), moveObject, touchAreaShiftX, touchAreaShiftY
    );

    if (shiftedGeometry != null) {
      draggedAnnotation.setGeometry(
        shiftedGeometry
      );
      annotationManager.internalUpdateSource();
      if (!annotationManager.getDragListeners().isEmpty()) {
        for (D d : annotationManager.getDragListeners()) {
          d.onAnnotationDrag(draggedAnnotation);
        }
      }
      return true;
    }
  }

  return false;
}
 
Example #9
Source File: TurfConversion.java    From mapbox-java with MIT License 4 votes vote down vote up
/**
 * Combines a FeatureCollection of geometries and returns
 * a {@link FeatureCollection} with "Multi-" geometries in it.
 * If the original FeatureCollection parameter has {@link Point}(s)
 * and/or {@link MultiPoint}s), the returned
 * FeatureCollection will include a {@link MultiPoint} object.
 *
 * If the original FeatureCollection parameter has
 * {@link LineString}(s) and/or {@link MultiLineString}s), the returned
 * FeatureCollection will include a {@link MultiLineString} object.
 *
 * If the original FeatureCollection parameter has
 * {@link Polygon}(s) and/or {@link MultiPolygon}s), the returned
 * FeatureCollection will include a {@link MultiPolygon} object.
 *
 * @param originalFeatureCollection a {@link FeatureCollection}
 *
 * @return a {@link FeatureCollection} with a "Multi-" geometry
 *    or "Multi-" geometries.
 *
 * @since 4.10.0
 **/
public static FeatureCollection combine(@NonNull FeatureCollection originalFeatureCollection) {
  if (originalFeatureCollection.features() == null) {
    throw new TurfException("Your FeatureCollection is null.");
  } else if (originalFeatureCollection.features().size() == 0) {
    throw new TurfException("Your FeatureCollection doesn't have any Feature objects in it.");
  }
  List<Point> pointList = new ArrayList<>(0);
  List<LineString> lineStringList = new ArrayList<>(0);
  List<Polygon> polygonList = new ArrayList<>(0);
  for (Feature singleFeature : originalFeatureCollection.features()) {
    Geometry singleFeatureGeometry = singleFeature.geometry();
    if (singleFeatureGeometry instanceof Point || singleFeatureGeometry instanceof MultiPoint) {
      if (singleFeatureGeometry instanceof Point) {
        pointList.add((Point) singleFeatureGeometry);
      } else {
        pointList.addAll(((MultiPoint) singleFeatureGeometry).coordinates());
      }
    } else if (singleFeatureGeometry instanceof LineString || singleFeatureGeometry
      instanceof MultiLineString) {
      if (singleFeatureGeometry instanceof LineString) {
        lineStringList.add((LineString) singleFeatureGeometry);
      } else {
        lineStringList.addAll(((MultiLineString) singleFeatureGeometry).lineStrings());
      }
    } else if (singleFeatureGeometry instanceof Polygon || singleFeatureGeometry
      instanceof MultiPolygon) {
      if (singleFeatureGeometry instanceof Polygon) {
        polygonList.add((Polygon) singleFeatureGeometry);
      } else {
        polygonList.addAll(((MultiPolygon) singleFeatureGeometry).polygons());
      }
    }
  }
  List<Feature> finalFeatureList = new ArrayList<>(0);
  if (!pointList.isEmpty()) {
    finalFeatureList.add(Feature.fromGeometry(MultiPoint.fromLngLats(pointList)));
  }
  if (!lineStringList.isEmpty()) {
    finalFeatureList.add(Feature.fromGeometry(MultiLineString.fromLineStrings(lineStringList)));
  }
  if (!polygonList.isEmpty()) {
    finalFeatureList.add(Feature.fromGeometry(MultiPolygon.fromPolygons(polygonList)));
  }
  return finalFeatureList.isEmpty() ? originalFeatureCollection
    : FeatureCollection.fromFeatures(finalFeatureList);
}
 
Example #10
Source File: TurfConversion.java    From mapbox-java with MIT License 3 votes vote down vote up
/**
 * Takes a {@link Feature} that contains {@link Polygon} and a properties {@link JsonObject} and
 * covert it to a {@link Feature} that contains {@link LineString} or {@link MultiLineString}.
 *
 * @param feature a {@link Feature} object that contains {@link Polygon}
 * @param properties a {@link JsonObject} that represents a feature's properties
 * @return  a {@link Feature} object that contains {@link LineString} or {@link MultiLineString}
 * @since 4.9.0
 */
public static Feature polygonToLine(@NonNull Feature feature, @Nullable JsonObject properties) {
  Geometry geometry = feature.geometry();
  if (geometry instanceof Polygon) {
    return polygonToLine((Polygon) geometry,properties != null ? properties :
            feature.type().equals("Feature") ? feature.properties() : new JsonObject());
  }
  throw new TurfException("Feature's geometry must be Polygon");
}
 
Example #11
Source File: TurfConversion.java    From mapbox-java with MIT License 3 votes vote down vote up
/**
 * Takes a {@link Feature} that contains {@link MultiPolygon} and a
 * properties {@link JsonObject} and
 * covert it to a {@link FeatureCollection} that contains
 * list of {@link Feature} of {@link LineString} or {@link MultiLineString}.
 *
 * @param feature a {@link Feature} object that contains {@link MultiPolygon}
 * @param properties a {@link JsonObject} that represents a feature's properties
 * @return  a {@link FeatureCollection} object that contains
 *   list of {@link Feature} of {@link LineString} or {@link MultiLineString}
 * @since 4.9.0
 */
public static FeatureCollection multiPolygonToLine(@NonNull Feature feature,
                                                   @Nullable JsonObject properties) {
  Geometry geometry = feature.geometry();
  if (geometry instanceof MultiPolygon) {
    return polygonToLine((MultiPolygon) geometry, properties != null ? properties :
            feature.type().equals("Feature") ? feature.properties() : new JsonObject());
  }
  throw new TurfException("Feature's geometry must be MultiPolygon");
}
 
Example #12
Source File: TurfMeasurement.java    From mapbox-java with MIT License 2 votes vote down vote up
/**
 * Takes one {@link Geometry} and returns its area in square meters.
 *
 * @param geometry input {@link Geometry}
 * @return area in square meters
 * @since 4.10.0
 */
public static double area(@NonNull Geometry geometry) {
  return calculateArea(geometry);
}
 
Example #13
Source File: CarmenFeature.java    From mapbox-java with MIT License 2 votes vote down vote up
/**
 * The geometry which makes up this feature. A Geometry object represents points, curves, and
 * surfaces in coordinate space. One of the seven geometries provided inside this library can be
 * passed in through one of the static factory methods.
 *
 * @return a single defined {@link Geometry} which makes this feature spatially aware
 * @since 1.0.0
 */
@Nullable
public abstract Geometry geometry();