use of eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition 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.WKTDefinition in project hale by halestudio.
the class GeopackageInstanceWriter method toCRSDefinition.
private CRSDefinition toCRSDefinition(SpatialReferenceSystem srs) {
String org = srs.getOrganization();
long code = srs.getOrganizationCoordsysId();
if (org != null && org.equals("EPSG")) {
// axis order is always x,y (longitude first)
return new CodeDefinition(org + ":" + code, true);
}
return new WKTDefinition(srs.getDefinition(), null);
}
use of eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition 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);
// Look up the code
CoordinateReferenceSystem resolved = CRS.decode(code);
// 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.WKTDefinition in project hale by halestudio.
the class CRSHelperFunctionsTest method testFromWKT.
@Test
public void testFromWKT() {
String wkt = "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]";
CRSDefinition crs = CRSHelperFunctions._fromWKT(wkt);
assertNotNull(crs);
assertNotNull(crs.getCRS());
assertTrue(crs instanceof WKTDefinition);
assertEquals(wkt, ((WKTDefinition) crs).getWkt());
}
use of eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition 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) {
//
}
}
}
Aggregations