Search in sources :

Example 11 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class GeoUtils method parseGeoPoint.

/**
 * Parse a {@link GeoPoint} with a {@link XContentParser}. A geopoint has one of the following forms:
 *
 * <ul>
 *     <li>Object: <pre>{&quot;lat&quot;: <i>&lt;latitude&gt;</i>, &quot;lon&quot;: <i>&lt;longitude&gt;</i>}</pre></li>
 *     <li>String: <pre>&quot;<i>&lt;latitude&gt;</i>,<i>&lt;longitude&gt;</i>&quot;</pre></li>
 *     <li>Geohash: <pre>&quot;<i>&lt;geohash&gt;</i>&quot;</pre></li>
 *     <li>Array: <pre>[<i>&lt;longitude&gt;</i>,<i>&lt;latitude&gt;</i>]</pre></li>
 * </ul>
 *
 * @param parser {@link XContentParser} to parse the value from
 * @param point A {@link GeoPoint} that will be reset by the values parsed
 * @return new {@link GeoPoint} parsed from the parse
 */
public static GeoPoint parseGeoPoint(XContentParser parser, GeoPoint point, final boolean ignoreZValue, EffectivePoint effectivePoint) throws IOException, OpenSearchParseException {
    double lat = Double.NaN;
    double lon = Double.NaN;
    String geohash = null;
    NumberFormatException numberFormatException = null;
    if (parser.currentToken() == Token.START_OBJECT) {
        try (XContentSubParser subParser = new XContentSubParser(parser)) {
            while (subParser.nextToken() != Token.END_OBJECT) {
                if (subParser.currentToken() == Token.FIELD_NAME) {
                    String field = subParser.currentName();
                    if (LATITUDE.equals(field)) {
                        subParser.nextToken();
                        switch(subParser.currentToken()) {
                            case VALUE_NUMBER:
                            case VALUE_STRING:
                                try {
                                    lat = subParser.doubleValue(true);
                                } catch (NumberFormatException e) {
                                    numberFormatException = e;
                                }
                                break;
                            default:
                                throw new OpenSearchParseException("latitude must be a number");
                        }
                    } else if (LONGITUDE.equals(field)) {
                        subParser.nextToken();
                        switch(subParser.currentToken()) {
                            case VALUE_NUMBER:
                            case VALUE_STRING:
                                try {
                                    lon = subParser.doubleValue(true);
                                } catch (NumberFormatException e) {
                                    numberFormatException = e;
                                }
                                break;
                            default:
                                throw new OpenSearchParseException("longitude must be a number");
                        }
                    } else if (GEOHASH.equals(field)) {
                        if (subParser.nextToken() == Token.VALUE_STRING) {
                            geohash = subParser.text();
                        } else {
                            throw new OpenSearchParseException("geohash must be a string");
                        }
                    } else {
                        throw new OpenSearchParseException("field must be either [{}], [{}] or [{}]", LATITUDE, LONGITUDE, GEOHASH);
                    }
                } else {
                    throw new OpenSearchParseException("token [{}] not allowed", subParser.currentToken());
                }
            }
        }
        if (geohash != null) {
            if (!Double.isNaN(lat) || !Double.isNaN(lon)) {
                throw new OpenSearchParseException("field must be either lat/lon or geohash");
            } else {
                return point.parseGeoHash(geohash, effectivePoint);
            }
        } else if (numberFormatException != null) {
            throw new OpenSearchParseException("[{}] and [{}] must be valid double values", numberFormatException, LATITUDE, LONGITUDE);
        } else if (Double.isNaN(lat)) {
            throw new OpenSearchParseException("field [{}] missing", LATITUDE);
        } else if (Double.isNaN(lon)) {
            throw new OpenSearchParseException("field [{}] missing", LONGITUDE);
        } else {
            return point.reset(lat, lon);
        }
    } else if (parser.currentToken() == Token.START_ARRAY) {
        try (XContentSubParser subParser = new XContentSubParser(parser)) {
            int element = 0;
            while (subParser.nextToken() != Token.END_ARRAY) {
                if (subParser.currentToken() == Token.VALUE_NUMBER) {
                    element++;
                    if (element == 1) {
                        lon = subParser.doubleValue();
                    } else if (element == 2) {
                        lat = subParser.doubleValue();
                    } else if (element == 3) {
                        GeoPoint.assertZValue(ignoreZValue, subParser.doubleValue());
                    } else {
                        throw new OpenSearchParseException("[geo_point] field type does not accept > 3 dimensions");
                    }
                } else {
                    throw new OpenSearchParseException("numeric value expected");
                }
            }
        }
        return point.reset(lat, lon);
    } else if (parser.currentToken() == Token.VALUE_STRING) {
        String val = parser.text();
        return point.resetFromString(val, ignoreZValue, effectivePoint);
    } else {
        throw new OpenSearchParseException("geo_point expected");
    }
}
Also used : XContentSubParser(org.opensearch.common.xcontent.XContentSubParser) OpenSearchParseException(org.opensearch.OpenSearchParseException)

Example 12 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class GeoJson method createGeometry.

private static Geometry createGeometry(String type, List<Geometry> geometries, CoordinateNode coordinates, Boolean orientation, boolean defaultOrientation, boolean coerce, DistanceUnit.Distance radius) {
    ShapeType shapeType;
    if ("bbox".equalsIgnoreCase(type)) {
        shapeType = ShapeType.ENVELOPE;
    } else {
        shapeType = ShapeType.forName(type);
    }
    if (shapeType == ShapeType.GEOMETRYCOLLECTION) {
        if (geometries == null) {
            throw new OpenSearchParseException("geometries not included");
        }
        if (coordinates != null) {
            throw new OpenSearchParseException("parameter coordinates is not supported for type " + type);
        }
        verifyNulls(type, null, orientation, radius);
        return new GeometryCollection<>(geometries);
    }
    // We expect to have coordinates for all the rest
    if (coordinates == null) {
        throw new OpenSearchParseException("coordinates not included");
    }
    switch(shapeType) {
        case CIRCLE:
            if (radius == null) {
                throw new OpenSearchParseException("radius is not specified");
            }
            verifyNulls(type, geometries, orientation, null);
            Point point = coordinates.asPoint();
            return new Circle(point.getX(), point.getY(), point.getZ(), radius.convert(DistanceUnit.METERS).value);
        case POINT:
            verifyNulls(type, geometries, orientation, radius);
            return coordinates.asPoint();
        case MULTIPOINT:
            verifyNulls(type, geometries, orientation, radius);
            return coordinates.asMultiPoint();
        case LINESTRING:
            verifyNulls(type, geometries, orientation, radius);
            return coordinates.asLineString(coerce);
        case MULTILINESTRING:
            verifyNulls(type, geometries, orientation, radius);
            return coordinates.asMultiLineString(coerce);
        case POLYGON:
            verifyNulls(type, geometries, null, radius);
            // handle possible null in orientation
            return coordinates.asPolygon(orientation != null ? orientation : defaultOrientation, coerce);
        case MULTIPOLYGON:
            verifyNulls(type, geometries, null, radius);
            // handle possible null in orientation
            return coordinates.asMultiPolygon(orientation != null ? orientation : defaultOrientation, coerce);
        case ENVELOPE:
            verifyNulls(type, geometries, orientation, radius);
            return coordinates.asRectangle();
        default:
            throw new OpenSearchParseException("unsupported shape type " + type);
    }
}
Also used : GeometryCollection(org.opensearch.geometry.GeometryCollection) Circle(org.opensearch.geometry.Circle) OpenSearchParseException(org.opensearch.OpenSearchParseException) ShapeType(org.opensearch.geometry.ShapeType) MultiPoint(org.opensearch.geometry.MultiPoint) Point(org.opensearch.geometry.Point)

Example 13 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class GeoPoint method resetFromCoordinates.

public GeoPoint resetFromCoordinates(String value, final boolean ignoreZValue) {
    String[] vals = value.split(",");
    if (vals.length > 3) {
        throw new OpenSearchParseException("failed to parse [{}], expected 2 or 3 coordinates " + "but found: [{}]", vals.length);
    }
    final double lat;
    final double lon;
    try {
        lat = Double.parseDouble(vals[0].trim());
    } catch (NumberFormatException ex) {
        throw new OpenSearchParseException("latitude must be a number");
    }
    try {
        lon = Double.parseDouble(vals[1].trim());
    } catch (NumberFormatException ex) {
        throw new OpenSearchParseException("longitude must be a number");
    }
    if (vals.length > 2) {
        GeoPoint.assertZValue(ignoreZValue, Double.parseDouble(vals[2].trim()));
    }
    return reset(lat, lon);
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException)

Example 14 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class GeoPoint method resetFromWKT.

private GeoPoint resetFromWKT(String value, boolean ignoreZValue) {
    Geometry geometry;
    try {
        geometry = new WellKnownText(false, new GeographyValidator(ignoreZValue)).fromWKT(value);
    } catch (Exception e) {
        throw new OpenSearchParseException("Invalid WKT format", e);
    }
    if (geometry.type() != ShapeType.POINT) {
        throw new OpenSearchParseException("[geo_point] supports only POINT among WKT primitives, " + "but found " + geometry.type());
    }
    Point point = (Point) geometry;
    return reset(point.getY(), point.getX());
}
Also used : Geometry(org.opensearch.geometry.Geometry) OpenSearchParseException(org.opensearch.OpenSearchParseException) GeographyValidator(org.opensearch.geometry.utils.GeographyValidator) LatLonPoint(org.apache.lucene.document.LatLonPoint) EffectivePoint(org.opensearch.common.geo.GeoUtils.EffectivePoint) Point(org.opensearch.geometry.Point) WellKnownText(org.opensearch.geometry.utils.WellKnownText) OpenSearchParseException(org.opensearch.OpenSearchParseException) IOException(java.io.IOException)

Example 15 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class GeoWKTParser method parseCoordinateList.

private static List<Coordinate> parseCoordinateList(StreamTokenizer stream, final boolean ignoreZValue, final boolean coerce) throws IOException, OpenSearchParseException {
    CoordinatesBuilder coordinates = new CoordinatesBuilder();
    boolean isOpenParen = false;
    if (isNumberNext(stream) || (isOpenParen = nextWord(stream).equals(LPAREN))) {
        coordinates.coordinate(parseCoordinate(stream, ignoreZValue, coerce));
    }
    if (isOpenParen && nextCloser(stream).equals(RPAREN) == false) {
        throw new OpenSearchParseException("expected: [{}]" + RPAREN + " but found: [{}]" + tokenString(stream), stream.lineno());
    }
    while (nextCloserOrComma(stream).equals(COMMA)) {
        isOpenParen = false;
        if (isNumberNext(stream) || (isOpenParen = nextWord(stream).equals(LPAREN))) {
            coordinates.coordinate(parseCoordinate(stream, ignoreZValue, coerce));
        }
        if (isOpenParen && nextCloser(stream).equals(RPAREN) == false) {
            throw new OpenSearchParseException("expected: " + RPAREN + " but found: " + tokenString(stream), stream.lineno());
        }
    }
    return coordinates.build();
}
Also used : CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) OpenSearchParseException(org.opensearch.OpenSearchParseException)

Aggregations

OpenSearchParseException (org.opensearch.OpenSearchParseException)105 XContentParser (org.opensearch.common.xcontent.XContentParser)34 HashMap (java.util.HashMap)27 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)15 ArrayList (java.util.ArrayList)14 Matchers.containsString (org.hamcrest.Matchers.containsString)12 Map (java.util.Map)11 IOException (java.io.IOException)10 List (java.util.List)7 ParsingException (org.opensearch.common.ParsingException)5 GeoPoint (org.opensearch.common.geo.GeoPoint)5 Token (org.opensearch.common.xcontent.XContentParser.Token)5 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)5 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)4 UncheckedIOException (java.io.UncheckedIOException)3 DateTimeParseException (java.time.format.DateTimeParseException)3 HashSet (java.util.HashSet)3 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)3 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)3 PointBuilder (org.opensearch.common.geo.builders.PointBuilder)3