use of org.postgis.PGgeometry in project querydsl by querydsl.
the class PGgeometryType method setValue.
@Override
public void setValue(PreparedStatement st, int startIndex, Geometry value) throws SQLException {
PGgeometry geometry = new PGgeometry(PGgeometryConverter.convert(value));
st.setObject(startIndex, geometry);
}
use of org.postgis.PGgeometry 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 org.postgis.PGgeometry 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");
}
Aggregations