Search in sources :

Example 1 with ParseException

use of io.arlas.server.core.exceptions.ParseException in project ARLAS-server by gisaia.

the class GeoTypeMapper method getGeoJsonObject.

public static GeoJsonObject getGeoJsonObject(Object elasticsearchGeoField, GeoTypeEnum type) throws ArlasException {
    GeoJsonObject geoObject = null;
    String parseExceptionMsg = "Unable to parse " + elasticsearchGeoField.toString() + "as valid " + type;
    String loggerMsg = "Unable to parse " + elasticsearchGeoField.toString() + "as valid " + type + " from " + elasticsearchGeoField.getClass();
    switch(type) {
        case GEOPOINT_AS_STRING:
        case GEOHASH:
            try {
                GeoPoint geoPoint = new GeoPoint(elasticsearchGeoField.toString());
                geoObject = new Point(geoPoint.getLon(), geoPoint.getLat());
            } catch (Exception e) {
                LOGGER.error(loggerMsg, e);
                throw new ParseException(parseExceptionMsg);
            }
            break;
        case GEOPOINT_AS_ARRAY:
            try {
                geoObject = new Point(((Number) ((ArrayList) elasticsearchGeoField).get(0)).doubleValue(), ((Number) ((ArrayList) elasticsearchGeoField).get(1)).doubleValue());
            } catch (Exception e) {
                LOGGER.error(loggerMsg, e);
                throw new ParseException(parseExceptionMsg);
            }
            break;
        case GEOHASH_ARRAY:
            try {
                List geohashes = (ArrayList) elasticsearchGeoField;
                int middleIndex = (geohashes.size() / 2);
                GeoPoint geoPoint = new GeoPoint(geohashes.get(middleIndex).toString());
                geoObject = new Point(geoPoint.getLon(), geoPoint.getLat());
            } catch (Exception e) {
                LOGGER.error(loggerMsg, e);
                throw new ParseException(parseExceptionMsg);
            }
            break;
        case GEOPOINT:
            try {
                geoObject = new Point(((Number) ((HashMap) elasticsearchGeoField).get("lon")).doubleValue(), ((Number) ((HashMap) elasticsearchGeoField).get("lat")).doubleValue());
            } catch (Exception e) {
                LOGGER.error(loggerMsg, e);
                throw new ParseException(parseExceptionMsg);
            }
            break;
        case GEOJSON:
            // Standard GeoJSON object
            try {
                geoObject = reader.readValue(mapper.writer().writeValueAsString(elasticsearchGeoField));
            } catch (IOException e) {
                LOGGER.error(loggerMsg, e);
                throw new ParseException(parseExceptionMsg);
            }
            break;
        case WKT:
            try {
                GeoJsonWriter geoJsonWriter = new GeoJsonWriter();
                Geometry g = GeoUtil.readWKT(elasticsearchGeoField.toString());
                geoObject = reader.readValue(geoJsonWriter.write(g));
            } catch (IOException e) {
                LOGGER.error(loggerMsg, e);
                throw new ParseException(parseExceptionMsg);
            }
            break;
    }
    return geoObject;
}
Also used : GeoJsonWriter(org.locationtech.jts.io.geojson.GeoJsonWriter) ArrayList(java.util.ArrayList) Point(org.geojson.Point) GeoPoint(org.elasticsearch.common.geo.GeoPoint) IOException(java.io.IOException) IOException(java.io.IOException) ParseException(io.arlas.server.core.exceptions.ParseException) ArlasException(io.arlas.server.core.exceptions.ArlasException) NotImplementedException(io.arlas.server.core.exceptions.NotImplementedException) Point(org.geojson.Point) GeoPoint(org.elasticsearch.common.geo.GeoPoint) Geometry(org.locationtech.jts.geom.Geometry) GeoPoint(org.elasticsearch.common.geo.GeoPoint) ArrayList(java.util.ArrayList) List(java.util.List) ParseException(io.arlas.server.core.exceptions.ParseException) GeoJsonObject(org.geojson.GeoJsonObject)

Aggregations

ArlasException (io.arlas.server.core.exceptions.ArlasException)1 NotImplementedException (io.arlas.server.core.exceptions.NotImplementedException)1 ParseException (io.arlas.server.core.exceptions.ParseException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 GeoPoint (org.elasticsearch.common.geo.GeoPoint)1 GeoJsonObject (org.geojson.GeoJsonObject)1 Point (org.geojson.Point)1 Geometry (org.locationtech.jts.geom.Geometry)1 GeoJsonWriter (org.locationtech.jts.io.geojson.GeoJsonWriter)1