Search in sources :

Example 21 with AttributeType

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

the class GeoJSONWriteTest method copy.

/**
 * @param deep true for a deep copy
 */
private static Feature copy(Feature feature, boolean deep) {
    final FeatureType type = feature.getType();
    final Feature cp = type.newInstance();
    final Collection<? extends PropertyType> props = type.getProperties(true);
    for (PropertyType pt : props) {
        if (pt instanceof AttributeType) {
            final String name = pt.getName().toString();
            final Object val = feature.getPropertyValue(name);
            if (val != null) {
                cp.setPropertyValue(name, deep ? deepCopy(val) : val);
            }
        } else if (pt instanceof FeatureAssociationRole) {
            final String name = pt.getName().toString();
            final Object val = feature.getPropertyValue(name);
            if (deep) {
                if (val != null) {
                    cp.setPropertyValue(name, deepCopy(val));
                }
            } else {
                if (val instanceof Collection) {
                    final Collection col = (Collection) val;
                    final Collection cpCol = new ArrayList(col.size());
                    for (Iterator ite = col.iterator(); ite.hasNext(); ) {
                        cpCol.add(copy((Feature) ite.next()));
                    }
                    cp.setPropertyValue(name, cpCol);
                } else if (val != null) {
                    cp.setPropertyValue(name, copy((Feature) val));
                }
            }
        }
    }
    return cp;
}
Also used : FeatureType(org.opengis.feature.FeatureType) AttributeType(org.opengis.feature.AttributeType) PropertyType(org.opengis.feature.PropertyType) SimpleInternationalString(org.apache.sis.util.SimpleInternationalString) Feature(org.opengis.feature.Feature) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole)

Example 22 with AttributeType

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

the class CSVStore method createHeader.

String createHeader(final FeatureType type) throws DataStoreException {
    final StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (PropertyType desc : type.getProperties(true)) {
        if (AttributeConvention.contains(desc.getName()))
            continue;
        if (first) {
            first = false;
        } else {
            sb.append(separator);
        }
        sb.append(desc.getName().tip());
        sb.append('(');
        final Class clazz = ((AttributeType) desc).getValueClass();
        if (Number.class.isAssignableFrom(clazz) || float.class.equals(clazz) || double.class.equals(clazz) || int.class.equals(clazz) || short.class.equals(clazz) || byte.class.equals(clazz)) {
            sb.append(clazz.getSimpleName());
        } else if (clazz.equals(String.class)) {
            sb.append("String");
        } else if (clazz.equals(Date.class)) {
            sb.append("Date");
        } else if (clazz.equals(Boolean.class)) {
            sb.append("boolean");
        } else if (Geometry.class.isAssignableFrom(clazz)) {
            sb.append(IdentifiedObjects.toString(IdentifiedObjects.getIdentifier(FeatureExt.getCRS(desc), null)));
        } else {
            // unsuported, output it as text
            sb.append("String");
        }
        sb.append(')');
    }
    return sb.toString();
}
Also used : AttributeType(org.opengis.feature.AttributeType) PropertyType(org.opengis.feature.PropertyType)

Example 23 with AttributeType

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

the class DbaseFileHeader method createDescriptors.

/**
 * Create the list of matching attribute descriptor from header informations.
 *
 * @return List of AttributDescriptor
 */
public List<AttributeType> createDescriptors() {
    final int nbFields = getNumFields();
    final SingleAttributeTypeBuilder atb = new SingleAttributeTypeBuilder();
    final List<AttributeType> attributes = new ArrayList<>(nbFields);
    for (int i = 0; i < nbFields; i++) {
        final String name = getFieldName(i);
        final Class attributeClass = getFieldClass(i);
        final int length = getFieldLength(i);
        atb.reset();
        atb.setName(name);
        atb.setValueClass(attributeClass);
        atb.setLength(length);
        attributes.add(atb.build());
    }
    return attributes;
}
Also used : SingleAttributeTypeBuilder(org.geotoolkit.feature.SingleAttributeTypeBuilder) AttributeType(org.opengis.feature.AttributeType) ArrayList(java.util.ArrayList)

Example 24 with AttributeType

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

the class FeatureTypeUtils method writeFeatureType.

/**
 * Write a FeatureType in output File.
 *
 * @param ft
 * @param output
 * @throws IOException
 */
public static void writeFeatureType(FeatureType ft, Path output) throws IOException, DataStoreException {
    ArgumentChecks.ensureNonNull("FeatureType", ft);
    ArgumentChecks.ensureNonNull("outputFile", output);
    Optional<AttributeType<?>> geom = GeoJSONUtils.castOrUnwrap(FeatureExt.getDefaultGeometrySafe(ft));
    try (OutputStream outStream = Files.newOutputStream(output, CREATE, WRITE, TRUNCATE_EXISTING);
        JsonGenerator writer = GeoJSONParser.JSON_FACTORY.createGenerator(outStream, JsonEncoding.UTF8)) {
        writer.useDefaultPrettyPrinter();
        // start write feature collection.
        writer.writeStartObject();
        writer.writeStringField(TITLE, ft.getName().tip().toString());
        writer.writeStringField(TYPE, OBJECT);
        writer.writeStringField(JAVA_TYPE, "FeatureType");
        if (ft.getDescription() != null) {
            writer.writeStringField(DESCRIPTION, ft.getDescription().toString());
        }
        if (geom.isPresent()) {
            writeGeometryType(geom.get(), writer);
        }
        writeProperties(ft, writer);
        writer.writeEndObject();
        writer.flush();
    }
}
Also used : AttributeType(org.opengis.feature.AttributeType) OutputStream(java.io.OutputStream) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 25 with AttributeType

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

the class FeatureTypeUtils method writeProperties.

private static void writeProperties(FeatureType ft, JsonGenerator writer) throws IOException {
    writer.writeObjectFieldStart(PROPERTIES);
    Collection<? extends PropertyType> descriptors = ft.getProperties(true);
    List<String> required = new ArrayList<>();
    for (PropertyType type : descriptors) {
        boolean isRequired = false;
        if (type instanceof FeatureAssociationRole) {
            isRequired = writeComplexType((FeatureAssociationRole) type, ((FeatureAssociationRole) type).getValueType(), writer);
        } else if (type instanceof AttributeType) {
            if (Geometry.class.isAssignableFrom(((AttributeType) type).getValueClass())) {
            // GeometryType geometryType = (GeometryType) type;
            // isRequired = writeGeometryType(descriptor, geometryType, writer);
            } else {
                isRequired = writeAttributeType(ft, (AttributeType) type, writer);
            }
        }
        if (isRequired) {
            required.add(type.getName().tip().toString());
        }
    }
    if (!required.isEmpty()) {
        writer.writeArrayFieldStart(REQUIRED);
        for (String req : required) {
            writer.writeString(req);
        }
        writer.writeEndArray();
    }
    writer.writeEndObject();
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) AttributeType(org.opengis.feature.AttributeType) ArrayList(java.util.ArrayList) SimpleInternationalString(org.apache.sis.util.SimpleInternationalString) InternationalString(org.opengis.util.InternationalString) PropertyType(org.opengis.feature.PropertyType) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole)

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