Search in sources :

Example 1 with GeoJSONDecodingException

use of org.n52.svalbard.coding.json.GeoJSONDecodingException in project arctic-sea by 52North.

the class GeoJSONDecoder method decodeCRS.

protected GeometryFactory decodeCRS(JsonNode node, GeometryFactory factory) throws GeoJSONDecodingException {
    if (!node.path(JSONConstants.CRS).hasNonNull(JSONConstants.TYPE)) {
        throw new GeoJSONDecodingException("Missing CRS type");
    }
    String type = node.path(JSONConstants.CRS).path(JSONConstants.TYPE).textValue();
    JsonNode properties = node.path(JSONConstants.CRS).path(JSONConstants.PROPERTIES);
    switch(type) {
        case JSONConstants.NAME:
            return decodeNamedCRS(properties, factory);
        case JSONConstants.LINK:
            return decodeLinkedCRS(properties, factory);
        default:
            throw new GeoJSONDecodingException("Unknown CRS type: " + type);
    }
}
Also used : GeoJSONDecodingException(org.n52.svalbard.coding.json.GeoJSONDecodingException) JsonNode(com.fasterxml.jackson.databind.JsonNode) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString)

Example 2 with GeoJSONDecodingException

use of org.n52.svalbard.coding.json.GeoJSONDecodingException in project arctic-sea by 52North.

the class GeoJSONDecoder method decodeCoordinate.

protected Coordinate decodeCoordinate(JsonNode node) throws GeoJSONDecodingException {
    if (!node.isArray()) {
        throw new GeoJSONDecodingException(EXPECTED_ARRAY);
    }
    final int dim = node.size();
    if (dim < DIM_2D) {
        throw new GeoJSONDecodingException("coordinates may have at least 2 dimensions");
    }
    if (dim > DIM_3D) {
        throw new GeoJSONDecodingException("coordinates may have at most 3 dimensions");
    }
    final Coordinate coordinate = new Coordinate();
    for (int i = 0; i < dim; ++i) {
        if (node.get(i).isNumber()) {
            coordinate.setOrdinate(i, node.get(i).doubleValue());
        } else {
            throw new GeoJSONDecodingException("coordinate index " + i + " has to be a number");
        }
    }
    return coordinate;
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) GeoJSONDecodingException(org.n52.svalbard.coding.json.GeoJSONDecodingException) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint)

Example 3 with GeoJSONDecodingException

use of org.n52.svalbard.coding.json.GeoJSONDecodingException in project arctic-sea by 52North.

the class GeoJSONDecoder method decodePolygonCoordinates.

protected Polygon decodePolygonCoordinates(JsonNode coordinates, GeometryFactory fac) throws GeoJSONDecodingException {
    if (!coordinates.isArray()) {
        throw new GeoJSONDecodingException(EXPECTED_ARRAY);
    }
    if (coordinates.size() < 1) {
        throw new GeoJSONDecodingException("missing polygon shell");
    }
    LinearRing shell = fac.createLinearRing(decodeCoordinates(coordinates.get(0)));
    LinearRing[] holes = new LinearRing[coordinates.size() - 1];
    for (int i = 1; i < coordinates.size(); ++i) {
        holes[i - 1] = fac.createLinearRing(decodeCoordinates(coordinates.get(i)));
    }
    return fac.createPolygon(shell, holes);
}
Also used : GeoJSONDecodingException(org.n52.svalbard.coding.json.GeoJSONDecodingException) LinearRing(org.locationtech.jts.geom.LinearRing) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint)

Example 4 with GeoJSONDecodingException

use of org.n52.svalbard.coding.json.GeoJSONDecodingException in project arctic-sea by 52North.

the class GeoJSONDecoder method decodeGeometry.

protected Geometry decodeGeometry(Object o, GeometryFactory parentFactory) throws GeoJSONDecodingException {
    if (!(o instanceof JsonNode)) {
        throw new GeoJSONDecodingException("Cannot decode " + o);
    }
    final JsonNode node = (JsonNode) o;
    final String type = getType(node);
    final GeometryFactory factory = getGeometryFactory(node, parentFactory);
    switch(type) {
        case JSONConstants.POINT:
            return decodePoint(node, factory);
        case JSONConstants.MULTI_POINT:
            return decodeMultiPoint(node, factory);
        case JSONConstants.LINE_STRING:
            return decodeLineString(node, factory);
        case JSONConstants.MULTI_LINE_STRING:
            return decodeMultiLineString(node, factory);
        case JSONConstants.POLYGON:
            return decodePolygon(node, factory);
        case JSONConstants.MULTI_POLYGON:
            return decodeMultiPolygon(node, factory);
        case JSONConstants.GEOMETRY_COLLECTION:
            return decodeGeometryCollection(node, factory);
        default:
            throw new GeoJSONDecodingException("Unkown geometry type: " + type);
    }
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) GeoJSONDecodingException(org.n52.svalbard.coding.json.GeoJSONDecodingException) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString)

Example 5 with GeoJSONDecodingException

use of org.n52.svalbard.coding.json.GeoJSONDecodingException in project arctic-sea by 52North.

the class GeoJSONDecoder method decodeGeometryCollection.

protected GeometryCollection decodeGeometryCollection(JsonNode node, GeometryFactory fac) throws GeoJSONDecodingException {
    final JsonNode geometries = node.path(JSONConstants.GEOMETRIES);
    if (!geometries.isArray()) {
        throw new GeoJSONDecodingException("expected 'geometries' array");
    }
    Geometry[] geoms = new Geometry[geometries.size()];
    for (int i = 0; i < geometries.size(); ++i) {
        geoms[i] = decodeGeometry(geometries.get(i), fac);
    }
    return fac.createGeometryCollection(geoms);
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) JsonNode(com.fasterxml.jackson.databind.JsonNode) GeoJSONDecodingException(org.n52.svalbard.coding.json.GeoJSONDecodingException) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint)

Aggregations

GeoJSONDecodingException (org.n52.svalbard.coding.json.GeoJSONDecodingException)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 MultiPoint (org.locationtech.jts.geom.MultiPoint)3 Point (org.locationtech.jts.geom.Point)3 LineString (org.locationtech.jts.geom.LineString)2 MultiLineString (org.locationtech.jts.geom.MultiLineString)2 Coordinate (org.locationtech.jts.geom.Coordinate)1 Geometry (org.locationtech.jts.geom.Geometry)1 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)1 LinearRing (org.locationtech.jts.geom.LinearRing)1