use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition 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.geometry.CRSDefinition in project hale by halestudio.
the class OSerializationHelper method deserialize.
/**
* Deserialize a serialized value wrapped in the given document.
*
* @param doc the document
* @param parent the parent group
* @param childName the name of the child the value is associated to
* @return the deserialized value
*/
public static Object deserialize(ODocument doc, OGroup parent, QName childName) {
int serType = doc.field(FIELD_SERIALIZATION_TYPE);
switch(serType) {
case SERIALIZATION_TYPE_STRING:
{
// convert a string value back to its original form
Object val = doc.field(FIELD_STRING_VALUE);
String stringVal = (val == null) ? (null) : (val.toString());
ConvertProxy cp = CONVERTER_IDS.getObject((String) doc.field(FIELD_CONVERT_ID));
if (cp != null) {
return cp.convert(stringVal);
}
return stringVal;
}
case SERIALIZATION_TYPE_COLLECTION:
{
// recreate collection
Object val = doc.field(FIELD_VALUES);
Object typeVal = doc.field(FIELD_COLLECTION_TYPE);
CollectionType type = (typeVal != null) ? (CollectionType.valueOf(typeVal.toString())) : (CollectionType.LIST);
if (val instanceof Collection<?>) {
Collection<?> values = (Collection<?>) val;
Collection<Object> target = createCollection(type);
for (Object element : values) {
Object convElement = convertFromDB(element, parent, childName);
target.add(convElement);
}
return target;
}
}
break;
}
ORecordBytes record = (ORecordBytes) doc.field(BINARY_WRAPPER_FIELD);
Object result;
switch(serType) {
case SERIALIZATION_TYPE_BYTEARRAY:
result = record.toStream();
break;
case SERIALIZATION_TYPE_GEOM:
case SERIALIZATION_TYPE_GEOM_PROP:
ExtendedWKBReader reader = new ExtendedWKBReader();
try {
result = reader.read(record.toStream());
} catch (ParseException e1) {
throw new IllegalStateException("Unable to parse WKB to restore geometry", e1);
}
break;
case SERIALIZATION_TYPE_JAVA:
default:
ByteArrayInputStream bytes = new ByteArrayInputStream(record.toStream());
try {
ObjectInputStream in = new ObjectInputStream(bytes) {
@Override
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
Class<?> result = resolved.get(desc.getName());
if (result == null) {
result = OsgiUtils.loadClass(desc.getName(), null);
if (resolved.size() > 200) {
resolved.entrySet().iterator().remove();
}
resolved.put(desc.getName(), result);
}
if (result == null) {
throw new IllegalStateException("Class " + desc.getName() + " not found");
}
return result;
}
};
result = in.readObject();
} catch (Exception e) {
throw new IllegalStateException("Could not deserialize field value.", e);
}
break;
}
if (serType == SERIALIZATION_TYPE_GEOM_PROP) {
// wrap geometry in geometry property
// determine CRS
CRSDefinition crs = null;
Object crsId = doc.field(FIELD_CRS_ID);
if (crsId != null) {
crs = CRS_IDS.getObject(crsId.toString());
}
// create geometry property
GeometryProperty<Geometry> prop = new DefaultGeometryProperty<Geometry>(crs, (Geometry) result);
return prop;
}
return result;
}
use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition in project hale by halestudio.
the class GeopackageInstanceWriter method convertGeometry.
private GeoPackageGeometryData convertGeometry(Object someGeom, GeometryColumns geomColumns, SimpleLog log) {
Geometry geom = null;
CRSDefinition sourceCrs = null;
if (someGeom instanceof GeometryProperty<?>) {
GeometryProperty<?> prop = (GeometryProperty<?>) someGeom;
geom = prop.getGeometry();
sourceCrs = prop.getCRSDefinition();
} else if (someGeom instanceof Geometry) {
geom = (Geometry) someGeom;
}
GeoPackageGeometryData geometryData = new GeoPackageGeometryData(geomColumns.getSrsId());
if (geom != null) {
SpatialReferenceSystem targetSrs = geomColumns.getSrs();
CRSDefinition targetCrs = toCRSDefinition(targetSrs);
// do conversion to target CRS (if possible)
Geometry targetGeometry = geom;
try {
if (sourceCrs != null && targetCrs != null) {
MathTransform transform = CRS.findMathTransform(sourceCrs.getCRS(), targetCrs.getCRS());
targetGeometry = JTS.transform(geom, transform);
}
} catch (Exception e) {
log.error("Failed to convert geometry to target SRS " + targetSrs.getSrsName());
}
// XXX also an option to only use a SrsId or use a SrsId that
// differs from the column SrsId?
byte[] wkb = new WKBWriter().write(targetGeometry);
mil.nga.sf.Geometry geometry = GeometryReader.readGeometry(new ByteReader(wkb));
geometryData.setGeometry(geometry);
}
return geometryData;
}
use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition in project hale by halestudio.
the class AbstractCRSManager method getCRS.
@Override
public CRSDefinition getCRS(TypeDefinition parentType, List<QName> propertyPath, CRSDefinition defaultCrs) {
CRSDefinition result = null;
String resourceId = reader.getResourceIdentifier();
if (resourceId == null) {
// TODO warn about no resource Id?
// setting for any resource
resourceId = "";
} else {
resourceId = "resource-" + resourceId + ":";
}
// first, try configuration
// configuration for property
StringBuffer keybuilder = new StringBuffer();
keybuilder.append(resourceId);
keybuilder.append(PREFIX_PARAM_CRS);
keybuilder.append(parentType.getName());
for (QName property : propertyPath) {
keybuilder.append('/');
keybuilder.append(property);
}
final String propertyKey = keybuilder.toString();
result = CRSDefinitionManager.getInstance().parse(loadValue(propertyKey));
// overall configuration for resource
if (result == null && !resourceId.isEmpty()) {
result = CRSDefinitionManager.getInstance().parse(loadValue(resourceId + PARAM_DEFAULT_CRS));
}
// overall configuration
if (result == null) {
result = CRSDefinitionManager.getInstance().parse(loadValue(PARAM_DEFAULT_CRS));
}
if (result == null && provider != null) {
// consult default CRS provider
result = provider.getCRS(parentType, propertyPath, defaultCrs);
if (result != null) {
// store in configuration
storeValue(propertyKey, CRSDefinitionManager.getInstance().asString(result));
}
}
return result;
}
use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition in project hale by halestudio.
the class EPSGResolveCache method resolveCRS.
@Override
public CRSDefinition resolveCRS(final CoordinateReferenceSystem crs) {
CRSDefinition result = cached.get(crs);
if (result == null) {
result = CRSDefinitionUtil.lookupCrs(crs);
cached.put(crs, result);
}
return result;
}
Aggregations