use of eu.esdihumboldt.hale.io.jdbc.spatialite.internal.SrsMetadata in project hale by halestudio.
the class SpatiaLiteGeometries method convertGeometry.
@Override
public Object convertGeometry(GeometryProperty<?> geom, TypeDefinition columnType, SQLiteConnection connection) throws Exception {
// show error and abort if SpatiaLite is not available
if (!SpatiaLiteHelper.isSpatialLiteLoadedReport(connection, true)) {
throw new IllegalStateException("SpatiaLite module is not available");
}
// Transform from sourceCRS to targetCRS
GeometryMetadata columnTypeMetadata = columnType.getConstraint(GeometryMetadata.class);
// transform
CoordinateReferenceSystem targetCRS = null;
String authName = columnTypeMetadata.getAuthName();
if (authName != null && authName.equalsIgnoreCase("EPSG")) {
targetCRS = CRS.decode(authName + ":" + columnTypeMetadata.getSrs());
} 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);
// encode JTS Geometry
return encodeGeometryValue(targetGeometry, columnTypeMetadata.getSrs(), columnTypeMetadata.getDimension(), connection);
} else {
targetGeometry = geom.getGeometry();
String srid = "-1";
if (geom.getCRSDefinition() != null) {
// try to get SRID for source SRS
String epsgCode = CRSDefinitionUtil.getEPSG(geom.getCRSDefinition());
if (epsgCode != null) {
try {
int epsgNumber = Integer.parseInt(epsgCode);
SrsMetadata srsMeta = SpatiaLiteSupportFactory.getInstance().createSpatiaLiteSupport(connection).getSrsMetadata(connection, "epsg", epsgNumber);
if (srsMeta != null) {
srid = String.valueOf(srsMeta.getSrid());
}
} catch (NumberFormatException e) {
// ignore
}
}
}
// encode JTS Geometry
return encodeGeometryValue(targetGeometry, srid, columnTypeMetadata.getDimension(), connection);
}
}
use of eu.esdihumboldt.hale.io.jdbc.spatialite.internal.SrsMetadata in project hale by halestudio.
the class SpatiaLiteGeometries method configureGeometryColumnType.
@Override
public Class<? extends Geometry> configureGeometryColumnType(SQLiteConnection connection, BaseColumn<?> column, DefaultTypeDefinition type) {
String colName = column.getName();
String tabName = column.getParent().getName();
SpatiaLiteSupport slSupport = SpatiaLiteSupportFactory.getInstance().createSpatiaLiteSupport(connection);
// warn if SpatiaLite is not available
SpatiaLiteHelper.isSpatialLiteLoadedReport(connection, false);
GeometryTypeMetadata geomTypeMeta = slSupport.getGeometryTypeMetadata(connection, tabName, colName);
if (geomTypeMeta != null) {
SrsMetadata srsMeta = slSupport.getSrsMetadata(connection, geomTypeMeta.getSrid());
GeometryMetadata columnTypeConstraint;
if (srsMeta != null) {
// Create constraint to save the informations
columnTypeConstraint = new GeometryMetadata(Integer.toString(srsMeta.getAuthSrid()), geomTypeMeta.getCoordDimension(), srsMeta.getSrText(), srsMeta.getAuthName());
} else {
// no SRS information, just dimension
columnTypeConstraint = new GeometryMetadata(geomTypeMeta.getCoordDimension());
}
type.setConstraint(columnTypeConstraint);
return geomTypeMeta.getGeomType();
} else {
// no geometry column could be found
return null;
}
}
Aggregations