Search in sources :

Example 1 with GeoJSONObject

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

the class GeoJSONStore method readType.

/**
 * Read FeatureType from a JSON-Schema file if exist or directly from the
 * input JSON file.
 *
 * @return
 * @throws DataStoreException
 * @throws IOException
 */
private FeatureType readType() throws DataStoreException, IOException {
    if (Files.exists(descFile) && Files.size(descFile) != 0) {
        // build FeatureType from description JSON.
        return FeatureTypeUtils.readFeatureType(descFile);
    } else {
        if (Files.exists(jsonFile) && Files.size(jsonFile) != 0) {
            final String name = GeoJSONUtils.getNameWithoutExt(jsonFile);
            final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
            ftb.setName(name);
            // build FeatureType from the first Feature of JSON file.
            final GeoJSONObject obj = GeoJSONParser.parse(jsonFile, true);
            if (obj == null) {
                throw new DataStoreException("Invalid GeoJSON file " + jsonFile.toString());
            }
            CoordinateReferenceSystem crs = GeoJSONUtils.getCRS(obj);
            if (obj instanceof GeoJSONFeatureCollection) {
                GeoJSONFeatureCollection jsonFeatureCollection = (GeoJSONFeatureCollection) obj;
                if (!jsonFeatureCollection.hasNext()) {
                    // empty FeatureCollection error ?
                    throw new DataStoreException("Empty GeoJSON FeatureCollection " + jsonFile.toString());
                } else {
                    // TODO should we analyse all Features from FeatureCollection to be sure
                    // that each Feature properties JSON object define exactly the same properties
                    // with the same bindings ?
                    GeoJSONFeature jsonFeature = jsonFeatureCollection.next();
                    fillTypeFromFeature(ftb, crs, jsonFeature, false);
                }
            } else if (obj instanceof GeoJSONFeature) {
                GeoJSONFeature jsonFeature = (GeoJSONFeature) obj;
                fillTypeFromFeature(ftb, crs, jsonFeature, true);
            } else if (obj instanceof GeoJSONGeometry) {
                ftb.addAttribute(String.class).setName("fid").addRole(AttributeRole.IDENTIFIER_COMPONENT);
                ftb.addAttribute(findBinding((GeoJSONGeometry) obj)).setName("geometry").setCRS(crs).addRole(AttributeRole.DEFAULT_GEOMETRY);
            }
            return ftb.build();
        } else {
            throw new DataStoreException("Can't create FeatureType from empty/not found Json file " + jsonFile.getFileName().toString());
        }
    }
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) DataStoreException(org.apache.sis.storage.DataStoreException) GeoJSONFeature(org.geotoolkit.internal.geojson.binding.GeoJSONFeature) GeoJSONMultiLineString(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiLineString) GeoJSONLineString(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONLineString) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) GeoJSONFeatureCollection(org.geotoolkit.internal.geojson.binding.GeoJSONFeatureCollection)

Example 2 with GeoJSONObject

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

the class GeoJSONStore method getEnvelope.

@Override
public Optional<Envelope> getEnvelope() throws DataStoreException {
    Envelope envelope = null;
    rwLock.readLock().lock();
    try {
        final GeoJSONObject obj = GeoJSONParser.parse(jsonFile, true);
        final CoordinateReferenceSystem crs = GeoJSONUtils.getCRS(obj);
        envelope = GeoJSONUtils.getEnvelope(obj, crs);
    } catch (IOException e) {
        throw new DataStoreException(e.getMessage(), e);
    } finally {
        rwLock.readLock().unlock();
    }
    return Optional.ofNullable(envelope);
}
Also used : GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) DataStoreException(org.apache.sis.storage.DataStoreException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) IOException(java.io.IOException) Envelope(org.opengis.geometry.Envelope)

Example 3 with GeoJSONObject

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

the class ConvertersTestUtils method getGeometryFromGeoJsonContent.

/**
 * Helper method that read a geometry in a json file
 * @param path path of the file containing the geometry
 * @return the read geometry
 * @throws IOException on IO errors when reading the file
 * @throws FactoryException when the crs of the geometry cannot be retrieved
 */
public static Geometry getGeometryFromGeoJsonContent(final Path path) throws IOException, FactoryException {
    final GeoJSONObject geoJsonObject = GeoJSONParser.parse(path);
    GeoJSONGeometry geoJsonGeometry = null;
    if (geoJsonObject instanceof GeoJSONFeature) {
        GeoJSONFeature jsonFeature = (GeoJSONFeature) geoJsonObject;
        geoJsonGeometry = jsonFeature.getGeometry();
    } else if (geoJsonObject instanceof GeoJSONGeometry)
        geoJsonGeometry = (GeoJSONGeometry) geoJsonObject;
    else
        fail();
    return WPSConvertersUtils.convertGeoJSONGeometryToGeometry(geoJsonGeometry);
}
Also used : GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) GeoJSONFeature(org.geotoolkit.internal.geojson.binding.GeoJSONFeature) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry)

Example 4 with GeoJSONObject

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

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

the class GeoJSONReadTest method parserTest.

/**
 * Test GeoJSONParser full and lazy reading on FeatureCollection
 */
@Test
public void parserTest() throws URISyntaxException, IOException {
    URL fcFile = GeoJSONReadTest.class.getResource("/org/apache/sis/internal/storage/geojson/featurecollection.json");
    Path fcPath = Paths.get(fcFile.toURI());
    // test with full reading
    GeoJSONObject geoJSONObject = GeoJSONParser.parse(fcPath, false);
    assertTrue(geoJSONObject instanceof GeoJSONFeatureCollection);
    GeoJSONFeatureCollection geojsonFC = (GeoJSONFeatureCollection) geoJSONObject;
    assertFalse(geojsonFC.isLazyMode());
    assertEquals(7, geojsonFC.getFeatures().size());
    for (int i = 0; i < 7; i++) {
        assertTrue(geojsonFC.hasNext());
        assertNotNull(geojsonFC.next());
    }
    // end of collection
    assertFalse(geojsonFC.hasNext());
    // test in lazy reading
    geoJSONObject = GeoJSONParser.parse(fcPath, true);
    assertTrue(geoJSONObject instanceof GeoJSONFeatureCollection);
    geojsonFC = (GeoJSONFeatureCollection) geoJSONObject;
    assertTrue(geojsonFC.isLazyMode());
    // lazy don't know number of features
    assertEquals(0, geojsonFC.getFeatures().size());
    for (int i = 0; i < 7; i++) {
        assertTrue(geojsonFC.hasNext());
        assertNotNull(geojsonFC.next());
    }
    // end of collection
    assertFalse(geojsonFC.hasNext());
}
Also used : Path(java.nio.file.Path) GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) URL(java.net.URL) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint) GeoJSONFeatureCollection(org.geotoolkit.internal.geojson.binding.GeoJSONFeatureCollection) Test(org.junit.Test)

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