Search in sources :

Example 6 with SpatialContextFactory

use of org.locationtech.spatial4j.context.SpatialContextFactory in project janusgraph by JanusGraph.

the class SerializerTest method testLegacyNonJtsSerialization.

@Test
public void testLegacyNonJtsSerialization() throws Exception {
    final SpatialContextFactory factory = new SpatialContextFactory();
    factory.geo = true;
    final SpatialContext context = new SpatialContext(factory);
    BinaryCodec binaryCodec = new BinaryCodec(context, factory);
    Shape[] shapes = new Shape[] { context.getShapeFactory().pointXY(2.5, 0.5), context.getShapeFactory().rect(2.5, 3.5, 0.5, 1.5), context.getShapeFactory().circle(2.5, 0.5, DistanceUtils.dist2Degrees(5, DistanceUtils.EARTH_MEAN_RADIUS_KM)) };
    DataOutput out = serialize.getDataOutput(128);
    for (final Shape shape : shapes) {
        // manually serialize with non-JTS codec
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        outputStream.write(1);
        try (DataOutputStream dataOutput = new DataOutputStream(outputStream)) {
            binaryCodec.writeShape(dataOutput, shape);
            dataOutput.flush();
        }
        outputStream.flush();
        byte[] bytes = outputStream.toByteArray();
        VariableLong.writePositive(out, bytes.length);
        out.putBytes(bytes);
    }
    // deserialize with standard serializer
    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    assertEquals(Geoshape.geoshape(shapes[0]), serialize.readObjectNotNull(b, Geoshape.class));
    assertEquals(Geoshape.geoshape(shapes[1]), serialize.readObjectNotNull(b, Geoshape.class));
    assertEquals(Geoshape.geoshape(shapes[2]), serialize.readObjectNotNull(b, Geoshape.class));
}
Also used : BinaryCodec(org.locationtech.spatial4j.io.BinaryCodec) DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) SpatialContextFactory(org.locationtech.spatial4j.context.SpatialContextFactory) SpatialContext(org.locationtech.spatial4j.context.SpatialContext) Shape(org.locationtech.spatial4j.shape.Shape) DataOutputStream(java.io.DataOutputStream) Geoshape(org.janusgraph.core.attribute.Geoshape) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Aggregations

SpatialContextFactory (org.locationtech.spatial4j.context.SpatialContextFactory)6 RectangleImpl (org.locationtech.spatial4j.shape.impl.RectangleImpl)4 QuadPrefixTree (org.apache.lucene.spatial.prefix.tree.QuadPrefixTree)3 Test (org.junit.Test)2 Repeat (com.carrotsearch.randomizedtesting.annotations.Repeat)1 DataOutputStream (java.io.DataOutputStream)1 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)1 FieldType (org.apache.lucene.document.FieldType)1 PackedQuadPrefixTree (org.apache.lucene.spatial.prefix.tree.PackedQuadPrefixTree)1 SpatialOperation (org.apache.lucene.spatial.query.SpatialOperation)1 Geoshape (org.janusgraph.core.attribute.Geoshape)1 ReadBuffer (org.janusgraph.diskstorage.ReadBuffer)1 DataOutput (org.janusgraph.graphdb.database.serialize.DataOutput)1 Test (org.junit.jupiter.api.Test)1 SpatialContext (org.locationtech.spatial4j.context.SpatialContext)1 BinaryCodec (org.locationtech.spatial4j.io.BinaryCodec)1 Circle (org.locationtech.spatial4j.shape.Circle)1 Point (org.locationtech.spatial4j.shape.Point)1 Shape (org.locationtech.spatial4j.shape.Shape)1