Search in sources :

Example 1 with GeoShapeType

use of org.opensearch.common.geo.GeoShapeType in project OpenSearch by opensearch-project.

the class GeoJsonParser method parse.

protected static ShapeBuilder parse(XContentParser parser, AbstractShapeGeometryFieldMapper shapeMapper) throws IOException {
    GeoShapeType shapeType = null;
    DistanceUnit.Distance radius = null;
    CoordinateNode coordinateNode = null;
    GeometryCollectionBuilder geometryCollections = null;
    Orientation orientation = (shapeMapper == null) ? AbstractShapeGeometryFieldMapper.Defaults.ORIENTATION.value() : shapeMapper.orientation();
    Explicit<Boolean> coerce = (shapeMapper == null) ? AbstractShapeGeometryFieldMapper.Defaults.COERCE : shapeMapper.coerce();
    Explicit<Boolean> ignoreZValue = (shapeMapper == null) ? AbstractShapeGeometryFieldMapper.Defaults.IGNORE_Z_VALUE : shapeMapper.ignoreZValue();
    String malformedException = null;
    XContentParser.Token token;
    try (XContentParser subParser = new XContentSubParser(parser)) {
        while ((token = subParser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                String fieldName = subParser.currentName();
                if (ShapeParser.FIELD_TYPE.match(fieldName, subParser.getDeprecationHandler())) {
                    subParser.nextToken();
                    final GeoShapeType type = GeoShapeType.forName(subParser.text());
                    if (shapeType != null && shapeType.equals(type) == false) {
                        malformedException = ShapeParser.FIELD_TYPE + " already parsed as [" + shapeType + "] cannot redefine as [" + type + "]";
                    } else {
                        shapeType = type;
                    }
                } else if (ShapeParser.FIELD_COORDINATES.match(fieldName, subParser.getDeprecationHandler())) {
                    subParser.nextToken();
                    CoordinateNode tempNode = parseCoordinates(subParser, ignoreZValue.value());
                    if (coordinateNode != null && tempNode.numDimensions() != coordinateNode.numDimensions()) {
                        throw new OpenSearchParseException("Exception parsing coordinates: " + "number of dimensions do not match");
                    }
                    coordinateNode = tempNode;
                } else if (ShapeParser.FIELD_GEOMETRIES.match(fieldName, subParser.getDeprecationHandler())) {
                    if (shapeType == null) {
                        shapeType = GeoShapeType.GEOMETRYCOLLECTION;
                    } else if (shapeType.equals(GeoShapeType.GEOMETRYCOLLECTION) == false) {
                        malformedException = "cannot have [" + ShapeParser.FIELD_GEOMETRIES + "] with type set to [" + shapeType + "]";
                    }
                    subParser.nextToken();
                    geometryCollections = parseGeometries(subParser, shapeMapper);
                } else if (CircleBuilder.FIELD_RADIUS.match(fieldName, subParser.getDeprecationHandler())) {
                    if (shapeType == null) {
                        shapeType = GeoShapeType.CIRCLE;
                    } else if (shapeType != null && shapeType.equals(GeoShapeType.CIRCLE) == false) {
                        malformedException = "cannot have [" + CircleBuilder.FIELD_RADIUS + "] with type set to [" + shapeType + "]";
                    }
                    subParser.nextToken();
                    radius = DistanceUnit.Distance.parseDistance(subParser.text());
                } else if (ShapeParser.FIELD_ORIENTATION.match(fieldName, subParser.getDeprecationHandler())) {
                    if (shapeType != null && (shapeType.equals(GeoShapeType.POLYGON) || shapeType.equals(GeoShapeType.MULTIPOLYGON)) == false) {
                        malformedException = "cannot have [" + ShapeParser.FIELD_ORIENTATION + "] with type set to [" + shapeType + "]";
                    }
                    subParser.nextToken();
                    orientation = ShapeBuilder.Orientation.fromString(subParser.text());
                } else {
                    subParser.nextToken();
                    subParser.skipChildren();
                }
            }
        }
    }
    if (malformedException != null) {
        throw new OpenSearchParseException(malformedException);
    } else if (shapeType == null) {
        throw new OpenSearchParseException("shape type not included");
    } else if (coordinateNode == null && GeoShapeType.GEOMETRYCOLLECTION != shapeType) {
        throw new OpenSearchParseException("coordinates not included");
    } else if (geometryCollections == null && GeoShapeType.GEOMETRYCOLLECTION == shapeType) {
        throw new OpenSearchParseException("geometries not included");
    } else if (radius != null && GeoShapeType.CIRCLE != shapeType) {
        throw new OpenSearchParseException("field [{}] is supported for [{}] only", CircleBuilder.FIELD_RADIUS, CircleBuilder.TYPE);
    }
    if (shapeType.equals(GeoShapeType.GEOMETRYCOLLECTION)) {
        return geometryCollections;
    }
    return shapeType.getBuilder(coordinateNode, radius, orientation, coerce.value());
}
Also used : Orientation(org.opensearch.common.geo.builders.ShapeBuilder.Orientation) GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) XContentSubParser(org.opensearch.common.xcontent.XContentSubParser) OpenSearchParseException(org.opensearch.OpenSearchParseException) DistanceUnit(org.opensearch.common.unit.DistanceUnit) GeoShapeType(org.opensearch.common.geo.GeoShapeType) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

OpenSearchParseException (org.opensearch.OpenSearchParseException)1 GeoShapeType (org.opensearch.common.geo.GeoShapeType)1 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)1 Orientation (org.opensearch.common.geo.builders.ShapeBuilder.Orientation)1 DistanceUnit (org.opensearch.common.unit.DistanceUnit)1 XContentParser (org.opensearch.common.xcontent.XContentParser)1 XContentSubParser (org.opensearch.common.xcontent.XContentSubParser)1