Search in sources :

Example 6 with GeometryMetadata

use of eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata in project hale by halestudio.

the class SpatiaLiteGeometries method convertGeometry.

@Override
public Object convertGeometry(GeometryProperty<?> geom, TypeDefinition columnType, SQLiteConnection connection) throws Exception {
    // show error and abort if SpatiaLite is not available
    if (!SpatiaLiteHelper.isSpatialLiteLoadedReport(connection, true)) {
        throw new IllegalStateException("SpatiaLite module is not available");
    }
    // Transform from sourceCRS to targetCRS
    GeometryMetadata columnTypeMetadata = columnType.getConstraint(GeometryMetadata.class);
    // transform
    CoordinateReferenceSystem targetCRS = null;
    String authName = columnTypeMetadata.getAuthName();
    if (authName != null && authName.equalsIgnoreCase("EPSG")) {
        targetCRS = CRS.decode(authName + ":" + columnTypeMetadata.getSrs());
    } else {
        String wkt = columnTypeMetadata.getSrsText();
        if (wkt != null && !wkt.isEmpty()) {
            targetCRS = CRS.parseWKT(wkt);
        }
    }
    Geometry targetGeometry;
    if (targetCRS != null) {
        MathTransform transform = CRS.findMathTransform(geom.getCRSDefinition().getCRS(), targetCRS);
        targetGeometry = JTS.transform(geom.getGeometry(), transform);
        // encode JTS Geometry
        return encodeGeometryValue(targetGeometry, columnTypeMetadata.getSrs(), columnTypeMetadata.getDimension(), connection);
    } else {
        targetGeometry = geom.getGeometry();
        String srid = "-1";
        if (geom.getCRSDefinition() != null) {
            // try to get SRID for source SRS
            String epsgCode = CRSDefinitionUtil.getEPSG(geom.getCRSDefinition());
            if (epsgCode != null) {
                try {
                    int epsgNumber = Integer.parseInt(epsgCode);
                    SrsMetadata srsMeta = SpatiaLiteSupportFactory.getInstance().createSpatiaLiteSupport(connection).getSrsMetadata(connection, "epsg", epsgNumber);
                    if (srsMeta != null) {
                        srid = String.valueOf(srsMeta.getSrid());
                    }
                } catch (NumberFormatException e) {
                // ignore
                }
            }
        }
        // encode JTS Geometry
        return encodeGeometryValue(targetGeometry, srid, columnTypeMetadata.getDimension(), connection);
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) SrsMetadata(eu.esdihumboldt.hale.io.jdbc.spatialite.internal.SrsMetadata) GeometryMetadata(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata) MathTransform(org.opengis.referencing.operation.MathTransform) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 7 with GeometryMetadata

use of eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata in project hale by halestudio.

the class SpatiaLiteGeometries method configureGeometryColumnType.

@Override
public Class<? extends Geometry> configureGeometryColumnType(SQLiteConnection connection, BaseColumn<?> column, DefaultTypeDefinition type) {
    String colName = column.getName();
    String tabName = column.getParent().getName();
    SpatiaLiteSupport slSupport = SpatiaLiteSupportFactory.getInstance().createSpatiaLiteSupport(connection);
    // warn if SpatiaLite is not available
    SpatiaLiteHelper.isSpatialLiteLoadedReport(connection, false);
    GeometryTypeMetadata geomTypeMeta = slSupport.getGeometryTypeMetadata(connection, tabName, colName);
    if (geomTypeMeta != null) {
        SrsMetadata srsMeta = slSupport.getSrsMetadata(connection, geomTypeMeta.getSrid());
        GeometryMetadata columnTypeConstraint;
        if (srsMeta != null) {
            // Create constraint to save the informations
            columnTypeConstraint = new GeometryMetadata(Integer.toString(srsMeta.getAuthSrid()), geomTypeMeta.getCoordDimension(), srsMeta.getSrText(), srsMeta.getAuthName());
        } else {
            // no SRS information, just dimension
            columnTypeConstraint = new GeometryMetadata(geomTypeMeta.getCoordDimension());
        }
        type.setConstraint(columnTypeConstraint);
        return geomTypeMeta.getGeomType();
    } else {
        // no geometry column could be found
        return null;
    }
}
Also used : SrsMetadata(eu.esdihumboldt.hale.io.jdbc.spatialite.internal.SrsMetadata) GeometryMetadata(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata) SpatiaLiteSupport(eu.esdihumboldt.hale.io.jdbc.spatialite.internal.SpatiaLiteSupport) GeometryTypeMetadata(eu.esdihumboldt.hale.io.jdbc.spatialite.internal.GeometryTypeMetadata)

Aggregations

GeometryMetadata (eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata)7 Geometry (com.vividsolutions.jts.geom.Geometry)5 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)3 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)2 CodeDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition)2 WKTDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition)2 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)2 SrsMetadata (eu.esdihumboldt.hale.io.jdbc.spatialite.internal.SrsMetadata)2 SQLException (java.sql.SQLException)2 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)2 MathTransform (org.opengis.referencing.operation.MathTransform)2 PGgeometry (org.postgis.PGgeometry)2 MultiPoint (com.vividsolutions.jts.geom.MultiPoint)1 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)1 Point (com.vividsolutions.jts.geom.Point)1 ValueProperties (eu.esdihumboldt.hale.common.core.io.ValueProperties)1 GeometryTypeMetadata (eu.esdihumboldt.hale.io.jdbc.spatialite.internal.GeometryTypeMetadata)1 SpatiaLiteSupport (eu.esdihumboldt.hale.io.jdbc.spatialite.internal.SpatiaLiteSupport)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1