Skip to content

Commit

Permalink
Support returning array of points using resultDataContents: "rest""
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Oct 12, 2018
1 parent 0b19162 commit db423d7
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 22 deletions.
Expand Up @@ -19,6 +19,11 @@
*/
package org.neo4j.server.helpers;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZonedDateTime;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAmount;
import java.util.Iterator;
Expand Down Expand Up @@ -97,6 +102,30 @@ else if ( property instanceof String[] )
{
return dispatchStringArrayProperty( (String[]) property, param );
}
else if ( property instanceof Point[] )
{
return dispatchPointArrayProperty( (Point[]) property, param );
}
//TODO
// else if ( property instanceof ZonedDateTime[] )
// {
// }
// else if ( property instanceof LocalDateTime[] )
// {
// }
// else if ( property instanceof LocalTime[] )
// {
// }
// else if ( property instanceof OffsetTime[] )
// {
// }
// else if ( property instanceof LocalDate[] )
// {
// }
// else if ( property instanceof TemporalAmount[] )
// {
//
// }
else if ( property instanceof Object[] )
{
return dispatchOtherArray( (Object[]) property, param );
Expand Down Expand Up @@ -681,6 +710,23 @@ protected T dispatchStringArrayProperty( PropertyArray<String[], String> array,
return dispatchArray( array, param );
}

protected T dispatchPointArrayProperty( final Point[] property, K param )
{
return dispatchPointArrayProperty( new BoxedArray<Point[], Point>( property )
{
@Override
public Point[] getClonedArray()
{
return property.clone();
}
}, param );
}

protected T dispatchPointArrayProperty( PropertyArray<Point[], Point> array, K param )
{
return dispatchArray( array, param );
}

protected T dispatchByteArrayProperty( PropertyArray<byte[], Byte> array, K param )
{
return dispatchNumberArray( array, param );
Expand Down
Expand Up @@ -24,6 +24,7 @@
import java.util.Iterator;

import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.spatial.Point;
import org.neo4j.helpers.collection.IterableWrapper;
import org.neo4j.helpers.collection.PrefetchingIterator;

Expand Down Expand Up @@ -105,6 +106,24 @@ protected Representation underlyingObjectToObject( String value )
} );
}

public static ListRepresentation points( Point... values )
{
return point( Arrays.asList( values ) );
}

public static ListRepresentation point( Iterable<Point> values )
{
return new ListRepresentation( RepresentationType.POINT, new IterableWrapper<Representation, Point>(
values )
{
@Override
protected Representation underlyingObjectToObject( Point value )
{
return ValueRepresentation.point( value );
}
} );
}

public static ListRepresentation relationshipTypes( Iterable<RelationshipType> types )
{
return new ListRepresentation( RepresentationType.RELATIONSHIP_TYPE,
Expand Down
Expand Up @@ -71,6 +71,13 @@ public static ValueRepresentation string( String value )
return new ValueRepresentation( RepresentationType.STRING, value );
}

public static ValueRepresentation point( Point value )
{
//TODO sending in the point here looks like it will use Jackson object mapper
// to serialize, maybe send in a Map instead
return new ValueRepresentation( RepresentationType.POINT, value );
}

@SuppressWarnings( "boxing" )
public static ValueRepresentation number( int value )
{
Expand Down Expand Up @@ -233,6 +240,12 @@ protected Representation dispatchStringArrayProperty( String[] property, Void pa
return ListRepresentation.strings( property );
}

@Override
protected Representation dispatchPointArrayProperty( Point[] property, Void param )
{
return ListRepresentation.points( property );
}

@SuppressWarnings( "unchecked" )
private Iterable<Representation> dispatch( PropertyArray<?,?> array )
{
Expand Down
Expand Up @@ -79,28 +79,7 @@ protected Void dispatchPointProperty( Point property, String param )
{
MappingWriter pointWriter = writer.newMapping( RepresentationType.POINT, param );

pointWriter.writeString( "type", property.getGeometryType() );

//write coordinates
ListWriter coordinatesWriter = pointWriter.newList( RepresentationType.DOUBLE, "coordinates" );
for ( Double coordinate : property.getCoordinate().getCoordinate() )
{
coordinatesWriter.writeFloatingPointNumber( RepresentationType.DOUBLE, coordinate );
}
coordinatesWriter.done();

//Write coordinate reference system
CRS crs = property.getCRS();
MappingWriter crsWriter = pointWriter.newMapping( RepresentationType.MAP, "crs" );
crsWriter.writeInteger( RepresentationType.INTEGER, "srid", crs.getCode() );
crsWriter.writeString( "name", crs.getType() );
crsWriter.writeString( "type", "link" );
MappingWriter propertiesWriter = crsWriter.newMapping( Representation.MAP, "properties" );
propertiesWriter.writeString( "href", crs.getHref() + "ogcwkt/" );
propertiesWriter.writeString( "type","ogcwkt" );
propertiesWriter.done();
crsWriter.done();

writePoint( pointWriter, property );
pointWriter.done();
return null;
}
Expand Down Expand Up @@ -187,6 +166,20 @@ protected Void dispatchStringArrayProperty( String[] property, String param )
return null;
}

@Override
protected Void dispatchPointArrayProperty( Point[] property, String param )
{
ListWriter list = writer.newList( RepresentationType.POINT, param );
for ( Point p : property )
{
MappingWriter pointWriter = list.newMapping( RepresentationType.POINT );
writePoint( pointWriter, p);
pointWriter.done();
}
list.done();
return null;
}

@Override
@SuppressWarnings( "boxing" )
protected Void dispatchByteArrayProperty( PropertyArray<byte[], Byte> array, String param )
Expand Down Expand Up @@ -290,6 +283,30 @@ protected Void dispatchBooleanArrayProperty( PropertyArray<boolean[], Boolean> a
list.done();
return null;
}

private void writePoint( MappingWriter pointWriter, Point property )
{
pointWriter.writeString( "type", property.getGeometryType() );
//write coordinates
ListWriter coordinatesWriter = pointWriter.newList( RepresentationType.DOUBLE, "coordinates" );
for ( Double coordinate : property.getCoordinate().getCoordinate() )
{
coordinatesWriter.writeFloatingPointNumber( RepresentationType.DOUBLE, coordinate );
}
coordinatesWriter.done();

//Write coordinate reference system
CRS crs = property.getCRS();
MappingWriter crsWriter = pointWriter.newMapping( RepresentationType.MAP, "crs" );
crsWriter.writeInteger( RepresentationType.INTEGER, "srid", crs.getCode() );
crsWriter.writeString( "name", crs.getType() );
crsWriter.writeString( "type", "link" );
MappingWriter propertiesWriter = crsWriter.newMapping( Representation.MAP, "properties" );
propertiesWriter.writeString( "href", crs.getHref() + "ogcwkt/" );
propertiesWriter.writeString( "type","ogcwkt" );
propertiesWriter.done();
crsWriter.done();
}
}

public static Representation value( Object property )
Expand Down
Expand Up @@ -30,6 +30,7 @@
import static org.neo4j.helpers.collection.MapUtil.genericMap;
import static org.neo4j.server.rest.repr.ValueRepresentation.bool;
import static org.neo4j.server.rest.repr.ValueRepresentation.number;
import static org.neo4j.server.rest.repr.ValueRepresentation.point;
import static org.neo4j.server.rest.repr.ValueRepresentation.string;

/**
Expand Down Expand Up @@ -93,6 +94,17 @@ protected Representation dispatchStringArrayProperty( String[] array, String par
return new ListRepresentation( "", values );
}

@Override
protected Representation dispatchPointArrayProperty( Point[] array, String param )
{
ArrayList<Representation> values = new ArrayList<>();
for ( Point p : array )
{
values.add( point( p) );
}
return new ListRepresentation( "", values );
}

@Override
@SuppressWarnings( "boxing" )
protected Representation dispatchShortArrayProperty( PropertyArray<short[], Short> array, String param )
Expand Down
Expand Up @@ -138,6 +138,32 @@ public void shouldHandlePointsUsingRestResultDataContent() throws Exception
assertCrsEqual( WGS84, row );
}

@Test
public void shouldHandleArrayOfPointsUsingRestResultDataContent() throws Exception
{
//Given
GraphDatabaseFacade db = server().getDatabase().getGraph();
try ( Transaction tx = db.beginTx() )
{
Node node = db.createNode( label( "N" ) );
node.setProperty( "coordinates", new Point[]{pointValue( WGS84, 30.655691, 104.081602 )});
tx.success();
}

//When
HTTP.Response response = runQuery( "MATCH (n:N) RETURN n", "rest" );

assertEquals( 200, response.status() );
assertNoErrors( response );

//Then
JsonNode row = response.get( "results" ).get( 0 ).get( "data" ).get( 0 ).get( "rest" )
.get( 0 ).get( "data" ).get( "coordinates" ).get( 0 );
assertGeometryTypeEqual( GeometryType.GEOMETRY_POINT, row );
assertCoordinatesEqual( new double[]{30.655691, 104.081602}, row );
assertCrsEqual( WGS84, row );
}

private static void testPoint( String query, double[] expectedCoordinate, CoordinateReferenceSystem expectedCrs, String expectedType ) throws Exception
{
HTTP.Response response = runQuery( query );
Expand Down

0 comments on commit db423d7

Please sign in to comment.