Search in sources :

Example 1 with GeoJSONFeature

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

the class GeoJSONReader method toFeature.

/**
 * Convert a GeoJSONFeature to geotk Feature.
 *
 * @param jsonFeature
 * @param ft
 * @return
 */
Feature toFeature(GeoJSONFeature jsonFeature, FeatureType ft) throws BackingStoreException {
    FTypeInformation fti = ftInfos.computeIfAbsent(ft, FTypeInformation::new);
    // create empty feature
    final Feature feature = ft.newInstance();
    if (fti.geometryName != null) {
        // Build geometry
        final Geometry geom = GeoJSONGeometry.toJTS(jsonFeature.getGeometry(), fti.crs);
        if (fti.hasIdentifier) {
            Object id = jsonFeature.getId();
            if (id == null) {
                id = currentFeatureIdx;
            }
            feature.setPropertyValue(AttributeConvention.IDENTIFIER, fti.idConverter.apply(id));
        }
        feature.setPropertyValue(fti.geometryName, geom);
    }
    // recursively fill other properties
    final Map<String, Object> properties = jsonFeature.getProperties();
    fillFeature(feature, properties);
    currentFeatureIdx++;
    return feature;
}
Also used : GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) Geometry(org.locationtech.jts.geom.Geometry) GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) Feature(org.opengis.feature.Feature) GeoJSONFeature(org.geotoolkit.internal.geojson.binding.GeoJSONFeature)

Example 2 with GeoJSONFeature

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

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

the class GeoJSONReader method fillFeature.

/**
 * Recursively fill a ComplexAttribute with properties map
 *
 * @param feature
 * @param properties
 */
private void fillFeature(Feature feature, Map<String, Object> properties) throws BackingStoreException {
    final FeatureType featureType = feature.getType();
    for (final PropertyType type : featureType.getProperties(true)) {
        final String attName = type.getName().toString();
        final Object value = properties.get(attName);
        if (value == null) {
            continue;
        }
        if (type instanceof FeatureAssociationRole) {
            final FeatureAssociationRole asso = (FeatureAssociationRole) type;
            final FeatureType assoType = asso.getValueType();
            final Class<?> valueClass = value.getClass();
            if (valueClass.isArray()) {
                Class<?> base = value.getClass().getComponentType();
                if (!Map.class.isAssignableFrom(base)) {
                    LOGGER.log(Level.WARNING, "Invalid complex property value " + value);
                }
                final int size = Array.getLength(value);
                if (size > 0) {
                    // list of objects
                    final List<Feature> subs = new ArrayList<>();
                    for (int i = 0; i < size; i++) {
                        Object subValue = Array.get(value, i);
                        final Feature subComplexAttribute;
                        if (subValue instanceof Map) {
                            subComplexAttribute = assoType.newInstance();
                            fillFeature(subComplexAttribute, (Map) Array.get(value, i));
                        } else if (subValue instanceof GeoJSONFeature) {
                            subComplexAttribute = toFeature((GeoJSONFeature) subValue, assoType);
                        } else {
                            throw new IllegalArgumentException("Sub value must be a GeoJSONFeature or a map");
                        }
                        subs.add(subComplexAttribute);
                    }
                    feature.setPropertyValue(attName, subs);
                }
            } else if (value instanceof Map) {
                final Feature subComplexAttribute = assoType.newInstance();
                fillFeature(subComplexAttribute, (Map) value);
                feature.setPropertyValue(attName, subComplexAttribute);
            } else if (value instanceof GeoJSONFeature) {
                final Feature subComplexAttribute = toFeature((GeoJSONFeature) value, assoType);
                feature.setPropertyValue(attName, subComplexAttribute);
            } else if (value instanceof GeoJSONFeatureCollection) {
                GeoJSONFeatureCollection collection = (GeoJSONFeatureCollection) value;
                final List<Feature> subFeatures = new ArrayList<>();
                for (GeoJSONFeature subFeature : collection.getFeatures()) {
                    subFeatures.add(toFeature(subFeature, assoType));
                }
                feature.setPropertyValue(attName, subFeatures);
            } else {
                LOGGER.warning("Unexpected attribute value type:" + value.getClass());
            }
        } else if (type instanceof AttributeType) {
            final Attribute<?> property = (Attribute<?>) feature.getProperty(type.getName().toString());
            fillProperty(property, value);
        }
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) Attribute(org.opengis.feature.Attribute) GeoJSONFeature(org.geotoolkit.internal.geojson.binding.GeoJSONFeature) ArrayList(java.util.ArrayList) PropertyType(org.opengis.feature.PropertyType) Feature(org.opengis.feature.Feature) GeoJSONFeature(org.geotoolkit.internal.geojson.binding.GeoJSONFeature) GeoJSONFeatureCollection(org.geotoolkit.internal.geojson.binding.GeoJSONFeatureCollection) AttributeType(org.opengis.feature.AttributeType) GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) ArrayList(java.util.ArrayList) List(java.util.List) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole) HashMap(java.util.HashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap)

Example 4 with GeoJSONFeature

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

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

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