Search in sources :

Example 1 with GeoJSONGeometryCollection

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

the class GeoJSONWriter method writeGeoJSONGeometry.

/**
 * Write a GeoJSONGeometry
 *
 * @param jsonGeometry
 * @throws IOException
 */
private void writeGeoJSONGeometry(GeoJSONGeometry jsonGeometry) throws IOException {
    writer.writeStartObject();
    writer.writeStringField(TYPE, jsonGeometry.getType());
    if (jsonGeometry instanceof GeoJSONGeometryCollection) {
        List<GeoJSONGeometry> geometries = ((GeoJSONGeometryCollection) jsonGeometry).getGeometries();
        // "geometries" : [
        writer.writeArrayFieldStart(GEOMETRIES);
        for (GeoJSONGeometry geometry : geometries) {
            writeGeoJSONGeometry(geometry);
        }
        // "]"
        writer.writeEndArray();
    } else {
        // "coordinates" : [
        writer.writeArrayFieldStart(COORDINATES);
        if (jsonGeometry instanceof GeoJSONPoint) {
            writeArray(((GeoJSONPoint) jsonGeometry).getCoordinates());
        } else if (jsonGeometry instanceof GeoJSONLineString) {
            writeArray(((GeoJSONLineString) jsonGeometry).getCoordinates());
        } else if (jsonGeometry instanceof GeoJSONPolygon) {
            writeArray(((GeoJSONPolygon) jsonGeometry).getCoordinates());
        } else if (jsonGeometry instanceof GeoJSONMultiPoint) {
            writeArray(((GeoJSONMultiPoint) jsonGeometry).getCoordinates());
        } else if (jsonGeometry instanceof GeoJSONMultiLineString) {
            writeArray(((GeoJSONMultiLineString) jsonGeometry).getCoordinates());
        } else if (jsonGeometry instanceof GeoJSONMultiPolygon) {
            writeArray(((GeoJSONMultiPolygon) jsonGeometry).getCoordinates());
        } else {
            throw new IllegalArgumentException("Unsupported geometry type : " + jsonGeometry);
        }
        // "]"
        writer.writeEndArray();
    }
    writer.writeEndObject();
}
Also used : GeoJSONMultiLineString(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiLineString) GeoJSONPoint(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONPoint) GeoJSONGeometryCollection(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONGeometryCollection) GeoJSONPolygon(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONPolygon) GeoJSONLineString(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONLineString) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) GeoJSONMultiPoint(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiPoint) GeoJSONMultiPolygon(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiPolygon)

Example 2 with GeoJSONGeometryCollection

use of org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONGeometryCollection 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)2 GeoJSONGeometryCollection (org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONGeometryCollection)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 JAXBElement (javax.xml.bind.JAXBElement)1 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)1 AbstractGeometry (org.geotoolkit.gml.xml.AbstractGeometry)1 GeoJSONLineString (org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONLineString)1 GeoJSONMultiLineString (org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiLineString)1 GeoJSONMultiPoint (org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiPoint)1 GeoJSONMultiPolygon (org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiPolygon)1 GeoJSONPoint (org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONPoint)1 GeoJSONPolygon (org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONPolygon)1 GeoJSONObject (org.geotoolkit.internal.geojson.binding.GeoJSONObject)1 Geometry (org.locationtech.jts.geom.Geometry)1 FactoryException (org.opengis.util.FactoryException)1