Search in sources :

Example 11 with Instance

use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.

the class GmlInstanceCollectionTest method testWVAInstances.

private void testWVAInstances(InstanceCollection instances) {
    String ns = "http://www.esdi-humboldt.org/waterVA";
    String gmlNs = "http://www.opengis.net/gml";
    ResourceIterator<Instance> it = instances.iterator();
    try {
        assertTrue(it.hasNext());
        Instance instance = it.next();
        assertNotNull(instance);
        // check type and element
        TypeDefinition type = instance.getDefinition();
        assertEquals(new QName(ns, "Watercourses_VA_Type"), type.getName());
        XmlElements elements = type.getConstraint(XmlElements.class);
        Collection<? extends XmlElement> elementCollection = elements.getElements();
        assertEquals(1, elementCollection.size());
        XmlElement element = elementCollection.iterator().next();
        assertEquals(new QName(ns, "Watercourses_VA"), element.getName());
        // check instance
        // check a simple property first (FGW_ID)
        Object[] fgwID = instance.getProperty(new QName(ns, "FGW_ID"));
        assertNotNull(fgwID);
        assertEquals(1, fgwID.length);
        assertEquals("81011403", fgwID[0]);
        // the_geom
        Object[] the_geom = instance.getProperty(new QName(ns, "the_geom"));
        assertNotNull(the_geom);
        assertEquals(1, the_geom.length);
        assertTrue(the_geom[0] instanceof Instance);
        // MultiLineString
        Object[] multiLineString = ((Instance) the_geom[0]).getProperty(new QName(gmlNs, "MultiLineString"));
        assertNotNull(multiLineString);
        assertEquals(1, multiLineString.length);
        assertTrue(multiLineString[0] instanceof Instance);
        // TODO the MultiLineString should have a GeometryProperty value
        // with a MultiLineString as geometry and a CRS definition
        // ...getValue()
        // srsName
        Object[] srsName = ((Instance) multiLineString[0]).getProperty(new QName("srsName"));
        assertNotNull(srsName);
        assertEquals(1, srsName.length);
        assertEquals("EPSG:31251", srsName[0].toString());
        // lineStringMember
        Object[] lineStringMember = ((Instance) multiLineString[0]).getProperty(new QName(gmlNs, "lineStringMember"));
        assertNotNull(lineStringMember);
        assertEquals(1, lineStringMember.length);
        assertTrue(lineStringMember[0] instanceof Instance);
        // LineString
        Object[] lineString = ((Instance) lineStringMember[0]).getProperty(new QName(gmlNs, "LineString"));
        assertNotNull(lineString);
        assertEquals(1, lineString.length);
        assertTrue(lineString[0] instanceof Instance);
        // TODO the LineString should have a GeometryProperty value with a
        // LineString as geometry and a CRS definition
        // ...getValue()
        // choice
        Object[] choice_1 = ((Instance) lineString[0]).getProperty(new QName(gmlNs + "/LineStringType", "choice_1"));
        assertNotNull(choice_1);
        assertEquals(1, choice_1.length);
        assertTrue(choice_1[0] instanceof Group);
        // coordinates
        Object[] coordinates = ((Group) choice_1[0]).getProperty(new QName(gmlNs, "coordinates"));
        assertNotNull(coordinates);
        assertEquals(1, coordinates.length);
        assertTrue(coordinates[0] instanceof Instance);
        assertTrue(((Instance) coordinates[0]).getValue().toString().contains("-39799.68820381"));
        // only one instance should be present
        assertFalse(it.hasNext());
    } finally {
        it.close();
    }
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group) XmlElements(eu.esdihumboldt.hale.io.xsd.constraint.XmlElements) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 12 with Instance

use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.

the class StreamGmlHelper method parseProperties.

/**
 * Populates an instance or group with its properties based on the given XML
 * stream reader.
 *
 * @param reader the XML stream reader
 * @param group the group to populate with properties
 * @param strict if associating elements with properties should be done
 *            strictly according to the schema, otherwise a fall-back is
 *            used trying to populate values also on invalid property paths
 * @param srsDimension the dimension of the instance or <code>null</code>
 * @param crsProvider CRS provider in case no CRS is specified, may be
 *            <code>null</code>
 * @param crs CRS definition to use if the property contains a geometry that
 *            does not carry its own srsName attribute
 * @param parentType the type of the topmost instance
 * @param propertyPath the property path down from the topmost instance
 * @param onlyAttributes if only attributes should be parsed
 * @param ignoreNamespaces if parsing of the XML instances should allow
 *            types and properties with namespaces that differ from those
 *            defined in the schema
 * @param ioProvider the I/O Provider to get value
 * @throws XMLStreamException if parsing the properties failed
 */
private static void parseProperties(XMLStreamReader reader, MutableGroup group, boolean strict, Integer srsDimension, CRSProvider crsProvider, CRSDefinition crs, TypeDefinition parentType, List<QName> propertyPath, boolean onlyAttributes, boolean ignoreNamespaces, IOProvider ioProvider) throws XMLStreamException {
    final MutableGroup topGroup = group;
    // attributes (usually only present in Instances)
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        QName propertyName = reader.getAttributeName(i);
        // XXX might also be inside a group? currently every attribute group
        // should be flattened
        // for group support there would have to be some other kind of
        // handling than for elements, cause order doesn't matter for
        // attributes
        ChildDefinition<?> child = GroupUtil.findChild(group.getDefinition(), propertyName, ignoreNamespaces);
        if (child != null && child.asProperty() != null) {
            // add property value
            addSimpleProperty(group, child.asProperty(), reader.getAttributeValue(i));
        } else {
            // suppress warnings for xsi attributes (e.g. xsi:nil)
            boolean suppress = XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI.equals(propertyName.getNamespaceURI());
            if (!suppress) {
                log.warn(MessageFormat.format("No property ''{0}'' found in type ''{1}'', value is ignored", propertyName, group.getDefinition().getIdentifier()));
            }
        }
    }
    Stack<MutableGroup> groups = new Stack<MutableGroup>();
    groups.push(topGroup);
    // elements
    if (!onlyAttributes && hasElements(group.getDefinition())) {
        int open = 1;
        while (open > 0 && reader.hasNext()) {
            int event = reader.next();
            switch(event) {
                case XMLStreamConstants.START_ELEMENT:
                    // determine property definition, allow fall-back to
                    // non-strict mode
                    GroupProperty gp = GroupUtil.determineProperty(groups, reader.getName(), !strict, ignoreNamespaces);
                    if (gp != null) {
                        // update the stack from the path
                        groups = gp.getPath().getAllGroups(strict);
                        // get group object from stack
                        group = groups.peek();
                        PropertyDefinition property = gp.getProperty();
                        List<QName> path = new ArrayList<QName>(propertyPath);
                        path.add(property.getName());
                        if (hasElements(property.getPropertyType())) {
                            // use an instance as value
                            Instance inst = parseInstance(reader, property.getPropertyType(), null, strict, srsDimension, crsProvider, parentType, path, true, ignoreNamespaces, ioProvider, crs);
                            if (inst != null) {
                                group.addProperty(property.getName(), inst);
                            }
                        } else {
                            if (hasAttributes(property.getPropertyType())) {
                                // no elements but attributes
                                // use an instance as value, it will be assigned
                                // an instance value if possible
                                Instance inst = parseInstance(reader, property.getPropertyType(), null, strict, srsDimension, crsProvider, parentType, path, true, ignoreNamespaces, ioProvider);
                                if (inst != null) {
                                    group.addProperty(property.getName(), inst);
                                }
                            } else {
                                // no elements and no attributes
                                // use simple value
                                String value = readText(reader);
                                if (value != null) {
                                    addSimpleProperty(group, property, value);
                                }
                            }
                        }
                    } else {
                        log.warn(MessageFormat.format("No property ''{0}'' found in type ''{1}'', value is ignored", reader.getLocalName(), topGroup.getDefinition().getIdentifier()));
                    }
                    if (reader.getEventType() != XMLStreamConstants.END_ELEMENT) {
                        // only increase open if the current event is not
                        // already the end element (because we used
                        // getElementText)
                        open++;
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    open--;
                    break;
            }
        }
    }
}
Also used : MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) MutableGroup(eu.esdihumboldt.hale.common.instance.model.MutableGroup) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) Stack(java.util.Stack)

Example 13 with Instance

use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.

the class StreamGmlWriter method write.

/**
 * Write the given instances to an {@link XMLStreamWriter}.<br>
 * <br>
 * Use {@link #createWriter(OutputStream, IOReporter)} to create a properly
 * configured writer for this method.
 *
 * @param instances the instance collection
 * @param writer the writer to write the instances to
 * @param reporter the reporter
 * @param progress the progress
 * @see #createWriter(OutputStream, IOReporter)
 */
protected void write(InstanceCollection instances, PrefixAwareStreamWriter writer, ProgressIndicator progress, IOReporter reporter) {
    this.writer = writer;
    try {
        final SubtaskProgressIndicator sub = new SubtaskProgressIndicator(progress) {

            @Override
            protected String getCombinedTaskName(String taskName, String subtaskName) {
                return taskName + " (" + subtaskName + ")";
            }
        };
        progress = sub;
        progress.begin(getTaskName(), instances.size());
        XmlElement container = findDefaultContainter(targetIndex, reporter);
        TypeDefinition containerDefinition = (container == null) ? (null) : (container.getType());
        QName containerName = (container == null) ? (null) : (container.getName());
        if (containerDefinition == null) {
            XmlElement containerElement = getConfiguredContainerElement(this, getXMLIndex());
            containerDefinition = containerElement.getType();
            containerName = containerElement.getName();
        }
        if (containerDefinition == null || containerName == null) {
            throw new IllegalStateException("No root element/container found");
        }
        /*
			 * Add schema for container to validation schemas, if the namespace
			 * differs from the main namespace or additional schemas.
			 * 
			 * Needed for validation based on schemaLocation attribute.
			 */
        if (!containerName.getNamespaceURI().equals(targetIndex.getNamespace()) && !additionalSchemas.containsKey(containerName.getNamespaceURI())) {
            try {
                @SuppressWarnings("null") final URI containerSchemaLoc = stripFragment(container.getLocation());
                if (containerSchemaLoc != null) {
                    addValidationSchema(containerName.getNamespaceURI(), new Locatable() {

                        @Override
                        public URI getLocation() {
                            return containerSchemaLoc;
                        }
                    }, null);
                }
            } catch (Exception e) {
                reporter.error(new IOMessageImpl("Could not determine location of container definition", e));
            }
        }
        // additional schema namespace prefixes
        for (Entry<String, String> schemaNs : additionalSchemaPrefixes.entrySet()) {
            GmlWriterUtil.addNamespace(writer, schemaNs.getKey(), schemaNs.getValue());
        }
        writer.writeStartDocument();
        if (documentWrapper != null) {
            documentWrapper.startWrap(writer, reporter);
        }
        GmlWriterUtil.writeStartElement(writer, containerName);
        // generate mandatory id attribute (for feature collection)
        String containerId = getParameter(PARAM_CONTAINER_ID).as(String.class);
        GmlWriterUtil.writeID(writer, containerDefinition, null, false, containerId);
        // write schema locations
        StringBuffer locations = new StringBuffer();
        String noNamespaceLocation = null;
        if (targetIndex.getNamespace() != null && !targetIndex.getNamespace().isEmpty()) {
            locations.append(targetIndex.getNamespace());
            // $NON-NLS-1$
            locations.append(" ");
            locations.append(targetIndex.getLocation().toString());
        } else {
            noNamespaceLocation = targetIndex.getLocation().toString();
        }
        for (Entry<String, Locatable> schema : additionalSchemas.entrySet()) {
            if (schema.getKey() != null && !schema.getKey().isEmpty()) {
                if (locations.length() > 0) {
                    // $NON-NLS-1$
                    locations.append(" ");
                }
                locations.append(schema.getKey());
                // $NON-NLS-1$
                locations.append(" ");
                locations.append(schema.getValue().getLocation().toString());
            } else {
                noNamespaceLocation = schema.getValue().getLocation().toString();
            }
        }
        if (locations.length() > 0) {
            // $NON-NLS-1$
            writer.writeAttribute(SCHEMA_INSTANCE_NS, "schemaLocation", locations.toString());
        }
        if (noNamespaceLocation != null) {
            // $NON-NLS-1$
            writer.writeAttribute(// $NON-NLS-1$
            SCHEMA_INSTANCE_NS, // $NON-NLS-1$
            "noNamespaceSchemaLocation", noNamespaceLocation);
        }
        writeAdditionalElements(writer, containerDefinition, reporter);
        // write the instances
        ResourceIterator<Instance> itInstance = instances.iterator();
        try {
            Map<TypeDefinition, DefinitionPath> paths = new HashMap<TypeDefinition, DefinitionPath>();
            long lastUpdate = 0;
            int count = 0;
            Descent lastDescent = null;
            while (itInstance.hasNext() && !progress.isCanceled()) {
                Instance instance = itInstance.next();
                TypeDefinition type = instance.getDefinition();
                /*
					 * Skip all objects that are no features when writing to a
					 * GML feature collection.
					 */
                boolean skip = useFeatureCollection && !GmlWriterUtil.isFeatureType(type);
                if (skip) {
                    progress.advance(1);
                    continue;
                }
                // get stored definition path for the type
                DefinitionPath defPath;
                if (paths.containsKey(type)) {
                    // get the stored path, may be null
                    defPath = paths.get(type);
                } else {
                    // determine a valid definition path in the container
                    defPath = findMemberAttribute(containerDefinition, containerName, type);
                    // store path (may be null)
                    paths.put(type, defPath);
                }
                if (defPath != null) {
                    // write the feature
                    lastDescent = Descent.descend(writer, defPath, lastDescent, false);
                    writeMember(instance, type, reporter);
                } else {
                    reporter.warn(new IOMessageImpl(MessageFormat.format("No compatible member attribute for type {0} found in root element {1}, one instance was skipped", type.getDisplayName(), containerName.getLocalPart()), null));
                }
                progress.advance(1);
                count++;
                long now = System.currentTimeMillis();
                // only update every 100 milliseconds
                if (now - lastUpdate > 100 || !itInstance.hasNext()) {
                    lastUpdate = now;
                    sub.subTask(String.valueOf(count) + " instances");
                }
            }
            if (lastDescent != null) {
                lastDescent.close();
            }
        } finally {
            itInstance.close();
        }
        // FeatureCollection
        writer.writeEndElement();
        if (documentWrapper != null) {
            documentWrapper.endWrap(writer, reporter);
        }
        writer.writeEndDocument();
        writer.close();
        reporter.setSuccess(reporter.getErrors().isEmpty());
    } catch (Exception e) {
        reporter.error(new IOMessageImpl(e.getLocalizedMessage(), e));
        reporter.setSuccess(false);
    } finally {
        progress.end();
    }
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) SubtaskProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.SubtaskProgressIndicator) URI(java.net.URI) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) URISyntaxException(java.net.URISyntaxException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) DefinitionPath(eu.esdihumboldt.hale.io.gml.writer.internal.geometry.DefinitionPath) Descent(eu.esdihumboldt.hale.io.gml.writer.internal.geometry.Descent) Locatable(eu.esdihumboldt.hale.common.core.io.supplier.Locatable)

Example 14 with Instance

use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.

the class JacksonMapper method streamWriteGeoJSONCollection.

/**
 * Writes a collection of instances as GeoJSON
 *
 * @param out the output supplier
 * @param instances the collection of instances
 * @param config the default geometry configuration
 * @param reporter the reporter
 * @throws IOException if writing the instances fails
 */
public void streamWriteGeoJSONCollection(LocatableOutputSupplier<? extends OutputStream> out, InstanceCollection instances, GeoJSONConfig config, IOReporter reporter) throws IOException {
    try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out.getOutput(), Charset.forName("UTF-8")))) {
        JsonFactory jsonFactory = new JsonFactory();
        geometryJson = new GeometryJSON();
        jsonGen = jsonFactory.createJsonGenerator(writer);
        jsonGen.useDefaultPrettyPrinter();
        jsonGen.writeStartObject();
        jsonGen.writeStringField("type", "FeatureCollection");
        jsonGen.writeArrayFieldStart("features");
        // iterate through Instances
        try (ResourceIterator<Instance> itInstance = instances.iterator()) {
            while (itInstance.hasNext()) {
                Instance instance = itInstance.next();
                streamWriteGeoJSONInstance(instance, config, reporter);
            }
        }
        jsonGen.writeEndArray();
        jsonGen.writeEndObject();
        jsonGen.flush();
    }
}
Also used : GeometryJSON(org.geotools.geojson.geom.GeometryJSON) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) JsonFactory(org.codehaus.jackson.JsonFactory) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 15 with Instance

use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.

the class PointHandlerTest method testPointGml32_Grid.

/**
 * Test point geometries read from a GML 3.2 file.
 *
 * @throws Exception if an error occurs
 */
@Test
public void testPointGml32_Grid() throws Exception {
    InstanceCollection instances = loadXMLInstances(getClass().getResource("/data/gml/geom-gml32.xsd").toURI(), getClass().getResource("/data/point/sample-point-gml32.xml").toURI(), gridConfig);
    // three instances expected
    ResourceIterator<Instance> it = instances.iterator();
    try {
        // 1. PointProperty with Point defined through coordinates
        assertTrue("First sample feature missing", it.hasNext());
        Instance instance = it.next();
        checkSingleGeometry(instance, gridChecker);
        // 2. GeometryProperty with Point defined through coordinates
        assertTrue("Second sample feature missing", it.hasNext());
        instance = it.next();
        checkSingleGeometry(instance, gridChecker);
        // 3. PointProperty with Point defined through pos
        assertTrue("Third sample feature missing", it.hasNext());
        instance = it.next();
        checkSingleGeometry(instance, gridChecker);
        // 4. GeometryProperty with Point defined through pos
        assertTrue("Fourth sample feature missing", it.hasNext());
        instance = it.next();
        checkSingleGeometry(instance, gridChecker);
    } finally {
        it.close();
    }
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) Test(org.junit.Test) AbstractHandlerTest(eu.esdihumboldt.hale.io.gml.geometry.handler.internal.AbstractHandlerTest)

Aggregations

Instance (eu.esdihumboldt.hale.common.instance.model.Instance)203 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)131 Test (org.junit.Test)122 AbstractHandlerTest (eu.esdihumboldt.hale.io.gml.geometry.handler.internal.AbstractHandlerTest)97 QName (javax.xml.namespace.QName)29 ArrayList (java.util.ArrayList)26 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)25 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)23 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)22 Group (eu.esdihumboldt.hale.common.instance.model.Group)15 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)13 Coordinate (com.vividsolutions.jts.geom.Coordinate)12 Geometry (com.vividsolutions.jts.geom.Geometry)12 FamilyInstance (eu.esdihumboldt.hale.common.instance.model.FamilyInstance)10 Polygon (com.vividsolutions.jts.geom.Polygon)9 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)8 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)8 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)8 HashSet (java.util.HashSet)8 Point (com.vividsolutions.jts.geom.Point)7