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