use of org.opensearch.geometry.Point 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.geometry.Point 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.geometry.Point in project OpenSearch by opensearch-project.
the class GeoPolygonDecomposer method edges.
private static Edge[] edges(Edge[] edges, int numHoles, List<List<Point[]>> components) {
ArrayList<Edge> mainEdges = new ArrayList<>(edges.length);
for (int i = 0; i < edges.length; i++) {
if (edges[i].component >= 0) {
double[] partitionPoint = new double[3];
int length = component(edges[i], -(components.size() + numHoles + 1), mainEdges, partitionPoint);
List<Point[]> component = new ArrayList<>();
component.add(coordinates(edges[i], new Point[length + 1], partitionPoint));
components.add(component);
}
}
return mainEdges.toArray(new Edge[mainEdges.size()]);
}
use of org.opensearch.geometry.Point in project OpenSearch by opensearch-project.
the class GeoPolygonDecomposer method assign.
private static void assign(Edge[] holes, Point[][] points, int numHoles, Edge[] edges, List<List<Point[]>> components) {
// is part of the polygon the hole belongs to.
for (int i = 0; i < numHoles; i++) {
// To do the assignment we assume (and later, elsewhere, check) that each hole is within
// a single component, and the components do not overlap. Based on this assumption, it's
// enough to find a component that contains some vertex of the hole, and
// holes[i].coordinate is such a vertex, so we use that one.
// First, we sort all the edges according to their order of intersection with the line
// of longitude through holes[i].coordinate, in order from south to north. Edges that do
// not intersect this line are sorted to the end of the array and of no further interest
// here.
final Edge current = new Edge(holes[i].coordinate, holes[i].next);
current.intersect = current.coordinate;
final int intersections = intersections(current.coordinate.getX(), edges);
if (intersections == 0) {
// holes[i].coordinate, so there's no way this hole is within the polygon.
throw new InvalidShapeException("Invalid shape: Hole is not within polygon");
}
// Next we do a binary search to find the position of holes[i].coordinate in the array.
// The binary search returns the index of an exact match, or (-insertionPoint - 1) if
// the vertex lies between the intersections of edges[insertionPoint] and
// edges[insertionPoint+1]. The latter case is vastly more common.
final int pos;
boolean sharedVertex = false;
if (((pos = Arrays.binarySearch(edges, 0, intersections, current, INTERSECTION_ORDER)) >= 0) && !(sharedVertex = (edges[pos].intersect.equals(current.coordinate)))) {
// TODO Can this actually happen? Needs a test to exercise it, or else needs to be removed.
throw new InvalidShapeException("Invalid shape: Hole is not within polygon");
}
final int index;
if (sharedVertex) {
// holes[i].coordinate lies exactly on an edge.
// TODO Should this be pos instead of 0? This assigns exact matches to the southernmost component.
index = 0;
} else if (pos == -1) {
// holes[i].coordinate is strictly south of all intersections. Assign it to the
// southernmost component, and allow later validation to spot that it is not
// entirely within the chosen component.
index = 0;
} else {
// holes[i].coordinate is strictly north of at least one intersection. Assign it to
// the component immediately to its south.
index = -(pos + 2);
}
final int component = -edges[index].component - numHoles - 1;
components.get(component).add(points[i]);
}
}
use of org.opensearch.geometry.Point in project OpenSearch by opensearch-project.
the class ExternalMapper method parse.
@Override
public void parse(ParseContext context) throws IOException {
byte[] bytes = "Hello world".getBytes(Charset.defaultCharset());
binMapper.parse(context.createExternalValueContext(bytes));
boolMapper.parse(context.createExternalValueContext(true));
// Let's add a Dummy Point
double lat = 42.0;
double lng = 51.0;
ArrayList<GeoPoint> points = new ArrayList<>();
points.add(new GeoPoint(lat, lng));
pointMapper.parse(context.createExternalValueContext(points));
// Let's add a Dummy Shape
if (shapeMapper instanceof GeoShapeFieldMapper) {
shapeMapper.parse(context.createExternalValueContext(new Point(-100, 45)));
} else {
PointBuilder pb = new PointBuilder(-100, 45);
shapeMapper.parse(context.createExternalValueContext(pb.buildS4J()));
}
context = context.createExternalValueContext(generatedValue);
// Let's add a Original String
stringMapper.parse(context);
multiFields.parse(this, context);
}
Aggregations