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

The following examples show how to use com.mapbox.geojson.Feature#fromJson() . 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: TurfMiscTest.java    From mapbox-java with MIT License 6 votes vote down vote up
@Test
public void testTurfLineSliceVertical() throws IOException, TurfException {
  Point start = Point.fromLngLat(-121.25447809696198, 38.70582415504791);
  Point stop = Point.fromLngLat(-121.25447809696198, 38.70634324369764);

  Feature vertical = Feature.fromJson(loadJsonFixture(LINE_SLICE_VERTICAL));

  LineString sliced = TurfMisc.lineSlice(start, stop, vertical);
  assertNotNull(sliced);

  // No duplicated coords
  assertEquals(2, sliced.coordinates().size());

  // Vertical slice does not collapse to 1st coord
  assertNotEquals(sliced.coordinates().get(0), sliced.coordinates().get(1));
}
 
Example 2
Source File: TurfMiscTest.java    From mapbox-java with MIT License 6 votes vote down vote up
@Test
 public void testLineSliceAlongOvershootLine1() throws IOException, TurfException {
  Feature line1 = Feature.fromJson(loadJsonFixture(LINE_SLICE_ALONG_LINE_ONE));
  LineString lineStringLine1 = (LineString) line1.geometry();

  double start = 500;
  double stop = 1500;

  Point start_point = TurfMeasurement.along(lineStringLine1, start, TurfConstants.UNIT_MILES);
  Point end_point = TurfMeasurement.along(lineStringLine1, stop, TurfConstants.UNIT_MILES);
  LineString sliced = TurfMisc.lineSliceAlong(line1, start, stop, TurfConstants.UNIT_MILES);

  assertEquals(sliced.coordinates().get(0).coordinates(),
    start_point.coordinates());
  assertEquals(sliced.coordinates().get(sliced.coordinates().size() - 1).coordinates(),
    end_point.coordinates());
}
 
Example 3
Source File: TurfMeasurementTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void centerFeatureWithProperties() {
  JsonObject properties = new JsonObject();
  properties.addProperty("key", "value");
  Feature inputFeature = Feature.fromJson(loadJsonFixture(TURF_AREA_POLYGON_GEOJSON));
  Feature returnedCenterFeature = TurfMeasurement.center(inputFeature, properties, null);
  Point returnedPoint = (Point) returnedCenterFeature.geometry();
  if (returnedPoint != null) {
    assertEquals(133.5, returnedPoint.longitude(), 0);
    assertEquals(-27.0, returnedPoint.latitude(), 0);
    if (returnedCenterFeature.properties() != null) {
      assertTrue(returnedCenterFeature.properties().toString().contains("{\"key\":\"value\"}"));
    }
  }
}
 
Example 4
Source File: TurfMiscTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void testTurfLineSliceLine1() throws IOException, TurfException {
  Point start = Point.fromLngLat(-97.79617309570312, 22.254624939561698);
  Point stop = Point.fromLngLat(-97.72750854492188, 22.057641623615734);

  Feature line1 = Feature.fromJson(loadJsonFixture(LINE_SLICE_ONE));

  LineString sliced = TurfMisc.lineSlice(start, stop, line1);
  assertNotNull(sliced);
}
 
Example 5
Source File: TurfMiscTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void testTurfLineSliceRoute1() throws IOException, TurfException {
  Point start = Point.fromLngLat(-79.0850830078125, 37.60117623656667);
  Point stop = Point.fromLngLat(-77.7667236328125, 38.65119833229951);

  Feature route1 = Feature.fromJson(loadJsonFixture(LINE_SLICE_ROUTE_ONE));

  LineString sliced = TurfMisc.lineSlice(start, stop, route1);
  assertNotNull(sliced);
}
 
Example 6
Source File: TurfTransformationTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
@Ignore
public void name() throws Exception {
  Feature feature = Feature.fromJson(loadJsonFixture(CIRCLE_IN));
  Polygon polygon = TurfTransformation.circle((Point) feature.geometry(),
    feature.getNumberProperty("radius").doubleValue());

  FeatureCollection featureCollection = FeatureCollection.fromJson(loadJsonFixture(CIRCLE_OUT));
  compareJson(featureCollection.features().get(1).geometry().toJson(), polygon.toJson());
}
 
Example 7
Source File: TurfMeasurementTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void centerFeatureWithId() {
  final String testIdString = "testId";
  Feature inputFeature = Feature.fromJson(loadJsonFixture(TURF_AREA_POLYGON_GEOJSON));
  Feature returnedCenterFeature = TurfMeasurement.center(inputFeature, null, testIdString);
  Point returnedPoint = (Point) returnedCenterFeature.geometry();
  if (returnedPoint != null) {
    assertEquals(133.5, returnedPoint.longitude(), 0);
    assertEquals(-27.0, returnedPoint.latitude(), 0);
    if (returnedCenterFeature.id() != null) {
      assertEquals(returnedCenterFeature.id(), testIdString);
    }
  }
}
 
Example 8
Source File: TurfJoinsTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void testPolyWithHole() throws TurfException, IOException {
  Point ptInHole = Point.fromLngLat(-86.69208526611328, 36.20373274711739);
  Point ptInPoly = Point.fromLngLat(-86.72229766845702, 36.20258997094334);
  Point ptOutsidePoly = Point.fromLngLat(-86.75079345703125, 36.18527313913089);
  Feature polyHole = Feature.fromJson(loadJsonFixture(POLY_WITH_HOLE_FIXTURE));

  assertFalse(TurfJoins.inside(ptInHole, (Polygon) polyHole.geometry()));
  assertTrue(TurfJoins.inside(ptInPoly, (Polygon) polyHole.geometry()));
  assertFalse(TurfJoins.inside(ptOutsidePoly, (Polygon) polyHole.geometry()));
}
 
Example 9
Source File: TurfMiscTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void testTurfLineSliceRawGeometry() throws IOException, TurfException {
  Point start = Point.fromLngLat(-97.79617309570312, 22.254624939561698);
  Point stop = Point.fromLngLat(-97.72750854492188, 22.057641623615734);

  Feature line1 = Feature.fromJson(loadJsonFixture(LINE_SLICE_ONE));

  LineString sliced = TurfMisc.lineSlice(start, stop, (LineString) line1.geometry());
  assertNotNull(sliced);
}
 
Example 10
Source File: TurfMeasurementTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void bboxFromPoint() throws IOException, TurfException {
  Feature feature = Feature.fromJson(loadJsonFixture(TURF_BBOX_POINT));
  double[] bbox = TurfMeasurement.bbox((Point) feature.geometry());

  assertEquals(4, bbox.length);
  assertEquals(102, bbox[0], DELTA);
  assertEquals(0.5, bbox[1], DELTA);
  assertEquals(102, bbox[2], DELTA);
  assertEquals(0.5, bbox[3], DELTA);
}
 
Example 11
Source File: TurfMeasurementTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void testTurfAlong() throws IOException, TurfException {
  Feature feature = Feature.fromJson(loadJsonFixture(TURF_ALONG_DC_LINE));
  LineString line = (LineString) feature.geometry();

  Point pt1 = TurfMeasurement.along(line, 1, "miles");
  Point pt2 = TurfMeasurement.along(line, 1.2, "miles");
  Point pt3 = TurfMeasurement.along(line, 1.4, "miles");
  Point pt4 = TurfMeasurement.along(line, 1.6, "miles");
  Point pt5 = TurfMeasurement.along(line, 1.8, "miles");
  Point pt6 = TurfMeasurement.along(line, 2, "miles");
  Point pt7 = TurfMeasurement.along(line, 100, "miles");
  Point pt8 = TurfMeasurement.along(line, 0, "miles");
  FeatureCollection fc = FeatureCollection.fromFeatures(new Feature[] {
    Feature.fromGeometry(pt1),
    Feature.fromGeometry(pt2),
    Feature.fromGeometry(pt3),
    Feature.fromGeometry(pt4),
    Feature.fromGeometry(pt5),
    Feature.fromGeometry(pt6),
    Feature.fromGeometry(pt7),
    Feature.fromGeometry(pt8)
  });

  for (Feature f : fc.features()) {
    assertNotNull(f);
    assertEquals("Feature", f.type());
    assertEquals("Point", f.geometry().type());
  }

  assertEquals(8, fc.features().size());
  assertEquals(((Point) fc.features().get(7).geometry()).longitude(), pt8.longitude(), DELTA);
  assertEquals(((Point) fc.features().get(7).geometry()).latitude(), pt8.latitude(), DELTA);
}
 
Example 12
Source File: TurfMeasurementTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void testLineDistanceMultiLineString() throws IOException, TurfException {
  Feature feature = Feature.fromJson(loadJsonFixture(LINE_DISTANCE_MULTILINESTRING));
  assertEquals(4705d, Math.round(1000
    * TurfMeasurement.length((MultiLineString) feature.geometry(),
    TurfConstants.UNIT_KILOMETERS)), DELTA);
}
 
Example 13
Source File: TurfMiscTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void testLineAlongStopLongerThanLength() throws IOException, TurfException {
  Feature line1 = Feature.fromJson(loadJsonFixture(LINE_SLICE_ALONG_LINE_ONE));
  LineString lineStringLine1 = (LineString) line1.geometry();

  double start = 500;
  double stop = 800000;
  Point start_point = TurfMeasurement.along(lineStringLine1, start, TurfConstants.UNIT_MILES);
  List<Point> lineCoordinates = lineStringLine1.coordinates();
  LineString sliced = TurfMisc.lineSliceAlong(line1, start, stop, TurfConstants.UNIT_MILES);
  assertEquals(sliced.coordinates().get(0).coordinates(),
    start_point.coordinates());
  assertEquals(sliced.coordinates().get(sliced.coordinates().size() - 1).coordinates(),
    lineCoordinates.get(lineCoordinates.size() - 1).coordinates());
}
 
Example 14
Source File: TurfMeasurementTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void testLineDistanceWithGeometries() throws IOException, TurfException {
  Feature route1 = Feature.fromJson(loadJsonFixture(LINE_DISTANCE_ROUTE_ONE));
  Feature route2 = Feature.fromJson(loadJsonFixture(LINE_DISTANCE_ROUTE_TWO));
  assertEquals(202, Math.round(TurfMeasurement.length((LineString) route1.geometry(),
    TurfConstants.UNIT_MILES)));
  Assert.assertEquals(741.7787396994203,
    TurfMeasurement.length((LineString) route2.geometry(), TurfConstants.UNIT_KILOMETERS), DELTA);
}
 
Example 15
Source File: TurfMiscTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void testLineAlongStartLongerThanLength() throws Exception {
  thrown.expect(TurfException.class);
  thrown.expectMessage(startsWith("Start position is beyond line"));

  Feature line1 = Feature.fromJson(loadJsonFixture(LINE_SLICE_ALONG_LINE_ONE));

  double start = 500000;
  double stop = 800000;
  TurfMisc.lineSliceAlong(line1, start, stop, TurfConstants.UNIT_MILES);
}
 
Example 16
Source File: TurfMiscTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void testTurfLineSliceRoute2() throws IOException, TurfException {
  Point start = Point.fromLngLat(-112.60660171508789, 45.96021963947196);
  Point stop = Point.fromLngLat(-111.97265625, 48.84302835299516);

  Feature route2 = Feature.fromJson(loadJsonFixture(LINE_SLICE_ROUTE_TWO));

  LineString sliced = TurfMisc.lineSlice(start, stop, route2);
  assertNotNull(sliced);
}
 
Example 17
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 18
Source File: TurfConversionTest.java    From mapbox-java with MIT License 4 votes vote down vote up
@Test
public void polygonToLine_Polygon() throws NullPointerException {
  Feature polygon = Feature.fromJson(loadJsonFixture(TURF_POLYGON_TO_LINE_PATH_IN + TURF_POLYGON_TO_LINE_FILENAME_POLYGON));
  Feature expected = Feature.fromJson(loadJsonFixture(TURF_POLYGON_TO_LINE_PATH_OUT + TURF_POLYGON_TO_LINE_FILENAME_POLYGON));
  compareJson(expected.toJson(), TurfConversion.polygonToLine(polygon).toJson());
}
 
Example 19
Source File: TurfConversionTest.java    From mapbox-java with MIT License 4 votes vote down vote up
@Test
public void polygonToLine_PolygonWithHole() throws NullPointerException {
  Feature polygon = Feature.fromJson(loadJsonFixture(TURF_POLYGON_TO_LINE_PATH_IN + TURF_POLYGON_TO_LINE_FILENAME_POLYGON_WITH_HOLE));
  Feature expected = Feature.fromJson(loadJsonFixture(TURF_POLYGON_TO_LINE_PATH_OUT + TURF_POLYGON_TO_LINE_FILENAME_POLYGON_WITH_HOLE));
  compareJson(expected.toJson(), TurfConversion.polygonToLine(polygon).toJson());
}
 
Example 20
Source File: TurfConversionTest.java    From mapbox-java with MIT License 4 votes vote down vote up
@Test
public void polygonToLine_MultiPolygonWithOuterDoughnut() throws NullPointerException {
  Feature multiPolygon = Feature.fromJson(loadJsonFixture(TURF_POLYGON_TO_LINE_PATH_IN + TURF_POLYGON_TO_LINE_FILENAME_MULTIPOLYGON_OUTER_DOUGHNUT));
  FeatureCollection expected = FeatureCollection.fromJson(loadJsonFixture(TURF_POLYGON_TO_LINE_PATH_OUT + TURF_POLYGON_TO_LINE_FILENAME_MULTIPOLYGON_OUTER_DOUGHNUT));
  compareJson(expected.toJson(), TurfConversion.multiPolygonToLine(multiPolygon).toJson());
}