Search in sources :

Example 11 with PropertyNotFoundException

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

the class FeatureExt method getDefaultGeometryValue.

/**
 * Get main geometry property value. The ways this method determines default
 * geometry property are the same as {@link #getDefaultGeometry(org.opengis.feature.FeatureType) }.
 *
 * @param input the feature to extract geometry from.
 * @return Value of the main geometric property of the given feature. The returned
 * optional will be empty only if the feature defines a geometric property, but has
 * no value for it.
 * @throws PropertyNotFoundException If no geometric property is available in
 * the given feature.
 * @throws IllegalStateException If we've found more than one geometry.
 */
public static Optional<Object> getDefaultGeometryValue(Feature input) throws PropertyNotFoundException, IllegalStateException {
    PropertyType geomType = null;
    Object geometry;
    try {
        geometry = input.getPropertyValue(AttributeConvention.GEOMETRY);
    } catch (PropertyNotFoundException ex) {
        try {
            geomType = FeatureExt.getDefaultGeometry(input.getType());
            geometry = input.getPropertyValue(geomType.getName().toString());
        } catch (RuntimeException e) {
            e.addSuppressed(ex);
            throw e;
        }
    }
    if (geometry instanceof Geometry) {
        // fix for bad readers who do not have crs set on geometries
        Geometry g = (Geometry) geometry;
        CoordinateReferenceSystem crs = Geometries.wrap(geometry).get().getCoordinateReferenceSystem();
        if (crs == null) {
            if (geomType == null) {
                crs = getCRS(input.getType().getProperty(AttributeConvention.GEOMETRY));
            } else {
                crs = getCRS(geomType);
            }
            if (crs != null) {
                g.setUserData(crs);
            }
        }
    }
    return Optional.ofNullable(geometry);
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) PropertyType(org.opengis.feature.PropertyType) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 12 with PropertyNotFoundException

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

the class DataBaseModel method analyzeResult.

/**
 * Analyze the metadata of the ResultSet to rebuild a feature type.
 */
public FeatureType analyzeResult(final ResultSet result, final String name) throws SQLException, DataStoreException {
    final SQLDialect dialect = store.getDialect();
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName(name);
    final ResultSetMetaData metadata = result.getMetaData();
    final int nbcol = metadata.getColumnCount();
    for (int i = 1; i <= nbcol; i++) {
        final String columnName = metadata.getColumnName(i);
        final String columnLabel = metadata.getColumnLabel(i);
        final String typeName = metadata.getColumnTypeName(i);
        final String schemaName = metadata.getSchemaName(i);
        final String tableName = metadata.getTableName(i);
        final int sqlType = metadata.getColumnType(i);
        final String sqlTypeName = metadata.getColumnTypeName(i);
        // search if we already have this minute
        PropertyType desc = null;
        final SchemaMetaModel schema = getSchemaMetaModel(schemaName);
        if (schema != null) {
            TableMetaModel table = schema.getTable(tableName);
            if (table != null) {
                try {
                    desc = table.getType(TableMetaModel.View.SIMPLE_FEATURE_TYPE).build().getProperty(columnName);
                } catch (PropertyNotFoundException ex) {
                }
            }
        }
        if (desc == null) {
            // could not find the original type
            // this column must be calculated
            final SingleAttributeTypeBuilder atb = new SingleAttributeTypeBuilder();
            final int nullable = metadata.isNullable(i);
            atb.setName(ensureGMLNS(columnLabel));
            atb.setMinimumOccurs(nullable == metadata.columnNullable ? 0 : 1);
            atb.setMaximumOccurs(1);
            atb.setName(ensureGMLNS(columnLabel));
            Connection cx = null;
            try {
                cx = store.getDataSource().getConnection();
                final Class type = dialect.getJavaType(sqlType, sqlTypeName);
                if (type.equals(Geometry.class)) {
                    // try to determine the real geometric type
                    dialect.decodeGeometryColumnType(atb, cx, result, i, true);
                } else {
                    // why so this a sencond time ?
                    atb.setName(columnLabel);
                    atb.setValueClass(type);
                }
            } catch (SQLException e) {
                throw new DataStoreException("Error occurred analyzing column : " + columnName, e);
            } finally {
                closeSafe(store.getLogger(), cx);
            }
            desc = atb.build();
        }
        ftb.addProperty(desc);
    }
    return ftb.build();
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) DataStoreException(org.apache.sis.storage.DataStoreException) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PropertyType(org.opengis.feature.PropertyType) ResultSetMetaData(java.sql.ResultSetMetaData) SingleAttributeTypeBuilder(org.geotoolkit.feature.SingleAttributeTypeBuilder) SQLDialect(org.geotoolkit.db.dialect.SQLDialect)

Example 13 with PropertyNotFoundException

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

the class PostgresFilterToSQL method setSRID.

/**
 * Set the current srid, extract it from feature type.
 * Required when encoding geometry.
 */
@Override
protected ValueReference setSRID(ValueReference property) {
    currentsrid = -1;
    if (featureType != null) {
        final AttributeType descriptor;
        final Object propObj = property.apply(featureType);
        if (propObj instanceof Operation) {
            final Operation op = (Operation) propObj;
            descriptor = Features.toAttribute(op).orElse(null);
            if (descriptor != null) {
                try {
                    // throw exception if not found
                    featureType.getProperty(descriptor.getName().tip().toString());
                    property = FilterUtilities.FF.property(descriptor.getName().tip().toString());
                } catch (PropertyNotFoundException ex) {
                    LOGGER.log(Level.FINE, "Unsupported Operation property", ex);
                }
            }
        } else if (propObj instanceof AttributeType) {
            descriptor = (AttributeType) property.apply(featureType);
        } else {
            descriptor = null;
        }
        if (descriptor != null && Geometry.class.isAssignableFrom(descriptor.getValueClass())) {
            Integer srid = (Integer) FeatureExt.getCharacteristicValue(descriptor, JDBCFeatureStore.JDBC_PROPERTY_SRID.getName().toString(), null);
            if (srid != null) {
                currentsrid = srid;
            }
        }
    }
    return property;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) AttributeType(org.opengis.feature.AttributeType) Operation(org.opengis.feature.Operation)

Example 14 with PropertyNotFoundException

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

the class MIFGeometryBuilder method buildType.

/**
 * Build a feature type which represents a MIF geometry.
 *
 * @param crs The CRS to put in feature type. If null, no CRS will be pass to the feature type.
 * @param parent
 * @return A {@link org.geotoolkit.feature.FeatureType} which describe a geometry (as MIF defines it).
 */
public FeatureType buildType(CoordinateReferenceSystem crs, FeatureType parent) {
    FeatureTypeBuilder builder = new FeatureTypeBuilder();
    // As parent's attributes are not shared, we must copy them to the new feature type.
    boolean addGeometry = true;
    final GenericName name;
    if (parent != null) {
        name = getName().push(parent.getName());
        // Check if there's already a geometric property.
        try {
            FeatureExt.getDefaultGeometry(parent);
            addGeometry = false;
        } catch (PropertyNotFoundException e) {
            LOGGER.log(Level.FINEST, "no geometry found in parent data type", e);
        }
        builder.setSuperTypes(parent);
        for (AttributeType desc : getAttributes()) {
            if (!parent.getProperties(true).contains(desc)) {
                builder.addAttribute(desc);
            }
        }
    } else {
        name = getName();
    }
    // If parent type has no geometry, we add one.
    if (addGeometry) {
        builder.addAttribute(getGeometryBinding()).setName(getName()).setCRS(crs).addRole(AttributeRole.DEFAULT_GEOMETRY);
    }
    builder.setName(name);
    return builder.build();
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) GenericName(org.opengis.util.GenericName) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) DefaultAttributeType(org.apache.sis.feature.DefaultAttributeType) AttributeType(org.opengis.feature.AttributeType)

Example 15 with PropertyNotFoundException

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

the class FeatureTypeExt method equalsIgnoreConvention.

/**
 * Test field equality ignoring convention properties.
 */
public static boolean equalsIgnoreConvention(FeatureType type1, FeatureType type2) {
    if (type1 == type2) {
        return true;
    }
    // check base properties
    if (!Objects.equals(type1.getName(), type2.getName()) || !Objects.equals(type1.getDefinition(), type2.getDefinition()) || !Objects.equals(type1.getDesignation(), type2.getDesignation()) || !Objects.equals(type1.getDesignation(), type2.getDesignation()) || !Objects.equals(type1.isAbstract(), type2.isAbstract())) {
        return false;
    }
    // check super types
    final Set<? extends FeatureType> super1 = type1.getSuperTypes();
    final Set<? extends FeatureType> super2 = type2.getSuperTypes();
    if (super1.size() != super2.size())
        return false;
    final Iterator<? extends FeatureType> site1 = super1.iterator();
    final Iterator<? extends FeatureType> site2 = super2.iterator();
    while (site1.hasNext()) {
        if (!equalsIgnoreConvention(site1.next(), site2.next()))
            return false;
    }
    // check properties
    final Set<GenericName> visited = new HashSet<>();
    for (PropertyType pt1 : type1.getProperties(true)) {
        visited.add(pt1.getName());
        if (AttributeConvention.contains(pt1.getName()))
            continue;
        try {
            final PropertyType pt2 = type2.getProperty(pt1.getName().toString());
            if (!equalsIgnoreConvention(pt1, pt2))
                return false;
        } catch (PropertyNotFoundException ex) {
            return false;
        }
    }
    for (PropertyType pt2 : type2.getProperties(true)) {
        if (AttributeConvention.contains(pt2.getName()) || visited.contains(pt2.getName()))
            continue;
        try {
            final PropertyType pt1 = type1.getProperty(pt2.getName().toString());
            if (!equalsIgnoreConvention(pt1, pt2))
                return false;
        } catch (PropertyNotFoundException ex) {
            return false;
        }
    }
    return true;
}
Also used : GenericName(org.opengis.util.GenericName) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) PropertyType(org.opengis.feature.PropertyType) HashSet(java.util.HashSet)

Aggregations

PropertyNotFoundException (org.opengis.feature.PropertyNotFoundException)24 PropertyType (org.opengis.feature.PropertyType)16 AttributeType (org.opengis.feature.AttributeType)13 FeatureType (org.opengis.feature.FeatureType)10 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)8 GenericName (org.opengis.util.GenericName)8 ArrayList (java.util.ArrayList)7 DataStoreException (org.apache.sis.storage.DataStoreException)7 Geometry (org.locationtech.jts.geom.Geometry)7 Feature (org.opengis.feature.Feature)7 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)6 MultiPolygon (org.locationtech.jts.geom.MultiPolygon)5 IOException (java.io.IOException)4 FeatureStoreRuntimeException (org.geotoolkit.storage.feature.FeatureStoreRuntimeException)4 LineString (org.locationtech.jts.geom.LineString)4 MultiLineString (org.locationtech.jts.geom.MultiLineString)4 Polygon (org.locationtech.jts.geom.Polygon)4 Operation (org.opengis.feature.Operation)4 HashSet (java.util.HashSet)3 FeatureSet (org.apache.sis.storage.FeatureSet)3