Search in sources :

Example 11 with GeoJSONObject

use of org.geotoolkit.internal.geojson.binding.GeoJSONObject in project geotoolkit by Geomatys.

the class ComplexToGeometryConverter method convert.

/**
 * {@inheritDoc}
 * @return Geometry.
 */
@Override
public Geometry convert(final Data source, final Map<String, Object> params) throws UnconvertibleObjectException {
    String dataMimeTypeIdentifier = null;
    try {
        final List<Object> data = source.getContent();
        final String mimeType = source.getMimeType();
        if (data.size() != 1)
            throw new UnconvertibleObjectException("Invalid data input : Only one geometry expected.");
        if (WPSMimeType.APP_GML.val().equalsIgnoreCase(mimeType) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(mimeType) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(mimeType)) {
            dataMimeTypeIdentifier = "GML";
            Object value = data.get(0);
            if (value instanceof JAXBElement) {
                value = ((JAXBElement) value).getValue();
            }
            AbstractGeometry abstractGeo;
            if (value instanceof AbstractGeometry) {
                abstractGeo = (AbstractGeometry) value;
            } else if (value instanceof String) {
                abstractGeo = WPSConvertersUtils.readGMLGeometryFromString((String) value);
            } else {
                throw new UnconvertibleObjectException("Invalid data input content for " + dataMimeTypeIdentifier + " geometry.");
            }
            return GeometrytoJTS.toJTS(abstractGeo);
        } else if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(mimeType)) {
            dataMimeTypeIdentifier = "GeoJSON";
            final String content = WPSConvertersUtils.geojsonContentAsString(source);
            final GeoJSONObject jsonObject = WPSConvertersUtils.readGeoJSONObjectsFromString(content);
            if (!(jsonObject instanceof GeoJSONFeature)) {
                throw new UnconvertibleObjectException("Expected a GeoJSONGeometry and found a " + jsonObject.getClass().getName());
            }
            Geometry geom = WPSConvertersUtils.convertGeoJSONGeometryToGeometry(((GeoJSONFeature) jsonObject).getGeometry());
            Class expectedClass = (Class) params.get(WPSObjectConverter.TARGET_CLASS);
            /*
                 * Linear ring does not exist in GeoJson spec, so we transform polygon geometry during conversion
                 */
            if (geom instanceof Polygon && LinearRing.class.equals(expectedClass)) {
                CoordinateReferenceSystem crs = JTS.findCoordinateReferenceSystem(geom);
                Polygon poly = (Polygon) geom;
                LinearRing lr = GF.createLinearRing(poly.getExteriorRing().getCoordinateSequence());
                JTS.setCRS(lr, crs);
                return lr;
            }
            return geom;
        } else if (WPSMimeType.APP_EWKT.val().equalsIgnoreCase(mimeType)) {
            Object value = data.get(0);
            if (value instanceof String) {
                String wkt = (String) value;
                int idx = wkt.indexOf(';');
                CoordinateReferenceSystem crs = null;
                if (idx > 0) {
                    try {
                        int srid = Integer.valueOf(wkt.substring(5, idx));
                        if (srid > 0) {
                            crs = CRS.forCode("EPSG:" + srid);
                            crs = AbstractCRS.castOrCopy(crs).forConvention(AxesConvention.RIGHT_HANDED);
                        }
                    } catch (IllegalArgumentException ex) {
                        throw new UnconvertibleObjectException("Incorrect SRID definition " + wkt);
                    }
                    wkt = wkt.substring(idx + 1);
                }
                final WKTReader reader = new WKTReader();
                final Geometry geom;
                try {
                    geom = reader.read(wkt);
                } catch (ParseException ex) {
                    throw new UnconvertibleObjectException("Incorrect WKT definition " + wkt);
                }
                geom.setUserData(crs);
                return geom;
            } else {
                throw new UnconvertibleObjectException("Expected a WKT String and found a " + value.getClass().getName());
            }
        } else {
            throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + source.getMimeType());
        }
    } catch (ClassCastException ex) {
        throw new UnconvertibleObjectException("Invalid data input : empty " + dataMimeTypeIdentifier + " geometry.", ex);
    } catch (FactoryException ex) {
        throw new UnconvertibleObjectException("Invalid data input : Cannot convert " + dataMimeTypeIdentifier + " geometry.", ex);
    } catch (MalformedURLException ex) {
        throw new UnconvertibleObjectException("Unable to read the CRS from the GeoJSONGeometry.", ex);
    } catch (IOException ex) {
        throw new UnconvertibleObjectException(ex);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) FactoryException(org.opengis.util.FactoryException) GeoJSONFeature(org.geotoolkit.internal.geojson.binding.GeoJSONFeature) JAXBElement(javax.xml.bind.JAXBElement) IOException(java.io.IOException) WKTReader(org.locationtech.jts.io.WKTReader) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) Geometry(org.locationtech.jts.geom.Geometry) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) ParseException(org.locationtech.jts.io.ParseException) Polygon(org.locationtech.jts.geom.Polygon) LinearRing(org.locationtech.jts.geom.LinearRing)

Aggregations

GeoJSONObject (org.geotoolkit.internal.geojson.binding.GeoJSONObject)11 GeoJSONGeometry (org.geotoolkit.internal.geojson.binding.GeoJSONGeometry)7 IOException (java.io.IOException)4 GeoJSONFeature (org.geotoolkit.internal.geojson.binding.GeoJSONFeature)4 JAXBElement (javax.xml.bind.JAXBElement)3 AbstractGeometry (org.geotoolkit.gml.xml.AbstractGeometry)3 Test (org.junit.Test)3 Geometry (org.locationtech.jts.geom.Geometry)3 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 MalformedURLException (java.net.MalformedURLException)2 DataStoreException (org.apache.sis.storage.DataStoreException)2 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)2 GeoJSONFeatureCollection (org.geotoolkit.internal.geojson.binding.GeoJSONFeatureCollection)2 LineString (org.locationtech.jts.geom.LineString)2 MultiLineString (org.locationtech.jts.geom.MultiLineString)2 FactoryException (org.opengis.util.FactoryException)2 GradientPaint (java.awt.GradientPaint)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1