use of org.opensearch.geometry.Polygon in project OpenSearch by opensearch-project.
the class WellKnownText method parsePolygon.
private Polygon parsePolygon(StreamTokenizer stream) throws IOException, ParseException {
if (nextEmptyOrOpen(stream).equals(EMPTY)) {
return Polygon.EMPTY;
}
nextOpener(stream);
ArrayList<Double> lats = new ArrayList<>();
ArrayList<Double> lons = new ArrayList<>();
ArrayList<Double> alts = new ArrayList<>();
parseCoordinates(stream, lats, lons, alts);
ArrayList<LinearRing> holes = new ArrayList<>();
while (nextCloserOrComma(stream).equals(COMMA)) {
holes.add(parsePolygonHole(stream));
}
closeLinearRingIfCoerced(lats, lons, alts);
LinearRing shell;
if (alts.isEmpty()) {
shell = new LinearRing(toArray(lons), toArray(lats));
} else {
shell = new LinearRing(toArray(lons), toArray(lats), toArray(alts));
}
if (holes.isEmpty()) {
return new Polygon(shell);
} else {
return new Polygon(shell, Collections.unmodifiableList(holes));
}
}
use of org.opensearch.geometry.Polygon in project OpenSearch by opensearch-project.
the class GeometryIO method readPolygon.
private static Polygon readPolygon(StreamInput in) throws IOException {
double[][] shellComponents = readLineComponents(in);
boolean orientation = in.readBoolean();
LinearRing shell = buildLinearRing(shellComponents, orientation);
int numberOfHoles = in.readVInt();
if (numberOfHoles > 0) {
List<LinearRing> holes = new ArrayList<>(numberOfHoles);
for (int i = 0; i < numberOfHoles; i++) {
holes.add(buildLinearRing(readLineComponents(in), orientation));
}
return new Polygon(shell, holes);
} else {
return new Polygon(shell);
}
}
use of org.opensearch.geometry.Polygon in project OpenSearch by opensearch-project.
the class GeometryIO method readMultiPolygon.
private static MultiPolygon readMultiPolygon(StreamInput in) throws IOException {
// orientation for BWC
in.readBoolean();
int size = in.readVInt();
List<Polygon> polygons = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
polygons.add(readPolygon(in));
}
return new MultiPolygon(polygons);
}
use of org.opensearch.geometry.Polygon in project OpenSearch by opensearch-project.
the class GeometryParserTests method testBasics.
public void testBasics() {
GeometryParser parser = new GeometryParser(true, randomBoolean(), randomBoolean());
// point
Point expectedPoint = new Point(-122.084110, 37.386637);
testBasics(parser, mapOf("lat", 37.386637, "lon", -122.084110), expectedPoint);
testBasics(parser, "37.386637, -122.084110", expectedPoint);
testBasics(parser, "POINT (-122.084110 37.386637)", expectedPoint);
testBasics(parser, Arrays.asList(-122.084110, 37.386637), expectedPoint);
testBasics(parser, mapOf("type", "Point", "coordinates", Arrays.asList(-122.084110, 37.386637)), expectedPoint);
// line
Line expectedLine = new Line(new double[] { 0, 1 }, new double[] { 0, 1 });
testBasics(parser, "LINESTRING(0 0, 1 1)", expectedLine);
testBasics(parser, mapOf("type", "LineString", "coordinates", Arrays.asList(Arrays.asList(0, 0), Arrays.asList(1, 1))), expectedLine);
// polygon
Polygon expectedPolygon = new Polygon(new LinearRing(new double[] { 0, 1, 1, 0, 0 }, new double[] { 0, 0, 1, 1, 0 }));
testBasics(parser, "POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))", expectedPolygon);
testBasics(parser, mapOf("type", "Polygon", "coordinates", Arrays.asList(Arrays.asList(Arrays.asList(0, 0), Arrays.asList(1, 0), Arrays.asList(1, 1), Arrays.asList(0, 1), Arrays.asList(0, 0)))), expectedPolygon);
// geometry collection
testBasics(parser, Arrays.asList(Arrays.asList(-122.084110, 37.386637), "37.386637, -122.084110", "POINT (-122.084110 37.386637)", mapOf("type", "Point", "coordinates", Arrays.asList(-122.084110, 37.386637)), mapOf("type", "LineString", "coordinates", Arrays.asList(Arrays.asList(0, 0), Arrays.asList(1, 1))), "POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))"), new GeometryCollection<>(Arrays.asList(expectedPoint, expectedPoint, expectedPoint, expectedPoint, expectedLine, expectedPolygon)));
expectThrows(OpenSearchParseException.class, () -> testBasics(parser, "not a geometry", null));
}
use of org.opensearch.geometry.Polygon in project OpenSearch by opensearch-project.
the class GeometryIndexerTests method testPolygon.
public void testPolygon() {
Polygon polygon = new Polygon(new LinearRing(new double[] { 160, 200, 200, 160, 160 }, new double[] { 10, 10, 20, 20, 10 }));
Geometry indexed = new MultiPolygon(Arrays.asList(new Polygon(new LinearRing(new double[] { 180, 180, 160, 160, 180 }, new double[] { 10, 20, 20, 10, 10 })), new Polygon(new LinearRing(new double[] { -180, -180, -160, -160, -180 }, new double[] { 20, 10, 10, 20, 20 }))));
assertEquals(indexed, indexer.prepareForIndexing(polygon));
polygon = new Polygon(new LinearRing(new double[] { 160, 200, 200, 160, 160 }, new double[] { 10, 10, 20, 20, 10 }), Collections.singletonList(new LinearRing(new double[] { 165, 165, 195, 195, 165 }, new double[] { 12, 18, 18, 12, 12 })));
indexed = new MultiPolygon(Arrays.asList(new Polygon(new LinearRing(new double[] { 180, 180, 165, 165, 180, 180, 160, 160, 180 }, new double[] { 10, 12, 12, 18, 18, 20, 20, 10, 10 })), new Polygon(new LinearRing(new double[] { -180, -180, -160, -160, -180, -180, -165, -165, -180 }, new double[] { 12, 10, 10, 20, 20, 18, 18, 12, 12 }))));
assertEquals(indexed, indexer.prepareForIndexing(polygon));
}
Aggregations