Search in sources :

Example 1 with GeometryMetadata

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

the class PostGISGeometries method convertGeometry.

@Override
public Object convertGeometry(GeometryProperty<?> geom, TypeDefinition columnType, PGConnection pgconn) throws Exception {
    PGgeometry pGeometry = null;
    // Transform from sourceCRS to targetCRS
    GeometryMetadata columnTypeMetadata = columnType.getConstraint(GeometryMetadata.class);
    // transform
    CoordinateReferenceSystem targetCRS = null;
    String authName = columnTypeMetadata.getAuthName();
    if (authName != null && authName.equals("EPSG")) {
        // PostGIS assumes lon/lat
        targetCRS = CRS.decode(authName + ":" + columnTypeMetadata.getSrs(), true);
    } 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);
    } else {
        targetGeometry = geom.getGeometry();
    }
    // Convert the jts Geometry to postgis PGgeometry and set the SRSID
    pGeometry = new PGgeometry(targetGeometry.toText());
    try {
        pGeometry.getGeometry().setSrid(Integer.parseInt(columnTypeMetadata.getSrs()));
    } catch (Exception e) {
    // ignore
    }
    return pGeometry;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryMetadata(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata) MathTransform(org.opengis.referencing.operation.MathTransform) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) SQLException(java.sql.SQLException) PGgeometry(org.postgis.PGgeometry)

Example 2 with GeometryMetadata

use of eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata 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 3 with GeometryMetadata

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

the class GeometryMetadataFactory method restore.

@Override
public GeometryMetadata restore(Value value, Definition<?> definition, TypeResolver typeIndex, ClassResolver resolver) throws Exception {
    ValueProperties props = value.as(ValueProperties.class);
    if (props != null) {
        String srs = props.getSafe(NAME_SRS).as(String.class);
        int dimension = props.getSafe(NAME_DIMENSION).as(Integer.class, 0);
        String srsText = props.getSafe(NAME_SRS_TEXT).as(String.class);
        String auth_name = props.getSafe(NAME_AUTH_NAME).as(String.class);
        return new GeometryMetadata(srs, dimension, srsText, auth_name);
    } else {
        return new GeometryMetadata();
    }
}
Also used : ValueProperties(eu.esdihumboldt.hale.common.core.io.ValueProperties) GeometryMetadata(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata)

Example 4 with GeometryMetadata

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

the class PostGISGeometries 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, PGConnection connection, Supplier<CRSDefinition> crsProvider) throws Exception {
    if (geom instanceof PGgeometry) {
        PGgeometry pgeom = (PGgeometry) geom;
        // conversion to JTS via WKT
        // TODO use better conversion (p4b?)
        WKTReader2 reader = new WKTReader2();
        String value = pgeom.getGeometry().toString();
        if (value.startsWith(PGgeometry.SRIDPREFIX) && value.indexOf(';') >= 0) {
            value = value.substring(value.indexOf(';') + 1);
        }
        Geometry jtsGeom = reader.read(value);
        // determine CRS
        GeometryMetadata columnTypeMetadata = columnType.getConstraint(GeometryMetadata.class);
        CRSDefinition crsDef = null;
        String authName = columnTypeMetadata.getAuthName();
        if (authName != null && authName.equals("EPSG")) {
            // PostGIS assumes lon/lat order
            crsDef = new CodeDefinition(authName + ":" + columnTypeMetadata.getSrs(), true);
        } else {
            String wkt = columnTypeMetadata.getSrsText();
            if (wkt != null) {
                crsDef = new WKTDefinition(wkt, null);
            }
        }
        return new DefaultGeometryProperty<Geometry>(crsDef, jtsGeom);
    }
    throw new IllegalArgumentException("Only conversion of PGgeometry supported");
}
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) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) WKTDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition) WKTReader2(org.geotools.geometry.jts.WKTReader2) PGgeometry(org.postgis.PGgeometry)

Example 5 with GeometryMetadata

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

the class PostGISGeometries method configureGeometryColumnType.

@Override
public Class<? extends Geometry> configureGeometryColumnType(PGConnection connection, BaseColumn<?> column, DefaultTypeDefinition type) {
    Connection con = (Connection) connection;
    String columnValueName = column.getParent().getName();
    String geometryType = null;
    try {
        Statement stmt = con.createStatement();
        // Get the srid, dimension and geometry type
        ResultSet rs = stmt.executeQuery("SELECT srid,type,coord_dimension FROM geometry_columns WHERE f_table_name = " + "'" + columnValueName + "'");
        if (rs.next()) {
            geometryType = rs.getString("type");
            String dimension = rs.getString("coord_dimension");
            // Get the epsg code for the srid
            String srid = rs.getString("srid");
            ResultSet r = stmt.executeQuery("SELECT auth_srid, auth_name, srtext FROM spatial_ref_sys WHERE srid = " + srid);
            if (r.next()) {
                // Create Constraint to save the informations
                GeometryMetadata columnTypeConstraint = new GeometryMetadata(r.getString("auth_srid"), Integer.parseInt(dimension), r.getString("srtext"), r.getString("auth_name"));
                type.setConstraint(columnTypeConstraint);
            }
        } else {
        // XXX what if no SRID information is present? is that a case
        // that may still be valid?
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    // In this case we have no geometry column information
    if (geometryType == null) {
        // use geometry even if no geometry column is present describing it
        return Geometry.class;
    }
    // return the geometryType
    if (geometryType.equalsIgnoreCase("MultiPolygon")) {
        return MultiPolygon.class;
    } else if (geometryType.equalsIgnoreCase("MultiPoint")) {
        return MultiPoint.class;
    } else if (geometryType.equalsIgnoreCase("MultiLineString")) {
        return MultiLineString.class;
    } else // TODO: shouldn't this be LineString instead?
    if (geometryType.equalsIgnoreCase("LinearRing")) {
        return LinearRing.class;
    } else if (geometryType.equalsIgnoreCase("Point")) {
        return Point.class;
    } else if (geometryType.equalsIgnoreCase("Polygon")) {
        return Polygon.class;
    } else {
        return Geometry.class;
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) GeometryMetadata(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata) SQLException(java.sql.SQLException) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Statement(java.sql.Statement) Connection(java.sql.Connection) PGConnection(org.postgresql.PGConnection) ResultSet(java.sql.ResultSet) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint)

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