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;
}
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;
}
Aggregations