Search in sources :

Example 1 with CRS

use of org.geotools.referencing.CRS in project hale by halestudio.

the class MsSqlGeometries method convertGeometry.

@Override
public Object convertGeometry(GeometryProperty<?> geom, TypeDefinition columnType, SQLServerConnection connection, SimpleLog log) throws Exception {
    // We need Column Data type
    String columnDataType = columnType.getName().getLocalPart();
    CoordinateReferenceSystem targetCRS = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        int srId = 4326;
        if (geom.getCRSDefinition() != null) {
            // SRS Code
            String srsName = CRS.toSRS(geom.getCRSDefinition().getCRS());
            if (columnDataType.equals("geography")) {
                // getting Axis order of geometry CRS
                CRS.AxisOrder axisOrder = CRS.getAxisOrder(geom.getCRSDefinition().getCRS());
                // transform geometry to target crs with longiture first
                if (axisOrder != CRS.AxisOrder.EAST_NORTH) {
                    if (srsName != null && srsName.startsWith("EPSG")) {
                        targetCRS = CRS.decode(srsName, true);
                    }
                }
            }
            try {
                if (srsName != null) {
                    final int index = srsName.lastIndexOf(':');
                    if (index > 0) {
                        srsName = srsName.substring(index + 1).trim();
                    }
                    srId = Integer.parseInt(srsName);
                }
            } catch (NumberFormatException nfEx) {
            // TODO::Using UI ask user to enter default SRId, if
            // can not extract it.
            }
        }
        Geometry targetGeometry;
        if (targetCRS != null) {
            MathTransform transform = CRS.findMathTransform(geom.getCRSDefinition().getCRS(), targetCRS);
            targetGeometry = JTS.transform(geom.getGeometry(), transform);
        } else {
            targetGeometry = geom.getGeometry();
        }
        String sqlForBinaryValue = "DECLARE @g " + columnDataType + ";SET @g = " + columnDataType + "::STGeomFromText('" + targetGeometry.toText() + "'," + srId + ");SELECT @g;";
        stmt = connection.createStatement();
        rs = stmt.executeQuery(sqlForBinaryValue);
        if (rs.next()) {
            return rs.getString(1);
        }
    } finally {
        if (rs != null)
            try {
                rs.close();
            } catch (Exception e) {
            // 
            }
        if (stmt != null)
            try {
                stmt.close();
            } catch (Exception e) {
            // 
            }
    }
    return null;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) MathTransform(org.opengis.referencing.operation.MathTransform) CRS(org.geotools.referencing.CRS) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) ParseException(org.locationtech.jts.io.ParseException)

Example 2 with CRS

use of org.geotools.referencing.CRS in project hale by halestudio.

the class MsSqlGeometries method convertGeometry.

@Override
public Object convertGeometry(GeometryProperty<?> geom, TypeDefinition columnType, SQLServerConnection connection) throws Exception {
    // We need Column Data type
    String columnDataType = columnType.getName().getLocalPart();
    CoordinateReferenceSystem targetCRS = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        int srId = 4326;
        if (geom.getCRSDefinition() != null) {
            // SRS Code
            String srsName = CRS.toSRS(geom.getCRSDefinition().getCRS());
            if (columnDataType.equals("geography")) {
                // getting Axis order of geometry CRS
                CRS.AxisOrder axisOrder = CRS.getAxisOrder(geom.getCRSDefinition().getCRS());
                // transform geometry to target crs with longiture first
                if (axisOrder != CRS.AxisOrder.EAST_NORTH) {
                    if (srsName != null && srsName.startsWith("EPSG")) {
                        targetCRS = CRS.decode(srsName, true);
                    }
                }
            }
            try {
                if (srsName != null) {
                    final int index = srsName.lastIndexOf(':');
                    if (index > 0) {
                        srsName = srsName.substring(index + 1).trim();
                    }
                    srId = Integer.parseInt(srsName);
                }
            } catch (NumberFormatException nfEx) {
            // TODO::Using UI ask user to enter default SRId, if
            // can not extract it.
            }
        }
        Geometry targetGeometry;
        if (targetCRS != null) {
            MathTransform transform = CRS.findMathTransform(geom.getCRSDefinition().getCRS(), targetCRS);
            targetGeometry = JTS.transform(geom.getGeometry(), transform);
        } else {
            targetGeometry = geom.getGeometry();
        }
        String sqlForBinaryValue = "DECLARE @g " + columnDataType + ";SET @g = " + columnDataType + "::STGeomFromText('" + targetGeometry.toText() + "'," + srId + ");SELECT @g;";
        stmt = connection.createStatement();
        rs = stmt.executeQuery(sqlForBinaryValue);
        if (rs.next()) {
            return rs.getString(1);
        }
    } finally {
        if (rs != null)
            try {
                rs.close();
            } catch (Exception e) {
            // 
            }
        if (stmt != null)
            try {
                stmt.close();
            } catch (Exception e) {
            // 
            }
    }
    return null;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) MathTransform(org.opengis.referencing.operation.MathTransform) CRS(org.geotools.referencing.CRS) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) ParseException(com.vividsolutions.jts.io.ParseException)

Aggregations

ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 CRS (org.geotools.referencing.CRS)2 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)2 MathTransform (org.opengis.referencing.operation.MathTransform)2 Geometry (com.vividsolutions.jts.geom.Geometry)1 ParseException (com.vividsolutions.jts.io.ParseException)1 Geometry (org.locationtech.jts.geom.Geometry)1 ParseException (org.locationtech.jts.io.ParseException)1