use of org.apache.sis.feature.DefaultAttributeType in project sis by apache.
the class FeatureTypeBuilderTest method testAddAttribute.
/**
* Tests {@link FeatureTypeBuilder#addAttribute(Class)}.
*/
@Test
@DependsOnMethod("testInitialization")
public void testAddAttribute() {
final FeatureTypeBuilder builder = new FeatureTypeBuilder();
assertSame(builder, builder.setName("myScope", "myName"));
assertSame(builder, builder.setDefinition("test definition"));
assertSame(builder, builder.setDesignation("test designation"));
assertSame(builder, builder.setDescription("test description"));
assertSame(builder, builder.setAbstract(true));
builder.addAttribute(String.class).setName("name");
builder.addAttribute(Integer.class).setName("age");
builder.addAttribute(Point.class).setName("location").setCRS(HardCodedCRS.WGS84);
builder.addAttribute(Double.class).setName("score").setDefaultValue(10.0).setMinimumOccurs(5).setMaximumOccurs(50);
final DefaultFeatureType type = builder.build();
assertEquals("name", "myScope:myName", type.getName().toString());
assertEquals("definition", "test definition", type.getDefinition().toString());
assertEquals("description", "test description", type.getDescription().toString());
assertEquals("designation", "test designation", type.getDesignation().toString());
assertTrue("isAbstract", type.isAbstract());
final Iterator<? extends AbstractIdentifiedType> it = type.getProperties(true).iterator();
final DefaultAttributeType<?> a0 = (DefaultAttributeType<?>) it.next();
final DefaultAttributeType<?> a1 = (DefaultAttributeType<?>) it.next();
final DefaultAttributeType<?> a2 = (DefaultAttributeType<?>) it.next();
final DefaultAttributeType<?> a3 = (DefaultAttributeType<?>) it.next();
assertFalse("properties count", it.hasNext());
assertEquals("name", "name", a0.getName().toString());
assertEquals("name", "age", a1.getName().toString());
assertEquals("name", "location", a2.getName().toString());
assertEquals("name", "score", a3.getName().toString());
assertEquals("valueClass", String.class, a0.getValueClass());
assertEquals("valueClass", Integer.class, a1.getValueClass());
assertEquals("valueClass", Point.class, a2.getValueClass());
assertEquals("valueClass", Double.class, a3.getValueClass());
assertEquals("minimumOccurs", 1, a0.getMinimumOccurs());
assertEquals("minimumOccurs", 1, a1.getMinimumOccurs());
assertEquals("minimumOccurs", 1, a2.getMinimumOccurs());
assertEquals("minimumOccurs", 5, a3.getMinimumOccurs());
assertEquals("maximumOccurs", 1, a0.getMaximumOccurs());
assertEquals("maximumOccurs", 1, a1.getMaximumOccurs());
assertEquals("maximumOccurs", 1, a2.getMaximumOccurs());
assertEquals("maximumOccurs", 50, a3.getMaximumOccurs());
assertEquals("defaultValue", null, a0.getDefaultValue());
assertEquals("defaultValue", null, a1.getDefaultValue());
assertEquals("defaultValue", null, a2.getDefaultValue());
assertEquals("defaultValue", 10.0, a3.getDefaultValue());
assertFalse("characterizedByCRS", AttributeConvention.characterizedByCRS(a0));
assertFalse("characterizedByCRS", AttributeConvention.characterizedByCRS(a1));
assertTrue("characterizedByCRS", AttributeConvention.characterizedByCRS(a2));
assertFalse("characterizedByCRS", AttributeConvention.characterizedByCRS(a3));
}
use of org.apache.sis.feature.DefaultAttributeType in project sis by apache.
the class AttributeConventionTest method testGetCrsCharacteristic.
/**
* Tests {@code AttributeConvention.characterizedByCRS(IdentifiedType)} and
* {@code AttributeConvention.getCRSCharacteristic(Property)} methods.
*/
@Test
public void testGetCrsCharacteristic() {
final Map<String, ?> properties = Collections.singletonMap(DefaultAttributeType.NAME_KEY, "geometry");
DefaultAttributeType<Point> type = new DefaultAttributeType<>(properties, Point.class, 1, 1, null);
assertFalse("characterizedByCRS", AttributeConvention.characterizedByCRS(type));
assertNull("getCRSCharacteristic", AttributeConvention.getCRSCharacteristic(type.newInstance()));
/*
* Creates an attribute associated to an attribute (i.e. a "characteristic") for storing
* the Coordinate Reference System of the "geometry" attribute. Then test again.
*/
final DefaultAttributeType<CoordinateReferenceSystem> characteristic = new DefaultAttributeType<>(Collections.singletonMap(DefaultAttributeType.NAME_KEY, AttributeConvention.CRS_CHARACTERISTIC), CoordinateReferenceSystem.class, 1, 1, HardCodedCRS.WGS84);
type = new DefaultAttributeType<>(properties, Point.class, 1, 1, null, characteristic);
assertTrue("characterizedByCRS", AttributeConvention.characterizedByCRS(type));
assertEquals(HardCodedCRS.WGS84, AttributeConvention.getCRSCharacteristic(type.newInstance()));
assertEquals(HardCodedCRS.WGS84, AttributeConvention.getCRSCharacteristic(null, type));
/*
* Test again AttributeConvention.getCRSCharacteristic(…, PropertyType), but following link.
*/
final AbstractOperation link = FeatureOperations.link(Collections.singletonMap(DefaultAttributeType.NAME_KEY, "geom"), type);
final DefaultFeatureType feat = new DefaultFeatureType(Collections.singletonMap(DefaultAttributeType.NAME_KEY, "feat"), false, null, type, link);
assertEquals(HardCodedCRS.WGS84, AttributeConvention.getCRSCharacteristic(feat, link));
assertNull(AttributeConvention.getCRSCharacteristic(null, link));
}
use of org.apache.sis.feature.DefaultAttributeType in project sis by apache.
the class FeatureTypeBuilder method initialize.
/**
* Initializes this builder to the value of the given type.
* The caller is responsible to invoke {@link #clear()} (if needed) before this method.
*/
private void initialize(final DefaultFeatureType template) {
super.initialize(template);
feature = template;
isAbstract = template.isAbstract();
superTypes.addAll(template.getSuperTypes());
/*
* For each attribute and association, wrap those properties in a builder.
* For each operation, wrap them in pseudo-builder only if the operation
* is not one of the operations automatically generated by this builder.
*/
final Map<String, Set<AttributeRole>> propertyRoles = new HashMap<>();
for (final AbstractIdentifiedType property : template.getProperties(false)) {
PropertyTypeBuilder builder;
if (property instanceof DefaultAttributeType<?>) {
builder = new AttributeTypeBuilder<>(this, (DefaultAttributeType<?>) property);
} else if (property instanceof DefaultAssociationRole) {
builder = new AssociationRoleBuilder(this, (DefaultAssociationRole) property);
} else {
// Do not create OperationWrapper now - see below.
builder = null;
}
/*
* If the property name is one of our (Apache SIS specific) conventional names, try to reconstitute
* the attribute roles that caused FeatureTypeBuilder to produce such property. Those roles usually
* need to be applied on the source properties used for calculating the current property. There is
* usually at most one role for each source property, but we nevertheless allow an arbitrary amount.
*/
final AttributeRole role;
final GenericName name = property.getName();
if (AttributeConvention.IDENTIFIER_PROPERTY.equals(name)) {
role = AttributeRole.IDENTIFIER_COMPONENT;
} else if (AttributeConvention.GEOMETRY_PROPERTY.equals(name)) {
role = AttributeRole.DEFAULT_GEOMETRY;
} else if (AttributeConvention.ENVELOPE_PROPERTY.equals(name)) {
// If "sis:envelope" is an operation, skip it completely.
// It will be recreated if a default geometry exists.
role = null;
} else {
if (builder == null) {
// For all unknown operation, wrap as-is.
builder = new OperationWrapper(this, property);
}
role = null;
}
if (role != null) {
final Set<AttributeRole> rc = Collections.singleton(role);
if (property instanceof AbstractOperation) {
for (final String dependency : ((AbstractOperation) property).getDependencies()) {
propertyRoles.merge(dependency, rc, AttributeRole::merge);
}
} else {
propertyRoles.merge(name.toString(), rc, AttributeRole::merge);
}
}
if (builder != null) {
properties.add(builder);
}
}
/*
* At this point we finished to collect information about the attribute roles.
* Now assign those roles to the attribute builders. Note that some roles may
* be ignored if we didn't found a suitable builder. The roles inference done
* in this constructor is only a "best effort".
*/
if (!propertyRoles.isEmpty()) {
for (final Map.Entry<String, Set<AttributeRole>> entry : propertyRoles.entrySet()) {
final PropertyTypeBuilder property = forName(properties, entry.getKey());
if (property instanceof AttributeTypeBuilder<?>) {
((AttributeTypeBuilder<?>) property).roles().addAll(entry.getValue());
}
}
}
}
Aggregations