Source:
public Object convertGeometry(GeometryProperty<?> geom, TypeDefinition columnType, SQLServerConnection connection) throws Exception { if (geom instanceof DefaultGeometryProperty) { String columnDataType = columnType.getName().getLocalPart(); CoordinateReferenceSystem targetCRS = null; Statement stmt = null; ResultSet rs = null; try { int srId = 4326; if (geom.getCRSDefinition() != null) { String srsName = CRS.toSRS(geom.getCRSDefinition().getCRS()); CRS.AxisOrder axisOrder = CRS.getAxisOrder(geom.getCRSDefinition().getCRS()); 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) { } } 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; }

Target:
public Object convertGeometry(GeometryProperty<?> geom, TypeDefinition columnType, SQLServerConnection connection) throws Exception { String columnDataType = columnType.getName().getLocalPart(); CoordinateReferenceSystem targetCRS = null; Statement stmt = null; ResultSet rs = null; try { int srId = 4326; if (geom.getCRSDefinition() != null) { String srsName = CRS.toSRS(geom.getCRSDefinition().getCRS()); if (columnDataType.equals("geography")) { CRS.AxisOrder axisOrder = CRS.getAxisOrder(geom.getCRSDefinition().getCRS()); 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) { } } 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; }