Search in sources :

Example 11 with Attribute

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

the class FeatureExt method prepareGeometryExtractor.

/**
 * Try to create an operator to extract primary geometry from features of a specific type. This method offers the
 * following advantages:
 * <ul>
 *     <li>When read geometry does not define any CRS, we assign the one extracted from related property type.</li>
 *     <li>Property/characteristic analysis is done on assembly, to ensure minimal overhead on result function execution.</li>
 * </ul>
 *
 * @param targetType Type of the features that will be passed as input to the resulting function. Cannot be null.
 * @return A function for geometry extraction from features whose is or inherits from input type. Never null.
 * If we cannot return a valid value, an error will be thrown as specified by {@link #getDefaultGeometry(FeatureType)}.
 * @throws RuntimeException See {@link #getDefaultGeometry(FeatureType)}.
 */
public static Function<Feature, Geometry> prepareGeometryExtractor(final FeatureType targetType) {
    ensureNonNull("Target type", targetType);
    PropertyType geom = getDefaultGeometry(targetType);
    // Minor optimisation : directly use geometry attribute in case a link convention has been set.
    geom = Features.getLinkTarget(geom).map(name -> targetType.getProperty(name)).orElse(geom);
    final AttributeType<?> attr = Features.toAttribute(geom).orElseThrow(() -> new IllegalStateException("Cannot extract geometries when associate type is not an attribute"));
    final Class<?> vClass = attr.getValueClass();
    if (!Geometry.class.isAssignableFrom(vClass)) {
        throw new UnsupportedOperationException("Only JTS geometries are supported for now.");
    }
    // Name is built from geom, not attr, because attr can be a virtual result property, not present in source type.
    // For example, if you've got two numeric attributes x and y, then add a concatenation operation, you've got no
    // geometric attribute, but a geometric operation.
    final String name = geom.getName().toString();
    final CoordinateReferenceSystem crs = AttributeConvention.getCRSCharacteristic(targetType, attr);
    if (crs == null) {
        return f -> (Geometry) f.getPropertyValue(name);
    } else {
        return f -> {
            final Object value = f.getPropertyValue(name);
            if (value == null)
                return null;
            final Geometry geometry = (Geometry) value;
            final CoordinateReferenceSystem currentCrs;
            try {
                currentCrs = JTS.findCoordinateReferenceSystem(geometry);
            } catch (FactoryException e) {
                throw new BackingStoreException(e);
            }
            if (currentCrs == null)
                JTS.setCRS(geometry, crs);
            return geometry;
        };
    }
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) Arrays(java.util.Arrays) Array(java.lang.reflect.Array) URL(java.net.URL) Date(java.util.Date) BiFunction(java.util.function.BiFunction) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) Envelope(org.opengis.geometry.Envelope) DefaultFactories(org.apache.sis.internal.system.DefaultFactories) FeatureType(org.opengis.feature.FeatureType) IdentifiedType(org.opengis.feature.IdentifiedType) Map(java.util.Map) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole) URI(java.net.URI) NAME_KEY(org.apache.sis.feature.AbstractIdentifiedType.NAME_KEY) ParameterValue(org.opengis.parameter.ParameterValue) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) ArgumentChecks.ensureNonNull(org.apache.sis.util.ArgumentChecks.ensureNonNull) Feature(org.opengis.feature.Feature) NameFactory(org.opengis.util.NameFactory) DefaultAttributeType(org.apache.sis.feature.DefaultAttributeType) Geometries(org.apache.sis.internal.feature.Geometries) Parameters(org.apache.sis.parameter.Parameters) Predicate(java.util.function.Predicate) Collection(java.util.Collection) AttributeType(org.opengis.feature.AttributeType) Attribute(org.opengis.feature.Attribute) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Property(org.opengis.feature.Property) FilterUtilities(org.geotoolkit.filter.FilterUtilities) ArrayFeature(org.geotoolkit.internal.feature.ArrayFeature) List(java.util.List) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) Entry(java.util.Map.Entry) GeneralParameterValue(org.opengis.parameter.GeneralParameterValue) Optional(java.util.Optional) Geometry(org.locationtech.jts.geom.Geometry) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) FactoryException(org.opengis.util.FactoryException) Static(org.apache.sis.util.Static) DefaultNameSpace(org.apache.sis.util.iso.DefaultNameSpace) HashMap(java.util.HashMap) FeatureAssociation(org.opengis.feature.FeatureAssociation) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) DefaultFeatureType(org.apache.sis.feature.DefaultFeatureType) Operation(org.opengis.feature.Operation) ParameterDescriptorGroup(org.opengis.parameter.ParameterDescriptorGroup) BackingStoreException(org.apache.sis.util.collection.BackingStoreException) UID(java.rmi.server.UID) FeatureLoop(org.geotoolkit.internal.feature.FeatureLoop) ResourceId(org.opengis.filter.ResourceId) Iterator(java.util.Iterator) JTS(org.geotoolkit.geometry.jts.JTS) Features(org.apache.sis.feature.Features) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) GenericName(org.opengis.util.GenericName) PropertyType(org.opengis.feature.PropertyType) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) AttributeConvention(org.apache.sis.internal.feature.AttributeConvention) ObjectConverters(org.apache.sis.util.ObjectConverters) Collections(java.util.Collections) ParameterDescriptor(org.opengis.parameter.ParameterDescriptor) FactoryException(org.opengis.util.FactoryException) BackingStoreException(org.apache.sis.util.collection.BackingStoreException) PropertyType(org.opengis.feature.PropertyType) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 12 with Attribute

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

the class PatternOperation method apply.

@Override
public Attribute apply(Feature feature, ParameterValueGroup parameters) {
    final Attribute att = RESULTTYPE.newInstance();
    final Object[] values = new Object[refs.length];
    for (int i = 0; i < refs.length; i++) values[i] = feature.getPropertyValue(refs[i].toString());
    att.setValue(invoke(values));
    return att;
}
Also used : Attribute(org.opengis.feature.Attribute)

Example 13 with Attribute

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

the class XPathBinding method get.

// @Override
// public boolean canHandle(final Class type, final String xpath, final Class target) {
// 
// if(xpath == null || xpath.isEmpty()){
// return false;
// }
// 
// if (!ComplexAttribute.class.isAssignableFrom(type)
// && !PropertyType.class.isAssignableFrom(type)
// && !PropertyDescriptor.class.isAssignableFrom(type)) {
// return false; // we only work with complex types.
// }
// 
// return true;
// }
@Override
public <T> T get(C candidate, String path, Class<T> target) throws IllegalArgumentException {
    if (candidate == null)
        return null;
    try {
        final JaxenFeatureXPath xpath = JaxenFeatureXPath.create(path);
        Object v = xpath.evaluate(candidate);
        if (v instanceof Fake) {
            v = ((Fake) v).value;
        }
        if (v instanceof Collection) {
            // several property for this path
            final Collection properties = (Collection) v;
            if (target != null && target.isInstance(properties)) {
                return (T) properties;
            } else {
                final Iterator ite = properties.iterator();
                if (ite.hasNext()) {
                    v = ite.next();
                }
            }
        }
        if (v instanceof Fake && Property.class.isAssignableFrom(target)) {
            v = ((Fake) v).value;
        }
        if (v instanceof Property) {
            // extract value from property if necessary
            final Property prop = (Property) v;
            if (target != null && target.isInstance(prop)) {
                return (T) prop;
            } else {
                if (prop instanceof Attribute && ((Attribute) prop).getType().getMaximumOccurs() > 1) {
                    v = ((Attribute) prop).getValues();
                } else if (prop instanceof FeatureAssociation && ((FeatureAssociation) prop).getRole().getMaximumOccurs() > 1) {
                    v = ((FeatureAssociation) prop).getValues();
                } else {
                    v = prop.getValue();
                }
            }
        }
        if (target == null) {
            return (T) v;
        } else {
            return ObjectConverters.convert(v, target);
        }
    } catch (JaxenException ex) {
        Logger.getLogger("org.geotoolkit.filter.binding").log(Level.WARNING, null, ex);
    }
    return null;
}
Also used : FeatureAssociation(org.opengis.feature.FeatureAssociation) Fake(org.geotoolkit.filter.binding.JaxenFeatureNavigator.Fake) Attribute(org.opengis.feature.Attribute) JaxenException(org.jaxen.JaxenException) Iterator(java.util.Iterator) Collection(java.util.Collection) Property(org.opengis.feature.Property)

Example 14 with Attribute

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

the class GeometryMapping method readValue.

@Override
public void readValue(XMLStreamReader reader, GenericName propName, Feature feature) throws XMLStreamException {
    final String localName = reader.getLocalName();
    if (decorated) {
        // check if we are dealing with a link href
        String link = reader.getAttributeValue(GMLConvention.XLINK_NAMESPACE, "href");
        if (link != null) {
            toTagEnd(reader, localName);
            Attribute attribute = (Attribute) feature.getProperty(propName.toString());
            AttributeType<String> charType = (AttributeType) attribute.getType().characteristics().get(GMLConvention.XLINK_HREF.tip().toString());
            Attribute<String> charValue = charType.newInstance();
            charValue.setValue(link);
            attribute.characteristics().put(GMLConvention.XLINK_HREF.tip().toString(), charValue);
            return;
        }
    }
    boolean skipCurrent = decorated;
    int event;
    Object value;
    // backward compatible with incorrect old writings
    final String propertyName = propertyType.getName().tip().toString();
    if (propertyName.equals(localName)) {
        skipCurrent = true;
    }
    // special case for SurfacePropertyType which may contain a simple polygon
    boolean forceMultiPolygon = propertyName.equalsIgnoreCase("multipolygon");
    if (skipCurrent) {
        event = reader.next();
    } else {
        event = reader.getEventType();
    }
    while (event != START_ELEMENT) {
        if (event == END_ELEMENT) {
            return;
        }
        event = reader.next();
    }
    try {
        Unmarshaller unmarshaller = pool.acquireUnmarshaller();
        final Geometry jtsGeom;
        final Object geometry = ((JAXBElement) unmarshaller.unmarshal(reader)).getValue();
        if (geometry instanceof JTSGeometry) {
            final JTSGeometry isoGeom = (JTSGeometry) geometry;
            if (isoGeom instanceof JTSMultiCurve) {
                ((JTSMultiCurve) isoGeom).applyCRSonChild();
            }
            jtsGeom = isoGeom.getJTSGeometry();
        } else if (geometry instanceof PolygonType) {
            final PolygonType polygon = ((PolygonType) geometry);
            jtsGeom = polygon.getJTSPolygon().getJTSGeometry();
            if (polygon.getCoordinateReferenceSystem() != null) {
                JTS.setCRS(jtsGeom, polygon.getCoordinateReferenceSystem());
            }
        } else if (geometry instanceof LineStringPosListType) {
            final JTSLineString line = ((LineStringPosListType) geometry).getJTSLineString();
            jtsGeom = line.getJTSGeometry();
            if (line.getCoordinateReferenceSystem() != null) {
                JTS.setCRS(jtsGeom, line.getCoordinateReferenceSystem());
            }
        } else if (geometry instanceof AbstractGeometry) {
            try {
                jtsGeom = GeometrytoJTS.toJTS((AbstractGeometry) geometry, longitudeFirst, forceMultiPolygon);
            } catch (FactoryException ex) {
                throw new XMLStreamException("Factory Exception while transforming GML object to JTS", ex);
            }
        } else {
            throw new IllegalArgumentException("unexpected geometry type:" + geometry);
        }
        value = jtsGeom;
        value = JTSMapping.convertType(jtsGeom, ((AttributeType) propertyType).getValueClass());
        pool.recycle(unmarshaller);
    } catch (JAXBException ex) {
        String msg = ex.getMessage();
        if (msg == null && ex.getLinkedException() != null) {
            msg = ex.getLinkedException().getMessage();
        }
        throw new IllegalArgumentException("JAXB exception while reading the feature geometry: " + msg, ex);
    }
    JAXPStreamFeatureReader.setValue(feature, propertyType, propName, null, value);
}
Also used : AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) Attribute(org.opengis.feature.Attribute) FactoryException(org.opengis.util.FactoryException) JAXBException(javax.xml.bind.JAXBException) PolygonType(org.geotoolkit.internal.jaxb.PolygonType) JTSLineString(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSLineString) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) JAXBElement(javax.xml.bind.JAXBElement) JTSMultiCurve(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.aggregate.JTSMultiCurve) LineStringPosListType(org.geotoolkit.internal.jaxb.LineStringPosListType) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint) JTSLineString(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSLineString) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) JTSGeometry(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.JTSGeometry) Geometry(org.locationtech.jts.geom.Geometry) XMLStreamException(javax.xml.stream.XMLStreamException) AttributeType(org.opengis.feature.AttributeType) Unmarshaller(javax.xml.bind.Unmarshaller) JTSGeometry(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.JTSGeometry)

Example 15 with Attribute

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

the class JAXPStreamFeatureReader method resolveLinks.

/**
 * Replace each feature xlink href characteristic by it's real value if it exist.
 *
 * @param index
 * @param feature
 */
public static void resolveLinks(Map<String, Object> index, Feature feature) {
    final FeatureType type = feature.getType();
    for (PropertyType pt : type.getProperties(true)) {
        if (pt instanceof AttributeType) {
            AttributeType attType = (AttributeType) pt;
            if (attType.getMaximumOccurs() == 1) {
                Attribute att = (Attribute) feature.getProperty(pt.getName().toString());
                Object value = att.getValue();
                if (value == null) {
                    Attribute charatt = (Attribute) att.characteristics().get(GMLConvention.XLINK_HREF.tip().toString());
                    if (charatt != null) {
                        Object target = index.get(charatt.getValue());
                        if (target != null)
                            att.setValue(target);
                    }
                }
            }
        } else if (pt instanceof FeatureAssociationRole) {
        // TODO
        }
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) Attribute(org.opengis.feature.Attribute) AttributeType(org.opengis.feature.AttributeType) PropertyType(org.opengis.feature.PropertyType) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole)

Aggregations

Attribute (org.opengis.feature.Attribute)16 AttributeType (org.opengis.feature.AttributeType)8 FeatureAssociationRole (org.opengis.feature.FeatureAssociationRole)6 FeatureType (org.opengis.feature.FeatureType)6 Feature (org.opengis.feature.Feature)5 PropertyType (org.opengis.feature.PropertyType)5 GenericName (org.opengis.util.GenericName)4 Collection (java.util.Collection)3 Map (java.util.Map)3 Entry (java.util.Map.Entry)3 MapDef (org.geotoolkit.report.graphic.map.MapDef)3 FeatureAssociation (org.opengis.feature.FeatureAssociation)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 List (java.util.List)2 JAXBElement (javax.xml.bind.JAXBElement)2 JAXBException (javax.xml.bind.JAXBException)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 DefaultAttributeType (org.apache.sis.feature.DefaultAttributeType)2