use of org.opensearch.geometry.LinearRing 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.LinearRing 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.LinearRing in project OpenSearch by opensearch-project.
the class ShapeBuilderTests method testNewPolygon_coordinates.
public void testNewPolygon_coordinates() {
PolygonBuilder pb = new PolygonBuilder(new CoordinatesBuilder().coordinates(new Coordinate(-45, 30), new Coordinate(45, 30), new Coordinate(45, -30), new Coordinate(-45, -30), new Coordinate(-45, 30)));
Polygon poly = pb.toPolygonS4J();
LineString exterior = poly.getExteriorRing();
assertEquals(exterior.getCoordinateN(0), new Coordinate(-45, 30));
assertEquals(exterior.getCoordinateN(1), new Coordinate(45, 30));
assertEquals(exterior.getCoordinateN(2), new Coordinate(45, -30));
assertEquals(exterior.getCoordinateN(3), new Coordinate(-45, -30));
LinearRing polygon = pb.toPolygonGeometry().getPolygon();
assertEquals(polygon.getY(0), 30, 0d);
assertEquals(polygon.getX(0), -45, 0d);
assertEquals(polygon.getY(1), 30, 0d);
assertEquals(polygon.getX(1), 45, 0d);
assertEquals(polygon.getY(2), -30, 0d);
assertEquals(polygon.getX(2), 45, 0d);
assertEquals(polygon.getY(3), -30, 0d);
assertEquals(polygon.getX(3), -45, 0d);
}
use of org.opensearch.geometry.LinearRing in project OpenSearch by opensearch-project.
the class ShapeBuilderTests method testNewPolygon.
public void testNewPolygon() {
PolygonBuilder pb = new PolygonBuilder(new CoordinatesBuilder().coordinate(-45, 30).coordinate(45, 30).coordinate(45, -30).coordinate(-45, -30).coordinate(-45, 30));
Polygon poly = pb.toPolygonS4J();
LineString exterior = poly.getExteriorRing();
assertEquals(exterior.getCoordinateN(0), new Coordinate(-45, 30));
assertEquals(exterior.getCoordinateN(1), new Coordinate(45, 30));
assertEquals(exterior.getCoordinateN(2), new Coordinate(45, -30));
assertEquals(exterior.getCoordinateN(3), new Coordinate(-45, -30));
LinearRing polygon = pb.toPolygonGeometry().getPolygon();
assertEquals(polygon.getY(0), 30, 0d);
assertEquals(polygon.getX(0), -45, 0d);
assertEquals(polygon.getY(1), 30, 0d);
assertEquals(polygon.getX(1), 45, 0d);
assertEquals(polygon.getY(2), -30, 0d);
assertEquals(polygon.getX(2), 45, 0d);
assertEquals(polygon.getY(3), -30, 0d);
assertEquals(polygon.getX(3), -45, 0d);
}
use of org.opensearch.geometry.LinearRing 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));
}
Aggregations