Search in sources :

Example 1 with WKBWriter

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;
}
Also used : GeoPackageGeometryData(mil.nga.geopackage.geom.GeoPackageGeometryData) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) WKBWriter(org.locationtech.jts.io.WKBWriter) MathTransform(org.opengis.referencing.operation.MathTransform) ByteReader(mil.nga.sf.util.ByteReader) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) ConversionException(org.springframework.core.convert.ConversionException) SQLException(java.sql.SQLException) IOException(java.io.IOException) Geometry(org.locationtech.jts.geom.Geometry) SpatialReferenceSystem(mil.nga.geopackage.core.srs.SpatialReferenceSystem)

Example 2 with WKBWriter

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);
}
Also used : WKBWriter(org.locationtech.jts.io.WKBWriter)

Example 3 with WKBWriter

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;
}
Also used : DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) HashSet(java.util.HashSet) Set(java.util.Set) WKBWriter(org.locationtech.jts.io.WKBWriter) ORecordBytes(com.orientechnologies.orient.core.record.impl.ORecordBytes) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) Geometry(org.locationtech.jts.geom.Geometry) Coordinate(org.locationtech.jts.geom.Coordinate) ConversionService(org.springframework.core.convert.ConversionService) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

WKBWriter (org.locationtech.jts.io.WKBWriter)3 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)2 IOException (java.io.IOException)2 Geometry (org.locationtech.jts.geom.Geometry)2 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 ORecordBytes (com.orientechnologies.orient.core.record.impl.ORecordBytes)1 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)1 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 SpatialReferenceSystem (mil.nga.geopackage.core.srs.SpatialReferenceSystem)1 GeoPackageGeometryData (mil.nga.geopackage.geom.GeoPackageGeometryData)1 ByteReader (mil.nga.sf.util.ByteReader)1