use of eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition 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.instance.geometry.impl.CodeDefinition in project hale by halestudio.
the class CRSHelperFunctionsTest method testFromCode.
@Test
public void testFromCode() {
String code = "EPSG:4326";
Map<String, Object> args = new HashMap<>();
args.put("code", code);
CRSDefinition crs = CRSHelperFunctions._from(args);
assertNotNull(crs);
assertNotNull(crs.getCRS());
assertTrue(crs instanceof CodeDefinition);
assertEquals(code, ((CodeDefinition) crs).getCode());
}
use of eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition in project hale by halestudio.
the class CRSDefinitionUtil method lookupCrs.
/**
* Try to lookup the given CRS via Geotools. If the CRS can be resolved, the
* returned {@link CRSDefinition} will contain a
* {@link CoordinateReferenceSystem} with additional information like
* Bursa-Wolf parameters, otherwise the WKT definition of the input CRS will
* be used as is.
*
* @param crs The CRS to look up
* @return A {@link CodeDefinition} with the resolved CRS or a
* {@link WKTDefinition} if the CRS could not be resolved.
*/
public static CRSDefinition lookupCrs(CoordinateReferenceSystem crs) {
try {
Integer epsgCode = CRS.lookupEpsgCode(crs, true);
if (epsgCode != null) {
// We must use the "EPSG:" prefix here, otherwise Geotools will
// not honour the longitudeFirst parameter and will always
// return the lat/lon variant...
String code = SrsSyntax.EPSG_CODE.getPrefix() + String.valueOf(epsgCode);
// Check if the input CRS is lon/lat
boolean lonFirst = (CRS.getAxisOrder(crs) == AxisOrder.EAST_NORTH);
// Look up the code
CoordinateReferenceSystem resolved = CRS.decode(code, lonFirst);
// is still the same (not guaranteed)
if (CRS.getAxisOrder(crs).equals(CRS.getAxisOrder(resolved))) {
return new CodeDefinition(code, resolved);
}
}
} catch (FactoryException e) {
// Ignore
}
return new WKTDefinition(crs.toWKT(), crs);
}
use of eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition in project hale by halestudio.
the class MsSqlGeometries 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, SQLServerConnection connection, Supplier<CRSDefinition> crsProvider) throws Exception {
Statement stmt = null;
ResultSet rs = null;
try {
// We need Column Data type
String columnDataType = columnType.getName().getLocalPart();
String geomAsHex = BaseEncoding.base16().lowerCase().encode((byte[]) geom);
String sqlGeom = //
"SELECT top 1 GeomConvert.geom.STSrid srid, GeomConvert.geom.STAsText() as geomAsText, GeomConvert.geom.STGeometryType() as geomType " + //
"FROM " + "(SELECT cast(cast(temp.wkb as varbinary(max)) as " + columnDataType + //
") as geom " + //
"FROM " + "( select " + "0x" + geomAsHex + //
" as wkb) as temp" + //
") " + //
"as GeomConvert";
stmt = connection.createStatement();
rs = stmt.executeQuery(sqlGeom);
Geometry jtsGeom = null;
int srId = 0;
if (rs.next()) {
srId = rs.getInt(1);
String geomAsText = rs.getString(2);
String geomType = rs.getString(3);
// WKTReader does not support CircularString, CurvePolygon,
// CompoundCurve
WKTReader wktReader = getSpecificWktReader(geomType);
try {
// conversion to JTS via WKT
jtsGeom = wktReader.read(geomAsText);
} catch (ParseException e) {
log.error("Could not load geometry from database", e);
}
}
CRSDefinition crsDef = null;
String authName = SRSUtil.getAuthorityName(srId, connection);
if (authName != null && authName.equals("EPSG")) {
// For geography/geometry data type, SQL server assumes lon/lat
// axis order, if we read using SQL function
String epsgCode = authName + ":" + SRSUtil.getSRS(srId, connection);
if (columnDataType.equals("geography"))
crsDef = new CodeDefinition(epsgCode, true);
else
crsDef = new CodeDefinition(epsgCode, null);
} else {
String wkt = SRSUtil.getSRSText(srId, connection);
if (wkt != null) {
crsDef = new WKTDefinition(wkt, null);
}
}
if (crsDef == null) {
log.warn("Could not find spatial reference system id " + srId + " in MS sql server");
crsDef = crsProvider.get();
if (crsDef == null) {
log.warn("Could not retrieve default spatial reference for " + srId + " in MS sql server");
}
// saving in cache
if (crsDef != null) {
String srsName = CRS.toSRS(crsDef.getCRS());
if (srsName != null) {
final int index = srsName.lastIndexOf(':');
String authorityName = null;
String authorizedId = null;
if (index > 0) {
authorityName = srsName.substring(0, index);
authorizedId = srsName.substring(index + 1).trim();
}
// we don't need wkt.
SRSUtil.addSRSinCache(srId, authorityName, authorizedId, null);
}
}
}
return new DefaultGeometryProperty<Geometry>(crsDef, jtsGeom);
} finally {
if (rs != null)
try {
rs.close();
} catch (Exception e) {
//
}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
//
}
}
}
use of eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition 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