use of org.opensearch.common.geo.builders.ShapeBuilder in project OpenSearch by opensearch-project.
the class GeoWKTParser method parseExpectedType.
/**
* throws an exception if the parsed geometry type does not match the expected shape type
*/
public static ShapeBuilder parseExpectedType(XContentParser parser, final GeoShapeType shapeType, final AbstractShapeGeometryFieldMapper shapeMapper) throws IOException, OpenSearchParseException {
try (StringReader reader = new StringReader(parser.text())) {
Explicit<Boolean> ignoreZValue = (shapeMapper == null) ? AbstractShapeGeometryFieldMapper.Defaults.IGNORE_Z_VALUE : shapeMapper.ignoreZValue();
Explicit<Boolean> coerce = (shapeMapper == null) ? AbstractShapeGeometryFieldMapper.Defaults.COERCE : shapeMapper.coerce();
// setup the tokenizer; configured to read words w/o numbers
StreamTokenizer tokenizer = new StreamTokenizer(reader);
tokenizer.resetSyntax();
tokenizer.wordChars('a', 'z');
tokenizer.wordChars('A', 'Z');
tokenizer.wordChars(128 + 32, 255);
tokenizer.wordChars('0', '9');
tokenizer.wordChars('-', '-');
tokenizer.wordChars('+', '+');
tokenizer.wordChars('.', '.');
tokenizer.whitespaceChars(0, ' ');
tokenizer.commentChar('#');
ShapeBuilder builder = parseGeometry(tokenizer, shapeType, ignoreZValue.value(), coerce.value());
checkEOF(tokenizer);
return builder;
}
}
use of org.opensearch.common.geo.builders.ShapeBuilder in project OpenSearch by opensearch-project.
the class GeoWKTShapeParserTests method testParsePolyWithStoredZ.
public void testParsePolyWithStoredZ() throws IOException {
List<Coordinate> shellCoordinates = new ArrayList<>();
shellCoordinates.add(new Coordinate(100, 0, 0));
shellCoordinates.add(new Coordinate(101, 0, 0));
shellCoordinates.add(new Coordinate(101, 1, 0));
shellCoordinates.add(new Coordinate(100, 1, 5));
shellCoordinates.add(new Coordinate(100, 0, 5));
PolygonBuilder builder = new PolygonBuilder(new CoordinatesBuilder().coordinates(shellCoordinates));
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().value(builder.toWKT());
XContentParser parser = createParser(xContentBuilder);
parser.nextToken();
Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build();
Mapper.BuilderContext mockBuilderContext = new Mapper.BuilderContext(indexSettings, new ContentPath());
final LegacyGeoShapeFieldMapper mapperBuilder = (LegacyGeoShapeFieldMapper) (new LegacyGeoShapeFieldMapper.Builder("test").ignoreZValue(true).build(mockBuilderContext));
ShapeBuilder<?, ?, ?> shapeBuilder = ShapeParser.parse(parser, mapperBuilder);
assertEquals(shapeBuilder.numDimensions(), 3);
}
use of org.opensearch.common.geo.builders.ShapeBuilder in project OpenSearch by opensearch-project.
the class GeoWKTShapeParserTests method testParseOpenPolygon.
public void testParseOpenPolygon() throws IOException {
String openPolygon = "POLYGON ((100 5, 100 10, 90 10, 90 5))";
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().value(openPolygon);
XContentParser parser = createParser(xContentBuilder);
parser.nextToken();
Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build();
Mapper.BuilderContext mockBuilderContext = new Mapper.BuilderContext(indexSettings, new ContentPath());
final LegacyGeoShapeFieldMapper defaultMapperBuilder = (LegacyGeoShapeFieldMapper) (new LegacyGeoShapeFieldMapper.Builder("test").coerce(false).build(mockBuilderContext));
OpenSearchParseException exception = expectThrows(OpenSearchParseException.class, () -> ShapeParser.parse(parser, defaultMapperBuilder));
assertEquals("invalid LinearRing found (coordinates are not closed)", exception.getMessage());
final LegacyGeoShapeFieldMapper coercingMapperBuilder = (LegacyGeoShapeFieldMapper) (new LegacyGeoShapeFieldMapper.Builder("test").coerce(true).build(mockBuilderContext));
ShapeBuilder<?, ?, ?> shapeBuilder = ShapeParser.parse(parser, coercingMapperBuilder);
assertNotNull(shapeBuilder);
assertEquals("polygon ((100.0 5.0, 100.0 10.0, 90.0 10.0, 90.0 5.0, 100.0 5.0))", shapeBuilder.toWKT());
}
use of org.opensearch.common.geo.builders.ShapeBuilder in project OpenSearch by opensearch-project.
the class GeoShapeQueryTests method testIndexRectangleSpanningDateLine.
public void testIndexRectangleSpanningDateLine() throws Exception {
String mapping = Strings.toString(createRandomMapping());
client().admin().indices().prepareCreate("test").addMapping("type1", mapping, XContentType.JSON).get();
ensureGreen();
EnvelopeBuilder envelopeBuilder = new EnvelopeBuilder(new Coordinate(178, 10), new Coordinate(-178, -10));
XContentBuilder docSource = envelopeBuilder.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
ShapeBuilder filterShape = new PointBuilder(179, 0);
GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", filterShape);
geoShapeQueryBuilder.relation(ShapeRelation.INTERSECTS);
SearchResponse result = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
assertSearchResponse(result);
assertHitCount(result, 1);
}
use of org.opensearch.common.geo.builders.ShapeBuilder in project OpenSearch by opensearch-project.
the class GeoShapeQueryTests method testPointsOnlyExplicit.
public void testPointsOnlyExplicit() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("geo").field("type", "geo_shape").field("tree", randomBoolean() ? "quadtree" : "geohash").field("tree_levels", "6").field("distance_error_pct", "0.01").field("points_only", true).endObject().endObject().endObject().endObject());
client().admin().indices().prepareCreate("geo_points_only").addMapping("type1", mapping, XContentType.JSON).get();
ensureGreen();
// MULTIPOINT
ShapeBuilder shape = RandomShapeGenerator.createShape(random(), RandomShapeGenerator.ShapeType.MULTIPOINT);
client().prepareIndex("geo_points_only").setId("1").setSource(jsonBuilder().startObject().field("geo", shape).endObject()).setRefreshPolicy(IMMEDIATE).get();
// POINT
shape = RandomShapeGenerator.createShape(random(), RandomShapeGenerator.ShapeType.POINT);
client().prepareIndex("geo_points_only").setId("2").setSource(jsonBuilder().startObject().field("geo", shape).endObject()).setRefreshPolicy(IMMEDIATE).get();
// test that point was inserted
SearchResponse response = client().prepareSearch("geo_points_only").setQuery(matchAllQuery()).get();
assertHitCount(response, 2);
}
Aggregations