Search in sources :

Example 1 with PGgeometry

use of org.postgis.PGgeometry in project querydsl by querydsl.

the class PGgeometryType method setValue.

@Override
public void setValue(PreparedStatement st, int startIndex, Geometry value) throws SQLException {
    PGgeometry geometry = new PGgeometry(PGgeometryConverter.convert(value));
    st.setObject(startIndex, geometry);
}
Also used : PGgeometry(org.postgis.PGgeometry)

Example 2 with PGgeometry

use of org.postgis.PGgeometry 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 3 with PGgeometry

use of org.postgis.PGgeometry 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)

Aggregations

PGgeometry (org.postgis.PGgeometry)3 Geometry (com.vividsolutions.jts.geom.Geometry)2 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)2 GeometryMetadata (eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata)2 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)1 CodeDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition)1 WKTDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition)1 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)1 SQLException (java.sql.SQLException)1 WKTReader2 (org.geotools.geometry.jts.WKTReader2)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1 MathTransform (org.opengis.referencing.operation.MathTransform)1