Search in sources :

Example 21 with Point

use of org.opensearch.geometry.Point in project OpenSearch by opensearch-project.

the class WellKnownText method parsePoint.

private Point parsePoint(StreamTokenizer stream) throws IOException, ParseException {
    if (nextEmptyOrOpen(stream).equals(EMPTY)) {
        return Point.EMPTY;
    }
    double lon = nextNumber(stream);
    double lat = nextNumber(stream);
    Point pt;
    if (isNumberNext(stream)) {
        pt = new Point(lon, lat, nextNumber(stream));
    } else {
        pt = new Point(lon, lat);
    }
    nextCloser(stream);
    return pt;
}
Also used : Point(org.opensearch.geometry.Point) MultiPoint(org.opensearch.geometry.MultiPoint)

Example 22 with Point

use of org.opensearch.geometry.Point in project OpenSearch by opensearch-project.

the class WellKnownText method parseMultiPoint.

private MultiPoint parseMultiPoint(StreamTokenizer stream) throws IOException, ParseException {
    String token = nextEmptyOrOpen(stream);
    if (token.equals(EMPTY)) {
        return MultiPoint.EMPTY;
    }
    ArrayList<Double> lats = new ArrayList<>();
    ArrayList<Double> lons = new ArrayList<>();
    ArrayList<Double> alts = new ArrayList<>();
    ArrayList<Point> points = new ArrayList<>();
    parseCoordinates(stream, lats, lons, alts);
    for (int i = 0; i < lats.size(); i++) {
        if (alts.isEmpty()) {
            points.add(new Point(lons.get(i), lats.get(i)));
        } else {
            points.add(new Point(lons.get(i), lats.get(i), alts.get(i)));
        }
    }
    return new MultiPoint(Collections.unmodifiableList(points));
}
Also used : MultiPoint(org.opensearch.geometry.MultiPoint) ArrayList(java.util.ArrayList) Point(org.opensearch.geometry.Point) MultiPoint(org.opensearch.geometry.MultiPoint) Point(org.opensearch.geometry.Point) MultiPoint(org.opensearch.geometry.MultiPoint)

Example 23 with Point

use of org.opensearch.geometry.Point in project OpenSearch by opensearch-project.

the class Geohash method toBoundingBox.

/**
 * Computes the bounding box coordinates from a given geohash
 *
 * @param geohash Geohash of the defined cell
 * @return GeoRect rectangle defining the bounding box
 */
public static Rectangle toBoundingBox(final String geohash) {
    // bottom left is the coordinate
    Point bottomLeft = toPoint(geohash);
    int len = Math.min(12, geohash.length());
    long ghLong = longEncode(geohash, len);
    // shift away the level
    ghLong >>>= 4;
    // deinterleave
    long lon = BitUtil.deinterleave(ghLong >>> 1);
    long lat = BitUtil.deinterleave(ghLong);
    final int shift = (12 - len) * 5 + 2;
    if (lat < MAX_LAT_BITS) {
        // add 1 to lat and lon to get topRight
        ghLong = BitUtil.interleave((int) (lat + 1), (int) (lon + 1)) << 4 | len;
        final long mortonHash = BitUtil.flipFlop((ghLong >>> 4) << shift);
        Point topRight = new Point(decodeLongitude(mortonHash), decodeLatitude(mortonHash));
        return new Rectangle(bottomLeft.getX(), topRight.getX(), topRight.getY(), bottomLeft.getY());
    } else {
        // We cannot go north of north pole, so just using 90 degrees instead of calculating it using
        // add 1 to lon to get lon of topRight, we are going to use 90 for lat
        ghLong = BitUtil.interleave((int) lat, (int) (lon + 1)) << 4 | len;
        final long mortonHash = BitUtil.flipFlop((ghLong >>> 4) << shift);
        Point topRight = new Point(decodeLongitude(mortonHash), decodeLatitude(mortonHash));
        return new Rectangle(bottomLeft.getX(), topRight.getX(), 90D, bottomLeft.getY());
    }
}
Also used : Rectangle(org.opensearch.geometry.Rectangle) Point(org.opensearch.geometry.Point) Point(org.opensearch.geometry.Point)

Example 24 with Point

use of org.opensearch.geometry.Point in project OpenSearch by opensearch-project.

the class GeoJsonParserTests method testParsePoint.

@Override
public void testParsePoint() throws IOException {
    XContentBuilder pointGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Point").startArray("coordinates").value(100.0).value(0.0).endArray().endObject();
    Point expected = new Point(100.0, 0.0);
    assertGeometryEquals(expected, pointGeoJson);
}
Also used : Point(org.opensearch.geometry.Point) MultiPoint(org.opensearch.geometry.MultiPoint) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 25 with Point

use of org.opensearch.geometry.Point in project OpenSearch by opensearch-project.

the class GeoJsonParserTests method testThatParserExtractsCorrectTypeAndCoordinatesFromArbitraryJson.

public void testThatParserExtractsCorrectTypeAndCoordinatesFromArbitraryJson() throws IOException, ParseException {
    XContentBuilder pointGeoJson = XContentFactory.jsonBuilder().startObject().startObject("crs").field("type", "name").startObject("properties").field("name", "urn:ogc:def:crs:OGC:1.3:CRS84").endObject().endObject().field("bbox", "foobar").field("type", "point").field("bubu", "foobar").startArray("coordinates").value(100.0).value(0.0).endArray().startObject("nested").startArray("coordinates").value(200.0).value(0.0).endArray().endObject().startObject("lala").field("type", "NotAPoint").endObject().endObject();
    Point expectedPt = new Point(100, 0);
    assertGeometryEquals(expectedPt, pointGeoJson, false);
}
Also used : Point(org.opensearch.geometry.Point) MultiPoint(org.opensearch.geometry.MultiPoint) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

Point (org.opensearch.geometry.Point)34 MultiPoint (org.opensearch.geometry.MultiPoint)14 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)9 ArrayList (java.util.ArrayList)6 Geometry (org.opensearch.geometry.Geometry)5 IOException (java.io.IOException)4 InvalidShapeException (org.locationtech.spatial4j.exception.InvalidShapeException)4 XContentParser (org.opensearch.common.xcontent.XContentParser)4 Line (org.opensearch.geometry.Line)4 LinearRing (org.opensearch.geometry.LinearRing)4 Polygon (org.opensearch.geometry.Polygon)4 OpenSearchParseException (org.opensearch.OpenSearchParseException)3 GeometryCollection (org.opensearch.geometry.GeometryCollection)3 PointBuilder (org.opensearch.common.geo.builders.PointBuilder)2 MultiLine (org.opensearch.geometry.MultiLine)2 ParseException (java.text.ParseException)1 Collection (java.util.Collection)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1