Search in sources :

Example 1 with GeoShapeType

use of org.elasticsearch.common.geo.GeoShapeType in project crate by crate.

the class GeoJsonParser method parse.

protected static ShapeBuilder parse(XContentParser parser, GeoShapeFieldMapper shapeMapper) throws IOException {
    GeoShapeType shapeType = null;
    DistanceUnit.Distance radius = null;
    CoordinateNode coordinateNode = null;
    GeometryCollectionBuilder geometryCollections = null;
    ShapeBuilder.Orientation requestedOrientation = (shapeMapper == null) ? ShapeBuilder.Orientation.RIGHT : shapeMapper.fieldType().orientation();
    String malformedException = null;
    XContentParser.Token token;
    try {
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                String fieldName = parser.currentName();
                if (ShapeParser.FIELD_TYPE.match(fieldName, parser.getDeprecationHandler())) {
                    parser.nextToken();
                    final GeoShapeType type = GeoShapeType.forName(parser.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, parser.getDeprecationHandler())) {
                    parser.nextToken();
                    CoordinateNode tempNode = parseCoordinates(parser);
                    if (coordinateNode != null && tempNode.numDimensions() != coordinateNode.numDimensions()) {
                        throw new ElasticsearchParseException("Exception parsing coordinates: " + "number of dimensions do not match");
                    }
                    coordinateNode = tempNode;
                } else if (ShapeParser.FIELD_GEOMETRIES.match(fieldName, parser.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 + "]";
                    }
                    parser.nextToken();
                    geometryCollections = parseGeometries(parser, shapeMapper);
                } else if (CircleBuilder.FIELD_RADIUS.match(fieldName, parser.getDeprecationHandler())) {
                    if (shapeType == null) {
                        shapeType = GeoShapeType.CIRCLE;
                    } else if (shapeType.equals(GeoShapeType.CIRCLE) == false) {
                        malformedException = "cannot have [" + CircleBuilder.FIELD_RADIUS + "] with type set to [" + shapeType + "]";
                    }
                    parser.nextToken();
                    radius = DistanceUnit.Distance.parseDistance(parser.text());
                } else if (ShapeParser.FIELD_ORIENTATION.match(fieldName, parser.getDeprecationHandler())) {
                    if (shapeType != null && (shapeType.equals(GeoShapeType.POLYGON) || shapeType.equals(GeoShapeType.MULTIPOLYGON)) == false) {
                        malformedException = "cannot have [" + ShapeParser.FIELD_ORIENTATION + "] with type set to [" + shapeType + "]";
                    }
                    parser.nextToken();
                    requestedOrientation = ShapeBuilder.Orientation.fromString(parser.text());
                } else {
                    parser.nextToken();
                    parser.skipChildren();
                }
            }
        }
    } catch (Exception ex) {
        // Skip all other fields until the end of the object
        while (parser.currentToken() != XContentParser.Token.END_OBJECT && parser.currentToken() != null) {
            parser.nextToken();
            parser.skipChildren();
        }
        throw ex;
    }
    if (malformedException != null) {
        throw new ElasticsearchParseException(malformedException);
    } else if (shapeType == null) {
        throw new ElasticsearchParseException("shape type not included");
    } else if (coordinateNode == null && GeoShapeType.GEOMETRYCOLLECTION != shapeType) {
        throw new ElasticsearchParseException("coordinates not included");
    } else if (geometryCollections == null && GeoShapeType.GEOMETRYCOLLECTION == shapeType) {
        throw new ElasticsearchParseException("geometries not included");
    } else if (radius != null && GeoShapeType.CIRCLE != shapeType) {
        throw new ElasticsearchParseException("field [{}] is supported for [{}] only", CircleBuilder.FIELD_RADIUS, CircleBuilder.TYPE);
    }
    if (shapeType.equals(GeoShapeType.GEOMETRYCOLLECTION)) {
        return geometryCollections;
    }
    return shapeType.getBuilder(coordinateNode, radius, requestedOrientation);
}
Also used : GeometryCollectionBuilder(org.elasticsearch.common.geo.builders.GeometryCollectionBuilder) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) DistanceUnit(org.elasticsearch.common.unit.DistanceUnit) GeoShapeType(org.elasticsearch.common.geo.GeoShapeType) XContentParser(org.elasticsearch.common.xcontent.XContentParser) IOException(java.io.IOException) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException)

Aggregations

IOException (java.io.IOException)1 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)1 GeoShapeType (org.elasticsearch.common.geo.GeoShapeType)1 GeometryCollectionBuilder (org.elasticsearch.common.geo.builders.GeometryCollectionBuilder)1 ShapeBuilder (org.elasticsearch.common.geo.builders.ShapeBuilder)1 DistanceUnit (org.elasticsearch.common.unit.DistanceUnit)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1