use of eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata 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;
}
use of eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata in project hale by halestudio.
the class SpatiaLiteGeometries 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, SQLiteConnection connection, Supplier<CRSDefinition> crsProvider) throws Exception {
// show error and abort if SpatiaLite is not available
if (!SpatiaLiteHelper.isSpatialLiteLoadedReport(connection, true)) {
// don't throw, will prevent any data being loaded
// throw new IllegalStateException("SpatiaLite module is not available");
}
// decode geometry read from DB
GeometryMetadata columnTypeMetadata = columnType.getConstraint(GeometryMetadata.class);
Geometry jtsGeom = decodeGeometryValue(geom, columnTypeMetadata, connection);
// determine CRS
CRSDefinition crsDef = null;
String authName = columnTypeMetadata.getAuthName();
if (authName != null && authName.equalsIgnoreCase("EPSG")) {
String epsgCode = authName + ":" + columnTypeMetadata.getSrs();
crsDef = new CodeDefinition(epsgCode, null);
} else {
String wkt = columnTypeMetadata.getSrsText();
if (wkt != null) {
crsDef = new WKTDefinition(wkt, null);
}
}
return new DefaultGeometryProperty<Geometry>(crsDef, jtsGeom);
}
use of eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata in project hale by halestudio.
the class GeometryMetadataFactory method restore.
@Override
public GeometryMetadata restore(Value value, Definition<?> definition, TypeResolver typeIndex, ClassResolver resolver) throws Exception {
ValueProperties props = value.as(ValueProperties.class);
if (props != null) {
String srs = props.getSafe(NAME_SRS).as(String.class);
int dimension = props.getSafe(NAME_DIMENSION).as(Integer.class, 0);
String srsText = props.getSafe(NAME_SRS_TEXT).as(String.class);
String auth_name = props.getSafe(NAME_AUTH_NAME).as(String.class);
return new GeometryMetadata(srs, dimension, srsText, auth_name);
} else {
return new GeometryMetadata();
}
}
use of eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata 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");
}
use of eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata in project hale by halestudio.
the class PostGISGeometries method configureGeometryColumnType.
@Override
public Class<? extends Geometry> configureGeometryColumnType(PGConnection connection, BaseColumn<?> column, DefaultTypeDefinition type) {
Connection con = (Connection) connection;
String columnValueName = column.getParent().getName();
String geometryType = null;
try {
Statement stmt = con.createStatement();
// Get the srid, dimension and geometry type
ResultSet rs = stmt.executeQuery("SELECT srid,type,coord_dimension FROM geometry_columns WHERE f_table_name = " + "'" + columnValueName + "'");
if (rs.next()) {
geometryType = rs.getString("type");
String dimension = rs.getString("coord_dimension");
// Get the epsg code for the srid
String srid = rs.getString("srid");
ResultSet r = stmt.executeQuery("SELECT auth_srid, auth_name, srtext FROM spatial_ref_sys WHERE srid = " + srid);
if (r.next()) {
// Create Constraint to save the informations
GeometryMetadata columnTypeConstraint = new GeometryMetadata(r.getString("auth_srid"), Integer.parseInt(dimension), r.getString("srtext"), r.getString("auth_name"));
type.setConstraint(columnTypeConstraint);
}
} else {
// XXX what if no SRID information is present? is that a case
// that may still be valid?
}
} catch (SQLException e) {
e.printStackTrace();
}
// In this case we have no geometry column information
if (geometryType == null) {
// use geometry even if no geometry column is present describing it
return Geometry.class;
}
// return the geometryType
if (geometryType.equalsIgnoreCase("MultiPolygon")) {
return MultiPolygon.class;
} else if (geometryType.equalsIgnoreCase("MultiPoint")) {
return MultiPoint.class;
} else if (geometryType.equalsIgnoreCase("MultiLineString")) {
return MultiLineString.class;
} else // TODO: shouldn't this be LineString instead?
if (geometryType.equalsIgnoreCase("LinearRing")) {
return LinearRing.class;
} else if (geometryType.equalsIgnoreCase("Point")) {
return Point.class;
} else if (geometryType.equalsIgnoreCase("Polygon")) {
return Polygon.class;
} else {
return Geometry.class;
}
}
Aggregations