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>{"lat": <i><latitude></i>, "lon": <i><longitude></i>}</pre></li>
* <li>String: <pre>"<i><latitude></i>,<i><longitude></i>"</pre></li>
* <li>Geohash: <pre>"<i><geohash></i>"</pre></li>
* <li>Array: <pre>[<i><longitude></i>,<i><latitude></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");
}
}
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);
}
}
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);
}
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());
}
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();
}
Aggregations