Search in sources :

Example 16 with AbstractGeometry

use of org.geotoolkit.gml.xml.AbstractGeometry in project geotoolkit by Geomatys.

the class GeometryArrayToComplexConverter 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);
    }
    Object tmpGmlVersion = params.get(GMLVERSION);
    final String gmlVersion;
    if (tmpGmlVersion instanceof String) {
        gmlVersion = (String) tmpGmlVersion;
    } else {
        gmlVersion = "3.2.1";
    }
    if (WPSMimeType.APP_GML.val().equalsIgnoreCase(complex.getMimeType()) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(complex.getMimeType()) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(complex.getMimeType())) {
        try {
            for (final Geometry jtsGeom : source) {
                final AbstractGeometry gmlGeom = JTStoGeometry.toGML(gmlVersion, jtsGeom);
                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(complex.getMimeType())) {
        GeoJSONGeometry.GeoJSONGeometryCollection geometryCollection = new GeoJSONGeometry.GeoJSONGeometryCollection();
        for (Geometry geometry : source) geometryCollection.getGeometries().add(GeoJSONGeometry.toGeoJSONGeometry(geometry));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            GeoJSONStreamWriter.writeSingleGeometry(baos, WPSConvertersUtils.convertGeoJSONGeometryToGeometry(geometryCollection), 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 CRS.", ex);
        }
    } else
        throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + complex.getMimeType());
    return complex;
}
Also used : NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) FactoryException(org.opengis.util.FactoryException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Data(org.geotoolkit.wps.xml.v200.Data) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JTStoGeometry(org.geotoolkit.gml.JTStoGeometry) 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) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry)

Example 17 with AbstractGeometry

use of org.geotoolkit.gml.xml.AbstractGeometry 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 18 with AbstractGeometry

use of org.geotoolkit.gml.xml.AbstractGeometry 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)

Example 19 with AbstractGeometry

use of org.geotoolkit.gml.xml.AbstractGeometry 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)

Example 20 with AbstractGeometry

use of org.geotoolkit.gml.xml.AbstractGeometry in project geotoolkit by Geomatys.

the class ReadGeometry321Test method testCurve.

@Test
public void testCurve() throws Exception {
    final AbstractGeometry geom = read(ReadGeometry321Test.class.getResource("curve.gml"));
    final GeometryTransformer tr = new GeometryTransformer(geom);
    final Geometry result = tr.get();
    Assert.assertNotNull("Read geometry is null", result);
    Assert.assertTrue(String.format("Bad geometry type.%nExpected: %s%nBut was: %s", LineString.class, result.getClass()), LineString.class.isAssignableFrom(result.getClass()));
    final LineString col = (LineString) result;
    Envelope expectedEnvelope = new Envelope(12.98241111111111, 13.462391666666665, -87.81824444444445, -87.25221944444445);
    final Envelope actual = col.getEnvelopeInternal();
    Assert.assertEquals(expectedEnvelope.getMinX(), actual.getMinX(), 0.01);
    Assert.assertEquals(expectedEnvelope.getMinY(), actual.getMinY(), 0.01);
    Assert.assertEquals(expectedEnvelope.getMaxX(), actual.getMaxX(), 0.01);
    Assert.assertEquals(expectedEnvelope.getMaxY(), actual.getMaxY(), 0.01);
}
Also used : AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) Geometry(org.locationtech.jts.geom.Geometry) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) LineString(org.locationtech.jts.geom.LineString) MultiLineString(org.locationtech.jts.geom.MultiLineString) GeometryTransformer(org.geotoolkit.gml.GeometryTransformer) Envelope(org.locationtech.jts.geom.Envelope) ReadGeometryTest(org.geotoolkit.gml.ReadGeometryTest) Test(org.junit.Test)

Aggregations

AbstractGeometry (org.geotoolkit.gml.xml.AbstractGeometry)20 Geometry (org.locationtech.jts.geom.Geometry)14 FactoryException (org.opengis.util.FactoryException)11 JAXBElement (javax.xml.bind.JAXBElement)7 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)7 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 JAXBException (javax.xml.bind.JAXBException)4 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)4 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)4 LineString (org.locationtech.jts.geom.LineString)4 MultiLineString (org.locationtech.jts.geom.MultiLineString)4 MultiPolygon (org.locationtech.jts.geom.MultiPolygon)4 Polygon (org.locationtech.jts.geom.Polygon)4 Map (java.util.Map)3 JTStoGeometry (org.geotoolkit.gml.JTStoGeometry)3 CurveProperty (org.geotoolkit.gml.xml.CurveProperty)3 SurfaceProperty (org.geotoolkit.gml.xml.SurfaceProperty)3 GeoJSONGeometry (org.geotoolkit.internal.geojson.binding.GeoJSONGeometry)3 GeoJSONObject (org.geotoolkit.internal.geojson.binding.GeoJSONObject)3