Search in sources :

Example 6 with GeoJSONObject

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

Example 7 with GeoJSONObject

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

the class GeoJSONReadTest method parsingOrderBugTest.

@Test
public void parsingOrderBugTest() throws IOException {
    String geoJsonPolygonString = "" + "         {\n" + "           \"coordinates\": [\n" + "             [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],\n" + "               [100.0, 1.0], [100.0, 0.0] ]\n" + "           ],\n" + "           \"type\": \"Polygon\"\n" + "         }";
    GeoJSONObject obj = GeoJSONParser.parse(new ByteArrayInputStream(geoJsonPolygonString.getBytes()));
    Assert.assertTrue("A geometry should have been decoded", obj instanceof GeoJSONGeometry.GeoJSONPolygon);
}
Also used : GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) Test(org.junit.Test)

Example 8 with GeoJSONObject

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

the class ConvertersTestUtils method getGeometryArrayFromInputStream.

/**
 * Helper method that loads a geometry array from a json file
 * @param inputStream inputStream on the json file
 * @return a geometry array
 * @throws IOException
 * @throws FactoryException
 */
public static Geometry[] getGeometryArrayFromInputStream(final InputStream inputStream) throws IOException, FactoryException {
    final GeoJSONObject geoJsonObject = GeoJSONParser.parse(inputStream);
    if (!(geoJsonObject instanceof GeoJSONGeometry.GeoJSONGeometryCollection))
        fail();
    final GeoJSONGeometry.GeoJSONGeometryCollection geometryCollection = (GeoJSONGeometry.GeoJSONGeometryCollection) geoJsonObject;
    final Geometry parentGeometry = WPSConvertersUtils.convertGeoJSONGeometryToGeometry(geometryCollection);
    final Geometry[] geometryArray = new Geometry[parentGeometry.getNumGeometries()];
    for (int i = 0; i < geometryArray.length; i++) geometryArray[i] = parentGeometry.getGeometryN(i);
    return geometryArray;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) GradientPaint(java.awt.GradientPaint)

Example 9 with GeoJSONObject

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

the class WPSConvertersUtils method readGMLGeometryFromString.

/**
 * Helper method which extracts the GML Geometry from a String.
 *
 * @param content content string containing a GML geometry
 * @return a GeoJSONObject
 * @throws java.io.IOException on reading errors
 */
public static AbstractGeometry readGMLGeometryFromString(String content) throws IOException {
    ArgumentChecks.ensureNonEmpty("content", content);
    // Parse GeoJSON
    try (final StringReader contentReader = new StringReader(content)) {
        Unmarshaller um = GMLMarshallerPool.getInstance().acquireUnmarshaller();
        Object obj = um.unmarshal(contentReader);
        GMLMarshallerPool.getInstance().recycle(um);
        if (obj instanceof JAXBElement) {
            obj = ((JAXBElement) obj).getValue();
        }
        if (!(obj instanceof AbstractGeometry)) {
            throw new IOException("XML don't contains GML geometry");
        }
        return (AbstractGeometry) obj;
    } catch (JAXBException ex) {
        throw new IOException(ex);
    }
}
Also used : AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) JAXBException(javax.xml.bind.JAXBException) StringReader(java.io.StringReader) GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) JAXBElement(javax.xml.bind.JAXBElement) IOException(java.io.IOException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 10 with GeoJSONObject

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

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