Search in sources :

Example 1 with FeatureType

use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.

the class KmlFeatureUtilities method BuildSimpleFeature.

/**
 * Build simple feature
 * @param idgeom geometry id
 * @param values no geographic data
 * @param finalGeom geometry need to be insert in feature
 * @return a {@link SimpleFeature}
 */
private static Feature BuildSimpleFeature(int idgeom, Map<String, String> values, Geometry finalGeom) {
    // Building simplefeature
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    final String name = "Geometry";
    ftb.setName(name);
    ftb.addAttribute(Geometry.class).setName("geometry").setCRS(CommonCRS.WGS84.normalizedGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
    // loop on values to find data names
    for (String valName : values.keySet()) {
        ftb.addAttribute(String.class).setName(valName);
    }
    final FeatureType sft = ftb.build();
    final Feature simpleFeature = sft.newInstance();
    simpleFeature.setPropertyValue(AttributeConvention.IDENTIFIER, "feature" + idgeom);
    // add geometry
    simpleFeature.setPropertyValue("geometry", finalGeom);
    // add other data
    for (String valName : values.keySet()) {
        simpleFeature.setPropertyValue(valName, values.get(valName));
    }
    return simpleFeature;
}
Also used : AbstractGeometry(org.geotoolkit.data.kml.model.AbstractGeometry) DefaultMultiGeometry(org.geotoolkit.data.kml.model.DefaultMultiGeometry) Geometry(org.locationtech.jts.geom.Geometry) MultiGeometry(org.geotoolkit.data.kml.model.MultiGeometry) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) Feature(org.opengis.feature.Feature)

Example 2 with FeatureType

use of org.opengis.feature.FeatureType 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 3 with FeatureType

use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.

the class GeoJSONWriter method writeProperties.

/**
 * Write ComplexAttribute.
 *
 * @param edited
 * @param fieldName
 * @param writeFieldName
 * @throws IOException
 * @throws IllegalArgumentException
 */
private void writeProperties(Feature edited, String fieldName, boolean writeFieldName, Set<Feature> alreadyWritten) throws IOException, IllegalArgumentException {
    if (writeFieldName) {
        writer.writeObjectFieldStart(fieldName);
    } else {
        writer.writeStartObject();
    }
    FeatureType type = edited.getType();
    PropertyType defGeom = FeatureExt.getDefaultGeometrySafe(type).flatMap(Features::toAttribute).orElse(null);
    Collection<? extends PropertyType> descriptors = type.getProperties(true).stream().filter(GeoJSONUtils.IS_NOT_CONVENTION).filter(it -> !Objects.equals(defGeom, it)).collect(Collectors.toList());
    for (PropertyType propType : descriptors) {
        final String name = propType.getName().tip().toString();
        final Object value = edited.getPropertyValue(propType.getName().toString());
        if (propType instanceof AttributeType) {
            final AttributeType attType = (AttributeType) propType;
            if (attType.getMaximumOccurs() > 1) {
                writer.writeArrayFieldStart(name);
                for (Object v : (Collection) value) {
                    writeProperty(name, v, false, alreadyWritten);
                }
                writer.writeEndArray();
            } else {
                writeProperty(name, value, true, alreadyWritten);
            }
        } else if (propType instanceof FeatureAssociationRole) {
            final FeatureAssociationRole asso = (FeatureAssociationRole) propType;
            if (asso.getMaximumOccurs() > 1) {
                writer.writeFieldName(name);
                writeFeatureCollection((List<Feature>) value, asso.getValueType());
            } else {
                writeProperty(name, value, true, alreadyWritten);
            }
        } else if (propType instanceof Operation) {
            writeProperty(name, value, true, alreadyWritten);
        }
    }
    writer.writeEndObject();
}
Also used : GeoJSONMultiPoint(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiPoint) GeoJSONParser(org.geotoolkit.internal.geojson.GeoJSONParser) Link(org.geotoolkit.atom.xml.Link) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) TRUNCATE_EXISTING(java.nio.file.StandardOpenOption.TRUNCATE_EXISTING) NumberFormat(java.text.NumberFormat) Envelope(org.opengis.geometry.Envelope) Level(java.util.logging.Level) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) GeoJSONMultiPolygon(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiPolygon) FeatureType(org.opengis.feature.FeatureType) Operation(org.opengis.feature.Operation) JsonEncoding(com.fasterxml.jackson.core.JsonEncoding) Locale(java.util.Locale) GeoJSONMultiLineString(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiLineString) GeoJSONPoint(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONPoint) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole) CommonCRS(org.apache.sis.referencing.CommonCRS) GeoJSONUtils(org.geotoolkit.internal.geojson.GeoJSONUtils) Path(java.nio.file.Path) WRITE(java.nio.file.StandardOpenOption.WRITE) Feature(org.opengis.feature.Feature) Utilities(org.apache.sis.util.Utilities) IdentityHashMap(java.util.IdentityHashMap) Files(java.nio.file.Files) Collection(java.util.Collection) Set(java.util.Set) AttributeType(org.opengis.feature.AttributeType) Attribute(org.opengis.feature.Attribute) Features(org.apache.sis.feature.Features) Collectors(java.util.stream.Collectors) GeoJSONLineString(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONLineString) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) Objects(java.util.Objects) List(java.util.List) JsonFactory(com.fasterxml.jackson.core.JsonFactory) PropertyType(org.opengis.feature.PropertyType) java.io(java.io) AttributeConvention(org.apache.sis.internal.feature.AttributeConvention) CREATE(java.nio.file.StandardOpenOption.CREATE) GeoJSONGeometryCollection(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONGeometryCollection) FeatureExt(org.geotoolkit.feature.FeatureExt) Optional(java.util.Optional) Geometry(org.locationtech.jts.geom.Geometry) GeoJSONPolygon(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONPolygon) GeoJSONConstants(org.geotoolkit.storage.geojson.GeoJSONConstants) Collections(java.util.Collections) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) FeatureType(org.opengis.feature.FeatureType) AttributeType(org.opengis.feature.AttributeType) Collection(java.util.Collection) GeoJSONGeometryCollection(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONGeometryCollection) List(java.util.List) PropertyType(org.opengis.feature.PropertyType) GeoJSONMultiLineString(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiLineString) GeoJSONLineString(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONLineString) Operation(org.opengis.feature.Operation) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole)

Example 4 with FeatureType

use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.

the class Copy method execute.

/**
 *  {@inheritDoc }
 */
@Override
protected void execute() throws ProcessException {
    final FeatureStore sourceDS = inputParameters.getValue(SOURCE_STORE);
    final FeatureStore targetDS = inputParameters.getValue(TARGET_STORE);
    Session targetSS = inputParameters.getValue(TARGET_SESSION);
    final Boolean eraseParam = inputParameters.getValue(ERASE);
    final Boolean newVersion = inputParameters.getValue(NEW_VERSION);
    // Type name can be removed, it's embedded in the query param.
    final String typenameParam = inputParameters.getValue(TYPE_NAME);
    final Query queryParam = inputParameters.getValue(QUERY);
    final boolean doCommit = targetSS == null;
    final Session sourceSS = sourceDS.createSession(false);
    if (targetSS == null) {
        if (targetDS != null) {
            targetSS = targetDS.createSession(true);
        } else {
            throw new ProcessException("Input target_session or target_datastore missing.", this, null);
        }
    }
    boolean reBuildQuery = false;
    final String queryName;
    if (queryParam != null) {
        queryName = queryParam.getTypeName();
        reBuildQuery = true;
    } else if (typenameParam != null) {
        queryName = typenameParam;
    } else {
        queryName = "*";
    }
    final Set<GenericName> names;
    if ("*".equals(queryName)) {
        // all values
        try {
            names = sourceDS.getNames();
        } catch (DataStoreException ex) {
            throw new ProcessException(ex.getMessage(), this, ex);
        }
    } else {
        // pick only the wanted names
        names = new HashSet<>();
        final List<String> wanted = UnmodifiableArrayList.wrap(queryName.split(","));
        for (String s : wanted) {
            try {
                final FeatureType type = sourceDS.getFeatureType(s);
                names.add(type.getName());
            } catch (DataStoreException ex) {
                throw new ProcessException(ex.getMessage(), this, ex);
            }
        }
    }
    final float size = names.size();
    int inc = 0;
    for (GenericName n : names) {
        fireProgressing("Copying " + n + ".", (int) ((inc * 100f) / size), false);
        try {
            Query query;
            if (reBuildQuery) {
                Query builder = new Query();
                builder.copy(queryParam);
                builder.setTypeName(n);
                query = builder;
            } else {
                query = queryParam != null ? queryParam : new Query(n);
            }
            insert(n, sourceSS, targetSS, query, eraseParam, newVersion);
        } catch (DataStoreException ex) {
            throw new ProcessException(ex.getMessage(), this, ex);
        }
        inc++;
    }
    try {
        Date lastVersionDate = null;
        if (doCommit) {
            LOGGER.log(Level.INFO, "Commit all changes");
            targetSS.commit();
            // find last version
            for (GenericName n : names) {
                if (targetSS.getFeatureStore().getQueryCapabilities().handleVersioning()) {
                    final List<Version> versions = targetSS.getFeatureStore().getVersioning(n.toString()).list();
                    if (!versions.isEmpty()) {
                        if (lastVersionDate == null || versions.get(versions.size() - 1).getDate().getTime() > lastVersionDate.getTime()) {
                            lastVersionDate = versions.get(versions.size() - 1).getDate();
                        }
                    }
                }
            }
        }
        if (lastVersionDate != null) {
            outputParameters.getOrCreate(VERSION).setValue(lastVersionDate);
        }
    } catch (DataStoreException ex) {
        throw new ProcessException(ex.getMessage(), this, ex);
    } catch (VersioningException ex) {
        throw new ProcessException(ex.getMessage(), this, ex);
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) DataStoreException(org.apache.sis.storage.DataStoreException) Query(org.geotoolkit.storage.feature.query.Query) Date(java.util.Date) ProcessException(org.geotoolkit.process.ProcessException) GenericName(org.opengis.util.GenericName) Version(org.geotoolkit.version.Version) VersioningException(org.geotoolkit.version.VersioningException) FeatureStore(org.geotoolkit.storage.feature.FeatureStore) Session(org.geotoolkit.storage.feature.session.Session)

Example 5 with FeatureType

use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.

the class ProjectedGeometryTest method createProjectedGeometry.

private static ProjectedGeometry createProjectedGeometry(Geometry geometry, Dimension canvasBounds, AffineTransform objToDisp) throws NoninvertibleTransformException, TransformException, FactoryException {
    final int canvasWidth = canvasBounds.width;
    final int canvasHeight = canvasBounds.height;
    // build a maplayer
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("test");
    ftb.addAttribute(Geometry.class).setName("geom").setCRS(CommonCRS.WGS84.normalizedGeographic());
    final FeatureType type = ftb.build();
    final Feature feature = type.newInstance();
    JTS.setCRS(geometry, CommonCRS.WGS84.normalizedGeographic());
    feature.setPropertyValue("geom", geometry);
    final FeatureSet col = new InMemoryFeatureSet(type, Arrays.asList(feature));
    final List<GraphicalSymbol> symbols = new ArrayList<>();
    symbols.add(SF.mark(StyleConstants.MARK_SQUARE, SF.fill(Color.BLACK), SF.stroke(Color.BLACK, 0)));
    final Graphic graphic = SF.graphic(symbols, StyleConstants.LITERAL_ONE_FLOAT, FF.literal(2), StyleConstants.LITERAL_ZERO_FLOAT, null, null);
    final PointSymbolizer ps = SF.pointSymbolizer(graphic, null);
    final MutableStyle style = SF.style(ps);
    final MapLayer layer = MapBuilder.createLayer(col);
    layer.setStyle(style);
    // build a rendering canvas
    final J2DCanvasBuffered canvas = new J2DCanvasBuffered(CommonCRS.WGS84.normalizedGeographic(), new Dimension(canvasWidth, canvasHeight));
    canvas.applyTransform(objToDisp);
    final RenderingContext2D context = canvas.prepareContext(new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_ARGB).createGraphics());
    final ProjectedGeometry pg = new ProjectedGeometry(context);
    pg.setDataGeometry(geometry, CommonCRS.WGS84.normalizedGeographic());
    Envelope env = canvas.getVisibleEnvelope();
    System.out.println(env.getMinimum(0) + " " + env.getMaximum(0));
    System.out.println(env.getMinimum(1) + " " + env.getMaximum(1));
    return pg;
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) PointSymbolizer(org.opengis.style.PointSymbolizer) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) Graphic(org.opengis.style.Graphic) GraphicalSymbol(org.opengis.style.GraphicalSymbol) MapLayer(org.apache.sis.portrayal.MapLayer) ArrayList(java.util.ArrayList) RenderingContext2D(org.geotoolkit.display2d.canvas.RenderingContext2D) Dimension(java.awt.Dimension) Envelope(org.opengis.geometry.Envelope) Feature(org.opengis.feature.Feature) BufferedImage(java.awt.image.BufferedImage) MutableStyle(org.geotoolkit.style.MutableStyle) J2DCanvasBuffered(org.geotoolkit.display2d.canvas.J2DCanvasBuffered) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) FeatureSet(org.apache.sis.storage.FeatureSet)

Aggregations

FeatureType (org.opengis.feature.FeatureType)303 Feature (org.opengis.feature.Feature)146 Test (org.junit.Test)122 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)100 GenericName (org.opengis.util.GenericName)70 ArrayList (java.util.ArrayList)65 DataStoreException (org.apache.sis.storage.DataStoreException)56 PropertyType (org.opengis.feature.PropertyType)51 Coordinate (org.locationtech.jts.geom.Coordinate)50 Point (org.locationtech.jts.geom.Point)41 FeatureSet (org.apache.sis.storage.FeatureSet)37 Query (org.geotoolkit.storage.feature.query.Query)36 AttributeType (org.opengis.feature.AttributeType)36 FeatureAssociationRole (org.opengis.feature.FeatureAssociationRole)33 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)32 WritableFeatureSet (org.apache.sis.storage.WritableFeatureSet)31 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)27 Geometry (org.locationtech.jts.geom.Geometry)27 URL (java.net.URL)26 HashMap (java.util.HashMap)26