Search in sources :

Example 6 with GeoJSONFeature

use of org.geotoolkit.internal.geojson.binding.GeoJSONFeature 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 7 with GeoJSONFeature

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

the class GeoJSONReadTest method mixed_arrays.

@Test
public void mixed_arrays() throws Exception {
    try (final InputStream resource = GeoJSONReadTest.class.getResourceAsStream("/org/apache/sis/internal/storage/geojson/mixed_arrays.json")) {
        if (resource == null)
            throw new IllegalStateException("Cannot find a test resource");
        final GeoJSONObject readValue = GeoJSONParser.parse(resource);
        assertNotNull(readValue);
        assertTrue(readValue instanceof GeoJSONFeature);
        final GeoJSONFeature feature = (GeoJSONFeature) readValue;
        // Ensure no side-effect would break other parts of the feature reading
        final GeoJSONGeometry geom = feature.getGeometry();
        assertTrue("Read feature should contain a point, but we've read: " + geom, geom instanceof GeoJSONGeometry.GeoJSONPoint);
        // Now, we can check our arrays have been well-parsed
        final Map<String, Object> properties = feature.getProperties();
        assertPropertyIs(new double[] { 2., 3., 4. }, "numberMix1", properties);
        assertPropertyIs(new double[] { 2., 3., 4. }, "numberMix2", properties);
        assertPropertyIs(new int[] { 42, 51 }, "intArray", properties);
        assertPropertyIs(new long[] { 1, 7_000_000_000l }, "longArray", properties);
        assertPropertyIs(new Double[] { 2., null, 4. }, "numbersWithNullValues", properties);
        assertPropertyIs(new Object[] { null, null }, "onlyNullValues", properties);
        assertPropertyIs(new Object[0], "emptyArray", properties);
        assertPropertyIs(new Object[] { 2.0, "I'm a text", null }, "arbitraryMix", properties);
    }
}
Also used : GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) GeoJSONFeature(org.geotoolkit.internal.geojson.binding.GeoJSONFeature) GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) Test(org.junit.Test)

Aggregations

GeoJSONFeature (org.geotoolkit.internal.geojson.binding.GeoJSONFeature)7 GeoJSONObject (org.geotoolkit.internal.geojson.binding.GeoJSONObject)6 GeoJSONGeometry (org.geotoolkit.internal.geojson.binding.GeoJSONGeometry)5 GeoJSONFeatureCollection (org.geotoolkit.internal.geojson.binding.GeoJSONFeatureCollection)3 IOException (java.io.IOException)2 Geometry (org.locationtech.jts.geom.Geometry)2 Feature (org.opengis.feature.Feature)2 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 JAXBElement (javax.xml.bind.JAXBElement)1 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)1