Search in sources :

Example 11 with GeoJSONGeometry

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

the class GeoJSONWriter method writeSingleGeometry.

/**
 * Write a JTS Geometry
 *
 * @param geom
 * @throws IOException
 */
void writeSingleGeometry(Geometry geom) throws IOException {
    GeoJSONGeometry jsonGeometry = GeoJSONGeometry.toGeoJSONGeometry(geom);
    writeGeoJSONGeometry(jsonGeometry);
}
Also used : GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry)

Example 12 with GeoJSONGeometry

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

the class GeometryToComplexConverter method convert.

/**
 * {@inheritDoc}
 */
@Override
public Data convert(final Geometry source, final Map<String, Object> params) throws UnconvertibleObjectException {
    if (source == null) {
        throw new UnconvertibleObjectException("The output data should be defined.");
    } else if (params == null) {
        throw new UnconvertibleObjectException("Not enough information about data format");
    }
    final Object tmpMime = params.get(MIME);
    final String mime;
    if (tmpMime instanceof String) {
        mime = (String) tmpMime;
    } else {
        throw new UnconvertibleObjectException("No valid mime type given. We cannot determine output image format");
    }
    final Data complex = new Data();
    complex.setMimeType(mime);
    final Object tmpEncoding = params.get(ENCODING);
    if (tmpEncoding instanceof String) {
        complex.setEncoding((String) tmpEncoding);
    }
    final Object tmpSchema = params.get(SCHEMA);
    if (tmpSchema instanceof String) {
        complex.setSchema((String) tmpSchema);
    }
    Object tmpGmlVersion = params.get(GMLVERSION);
    final String gmlVersion;
    if (tmpGmlVersion instanceof String) {
        gmlVersion = (String) tmpGmlVersion;
    } else {
        gmlVersion = "3.2.1";
    }
    final String mimeType = complex.getMimeType();
    if (WPSMimeType.APP_GML.val().equalsIgnoreCase(mimeType) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(mimeType) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(mimeType)) {
        try {
            final AbstractGeometry gmlGeom = JTStoGeometry.toGML(gmlVersion, source);
            complex.getContent().add(gmlGeom);
        } catch (NoSuchAuthorityCodeException ex) {
            throw new UnconvertibleObjectException(ex);
        } catch (FactoryException ex) {
            throw new UnconvertibleObjectException(ex);
        }
    } else if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(mimeType)) {
        GeoJSONGeometry jsonGeometry = GeoJSONGeometry.toGeoJSONGeometry(source);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            GeoJSONStreamWriter.writeSingleGeometry(baos, WPSConvertersUtils.convertGeoJSONGeometryToGeometry(jsonGeometry), JsonEncoding.UTF8, WPSConvertersUtils.FRACTION_DIGITS, true);
            WPSConvertersUtils.addCDATAToComplex(baos.toString("UTF-8"), complex);
        } catch (UnsupportedEncodingException e) {
            throw new UnconvertibleObjectException("Can't convert output stream into String.", e);
        } catch (IOException ex) {
            throw new UnconvertibleObjectException(ex);
        } catch (FactoryException ex) {
            throw new UnconvertibleObjectException("Couldn't decode a CRS.", ex);
        }
    } else if (WPSMimeType.APP_EWKT.val().equalsIgnoreCase(mimeType)) {
        Geometry geom = source;
        int srid = 0;
        try {
            CoordinateReferenceSystem crs = Geometries.wrap(geom).get().getCoordinateReferenceSystem();
            if (crs != null) {
                final IdentifiedObjectFinder finder = IdentifiedObjects.newFinder("EPSG");
                finder.setIgnoringAxes(true);
                final CoordinateReferenceSystem epsgcrs = (CoordinateReferenceSystem) finder.findSingleton(crs);
                if (epsgcrs != null) {
                    srid = IdentifiedObjects.lookupEPSG(epsgcrs);
                    // force geometry in longitude first
                    final CoordinateReferenceSystem crs2 = ((AbstractCRS) crs).forConvention(AxesConvention.RIGHT_HANDED);
                    if (crs2 != crs) {
                        geom = org.apache.sis.internal.feature.jts.JTS.transform(geom, crs2);
                    }
                }
            }
        } catch (FactoryException | MismatchedDimensionException | TransformException ex) {
            throw new UnconvertibleObjectException(ex.getMessage(), ex);
        }
        String wkt = geom.toText();
        if (srid > 0) {
            wkt = "SRID=" + srid + ";" + wkt;
        }
        complex.getContent().add(wkt);
    } else {
        throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + complex.getMimeType());
    }
    return complex;
}
Also used : IdentifiedObjectFinder(org.apache.sis.referencing.factory.IdentifiedObjectFinder) NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) FactoryException(org.opengis.util.FactoryException) AbstractCRS(org.apache.sis.referencing.crs.AbstractCRS) TransformException(org.opengis.referencing.operation.TransformException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Data(org.geotoolkit.wps.xml.v200.Data) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) JTStoGeometry(org.geotoolkit.gml.JTStoGeometry) Geometry(org.locationtech.jts.geom.Geometry) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 13 with GeoJSONGeometry

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

the class ComplexToGeometryArrayConverter method convert.

/**
 * {@inheritDoc}
 * @return Geometry array.
 */
@Override
public Geometry[] convert(final Data source, final Map<String, Object> params) throws UnconvertibleObjectException {
    String dataMimeTypeIdentifier = null;
    try {
        final List<Object> data = source.getContent();
        if (!data.isEmpty()) {
            if (WPSMimeType.APP_GML.val().equalsIgnoreCase(source.getMimeType()) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(source.getMimeType()) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(source.getMimeType())) {
                dataMimeTypeIdentifier = "GML";
                final List<Geometry> geoms = new ArrayList<>();
                for (int i = 0; i < data.size(); i++) {
                    Object val = data.get(i);
                    if (val instanceof JAXBElement) {
                        val = ((JAXBElement) val).getValue();
                    }
                    if (val instanceof AbstractGeometry) {
                        geoms.add(GeometrytoJTS.toJTS((AbstractGeometry) val));
                    } else {
                        throw new UnconvertibleObjectException("Unknown geometry type at index: " + i);
                    }
                }
                return geoms.toArray(new Geometry[geoms.size()]);
            } else // array is passed as a GeoJSON FeatureCollection
            if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(source.getMimeType())) {
                dataMimeTypeIdentifier = "GeoJSON";
                final String content = WPSConvertersUtils.geojsonContentAsString(source);
                final GeoJSONObject jsonObject = WPSConvertersUtils.readGeoJSONObjectsFromString(content);
                // Check that we have a FeatureCollection
                if (!(jsonObject instanceof GeoJSONGeometryCollection))
                    throw new UnconvertibleObjectException("Expected a feature collection. Found : " + jsonObject.getClass().getName());
                // Check collection size, if it's empty raise an exception
                final GeoJSONGeometryCollection geometryCollection = (GeoJSONGeometryCollection) jsonObject;
                int collectionSize = geometryCollection.getGeometries().size();
                if (collectionSize == 0)
                    throw new UnconvertibleObjectException("Empty feature collection.");
                // Fill the Geometry array
                final Geometry[] geometryArray = new Geometry[collectionSize];
                Iterator<GeoJSONGeometry> iterator = geometryCollection.getGeometries().iterator();
                int index = 0;
                while (iterator.hasNext()) {
                    final GeoJSONGeometry jsonGeometry = iterator.next();
                    // GeometryUtils.toJTS(jsonGeometry, crs);
                    geometryArray[index] = WPSConvertersUtils.convertGeoJSONGeometryToGeometry(jsonGeometry);
                    index++;
                }
                return geometryArray;
            } else
                throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + source.getMimeType());
        } else {
            throw new UnconvertibleObjectException("Invalid data input : Empty geometry list.");
        }
    } catch (FactoryException ex) {
        throw new UnconvertibleObjectException("Invalid data input : Cannot convert " + dataMimeTypeIdentifier + " geometry.", ex);
    } catch (MalformedURLException ex) {
        throw new UnconvertibleObjectException("Unknown CRS code or unable to read the CRS from a geometry", 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) ArrayList(java.util.ArrayList) JAXBElement(javax.xml.bind.JAXBElement) IOException(java.io.IOException) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) 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) GeoJSONGeometryCollection(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONGeometryCollection) Iterator(java.util.Iterator) GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry)

Aggregations

GeoJSONGeometry (org.geotoolkit.internal.geojson.binding.GeoJSONGeometry)13 GeoJSONObject (org.geotoolkit.internal.geojson.binding.GeoJSONObject)6 GeoJSONFeature (org.geotoolkit.internal.geojson.binding.GeoJSONFeature)5 IOException (java.io.IOException)3 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)3 Geometry (org.locationtech.jts.geom.Geometry)3 BackingStoreException (org.apache.sis.util.collection.BackingStoreException)2 AbstractGeometry (org.geotoolkit.gml.xml.AbstractGeometry)2 GeoJSONFeatureCollection (org.geotoolkit.internal.geojson.binding.GeoJSONFeatureCollection)2 GeoJSONGeometryCollection (org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONGeometryCollection)2 GeoJSONLineString (org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONLineString)2 GeoJSONMultiLineString (org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiLineString)2 Test (org.junit.Test)2 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)2 FactoryException (org.opengis.util.FactoryException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1