Search in sources :

Example 1 with GeoPackageGeometryData

use of mil.nga.geopackage.geom.GeoPackageGeometryData 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 2 with GeoPackageGeometryData

use of mil.nga.geopackage.geom.GeoPackageGeometryData in project hale by halestudio.

the class GeopackageInstanceWriter method writeInstances.

/**
 * Write instances to the GeoPackage.
 *
 * @param geoPackage the GeoPackage
 * @param instances the instances to write
 * @param progress the progress indicator
 * @param reporter the reporter
 * @throws SQLException if an error occurs creating a database table
 */
protected void writeInstances(GeoPackage geoPackage, InstanceCollection instances, ProgressIndicator progress, IOReporter reporter) throws SQLException {
    try (ResourceIterator<Instance> it = instances.iterator()) {
        while (it.hasNext() && !progress.isCanceled()) {
            Instance instance = it.next();
            TypeDefinition type = instance.getDefinition();
            // TODO only specific types? (e.g. ignore some kinds of types?)
            String tableName = type.getName().getLocalPart();
            // determine table for type (and create if necessary)
            GeopackageTableType tableType = createTableIfNecessary(geoPackage, tableName, type, instance, reporter);
            switch(tableType) {
                case ATTRIBUTE:
                    AttributesDao attributes = geoPackage.getAttributesDao(tableName);
                    AttributesRow arow = attributes.newRow();
                    populateRow(arow, instance, reporter);
                    attributes.insert(arow);
                    break;
                case FEATURE:
                    FeatureDao features = geoPackage.getFeatureDao(tableName);
                    FeatureRow frow = features.newRow();
                    populateRow(frow, instance, reporter);
                    // set geometry
                    String geometryColumn = features.getGeometryColumnName();
                    // XXX instead of using value traverse (GeometryFinder) for
                    // geometry?
                    Object geom = new InstanceAccessor(instance).findChildren(geometryColumn).value();
                    GeoPackageGeometryData geomData = convertGeometry(geom, features.getGeometryColumns(), reporter);
                    frow.setGeometry(geomData);
                    features.insert(frow);
                    break;
            }
        }
    }
}
Also used : GeoPackageGeometryData(mil.nga.geopackage.geom.GeoPackageGeometryData) FeatureRow(mil.nga.geopackage.features.user.FeatureRow) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) AttributesDao(mil.nga.geopackage.attributes.AttributesDao) InstanceAccessor(eu.esdihumboldt.hale.common.instance.groovy.InstanceAccessor) FeatureDao(mil.nga.geopackage.features.user.FeatureDao) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) AttributesRow(mil.nga.geopackage.attributes.AttributesRow) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Aggregations

GeoPackageGeometryData (mil.nga.geopackage.geom.GeoPackageGeometryData)2 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 InstanceAccessor (eu.esdihumboldt.hale.common.instance.groovy.InstanceAccessor)1 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)1 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)1 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)1 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 AttributesDao (mil.nga.geopackage.attributes.AttributesDao)1 AttributesRow (mil.nga.geopackage.attributes.AttributesRow)1 SpatialReferenceSystem (mil.nga.geopackage.core.srs.SpatialReferenceSystem)1 FeatureDao (mil.nga.geopackage.features.user.FeatureDao)1 FeatureRow (mil.nga.geopackage.features.user.FeatureRow)1 ByteReader (mil.nga.sf.util.ByteReader)1 Geometry (org.locationtech.jts.geom.Geometry)1 LineString (org.locationtech.jts.geom.LineString)1 MultiLineString (org.locationtech.jts.geom.MultiLineString)1 WKBWriter (org.locationtech.jts.io.WKBWriter)1 MathTransform (org.opengis.referencing.operation.MathTransform)1