private static List<Point> formatCoordinates(String coordinates) { String[] coordPairs = coordinates.split(";", -1); List<Point> coordinatesFormatted = new ArrayList<>(); for (String coordPair : coordPairs) { String[] coords = coordPair.split(",", -1); coordinatesFormatted.add( Point.fromLngLat(Double.valueOf(coords[0]), Double.valueOf(coords[1]))); } return coordinatesFormatted; }
/** * Square distance between 2 points. * * @param p1 first {@link Point} * @param p2 second Point * @return square of the distance between two input points */ private static double getSqDist(Point p1, Point p2) { double dx = p1.longitude() - p2.longitude(); double dy = p1.latitude() - p2.latitude(); return dx * dx + dy * dy; }
/** * This returns a double value ranging from -180 to 180 representing the x or easting position of * this point. ideally, this value would be restricted to 6 decimal places to correctly follow the * GeoJson spec. * * @return a double value ranging from -180 to 180 representing the x or easting position of this * point * @since 3.0.0 */ public double longitude() { return coordinates().get(0); }
@Test public void fromJson() throws IOException { final String json = loadJsonFixture(SAMPLE_MULTIPOLYGON); MultiPolygon geo = MultiPolygon.fromJson(json); assertEquals(geo.type(), "MultiPolygon"); assertEquals(geo.coordinates().get(0).get(0).get(0).longitude(), 102.0, DELTA); assertEquals(geo.coordinates().get(0).get(0).get(0).latitude(), 2.0, DELTA); assertFalse(geo.coordinates().get(0).get(0).get(0).hasAltitude()); }
@Test public void turfAlong_returnsZeroWhenRouteIsPoint() throws Exception { List<Point> coords = new ArrayList<>(); coords.add(Point.fromLngLat(1.0, 1.0)); LineString lineString = LineString.fromLngLats(coords); Point point = TurfMeasurement.along(lineString, 0, TurfConstants.UNIT_METERS); assertEquals(1.0, point.latitude(), DELTA); assertEquals(1.0, point.longitude(), DELTA); }
@Test public void latitude_doesReturnCorrectValue() throws Exception { Point point = Point.fromLngLat(1.0, 2.0, 5.0); assertEquals(2, point.latitude(), DELTA); }
@Test public void longitude_doesReturnCorrectValue() throws Exception { Point point = Point.fromLngLat(1.0, 2.0, 5.0); assertEquals(1, point.longitude(), DELTA); }
@Test public void fromJson() throws IOException { final String json = loadJsonFixture(SAMPLE_MULTIPOINT); MultiPoint geo = MultiPoint.fromJson(json); assertEquals(geo.type(), "MultiPoint"); assertEquals(geo.coordinates().get(0).longitude(), 100.0, DELTA); assertEquals(geo.coordinates().get(0).latitude(), 0.0, DELTA); assertEquals(geo.coordinates().get(1).longitude(), 101.0, DELTA); assertEquals(geo.coordinates().get(1).latitude(), 1.0, DELTA); assertFalse(geo.coordinates().get(0).hasAltitude()); assertEquals(Double.NaN, geo.coordinates().get(0).altitude(), DELTA); }
@Test public void point_hasAltitudeValueNoBoundingBox() throws Exception { Point point = Point.fromLngLat(100, 0, 200); compareJson(loadJsonFixture(POINT_WITH_ALTITUDE_NO_BBOX_FIXTURE), point.toJson()); }
/** * Convenience method for getting the bounding box most westerly point (longitude) as a double * coordinate. * * @return the most westerly coordinate inside this bounding box * @since 3.0.0 */ public final double west() { return southwest().longitude(); }
/** * Convenience method for getting the bounding box most westerly point (longitude) as a double * coordinate. * * @return the most westerly coordinate inside this bounding box * @since 3.0.0 */ public final double north() { return northeast().latitude(); }
@Test public void altitude_doesReturnCorrectValueFromDoubleArray() throws Exception { double[] coords = new double[] {1.0, 2.0, 5.0}; Point point = Point.fromLngLat(coords); assertEquals(5, point.altitude(), DELTA); }
@Test public void bbox_nullWhenNotSet() throws Exception { Point point = Point.fromLngLat(1.0, 2.0); assertNull(point.bbox()); }
@Test public void passingInSingleFeature_doesHandleCorrectly() throws Exception { Point geometry = Point.fromLngLat(1.0, 2.0); Feature feature = Feature.fromGeometry(geometry); FeatureCollection geo = FeatureCollection.fromFeature(feature); assertNotNull(geo.features()); assertEquals(1, geo.features().size()); assertEquals(2.0, ((Point) geo.features().get(0).geometry()).coordinates().get(1), DELTA); }
/** * Optionally, the coordinate spec in GeoJson allows for altitude values to be placed inside the * coordinate array. If an altitude value was provided while initializing this instance, this will * return true. * * @return true if this instance of point contains an altitude value * @since 3.0.0 */ public boolean hasAltitude() { return !Double.isNaN(altitude()); }
@Test public void fromJsonHoles() throws IOException { final String json = loadJsonFixture(SAMPLE_POLYGON_HOLES); Polygon geo = Polygon.fromJson(json); assertEquals("Polygon", geo.type()); assertEquals(100.0, geo.coordinates().get(0).get(0).longitude(), DELTA); assertEquals(0.0, geo.coordinates().get(0).get(0).latitude(), DELTA); assertEquals(2, geo.coordinates().size()); assertEquals(100.8, geo.coordinates().get(1).get(0).longitude(), DELTA); assertEquals(0.8, geo.coordinates().get(1).get(0).latitude(), DELTA); assertFalse(geo.coordinates().get(0).get(0).hasAltitude()); }
@Test public void passingInSingleLineString_doesHandleCorrectly() throws Exception { List<Point> points = new ArrayList<>(); points.add(Point.fromLngLat(1.0, 2.0)); points.add(Point.fromLngLat(3.0, 4.0)); LineString geometry = LineString.fromLngLats(points); MultiLineString multiLineString = MultiLineString.fromLineString(geometry); assertNotNull(multiLineString); assertEquals(1, multiLineString.lineStrings().size()); assertEquals(2.0, multiLineString.lineStrings().get(0).coordinates().get(0).latitude(), DELTA); }
@Test public void point_deserializeArray() throws Exception { String jsonString = "[100.0, 0.0, 200.0]"; GsonBuilder gsonBuilder = new GsonBuilder() .registerTypeAdapter(Point.class, new PointDeserializer()); Point point = gsonBuilder.create().fromJson(jsonString, Point.class); assertEquals(100, point.longitude(), DELTA); assertEquals(0, point.latitude(), DELTA); assertEquals(200, point.altitude(), DELTA); }
@Test public void point_hasAltitudeValueWithBoundingBox() throws Exception { Point point = Point.fromLngLat(100, 0, 200, BoundingBox.fromLngLats(-100, -10, 100, 100, 10, 200)); compareJson(loadJsonFixture(POINT_WITH_ALTITUDE_AND_BBOX_FIXTURE), point.toJson()); }