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;
}
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();
}
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;
}
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();
}
}
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();
}
Aggregations