Search in sources :

Example 1 with ConversionLadder

use of eu.esdihumboldt.hale.io.gml.writer.internal.geometry.GeometryConverterRegistry.ConversionLadder in project hale by halestudio.

the class StreamGeometryWriter method write.

/**
 * Write a geometry to a stream for a GML document
 *
 * @param writer the XML stream writer
 * @param geometry the geometry
 * @param property the geometry property
 * @param srsName the SRS name of a common SRS for the whole document, may
 *            be <code>null</code>
 * @param report the reporter
 * @param decimalFormatter a decimal formatter to format geometry
 *            coordinates
 * @throws XMLStreamException if any error occurs writing the geometry
 */
public void write(XMLStreamWriter writer, Geometry geometry, PropertyDefinition property, String srsName, IOReporter report, DecimalFormat decimalFormatter) throws XMLStreamException {
    // write eventual required id
    GmlWriterUtil.writeRequiredID(writer, property.getPropertyType(), null, false);
    // write any srsName attribute on the parent element
    writeSrsName(writer, property.getPropertyType(), geometry, srsName);
    // write any srsDimension attribute on the parent element
    writeSrsDimension(writer, property.getPropertyType(), geometry);
    if (simplifyGeometry) {
        // reduce to internal geometry
        if (geometry instanceof GeometryCollection && ((GeometryCollection) geometry).getNumGeometries() == 1) {
            geometry = geometry.getGeometryN(0);
        }
    }
    Class<? extends Geometry> geomType = geometry.getClass();
    // remember if we already found a solution to this problem
    List<DefinitionPath> preferredPaths = restoreCandidate(property.getPropertyType(), geomType);
    if (preferredPaths == null) {
        // find candidates
        List<DefinitionPath> candidates = findCandidates(property, geomType);
        // if no candidate found, try with compatible geometries
        Class<? extends Geometry> originalType = geomType;
        Geometry originalGeometry = geometry;
        ConversionLadder ladder = GeometryConverterRegistry.getInstance().createLadder(geometry);
        while (candidates.isEmpty() && ladder.hasNext()) {
            geometry = ladder.next();
            geomType = geometry.getClass();
            log.info(// $NON-NLS-1$
            "Possible structure for writing " + originalType.getSimpleName() + " not found, trying " + geomType.getSimpleName() + // $NON-NLS-1$ //$NON-NLS-2$
            " instead");
            List<DefinitionPath> candPaths = restoreCandidate(property.getPropertyType(), geomType);
            if (candPaths != null) {
                // use stored candidate
                candidates = new ArrayList<>(candPaths);
            } else {
                candidates = findCandidates(property, geomType);
            }
        }
        if (candidates.isEmpty()) {
            // also try the generic geometry type
            geometry = originalGeometry;
            geomType = Geometry.class;
            log.info(// $NON-NLS-1$
            "Possible structure for writing " + originalType.getSimpleName() + // $NON-NLS-1$ //$NON-NLS-2$
            " not found, trying the generic geometry type instead");
            List<DefinitionPath> candPaths = restoreCandidate(property.getPropertyType(), geomType);
            if (candPaths != null) {
                // use stored candidate
                candidates = new ArrayList<>(candidates);
            } else {
                candidates = findCandidates(property, geomType);
            }
            // remember generic match for later
            storeCandidate(property.getPropertyType(), originalType, sortPreferredCandidates(candidates, geomType));
        }
        for (DefinitionPath candidate : candidates) {
            log.info(// $NON-NLS-1$ //$NON-NLS-2$
            "Geometry structure match: " + geomType.getSimpleName() + " - " + candidate);
        }
        if (candidates.isEmpty()) {
            log.error(// $NON-NLS-1$
            "No geometry structure match for " + originalType.getSimpleName() + // $NON-NLS-1$
            " found, writing WKT " + // $NON-NLS-1$
            "representation instead");
            writer.writeCharacters(originalGeometry.toText());
            return;
        }
        // determine preferred candidate
        preferredPaths = sortPreferredCandidates(candidates, geomType);
        // remember for later
        storeCandidate(property.getPropertyType(), geomType, preferredPaths);
    }
    DefinitionPath path = selectValidPath(preferredPaths, geometry);
    if (path != null) {
        // write geometry
        writeGeometry(writer, geometry, path, srsName, decimalFormatter);
    } else {
        report.error(new IOMessageImpl("No valid path found for encoding geometry, geometry is skipped.", null));
    }
}
Also used : GeometryCollection(com.vividsolutions.jts.geom.GeometryCollection) Geometry(com.vividsolutions.jts.geom.Geometry) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) ConversionLadder(eu.esdihumboldt.hale.io.gml.writer.internal.geometry.GeometryConverterRegistry.ConversionLadder)

Example 2 with ConversionLadder

use of eu.esdihumboldt.hale.io.gml.writer.internal.geometry.GeometryConverterRegistry.ConversionLadder in project hale by halestudio.

the class StreamGmlWriterTest method matchGeometries.

/**
 * Let the test fail if the given geometries don't match
 *
 * @param expected the expected geometry
 * @param value the geometry value
 */
public static void matchGeometries(Geometry expected, Geometry value) {
    if (expected.toString().equals(value.toString())) {
        // direct match
        return;
    }
    // check match for all no-loss conversions on value
    ConversionLadder ladder = GeometryConverterRegistry.getInstance().createNoLossLadder(value);
    while (ladder.hasNext()) {
        Geometry converted = ladder.next();
        if (expected.toString().equals(converted.toString())) {
            // match
            return;
        }
    }
    assertEquals(// $NON-NLS-1$
    "Geometry not compatible to expected geometry", // $NON-NLS-1$
    expected.toString(), value.toString());
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) ConversionLadder(eu.esdihumboldt.hale.io.gml.writer.internal.geometry.GeometryConverterRegistry.ConversionLadder)

Aggregations

Geometry (com.vividsolutions.jts.geom.Geometry)2 ConversionLadder (eu.esdihumboldt.hale.io.gml.writer.internal.geometry.GeometryConverterRegistry.ConversionLadder)2 GeometryCollection (com.vividsolutions.jts.geom.GeometryCollection)1 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1