Search in sources :

Example 36 with CRSDefinition

use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition in project hale by halestudio.

the class StreamGmlWriter method writeElement.

/**
 * Write a property element.
 *
 * @param value the element value
 * @param propDef the property definition
 * @param report the reporter
 * @throws XMLStreamException if writing the element fails
 */
private void writeElement(Object value, PropertyDefinition propDef, IOReporter report) throws XMLStreamException {
    Group group = null;
    if (value instanceof Group) {
        group = (Group) value;
        if (value instanceof Instance) {
            // extract value from instance
            value = ((Instance) value).getValue();
        }
    }
    if (group == null) {
        if (value == null) {
            // null value
            if (propDef.getConstraint(Cardinality.class).getMinOccurs() > 0) {
                // write empty element
                GmlWriterUtil.writeEmptyElement(writer, propDef.getName());
                // mark as nil
                writeElementValue(null, propDef);
            }
        // otherwise just skip it
        } else {
            GmlWriterUtil.writeStartElement(writer, propDef.getName());
            Pair<Geometry, CRSDefinition> pair = extractGeometry(value, true, report);
            if (pair != null) {
                String srsName = extractCode(pair.getSecond());
                // write geometry
                writeGeometry(pair.getFirst(), propDef, srsName, report);
            } else {
                // simple element with value
                // write value as content
                writeElementValue(value, propDef);
            }
            writer.writeEndElement();
        }
    } else {
        // children and maybe a value
        GmlWriterUtil.writeStartElement(writer, propDef.getName());
        boolean hasValue = propDef.getPropertyType().getConstraint(HasValueFlag.class).isEnabled();
        Pair<Geometry, CRSDefinition> pair = extractGeometry(value, true, report);
        // handle about annotated geometries
        if (!hasValue && pair != null) {
            String srsName = extractCode(pair.getSecond());
            // write geometry
            writeGeometry(pair.getFirst(), propDef, srsName, report);
        } else {
            boolean hasOnlyNilReason = hasOnlyNilReason(group);
            // write no elements if there is a value or only a nil reason
            boolean writeElements = !hasValue && !hasOnlyNilReason;
            boolean isNil = !writeElements && (!hasValue || value == null);
            // write all children
            writeProperties(group, group.getDefinition(), writeElements, isNil, report);
            // write value
            if (hasValue) {
                writeElementValue(value, propDef);
            } else if (hasOnlyNilReason) {
                // complex element with a nil value -> write xsi:nil if
                // possible
                /*
					 * XXX open question: should xsi:nil be there also if there
					 * are other attributes than nilReason?
					 */
                writeElementValue(null, propDef);
            }
        }
        writer.writeEndElement();
    }
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup) Group(eu.esdihumboldt.hale.common.instance.model.Group) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) HasValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag)

Example 37 with CRSDefinition

use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition in project hale by halestudio.

the class CRSDefinitionUtil method createDefinition.

/**
 * Create a {@link CRSDefinition} from an existing coordinate reference
 * system.
 *
 * @param crs the coordinate reference system
 * @param cache the cache for CRS resolving
 * @return the CRS definition
 */
public static CRSDefinition createDefinition(CoordinateReferenceSystem crs, @Nullable CRSResolveCache cache) {
    ReferenceIdentifier name = crs.getName();
    // try to find CRS in EPSG DB
    CRSDefinition def;
    if (cache != null) {
        def = cache.resolveCRS(crs);
    } else {
        def = lookupCrs(crs);
    }
    if (def != null) {
        return def;
    }
    // try by code
    if (name != null) {
        String code = name.getCode();
        if (code != null && !code.isEmpty()) {
            // try decoding
            try {
                boolean lonFirst = (CRS.getAxisOrder(crs) == AxisOrder.EAST_NORTH);
                crs = CRS.decode(code, lonFirst);
                return new CodeDefinition(code, crs);
            } catch (Exception e) {
            // ignore
            }
        }
    }
    // use WKT
    return new WKTDefinition(crs.toWKT(), crs);
}
Also used : ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) CodeDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition) WKTDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition) FactoryException(org.opengis.referencing.FactoryException)

Aggregations

CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)37 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)24 Geometry (org.locationtech.jts.geom.Geometry)14 CodeDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition)13 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)11 ArrayList (java.util.ArrayList)9 Point (org.locationtech.jts.geom.Point)9 WKTDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition)8 GeometryNotSupportedException (eu.esdihumboldt.hale.io.gml.geometry.GeometryNotSupportedException)8 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)7 GeometryFinder (eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder)6 DepthFirstInstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)6 InstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser)6 Coordinate (org.locationtech.jts.geom.Coordinate)6 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)5 GeometryMetadata (eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata)5 ParseException (java.text.ParseException)5 LineString (org.locationtech.jts.geom.LineString)5 Geometry (com.vividsolutions.jts.geom.Geometry)4 NoResultException (eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)3