Search in sources :

Example 36 with AttributeType

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

the class FeatureExt method toProperty.

/**
 * Build a {@link Property} from a {@link ParameterValue}.
 * @param parameter {@link ParameterValue}
 * @return a {@link Property}
 */
public static Property toProperty(final ParameterValue parameter) {
    final ParameterDescriptor descriptor = parameter.getDescriptor();
    final Object value = parameter.getValue();
    final AttributeType at = (AttributeType) FeatureTypeExt.toPropertyType(descriptor);
    final Attribute property = at.newInstance();
    property.setValue(value);
    return property;
}
Also used : Attribute(org.opengis.feature.Attribute) DefaultAttributeType(org.apache.sis.feature.DefaultAttributeType) AttributeType(org.opengis.feature.AttributeType) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) ParameterDescriptor(org.opengis.parameter.ParameterDescriptor)

Example 37 with AttributeType

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

the class SQLQueryBuilder method updateSQL.

/**
 * Generates an 'UPDATE' sql statement.
 */
public String updateSQL(final FeatureType featureType, final Map<String, ? extends Object> changes, Filter filter) throws DataStoreException, SQLException {
    final StringBuilder sql = new StringBuilder();
    sql.append("UPDATE ");
    dialect.encodeSchemaAndTableName(sql, databaseSchema, featureType.getName().tip().toString());
    sql.append(" SET ");
    for (final Map.Entry<String, ? extends Object> change : changes.entrySet()) {
        final String attributeName = change.getKey();
        final AttributeType attribute = (AttributeType) featureType.getProperty(attributeName);
        final Object value = change.getValue();
        dialect.encodeColumnName(sql, attributeName);
        sql.append('=');
        final Class valueClass = attribute.getValueClass();
        if (Geometry.class.isAssignableFrom(valueClass)) {
            final Geometry g = (Geometry) value;
            final int srid = getGeometrySRID(g, attribute);
            dialect.encodeGeometryValue(sql, g, srid);
        } else {
            dialect.encodeValue(sql, value, valueClass);
        }
        sql.append(',');
    }
    sql.setLength(sql.length() - 1);
    sql.append(' ');
    if (filter != null && !Filter.include().equals(filter)) {
        // replace any PropertyEqualsTo in true ID filters
        filter = (Filter) FIDFixVisitor.INSTANCE.visit(filter);
        sql.append(" WHERE ");
        sql.append(dialect.encodeFilter(filter, featureType));
    }
    return sql.toString();
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) AttributeType(org.opengis.feature.AttributeType) Map(java.util.Map)

Example 38 with AttributeType

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

the class SQLQueryBuilder method createTableSQL.

/**
 * Generates a 'CREATE TABLE' sql statement.
 */
public String createTableSQL(final FeatureType featureType, final Connection cx) throws SQLException {
    // figure out the names and types of the columns
    final String tableName = featureType.getName().tip().toString();
    final List<? extends PropertyType> descs = new ArrayList<>(featureType.getProperties(true));
    // remove convention fields, we do not save them
    for (int i = descs.size() - 1; i >= 0; i--) {
        if (AttributeConvention.contains(descs.get(i).getName())) {
            descs.remove(i);
        }
    }
    final int size = descs.size();
    final String[] columnNames = new String[size];
    final Class[] classes = new Class[size];
    final boolean[] nillable = new boolean[size];
    final List<String> pkeyColumn = new ArrayList<>();
    for (int i = 0; i < size; i++) {
        PropertyType desc = descs.get(i);
        columnNames[i] = desc.getName().tip().toString();
        while (desc instanceof Operation) {
            desc = (PropertyType) ((Operation) desc).getResult();
        }
        if (desc instanceof AttributeType) {
            final AttributeType attType = (AttributeType) desc;
            classes[i] = attType.getValueClass();
            nillable[i] = attType.getMinimumOccurs() <= 0;
        } else {
            throw new SQLException("Property type not supported : " + desc);
        }
        if (FeatureTypeExt.isPartOfPrimaryKey(featureType, desc.getName().toString())) {
            pkeyColumn.add(desc.getName().tip().toString());
        }
    }
    final String[] sqlTypeNames = getSQLTypeNames(classes, cx);
    for (int i = 0; i < sqlTypeNames.length; i++) {
        if (sqlTypeNames[i] == null) {
            throw new SQLException("Unable to map " + columnNames[i] + "( " + classes[i].getName() + ")");
        }
    }
    // build the create table sql -------------------------------------------
    final StringBuilder sql = new StringBuilder();
    sql.append("CREATE TABLE ");
    dialect.encodeSchemaAndTableName(sql, databaseSchema, tableName);
    sql.append(" ( ");
    if (pkeyColumn.isEmpty()) {
        // we create a primary key, this will modify the geature type but
        // we don't have any other solution
        dialect.encodeColumnName(sql, "fid");
        dialect.encodePrimaryKey(sql, Integer.class, "INTEGER");
        sql.append(", ");
    }
    // normal attributes
    for (int i = 0; i < columnNames.length; i++) {
        final AttributeType att = (AttributeType) featureType.getProperty(columnNames[i]);
        // the column name
        dialect.encodeColumnName(sql, columnNames[i]);
        sql.append(' ');
        if (pkeyColumn.contains(columnNames[i])) {
            dialect.encodePrimaryKey(sql, att.getValueClass(), sqlTypeNames[i]);
        } else if (sqlTypeNames[i].toUpperCase().startsWith("VARCHAR")) {
            Integer length = findVarcharColumnLength(att);
            if (length == null || length < 0) {
                length = 255;
            }
            dialect.encodeColumnType(sql, sqlTypeNames[i], length);
        } else {
            dialect.encodeColumnType(sql, sqlTypeNames[i], null);
        }
        // nullable
        if (nillable != null && !nillable[i]) {
            sql.append(" NOT NULL ");
        }
        // delegate to dialect to encode column postamble
        dialect.encodePostColumnCreateTable(sql, att);
        // sql.append(sqlTypeNames[i]);
        if (i < (sqlTypeNames.length - 1)) {
            sql.append(", ");
        }
    }
    sql.append(" ) ");
    // encode anything post create table
    dialect.encodePostCreateTable(sql, tableName);
    return sql.toString();
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PropertyType(org.opengis.feature.PropertyType) Operation(org.opengis.feature.Operation) DBRelationOperation(org.geotoolkit.db.DBRelationOperation) AttributeType(org.opengis.feature.AttributeType)

Example 39 with AttributeType

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

the class UnionProcess method unionFeatureToFC.

/**
 * Generate an union FeatureCollection comparing a Feature to a FeatureCollection.
 * During the second pass, we remove duplicates Features
 *
 * @param newFeatureType the new FeatureType
 * @param unionFC union FeatureCollection
 * @param inputGeomName attribute name of the used Geometry from inputFeature
 * @param unionGeomName attribute name of the used Geometry from unionFC
 * @param featureList Set of already created Features (it's used in order to remove duplicate Features)
 * @return the result FeatureCollection of an union between a Feature and a FeatureCollection
 */
static FeatureCollection unionFeatureToFC(final Feature inputFeature, final FeatureType newFeatureType, final FeatureCollection unionFC, final String inputGeomName, final String unionGeomName, final boolean firstPass, final Set<String> featureList) throws TransformException, FactoryException {
    final FeatureCollection resultFeatureList = FeatureStoreUtilities.collection(FeatureExt.getId(inputFeature).getIdentifier(), newFeatureType);
    /*
         * In order to get all part of Feature, add a second pass with the diffenrence between the FeatureGeometry
         * and united intersections. if return nothing we have all the geometry feature, else we add the difference
         */
    Geometry inputGeometry = org.geotoolkit.geometry.jts.JTS.getFactory().buildGeometry(Collections.EMPTY_LIST);
    for (final PropertyType inputProperty : inputFeature.getType().getProperties(true)) {
        if (AttributeConvention.isGeometryAttribute(inputProperty)) {
            final String name = inputProperty.getName().toString();
            if (name.equals(inputGeomName)) {
                inputGeometry = (Geometry) inputFeature.getPropertyValue(name);
            }
        }
    }
    Geometry remainingGeometry = inputGeometry;
    boolean isIntersected = false;
    // Check if each union Features intersect inputFeature. if yes, create a new Feature which is union of both
    try (final FeatureIterator unionIter = unionFC.iterator()) {
        while (unionIter.hasNext()) {
            final Feature unionFeature = unionIter.next();
            final String featureID;
            // Invert ID order for the second pass (firstpass "inputID U unionID", second pass "unionID U inputID")
            if (firstPass) {
                featureID = FeatureExt.getId(inputFeature).getIdentifier() + "-" + FeatureExt.getId(unionFeature).getIdentifier();
            } else {
                featureID = FeatureExt.getId(unionFeature).getIdentifier() + "-" + FeatureExt.getId(inputFeature).getIdentifier();
            }
            final Feature resultFeature = unionFeatureToFeature(inputFeature, unionFeature, newFeatureType, inputGeomName, unionGeomName, featureID, firstPass);
            // Else we add the resutl Feature to resultFeatureList
            if (resultFeature != null) {
                isIntersected = true;
                resultFeatureList.add(resultFeature);
                Geometry intersectGeom = (Geometry) resultFeature.getPropertyValue(AttributeConvention.GEOMETRY);
                remainingGeometry = remainingGeometry.difference(intersectGeom);
            }
        }
    }
    // If remaining Geometry is empty and isIntersecting boolean is false, mean the geometry
    if (remainingGeometry.isEmpty() && !isIntersected) {
        final Feature remainingFeature = newFeatureType.newInstance();
        FeatureExt.setId(remainingFeature, FeatureExt.getId(inputFeature));
        // Copy none Geometry attributes
        for (final PropertyType inputProperty : inputFeature.getType().getProperties(true)) {
            if (!(inputProperty instanceof AttributeType) || AttributeConvention.contains(inputProperty.getName()))
                continue;
            if (!AttributeConvention.isGeometryAttribute(inputProperty)) {
                final String name = inputProperty.getName().toString();
                remainingFeature.setPropertyValue(name, inputFeature.getPropertyValue(name));
            }
        }
        if (firstPass) {
            remainingFeature.setPropertyValue(inputGeomName, inputGeometry);
        } else {
            remainingFeature.setPropertyValue(unionGeomName, inputGeometry);
        }
        resultFeatureList.add(remainingFeature);
    }
    // Create a remaining Feature with the inputGeometry
    if (!(remainingGeometry.isEmpty())) {
        if (remainingGeometry.equalsTopo(inputGeometry)) {
            remainingGeometry = inputGeometry;
        }
        final Feature remainingFeature = newFeatureType.newInstance();
        FeatureExt.setId(remainingFeature, FeatureExt.getId(inputFeature));
        // Copy none Geometry attributes
        for (final PropertyType inputProperty : inputFeature.getType().getProperties(true)) {
            if (!(inputProperty instanceof AttributeType) || AttributeConvention.contains(inputProperty.getName()))
                continue;
            if (!AttributeConvention.isGeometryAttribute(inputProperty)) {
                final String name = inputProperty.getName().toString();
                remainingFeature.setPropertyValue(name, inputFeature.getPropertyValue(name));
            }
        }
        // System.out.println("LOG Empty remaining Geometry");
        if (firstPass) {
            remainingFeature.setPropertyValue(inputGeomName, remainingGeometry);
        } else {
            remainingFeature.setPropertyValue(unionGeomName, remainingGeometry);
        }
        resultFeatureList.add(remainingFeature);
    }
    final Collection<Feature> featureToRemove = new ArrayList<>();
    /* Check if created features are already present in featureList.
         * If yes, we delete them from the returning FeatureCollection
         * else we add them into the featureList
         */
    for (final Feature createdFeature : resultFeatureList) {
        final String createdFeatureID = FeatureExt.getId(createdFeature).getIdentifier();
        if (featureList.contains(createdFeatureID)) {
            featureToRemove.add(createdFeature);
        } else {
            featureList.add(createdFeatureID);
        }
    }
    // remove existing feature
    resultFeatureList.removeAll(featureToRemove);
    return resultFeatureList;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) AttributeType(org.opengis.feature.AttributeType) ArrayList(java.util.ArrayList) PropertyType(org.opengis.feature.PropertyType) Feature(org.opengis.feature.Feature)

Example 40 with AttributeType

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

the class ClipProcess method clipFeature.

/**
 * Clip a feature with the FeatureCollection's geometries
 *
 * @param newType the new FeatureType for the Feature
 * @param featureClippingList FeatureCollection used to clip
 */
public static Feature clipFeature(final Feature oldFeature, final FeatureType newType, final FeatureCollection featureClippingList) throws FactoryException, MismatchedDimensionException, TransformException {
    final Feature resultFeature = newType.newInstance();
    FeatureExt.setId(resultFeature, FeatureExt.getId(oldFeature));
    for (final PropertyType property : oldFeature.getType().getProperties(true)) {
        final String name = property.getName().toString();
        final Object value = oldFeature.getPropertyValue(name);
        // for each Geometry in the oldFeature
        if (AttributeConvention.isGeometryAttribute(property)) {
            final Geometry inputGeom = (Geometry) value;
            final CoordinateReferenceSystem inputGeomCRS = FeatureExt.getCRS(property);
            // loop and test intersection between each geometry of each clipping feature from
            // clipping FeatureCollection
            final List<Geometry> bufferInterGeometries = new ArrayList<>();
            try (final FeatureIterator clipIterator = featureClippingList.iterator()) {
                while (clipIterator.hasNext()) {
                    final Feature clipFeature = clipIterator.next();
                    for (PropertyType clipFeatureProperty : clipFeature.getType().getProperties(true)) {
                        if (AttributeConvention.isGeometryAttribute(clipFeatureProperty)) {
                            Geometry clipGeom = (Geometry) clipFeature.getPropertyValue(clipFeatureProperty.getName().toString());
                            final CoordinateReferenceSystem clipGeomCRS = FeatureExt.getCRS(clipFeatureProperty);
                            // re-project clipping geometry into input Feature geometry CRS
                            clipGeom = VectorProcessUtils.repojectGeometry(inputGeomCRS, clipGeomCRS, clipGeom);
                            final Geometry interGeometry = VectorProcessUtils.geometryIntersection(inputGeom, clipGeom);
                            // if an intersection geometry exist, store it into a buffer Collection
                            if (interGeometry != null) {
                                bufferInterGeometries.add(interGeometry);
                            }
                        }
                    }
                }
            }
            // if the feature intersect one of the feature clipping list
            final int size = bufferInterGeometries.size();
            if (size == 1) {
                resultFeature.setPropertyValue(name, bufferInterGeometries.get(0));
            } else if (size > 1) {
                final Geometry[] bufferArray = bufferInterGeometries.toArray(new Geometry[bufferInterGeometries.size()]);
                // create a GeometryCollection with all the intersections
                final GeometryCollection resultGeometry = GF.createGeometryCollection(bufferArray);
                resultFeature.setPropertyValue(name, resultGeometry);
            } else {
                return null;
            }
        } else if (property instanceof AttributeType && !(AttributeConvention.contains(property.getName()))) {
            // others properties (no geometry)
            resultFeature.setPropertyValue(name, value);
        }
    }
    return resultFeature;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) AttributeType(org.opengis.feature.AttributeType) ArrayList(java.util.ArrayList) PropertyType(org.opengis.feature.PropertyType) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Feature(org.opengis.feature.Feature)

Aggregations

AttributeType (org.opengis.feature.AttributeType)91 PropertyType (org.opengis.feature.PropertyType)55 FeatureType (org.opengis.feature.FeatureType)33 Feature (org.opengis.feature.Feature)29 Geometry (org.locationtech.jts.geom.Geometry)28 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)23 ArrayList (java.util.ArrayList)22 FeatureAssociationRole (org.opengis.feature.FeatureAssociationRole)22 PropertyNotFoundException (org.opengis.feature.PropertyNotFoundException)19 GenericName (org.opengis.util.GenericName)19 Operation (org.opengis.feature.Operation)16 DataStoreException (org.apache.sis.storage.DataStoreException)14 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)12 IOException (java.io.IOException)11 HashMap (java.util.HashMap)9 Attribute (org.opengis.feature.Attribute)9 Map (java.util.Map)8 DefaultAttributeType (org.apache.sis.feature.DefaultAttributeType)8 FeatureSet (org.apache.sis.storage.FeatureSet)8 Collection (java.util.Collection)7