use of org.locationtech.jts.io.WKBWriter 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 org.locationtech.jts.io.WKBWriter in project h2database by h2database.
the class ValueGeometry method convertToWKB.
private static byte[] convertToWKB(Geometry g) {
boolean includeSRID = g.getSRID() != 0;
int dimensionCount = getDimensionCount(g);
WKBWriter writer = new WKBWriter(dimensionCount, includeSRID);
return writer.write(g);
}
use of org.locationtech.jts.io.WKBWriter in project hale by halestudio.
the class OSerializationHelper method serialize.
/**
* Serialize and/or wrap a value not supported as field in OrientDB so it
* can be stored in the database.
*
* @param value the value to serialize
* @param log the log
* @return the document wrapping the value
*/
public static ODocument serialize(Object value, SimpleLog log) {
/*
* As collections of ORecordBytes are not supported (or rather of
* records that are no documents, see embeddedCollectionToStream in
* ORecordSerializerCSVAbstract ~578) they are wrapped in a document.
*/
ODocument doc = new ODocument();
// try conversion to string first
final ConversionService cs = HalePlatform.getService(ConversionService.class);
if (cs != null) {
// check if conversion allowed and possible
if (CONV_WHITE_LIST.contains(value.getClass()) && cs.canConvert(value.getClass(), String.class) && cs.canConvert(String.class, value.getClass())) {
String stringValue = cs.convert(value, String.class);
ConvertProxy convert = new ConvertProxy(cs, value.getClass());
doc.field(FIELD_CONVERT_ID, CONVERTER_IDS.getId(convert));
doc.field(FIELD_SERIALIZATION_TYPE, SERIALIZATION_TYPE_STRING);
doc.field(FIELD_STRING_VALUE, stringValue);
return doc;
}
}
if (value instanceof Collection) {
CollectionType type = null;
if (value instanceof List) {
type = CollectionType.LIST;
} else if (value instanceof Set) {
type = CollectionType.SET;
}
if (type != null) {
// wrap collection values
Collection<?> elements = (Collection<?>) value;
List<Object> values = new ArrayList<Object>();
for (Object element : elements) {
Object convElement = convertForDB(element, log);
values.add(convElement);
}
// set values
// XXX ok to always use EMBEDDEDLIST as type?
doc.field(FIELD_VALUES, values, OType.EMBEDDEDLIST);
doc.field(FIELD_SERIALIZATION_TYPE, SERIALIZATION_TYPE_COLLECTION);
doc.field(FIELD_COLLECTION_TYPE, type.name());
return doc;
}
}
ORecordBytes record = new ORecordBytes();
int serType = SERIALIZATION_TYPE_JAVA;
if (value instanceof GeometryProperty<?>) {
GeometryProperty<?> geomProp = (GeometryProperty<?>) value;
// store (runtime) CRS ID (XXX OK as storage is temporary)
doc.field(FIELD_CRS_ID, CRS_IDS.getId(geomProp.getCRSDefinition()));
// extract geometry
value = geomProp.getGeometry();
if (value != null) {
serType = SERIALIZATION_TYPE_GEOM_PROP;
} else {
return null;
}
}
if (value.getClass().isArray() && value.getClass().getComponentType().equals(byte.class)) {
// direct byte array support
record.fromStream((byte[]) value);
serType = SERIALIZATION_TYPE_BYTEARRAY;
}
if (value instanceof Geometry) {
// serialize geometry as WKB
Geometry geom = (Geometry) value;
Coordinate sample = geom.getCoordinate();
int dimension = (sample != null && !Double.isNaN(sample.z)) ? (3) : (2);
WKBWriter writer = new ExtendedWKBWriter(dimension);
record.fromStream(writer.write(geom));
if (serType != SERIALIZATION_TYPE_GEOM_PROP) {
serType = SERIALIZATION_TYPE_GEOM;
}
} else {
// object serialization
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try {
ObjectOutputStream out = new ObjectOutputStream(bytes);
out.writeObject(value);
} catch (IOException e) {
log.error("Could not serialize field value of type {0}, null value is used instead.", value.getClass().getName());
// cannot be stored - use null value instead
return null;
}
record.fromStream(bytes.toByteArray());
}
/*
* XXX Class name is set in OGroup.configureDocument, as the class name
* may only bet set after the database was set.
*/
// doc.setClassName(BINARY_WRAPPER_CLASSNAME);
doc.field(BINARY_WRAPPER_FIELD, record);
doc.field(FIELD_SERIALIZATION_TYPE, serType);
return doc;
}
Aggregations