Search in sources :

Example 1 with GeoJsonWriter

use of org.locationtech.jts.io.geojson.GeoJsonWriter in project dhis2-core by dhis2.

the class CoordinateUtils method getCoordinatesFromGeometry.

public static String getCoordinatesFromGeometry(Geometry geometry) {
    String coordinatesKey = "\"coordinates\":";
    String crsKey = ",\"crs\":";
    GeoJsonWriter gjw = new GeoJsonWriter();
    String geojson = gjw.write(geometry).trim();
    return geojson.substring(geojson.indexOf(coordinatesKey) + coordinatesKey.length(), geojson.indexOf(crsKey));
}
Also used : GeoJsonWriter(org.locationtech.jts.io.geojson.GeoJsonWriter)

Example 2 with GeoJsonWriter

use of org.locationtech.jts.io.geojson.GeoJsonWriter in project jts by locationtech.

the class GeoJsonTest method setUp.

@Override
public void setUp() throws Exception {
    this.geoJsonWriter = new GeoJsonWriter();
    this.geoJsonReader = new GeoJsonReader();
}
Also used : GeoJsonWriter(org.locationtech.jts.io.geojson.GeoJsonWriter) GeoJsonReader(org.locationtech.jts.io.geojson.GeoJsonReader)

Example 3 with GeoJsonWriter

use of org.locationtech.jts.io.geojson.GeoJsonWriter in project jts by locationtech.

the class GeometryOutput method writeGeoJSON.

private static String writeGeoJSON(Geometry geom) {
    GeoJsonWriter writer = new GeoJsonWriter();
    writer.setEncodeCRS(false);
    return writer.write(geom);
}
Also used : GeoJsonWriter(org.locationtech.jts.io.geojson.GeoJsonWriter)

Example 4 with GeoJsonWriter

use of org.locationtech.jts.io.geojson.GeoJsonWriter 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

GeoJsonWriter (org.locationtech.jts.io.geojson.GeoJsonWriter)4 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 GeoJsonReader (org.locationtech.jts.io.geojson.GeoJsonReader)1