Search in sources :

Example 1 with ByteReader

use of mil.nga.sf.util.ByteReader 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)

Aggregations

IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)1 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 SpatialReferenceSystem (mil.nga.geopackage.core.srs.SpatialReferenceSystem)1 GeoPackageGeometryData (mil.nga.geopackage.geom.GeoPackageGeometryData)1 ByteReader (mil.nga.sf.util.ByteReader)1 Geometry (org.locationtech.jts.geom.Geometry)1 WKBWriter (org.locationtech.jts.io.WKBWriter)1 MathTransform (org.opengis.referencing.operation.MathTransform)1 ConversionException (org.springframework.core.convert.ConversionException)1