@Test public void testInvariantFeatureOf4() { String json = "{ type: 'Feature', geometry: { type: 'Point', coordinates: [0, 0]}, " + "properties: {}}"; TurfAssertions.featureOf(Feature.fromJson(json), "Point", "myfn"); }
@Test public void testNullPropertiesJson() { String jsonString = "{\"type\":\"Feature\",\"bbox\":[1.0,2.0,3.0,4.0],\"geometry\":" + "{\"type\":\"LineString\",\"coordinates\":[[1.0,2.0],[2.0,3.0]]}}"; Feature feature = Feature.fromJson(jsonString); // Json( null Properties) -> Feature (empty Properties) -> Json(null Properties) String fromFeatureJsonString = feature.toJson(); assertEquals(fromFeatureJsonString, jsonString); }
@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); }
@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); }
@Test public void test_fromJson_toJson() throws IOException { final String jsonString = loadJsonFixture(SAMPLE_FEATURE_POINT); Feature featureFromJson = Feature.fromJson(jsonString); String jsonStringFromFeature = featureFromJson.toJson(); compareJson(jsonString, jsonStringFromFeature); } }
@Test public void testInvariantGetCoord() { String jsonFeature = "{type: 'Feature', geometry: {type: 'Point', coordinates: [1, 2]}}"; assertEquals(TurfMeta.getCoord(Feature.fromJson(jsonFeature)), Point.fromLngLat(1, 2)); } }
@Test public void testInvariantFeatureOf1() throws TurfException { String json = "{ type: 'Feature', geometry: { type: 'Point', coordinates: [0, 0] }, " + "properties: {}}"; thrown.expect(TurfException.class); thrown.expectMessage(startsWith(".featureOf() requires a name")); TurfAssertions.featureOf(Feature.fromJson(json), "Polygon", null); }
@Test public void testInvariantFeatureOf2() throws TurfException { String json = "{ type: 'Feature'}"; thrown.expect(TurfException.class); thrown.expectMessage(startsWith("Invalid input to foo, Feature with geometry required")); TurfAssertions.featureOf(Feature.fromJson(json), "Polygon", "foo"); }
@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)); }
@Test public void testInvariantFeatureOf3() throws TurfException { String json = "{ type: 'Feature', geometry: { type: 'Point', coordinates: [0, 0] }}"; thrown.expect(TurfException.class); thrown.expectMessage(startsWith("Invalid input to myfn: must be a Polygon, given Point")); TurfAssertions.featureOf(Feature.fromJson(json), "Polygon", "myfn"); }
@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); }
@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); }
@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); }
@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); }
@Test public void test_point_feature_fromJson() throws IOException { final String json = "{ \"type\": \"Feature\"," + "\"geometry\": { \"type\": \"Point\", \"coordinates\": [ 125.6, 10.1] }," + "\"properties\": {\"name\": \"Dinagat Islands\" }}"; Feature geo = Feature.fromJson(json); assertEquals(geo.type(), "Feature"); assertEquals(geo.geometry().type(), "Point"); assertEquals(((Point)geo.geometry()).longitude(), 125.6, DELTA); assertEquals(((Point)geo.geometry()).latitude(), 10.1, DELTA); assertEquals(geo.properties().get("name").getAsString(), "Dinagat Islands"); }
@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); }
@Test public void testInputPositions() throws IOException, TurfException { 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)); Polygon polygon = (Polygon) polyHole.geometry(); assertTrue(TurfJoins.inside(ptInPoly, polygon)); assertFalse(TurfJoins.inside(ptOutsidePoly, polygon)); }
@Test public void bboxFromPolygon() throws TurfException, IOException { Feature feature = Feature.fromJson(loadJsonFixture(TURF_BBOX_POLYGON)); double[] bbox = TurfMeasurement.bbox((Polygon) feature.geometry()); assertEquals(4, bbox.length); assertEquals(100, bbox[0], DELTA); assertEquals(0, bbox[1], DELTA); assertEquals(101, bbox[2], DELTA); assertEquals(1, bbox[3], DELTA); }
@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())); }
@Test public void testMultipolygonWithHole() throws TurfException, IOException { Point ptInHole = Point.fromLngLat(-86.69208526611328, 36.20373274711739); Point ptInPoly = Point.fromLngLat(-86.72229766845702, 36.20258997094334); Point ptInPoly2 = Point.fromLngLat(-86.75079345703125, 36.18527313913089); Point ptOutsidePoly = Point.fromLngLat(-86.75302505493164, 36.23015046460186); Feature multiPolyHole = Feature.fromJson(loadJsonFixture(MULTIPOLY_WITH_HOLE_FIXTURE)); assertFalse(TurfJoins.inside(ptInHole, (MultiPolygon) multiPolyHole.geometry())); assertTrue(TurfJoins.inside(ptInPoly, (MultiPolygon) multiPolyHole.geometry())); assertTrue(TurfJoins.inside(ptInPoly2, (MultiPolygon) multiPolyHole.geometry())); assertFalse(TurfJoins.inside(ptOutsidePoly, (MultiPolygon) multiPolyHole.geometry())); }