Search in sources :

Example 1 with AbstractOperation

use of org.apache.sis.feature.AbstractOperation 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));
}
Also used : AbstractOperation(org.apache.sis.feature.AbstractOperation) DefaultFeatureType(org.apache.sis.feature.DefaultFeatureType) DefaultAttributeType(org.apache.sis.feature.DefaultAttributeType) Point(com.esri.core.geometry.Point) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Test(org.junit.Test)

Example 2 with AbstractOperation

use of org.apache.sis.feature.AbstractOperation 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());
            }
        }
    }
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) AbstractIdentifiedType(org.apache.sis.feature.AbstractIdentifiedType) GenericName(org.opengis.util.GenericName) DefaultAssociationRole(org.apache.sis.feature.DefaultAssociationRole) AbstractOperation(org.apache.sis.feature.AbstractOperation) DefaultAttributeType(org.apache.sis.feature.DefaultAttributeType) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

AbstractOperation (org.apache.sis.feature.AbstractOperation)2 DefaultAttributeType (org.apache.sis.feature.DefaultAttributeType)2 Point (com.esri.core.geometry.Point)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 AbstractIdentifiedType (org.apache.sis.feature.AbstractIdentifiedType)1 DefaultAssociationRole (org.apache.sis.feature.DefaultAssociationRole)1 DefaultFeatureType (org.apache.sis.feature.DefaultFeatureType)1 Test (org.junit.Test)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1 GenericName (org.opengis.util.GenericName)1