Search in sources :

Example 1 with CRSDefinition

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

the class SpatiaLiteGeometries method convertToInstanceGeometry.

/**
 * @see eu.esdihumboldt.hale.io.jdbc.GeometryAdvisor#convertToInstanceGeometry(java.lang.Object,
 *      eu.esdihumboldt.hale.common.schema.model.TypeDefinition,
 *      java.lang.Object, java.util.function.Supplier)
 */
@Override
public GeometryProperty<?> convertToInstanceGeometry(Object geom, TypeDefinition columnType, SQLiteConnection connection, Supplier<CRSDefinition> crsProvider) throws Exception {
    // show error and abort if SpatiaLite is not available
    if (!SpatiaLiteHelper.isSpatialLiteLoadedReport(connection, true)) {
    // don't throw, will prevent any data being loaded
    // throw new IllegalStateException("SpatiaLite module is not available");
    }
    // decode geometry read from DB
    GeometryMetadata columnTypeMetadata = columnType.getConstraint(GeometryMetadata.class);
    Geometry jtsGeom = decodeGeometryValue(geom, columnTypeMetadata, connection);
    // determine CRS
    CRSDefinition crsDef = null;
    String authName = columnTypeMetadata.getAuthName();
    if (authName != null && authName.equalsIgnoreCase("EPSG")) {
        String epsgCode = authName + ":" + columnTypeMetadata.getSrs();
        crsDef = new CodeDefinition(epsgCode, null);
    } else {
        String wkt = columnTypeMetadata.getSrsText();
        if (wkt != null) {
            crsDef = new WKTDefinition(wkt, null);
        }
    }
    return new DefaultGeometryProperty<Geometry>(crsDef, jtsGeom);
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryMetadata(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) 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)

Example 2 with CRSDefinition

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

the class OSerializationHelper method deserialize.

/**
 * Deserialize a serialized value wrapped in the given document.
 *
 * @param doc the document
 * @param parent the parent group
 * @param childName the name of the child the value is associated to
 * @return the deserialized value
 */
public static Object deserialize(ODocument doc, OGroup parent, QName childName) {
    int serType = doc.field(FIELD_SERIALIZATION_TYPE);
    switch(serType) {
        case SERIALIZATION_TYPE_STRING:
            {
                // convert a string value back to its original form
                Object val = doc.field(FIELD_STRING_VALUE);
                String stringVal = (val == null) ? (null) : (val.toString());
                ConvertProxy cp = CONVERTER_IDS.getObject((String) doc.field(FIELD_CONVERT_ID));
                if (cp != null) {
                    return cp.convert(stringVal);
                }
                return stringVal;
            }
        case SERIALIZATION_TYPE_COLLECTION:
            {
                // recreate collection
                Object val = doc.field(FIELD_VALUES);
                Object typeVal = doc.field(FIELD_COLLECTION_TYPE);
                CollectionType type = (typeVal != null) ? (CollectionType.valueOf(typeVal.toString())) : (CollectionType.LIST);
                if (val instanceof Collection<?>) {
                    Collection<?> values = (Collection<?>) val;
                    Collection<Object> target = createCollection(type);
                    for (Object element : values) {
                        Object convElement = convertFromDB(element, parent, childName);
                        target.add(convElement);
                    }
                    return target;
                }
            }
            break;
    }
    ORecordBytes record = (ORecordBytes) doc.field(BINARY_WRAPPER_FIELD);
    Object result;
    switch(serType) {
        case SERIALIZATION_TYPE_BYTEARRAY:
            result = record.toStream();
            break;
        case SERIALIZATION_TYPE_GEOM:
        case SERIALIZATION_TYPE_GEOM_PROP:
            ExtendedWKBReader reader = new ExtendedWKBReader();
            try {
                result = reader.read(record.toStream());
            } catch (ParseException e1) {
                throw new IllegalStateException("Unable to parse WKB to restore geometry", e1);
            }
            break;
        case SERIALIZATION_TYPE_JAVA:
        default:
            ByteArrayInputStream bytes = new ByteArrayInputStream(record.toStream());
            try {
                ObjectInputStream in = new ObjectInputStream(bytes) {

                    @Override
                    protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
                        Class<?> result = resolved.get(desc.getName());
                        if (result == null) {
                            result = OsgiUtils.loadClass(desc.getName(), null);
                            if (resolved.size() > 200) {
                                resolved.entrySet().iterator().remove();
                            }
                            resolved.put(desc.getName(), result);
                        }
                        if (result == null) {
                            throw new IllegalStateException("Class " + desc.getName() + " not found");
                        }
                        return result;
                    }
                };
                result = in.readObject();
            } catch (Exception e) {
                throw new IllegalStateException("Could not deserialize field value.", e);
            }
            break;
    }
    if (serType == SERIALIZATION_TYPE_GEOM_PROP) {
        // wrap geometry in geometry property
        // determine CRS
        CRSDefinition crs = null;
        Object crsId = doc.field(FIELD_CRS_ID);
        if (crsId != null) {
            crs = CRS_IDS.getObject(crsId.toString());
        }
        // create geometry property
        GeometryProperty<Geometry> prop = new DefaultGeometryProperty<Geometry>(crs, (Geometry) result);
        return prop;
    }
    return result;
}
Also used : CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) ORecordBytes(com.orientechnologies.orient.core.record.impl.ORecordBytes) IOException(java.io.IOException) ParseException(org.locationtech.jts.io.ParseException) Geometry(org.locationtech.jts.geom.Geometry) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) ByteArrayInputStream(java.io.ByteArrayInputStream) Collection(java.util.Collection) ParseException(org.locationtech.jts.io.ParseException) ObjectStreamClass(java.io.ObjectStreamClass) ObjectInputStream(java.io.ObjectInputStream)

Example 3 with CRSDefinition

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

the class GeopackageInstanceWriter method convertGeometry.

private GeoPackageGeometryData convertGeometry(Object someGeom, GeometryColumns geomColumns, SimpleLog log) {
    Geometry geom = null;
    CRSDefinition sourceCrs = null;
    if (someGeom instanceof GeometryProperty<?>) {
        GeometryProperty<?> prop = (GeometryProperty<?>) someGeom;
        geom = prop.getGeometry();
        sourceCrs = prop.getCRSDefinition();
    } else if (someGeom instanceof Geometry) {
        geom = (Geometry) someGeom;
    }
    GeoPackageGeometryData geometryData = new GeoPackageGeometryData(geomColumns.getSrsId());
    if (geom != null) {
        SpatialReferenceSystem targetSrs = geomColumns.getSrs();
        CRSDefinition targetCrs = toCRSDefinition(targetSrs);
        // do conversion to target CRS (if possible)
        Geometry targetGeometry = geom;
        try {
            if (sourceCrs != null && targetCrs != null) {
                MathTransform transform = CRS.findMathTransform(sourceCrs.getCRS(), targetCrs.getCRS());
                targetGeometry = JTS.transform(geom, transform);
            }
        } catch (Exception e) {
            log.error("Failed to convert geometry to target SRS " + targetSrs.getSrsName());
        }
        // XXX also an option to only use a SrsId or use a SrsId that
        // differs from the column SrsId?
        byte[] wkb = new WKBWriter().write(targetGeometry);
        mil.nga.sf.Geometry geometry = GeometryReader.readGeometry(new ByteReader(wkb));
        geometryData.setGeometry(geometry);
    }
    return geometryData;
}
Also used : GeoPackageGeometryData(mil.nga.geopackage.geom.GeoPackageGeometryData) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) WKBWriter(org.locationtech.jts.io.WKBWriter) MathTransform(org.opengis.referencing.operation.MathTransform) ByteReader(mil.nga.sf.util.ByteReader) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) ConversionException(org.springframework.core.convert.ConversionException) SQLException(java.sql.SQLException) IOException(java.io.IOException) Geometry(org.locationtech.jts.geom.Geometry) SpatialReferenceSystem(mil.nga.geopackage.core.srs.SpatialReferenceSystem)

Example 4 with CRSDefinition

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

the class AbstractCRSManager method getCRS.

@Override
public CRSDefinition getCRS(TypeDefinition parentType, List<QName> propertyPath, CRSDefinition defaultCrs) {
    CRSDefinition result = null;
    String resourceId = reader.getResourceIdentifier();
    if (resourceId == null) {
        // TODO warn about no resource Id?
        // setting for any resource
        resourceId = "";
    } else {
        resourceId = "resource-" + resourceId + ":";
    }
    // first, try configuration
    // configuration for property
    StringBuffer keybuilder = new StringBuffer();
    keybuilder.append(resourceId);
    keybuilder.append(PREFIX_PARAM_CRS);
    keybuilder.append(parentType.getName());
    for (QName property : propertyPath) {
        keybuilder.append('/');
        keybuilder.append(property);
    }
    final String propertyKey = keybuilder.toString();
    result = CRSDefinitionManager.getInstance().parse(loadValue(propertyKey));
    // overall configuration for resource
    if (result == null && !resourceId.isEmpty()) {
        result = CRSDefinitionManager.getInstance().parse(loadValue(resourceId + PARAM_DEFAULT_CRS));
    }
    // overall configuration
    if (result == null) {
        result = CRSDefinitionManager.getInstance().parse(loadValue(PARAM_DEFAULT_CRS));
    }
    if (result == null && provider != null) {
        // consult default CRS provider
        result = provider.getCRS(parentType, propertyPath, defaultCrs);
        if (result != null) {
            // store in configuration
            storeValue(propertyKey, CRSDefinitionManager.getInstance().asString(result));
        }
    }
    return result;
}
Also used : CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) QName(javax.xml.namespace.QName)

Example 5 with CRSDefinition

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

the class EPSGResolveCache method resolveCRS.

@Override
public CRSDefinition resolveCRS(final CoordinateReferenceSystem crs) {
    CRSDefinition result = cached.get(crs);
    if (result == null) {
        result = CRSDefinitionUtil.lookupCrs(crs);
        cached.put(crs, result);
    }
    return result;
}
Also used : CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)

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