use of org.opengis.feature.PropertyType in project geotoolkit by Geomatys.
the class ViewFeatureTypeTest method filterAttributeTest.
@Test
public void filterAttributeTest() {
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("test");
ftb.addAttribute(String.class).setName("attString");
ftb.addAttribute(Double.class).setName("attDouble");
final FeatureType baseType = ftb.build();
// test view type
final ViewMapper viewType = new ViewMapper(baseType, "attDouble");
final Collection<? extends PropertyType> properties = viewType.getMappedType().getProperties(true);
assertEquals(1, properties.size());
final PropertyType attDouble = properties.iterator().next();
assertEquals(baseType.getProperty("attDouble"), attDouble);
// test feature
final Feature baseFeature = baseType.newInstance();
baseFeature.setPropertyValue("attString", "hello world");
baseFeature.setPropertyValue("attDouble", 123.456);
final Feature viewFeature = viewType.apply(baseFeature);
assertEquals(123.456, (Double) viewFeature.getPropertyValue("attDouble"), 0);
try {
viewFeature.getPropertyValue("attString");
fail("Property attString should not have been accessible");
} catch (PropertyNotFoundException ex) {
/*ok*/
}
}
use of org.opengis.feature.PropertyType in project geotoolkit by Geomatys.
the class ElementFeatureWriter method writeFeature.
/**
* Write the feature into the stream.
*
* @param feature The feature
*/
public Element writeFeature(final Feature feature, final Document rootDocument, boolean fragment) throws ParserConfigurationException {
final Document document;
if (rootDocument == null) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// then we have to create document-loader:
factory.setNamespaceAware(false);
DocumentBuilder loader = factory.newDocumentBuilder();
// creating a new DOM-document...
document = loader.newDocument();
} else {
document = rootDocument;
}
// the root element of the xml document (type of the feature)
final FeatureType type = feature.getType();
final GenericName typeName = type.getName();
final String namespace = NamesExt.getNamespace(typeName);
final String localPart = typeName.tip().toString();
final Element rootElement;
final Prefix prefix;
if (namespace != null) {
prefix = getPrefix(namespace);
rootElement = document.createElementNS(namespace, localPart);
rootElement.setPrefix(prefix.prefix);
} else {
rootElement = document.createElement(localPart);
prefix = null;
}
// if main document set the xmlns
if (!fragment) {
rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:gml", "http://www.opengis.net/gml");
}
final Attr idAttr = document.createAttributeNS(GML, "id");
idAttr.setValue(feature.getPropertyValue(AttributeConvention.IDENTIFIER).toString());
idAttr.setPrefix("gml");
rootElement.setAttributeNodeNS(idAttr);
if (rootDocument == null) {
document.appendChild(rootElement);
}
// write properties in the type order
for (final PropertyType desc : type.getProperties(true)) {
if (AttributeConvention.contains(desc.getName()))
continue;
if (desc.getName().tip().toString().startsWith("@")) {
// skip attributes
continue;
}
for (final Object valueA : Utils.propertyValueAsList(feature, desc.getName().toString())) {
final PropertyType typeA = desc;
final GenericName nameA = desc.getName();
final String nameProperty = nameA.tip().toString();
String namespaceProperty = NamesExt.getNamespace(nameA);
if (valueA instanceof Collection<?> && !(AttributeConvention.isGeometryAttribute(typeA))) {
for (final Object value : (Collection<?>) valueA) {
final Element element;
if (namespaceProperty != null) {
element = document.createElementNS(namespaceProperty, nameProperty);
} else {
element = document.createElement(nameProperty);
}
element.setTextContent(Utils.getStringValue(value));
if (prefix != null) {
element.setPrefix(prefix.prefix);
}
rootElement.appendChild(element);
}
} else if (valueA != null && valueA.getClass().isArray() && !(AttributeConvention.isGeometryAttribute(typeA))) {
final int length = Array.getLength(valueA);
for (int i = 0; i < length; i++) {
final Element element;
if (namespaceProperty != null) {
element = document.createElementNS(namespaceProperty, nameProperty);
} else {
element = document.createElement(nameProperty);
}
final Object value = Array.get(valueA, i);
final String textValue;
if (value != null && value.getClass().isArray()) {
// matrix
final StringBuilder sb = new StringBuilder();
final int length2 = Array.getLength(value);
for (int j = 0; j < length2; j++) {
final Object subValue = Array.get(value, j);
sb.append(Utils.getStringValue(subValue)).append(" ");
}
textValue = sb.toString();
} else {
textValue = Utils.getStringValue(value);
}
element.setTextContent(textValue);
if (prefix != null) {
element.setPrefix(prefix.prefix);
}
rootElement.appendChild(element);
}
} else if (valueA instanceof Map && !(AttributeConvention.isGeometryAttribute(typeA))) {
final Map<?, ?> map = (Map) valueA;
for (Entry<?, ?> entry : map.entrySet()) {
final Element element;
if (namespaceProperty != null) {
element = document.createElementNS(namespaceProperty, nameProperty);
} else {
element = document.createElement(nameProperty);
}
final Object key = entry.getKey();
if (key != null) {
element.setAttribute("name", (String) key);
}
element.setTextContent(Utils.getStringValue(entry.getValue()));
if (prefix != null) {
element.setPrefix(prefix.prefix);
}
rootElement.appendChild(element);
}
} else if (!(AttributeConvention.isGeometryAttribute(typeA))) {
String value = Utils.getStringValue(valueA);
if (value != null || (value == null && !Utils.isNillable(typeA))) {
if ((nameProperty.equals("name") || nameProperty.equals("description")) && !GML.equals(namespaceProperty)) {
namespaceProperty = GML;
LOGGER.finer("the property name and description of a feature must have the GML namespace");
}
final Element element;
if (namespaceProperty != null) {
element = document.createElementNS(namespaceProperty, nameProperty);
} else {
element = document.createElement(nameProperty);
}
if (value != null) {
element.setTextContent(value);
}
if (prefix != null) {
element.setPrefix(prefix.prefix);
}
rootElement.appendChild(element);
}
// we add the geometry
} else {
if (valueA != null) {
final Element element;
if (namespaceProperty != null) {
element = document.createElementNS(namespaceProperty, nameProperty);
} else {
element = document.createElement(nameProperty);
}
if (prefix != null) {
element.setPrefix(prefix.prefix);
}
final Geometry isoGeometry;
if (valueA instanceof org.locationtech.jts.geom.Geometry) {
org.locationtech.jts.geom.Geometry geomValue = (org.locationtech.jts.geom.Geometry) valueA;
CoordinateReferenceSystem crs = null;
try {
crs = JTS.findCoordinateReferenceSystem(geomValue);
} catch (FactoryException ex) {
LOGGER.log(Level.WARNING, "Cannot find CRS directly from jts geometry", ex);
}
if (crs == null) {
crs = FeatureExt.getCRS(typeA);
}
isoGeometry = JTSUtils.toISO(geomValue, crs);
} else if (valueA instanceof Geometry) {
isoGeometry = (Geometry) valueA;
} else {
throw new UnsupportedOperationException("Cannot serialize geometry object of type " + valueA.getClass());
}
try {
final Marshaller marshaller;
marshaller = POOL.acquireMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
marshaller.marshal(OBJECT_FACTORY.buildAnyGeometry(isoGeometry), element);
POOL.recycle(marshaller);
} catch (JAXBException ex) {
LOGGER.log(Level.WARNING, "JAXB Exception while marshalling the iso geometry: " + ex.getMessage(), ex);
}
rootElement.appendChild(element);
}
}
}
}
// writer.writeEndElement();
return rootElement;
}
use of org.opengis.feature.PropertyType 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
}
}
}
use of org.opengis.feature.PropertyType in project geotoolkit by Geomatys.
the class JAXPStreamFeatureReader method populateIndex.
private void populateIndex(Object obj) throws DataStoreException {
final String gmlId = GMLConvention.getGmlId(obj);
if (gmlId != null)
index.put(gmlId, obj);
if (obj instanceof Feature) {
final Feature feature = (Feature) obj;
for (PropertyType pt : feature.getType().getProperties(true)) {
if (pt instanceof AttributeType) {
AttributeType atType = (AttributeType) pt;
if (Geometry.class.isAssignableFrom(atType.getValueClass())) {
Object value = feature.getPropertyValue(pt.getName().toString());
populateIndex(value);
}
} else if (pt instanceof FeatureAssociationRole) {
Object value = feature.getPropertyValue(pt.getName().toString());
populateIndex(value);
}
}
} else if (obj instanceof FeatureSet) {
final FeatureSet fs = (FeatureSet) obj;
try (Stream<Feature> stream = fs.features(false)) {
Iterator<Feature> iterator = stream.iterator();
while (iterator.hasNext()) {
populateIndex(iterator.next());
}
}
} else if (obj instanceof Collection) {
final Collection col = (Collection) obj;
Iterator<Feature> iterator = col.iterator();
while (iterator.hasNext()) {
populateIndex(iterator.next());
}
}
}
use of org.opengis.feature.PropertyType in project geotoolkit by Geomatys.
the class JAXPStreamFeatureWriter method writeAttributeProperties.
/**
* Write atribute properties.
* If we found a nil reason than return is true
*
* TODO this is not a perfect way to know if a propery is null.
* but if we don't declare the property then we don't know the reason either...
*/
private boolean writeAttributeProperties(final Feature feature) throws XMLStreamException {
final FeatureType type = feature.getType();
boolean nil = false;
// write properties in the type order
for (final PropertyType desc : type.getProperties(true)) {
if (AttributeConvention.contains(desc.getName()))
continue;
if (!isAttributeProperty(desc.getName()))
continue;
if (desc.getName().tip().toString().equals("@id")) {
// gml id has already been written
continue;
}
Object value = feature.getPropertyValue(desc.getName().toString());
final GenericName nameA = desc.getName();
String nameProperty = nameA.tip().toString();
String namespaceProperty = getNamespace(nameA);
// remove the @
nameProperty = nameProperty.substring(1);
nil |= "nil".equals(nameProperty) && Boolean.TRUE.equals(value);
if (value instanceof Boolean) {
value = (Boolean) value ? "1" : "0";
}
String valueStr = Utils.getStringValue(value);
if (valueStr != null) {
if (namespaceProperty != null && !namespaceProperty.isEmpty()) {
writer.writeAttribute(namespaceProperty, nameProperty, valueStr);
} else {
writer.writeAttribute(nameProperty, valueStr);
}
}
}
return nil;
}
Aggregations