Search in sources :

Example 1 with GeoJSONGeometry

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

the class GeoJSONReader method toFeature.

/**
 * Convert a GeoJSONGeometry to Feature.
 *
 * @param jsonGeometry
 * @return
 */
protected Feature toFeature(GeoJSONGeometry jsonGeometry) {
    final FTypeInformation fti = ftInfos.get(featureType);
    final Feature feature = featureType.newInstance();
    final Geometry geom = GeoJSONGeometry.toJTS(jsonGeometry, fti.crs);
    feature.setPropertyValue(fti.geometryName, geom);
    return feature;
}
Also used : GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) Geometry(org.locationtech.jts.geom.Geometry) Feature(org.opengis.feature.Feature) GeoJSONFeature(org.geotoolkit.internal.geojson.binding.GeoJSONFeature)

Example 2 with GeoJSONGeometry

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

the class GeoJSONReader method read.

private void read() throws BackingStoreException {
    if (current != null)
        return;
    // first call
    if (toRead) {
        rwlock.readLock().lock();
        try {
            jsonObj = GeoJSONParser.parse(jsonFile, true);
        } catch (IOException e) {
            throw new BackingStoreException(e);
        } finally {
            toRead = false;
            rwlock.readLock().unlock();
        }
    }
    if (jsonObj instanceof GeoJSONFeatureCollection) {
        final GeoJSONFeatureCollection fc = (GeoJSONFeatureCollection) jsonObj;
        rwlock.readLock().lock();
        try {
            if (fc.hasNext()) {
                current = toFeature(fc.next());
            }
        } finally {
            rwlock.readLock().unlock();
        }
    } else if (jsonObj instanceof GeoJSONFeature) {
        current = toFeature((GeoJSONFeature) jsonObj);
        jsonObj = null;
    } else if (jsonObj instanceof GeoJSONGeometry) {
        current = toFeature((GeoJSONGeometry) jsonObj);
        jsonObj = null;
    }
}
Also used : GeoJSONFeature(org.geotoolkit.internal.geojson.binding.GeoJSONFeature) BackingStoreException(org.apache.sis.util.collection.BackingStoreException) IOException(java.io.IOException) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) GeoJSONFeatureCollection(org.geotoolkit.internal.geojson.binding.GeoJSONFeatureCollection)

Example 3 with GeoJSONGeometry

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

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

the class GeoJSONWriter method writeGeoJSONGeometry.

/**
 * Write a GeoJSONGeometry
 *
 * @param jsonGeometry
 * @throws IOException
 */
private void writeGeoJSONGeometry(GeoJSONGeometry jsonGeometry) throws IOException {
    writer.writeStartObject();
    writer.writeStringField(TYPE, jsonGeometry.getType());
    if (jsonGeometry instanceof GeoJSONGeometryCollection) {
        List<GeoJSONGeometry> geometries = ((GeoJSONGeometryCollection) jsonGeometry).getGeometries();
        // "geometries" : [
        writer.writeArrayFieldStart(GEOMETRIES);
        for (GeoJSONGeometry geometry : geometries) {
            writeGeoJSONGeometry(geometry);
        }
        // "]"
        writer.writeEndArray();
    } else {
        // "coordinates" : [
        writer.writeArrayFieldStart(COORDINATES);
        if (jsonGeometry instanceof GeoJSONPoint) {
            writeArray(((GeoJSONPoint) jsonGeometry).getCoordinates());
        } else if (jsonGeometry instanceof GeoJSONLineString) {
            writeArray(((GeoJSONLineString) jsonGeometry).getCoordinates());
        } else if (jsonGeometry instanceof GeoJSONPolygon) {
            writeArray(((GeoJSONPolygon) jsonGeometry).getCoordinates());
        } else if (jsonGeometry instanceof GeoJSONMultiPoint) {
            writeArray(((GeoJSONMultiPoint) jsonGeometry).getCoordinates());
        } else if (jsonGeometry instanceof GeoJSONMultiLineString) {
            writeArray(((GeoJSONMultiLineString) jsonGeometry).getCoordinates());
        } else if (jsonGeometry instanceof GeoJSONMultiPolygon) {
            writeArray(((GeoJSONMultiPolygon) jsonGeometry).getCoordinates());
        } else {
            throw new IllegalArgumentException("Unsupported geometry type : " + jsonGeometry);
        }
        // "]"
        writer.writeEndArray();
    }
    writer.writeEndObject();
}
Also used : GeoJSONMultiLineString(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiLineString) GeoJSONPoint(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONPoint) GeoJSONGeometryCollection(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONGeometryCollection) GeoJSONPolygon(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONPolygon) GeoJSONLineString(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONLineString) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) GeoJSONMultiPoint(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiPoint) GeoJSONMultiPolygon(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiPolygon)

Example 5 with GeoJSONGeometry

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

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