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;
}
Aggregations