Search in sources :

Example 1 with Coordinate

use of org.neo4j.graphdb.spatial.Coordinate in project neo4j by neo4j.

the class Neo4jJsonCodec method writeValue.

@Override
public void writeValue(JsonGenerator out, Object value) throws IOException {
    if (value instanceof PropertyContainer) {
        writePropertyContainer(out, (PropertyContainer) value, TransactionStateChecker.create(container));
    } else if (value instanceof Path) {
        writePath(out, ((Path) value).iterator(), TransactionStateChecker.create(container));
    } else if (value instanceof Iterable) {
        writeIterator(out, ((Iterable) value).iterator());
    } else if (value instanceof byte[]) {
        writeByteArray(out, (byte[]) value);
    } else if (value instanceof Map) {
        writeMap(out, (Map) value);
    } else if (value instanceof Geometry) {
        Geometry geom = (Geometry) value;
        Object coordinates = (geom instanceof Point) ? ((Point) geom).getCoordinate() : geom.getCoordinates();
        writeMap(out, genericMap(new LinkedHashMap<>(), "type", geom.getGeometryType(), "coordinates", coordinates, "crs", geom.getCRS()));
    } else if (value instanceof Coordinate) {
        Coordinate coordinate = (Coordinate) value;
        writeIterator(out, coordinate.getCoordinate().iterator());
    } else if (value instanceof CRS) {
        CRS crs = (CRS) value;
        writeMap(out, genericMap(new LinkedHashMap<>(), "name", crs.getType(), "type", "link", "properties", genericMap(new LinkedHashMap<>(), "href", crs.getHref() + "ogcwkt/", "type", "ogcwkt")));
    } else {
        super.writeValue(out, value);
    }
}
Also used : Path(org.neo4j.graphdb.Path) Geometry(org.neo4j.graphdb.spatial.Geometry) PropertyContainer(org.neo4j.graphdb.PropertyContainer) Coordinate(org.neo4j.graphdb.spatial.Coordinate) CRS(org.neo4j.graphdb.spatial.CRS) Point(org.neo4j.graphdb.spatial.Point) LinkedHashMap(java.util.LinkedHashMap) MapUtil.genericMap(org.neo4j.helpers.collection.MapUtil.genericMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with Coordinate

use of org.neo4j.graphdb.spatial.Coordinate in project neo4j by neo4j.

the class Neo4jJsonCodecTest method testGeometryWriting.

@Test
void testGeometryWriting() throws IOException {
    // Given
    List<Coordinate> points = new ArrayList<>();
    points.add(new Coordinate(1, 2));
    points.add(new Coordinate(2, 3));
    Geometry value = SpatialMocks.mockGeometry("LineString", points, mockCartesian());
    // When
    jsonCodec.writeValue(jsonGenerator, value);
    // Then
    verify(jsonGenerator, times(3)).writeEndObject();
}
Also used : Geometry(org.neo4j.graphdb.spatial.Geometry) Coordinate(org.neo4j.graphdb.spatial.Coordinate) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 3 with Coordinate

use of org.neo4j.graphdb.spatial.Coordinate in project neo4j by neo4j.

the class ExecutionResultSerializerTest method shouldErrorWhenSerializingUnknownGeometryType.

@Test
void shouldErrorWhenSerializingUnknownGeometryType() {
    // given
    var points = List.of(new Coordinate(1, 2), new Coordinate(2, 3));
    var row = Map.of("geom", SpatialMocks.mockGeometry("LineString", points, mockCartesian()));
    // when
    var e = assertThrows(RuntimeException.class, () -> {
        writeStatementStart(serializer, "geom");
        writeRecord(serializer, row, "geom");
    });
    writeError(serializer, Status.Statement.ExecutionFailed, e.getMessage());
    writeTransactionInfo(serializer);
    // then
    String result = output.toString(UTF_8);
    assertThat(result).startsWith("{\"results\":[{\"columns\":[\"geom\"],\"data\":[" + "{\"row\":[{\"type\":\"LineString\",\"coordinates\":[[1.0,2.0],[2.0,3.0]],\"crs\":" + "{\"srid\":7203,\"name\":\"cartesian\",\"type\":\"link\",\"properties\":" + "{\"href\":\"http://spatialreference.org/ref/sr-org/7203/ogcwkt/\",\"type\":\"ogcwkt\"}}}],\"meta\":[]}]}]," + "\"errors\":[{\"code\":\"Neo.DatabaseError.Statement.ExecutionFailed\"," + "\"message\":\"Unsupported Geometry type: type=MockGeometry, value=LineString\"");
}
Also used : Coordinate(org.neo4j.graphdb.spatial.Coordinate) Test(org.junit.jupiter.api.Test)

Example 4 with Coordinate

use of org.neo4j.graphdb.spatial.Coordinate in project neo4j by neo4j.

the class GraphDatabaseServiceExecuteTest method shouldNotReturnInternalCartesianPointType.

@Test
void shouldNotReturnInternalCartesianPointType() {
    // when
    try (Transaction transaction = db.beginTx()) {
        Result execute = transaction.execute("RETURN point({x: 13.37, y: 13.37, crs:'cartesian'}) AS p");
        // then
        Object obj = execute.next().get("p");
        assertThat(obj, Matchers.instanceOf(Point.class));
        Point point = (Point) obj;
        assertThat(point.getCoordinate(), equalTo(new Coordinate(13.37, 13.37)));
        CRS crs = point.getCRS();
        assertThat(crs.getCode(), equalTo(7203));
        assertThat(crs.getType(), equalTo("cartesian"));
        assertThat(crs.getHref(), equalTo("http://spatialreference.org/ref/sr-org/7203/"));
        transaction.commit();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Coordinate(org.neo4j.graphdb.spatial.Coordinate) CRS(org.neo4j.graphdb.spatial.CRS) Point(org.neo4j.graphdb.spatial.Point) Result(org.neo4j.graphdb.Result) Test(org.junit.jupiter.api.Test)

Example 5 with Coordinate

use of org.neo4j.graphdb.spatial.Coordinate in project neo4j by neo4j.

the class ExecutionResultSerializerTest method shouldSerializePointsAsListOfMapsOfProperties.

@Test
public void shouldSerializePointsAsListOfMapsOfProperties() throws Exception {
    // given
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ExecutionResultSerializer serializer = getSerializerWith(output);
    List<Coordinate> points = new ArrayList<>();
    points.add(new Coordinate(1, 2));
    points.add(new Coordinate(2, 3));
    Result executionResult = mockExecutionResult(map("geom", new MockPoint(12.3, 45.6, mockWGS84())), map("geom", new MockPoint(123, 456, mockCartesian())), map("geom", new MockGeometry("LineString", points, mockCartesian())));
    // when
    serializer.statementResult(executionResult, false);
    serializer.finish();
    // then
    String result = output.toString(UTF_8.name());
    assertEquals("{\"results\":[{\"columns\":[\"geom\"],\"data\":[" + "{\"row\":[{\"type\":\"Point\",\"coordinates\":[12.3,45.6],\"crs\":" + "{\"name\":\"WGS-84\",\"type\":\"link\",\"properties\":" + "{\"href\":\"http://spatialreference.org/ref/epsg/4326/ogcwkt/\",\"type\":\"ogcwkt\"}" + "}}],\"meta\":[null]}," + "{\"row\":[{\"type\":\"Point\",\"coordinates\":[123.0,456.0],\"crs\":" + "{\"name\":\"cartesian\",\"type\":\"link\",\"properties\":" + "{\"href\":\"http://spatialreference.org/ref/sr-org/7203/ogcwkt/\",\"type\":\"ogcwkt\"}" + "}}],\"meta\":[null]}," + "{\"row\":[{\"type\":\"LineString\",\"coordinates\":[[1.0,2.0],[2.0,3.0]],\"crs\":" + "{\"name\":\"cartesian\",\"type\":\"link\",\"properties\":" + "{\"href\":\"http://spatialreference.org/ref/sr-org/7203/ogcwkt/\",\"type\":\"ogcwkt\"}" + "}}],\"meta\":[null]}" + "]}],\"errors\":[]}", result);
}
Also used : Coordinate(org.neo4j.graphdb.spatial.Coordinate) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Result(org.neo4j.graphdb.Result) Test(org.junit.Test) Neo4jJsonCodecTest(org.neo4j.server.rest.transactional.Neo4jJsonCodecTest)

Aggregations

Coordinate (org.neo4j.graphdb.spatial.Coordinate)7 Geometry (org.neo4j.graphdb.spatial.Geometry)4 ArrayList (java.util.ArrayList)3 Test (org.junit.jupiter.api.Test)3 CRS (org.neo4j.graphdb.spatial.CRS)3 Point (org.neo4j.graphdb.spatial.Point)3 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 Path (org.neo4j.graphdb.Path)2 Result (org.neo4j.graphdb.Result)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Temporal (java.time.temporal.Temporal)1 TemporalAmount (java.time.temporal.TemporalAmount)1 Entity (org.neo4j.graphdb.Entity)1 PropertyContainer (org.neo4j.graphdb.PropertyContainer)1 Transaction (org.neo4j.graphdb.Transaction)1 MapUtil.genericMap (org.neo4j.helpers.collection.MapUtil.genericMap)1 MapUtil.genericMap (org.neo4j.internal.helpers.collection.MapUtil.genericMap)1 TransactionStateChecker (org.neo4j.server.http.cypher.TransactionStateChecker)1