Search in sources :

Example 6 with GeoJSONGeometry

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

the class GeometryToComplexConverterTest method getGeometry.

/**
 * Helper method that read a json file containing a single Geometry
 * @param jsonFile file to read
 * @return a geometry
 * @throws IOException
 * @throws FactoryException
 */
private static Geometry getGeometry(Path jsonFile) throws IOException, FactoryException {
    final GeoJSONObject geoJsonObject = GeoJSONParser.parse(jsonFile);
    if (!(geoJsonObject instanceof GeoJSONGeometry))
        fail();
    final GeoJSONGeometry jsonGeometry = (GeoJSONGeometry) geoJsonObject;
    return WPSConvertersUtils.convertGeoJSONGeometryToGeometry(jsonGeometry);
}
Also used : GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry)

Example 7 with GeoJSONGeometry

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

the class GeoJSONWriteTest method readingAsWritingPolygonTest.

@Test
public void readingAsWritingPolygonTest() throws IOException {
    final String simplePolygonResource = "/org/apache/sis/internal/storage/geojson/simplePolygon.json";
    try (final InputStream inputStream = GeoJSONWriteTest.class.getResourceAsStream(simplePolygonResource);
        final InputStream controlInputStream = GeoJSONWriteTest.class.getResourceAsStream(simplePolygonResource)) {
        final ObjectMapper mapper = new ObjectMapper();
        final GeoJSONGeometry json = mapper.readValue(inputStream, GeoJSONGeometry.class);
        assertTrue(json instanceof GeoJSONGeometry.GeoJSONPolygon);
        final String expectedJSON = IOUtilities.toString(controlInputStream, Charset.forName("UTF-8"));
        final String writtenJSON = mapper.writeValueAsString(json);
        assertNotNull(writtenJSON);
        assertNotNull(expectedJSON);
        assertEquals(expectedJSON.trim(), mapper.writeValueAsString(json).trim());
    }
}
Also used : SimpleInternationalString(org.apache.sis.util.SimpleInternationalString) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 8 with GeoJSONGeometry

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

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

the class GeoJSONReader method fillProperty.

/**
 * Try to convert value as expected in PropertyType description.
 *
 * @param prop
 * @param value
 */
private void fillProperty(Attribute prop, Object value) throws BackingStoreException {
    Object convertValue = null;
    try {
        if (value instanceof GeoJSONGeometry) {
            convertValue = GeoJSONGeometry.toJTS((GeoJSONGeometry) value, null);
        } else if (value != null) {
            final AttributeType<?> propertyType = prop.getType();
            final Class binding = propertyType.getValueClass();
            if (value.getClass().isArray() && binding.isArray()) {
                int nbdim = 1;
                Class base = value.getClass().getComponentType();
                while (base.isArray()) {
                    base = base.getComponentType();
                    nbdim++;
                }
                convertValue = rebuildArray(value, base, nbdim);
            } else {
                convertValue = convert(value, binding);
            }
        }
    } catch (UnconvertibleObjectException e1) {
        throw new BackingStoreException(String.format("Inconvertible property %s : %s", prop.getName().tip().toString(), e1.getMessage()), e1);
    }
    prop.setValue(convertValue);
}
Also used : UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) AttributeType(org.opengis.feature.AttributeType) BackingStoreException(org.apache.sis.util.collection.BackingStoreException) GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry)

Example 10 with GeoJSONGeometry

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

the class GeoJSONWriter method writeSingleGeometry.

/**
 * Write a GeometryAttribute
 *
 * @param geom
 * @throws IOException
 */
void writeSingleGeometry(Attribute geom) throws IOException {
    GeoJSONGeometry jsonGeometry = GeoJSONGeometry.toGeoJSONGeometry((Geometry) geom.getValue());
    writeGeoJSONGeometry(jsonGeometry);
}
Also used : 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