1

i'm trying to add a lon lat point to a node. i was unable to find any doc regarding which java class should be used.

Here is what i have:

@NodeEntity
public class Coordination {

    @Id
    @GeneratedValue
    private Long id;

    @Index
    private Value point;


    private Double lon;
    private Double lat;
    @Relationship(type = "GEO_LOCATION", direction = Relationship.INCOMING)
    private List<GeoLocation> geoLocations;

    public Coordination(Double lon, Double lat) {
        Value point = new PointValue(new InternalPoint2D(4326, lon, lat));
        this.lon = lon;
        this.lat = lat;
    }
}

Value class is not working for me. what do i miss?

USer22999299
  • 5,284
  • 9
  • 46
  • 78

1 Answers1

2

Using a CompositeAttributeConverter it is possible to define your own class to store lat and lon coordinates. However, you can also use the built-in Point and corresponding Distance classes defined in Spring Data Commons.

Here's an example from the test cases in Spring Data Neo4j:

public class Restaurant implements Comparable<Restaurant> {

    @Id @GeneratedValue private Long id;
    private String name;
    @Convert(PointConverter.class) private Point location; //Encapsulates lat/lon 
    private int zip;
    private double score;
    private String description;
    private boolean halal;

You can then define methods such as:

import org.springframework.data.geo.Point;
import org.springframework.data.neo4j.conversion.PointConverter;

public interface RestaurantRepository extends Neo4jRepository<Restaurant, Long> {

    List<Restaurant> findByNameAndLocationNear(String name, Distance distance, Point point);

    List<Restaurant> findByLocationNearAndName(Distance distance, Point point, String name);
}

The finder method will generate CYPHER that passes the following arguments to a distance function:

  • The lat and lon properties defined on your node
  • The at and lon arguments passed in as an argument to the finder method.

Is this what you were looking for? If yes great! If not, could you please clarify your question.

Jasper Blues
  • 28,258
  • 22
  • 102
  • 185
  • could you share the import statements of Point? for both classes? – USer22999299 Nov 21 '18 at 16:14
  • Coordination findByPoint(@Param("point") Point point); causing the following error : org.neo4j.ogm.exception.core.MappingException: Properties with a CompositeAttributeConverter are not supported by Filters in this version of OGM. Consider implementing a custom FilterFunction. – USer22999299 Nov 21 '18 at 16:24
  • had no problem to save the objects, but when using findByPoint(Poinit point) i'm getting Properties with a CompositeAttributeConverter are not supported by Filters in this version of OGM. Consider implementing a custom FilterFunction. – USer22999299 Nov 21 '18 at 17:59
  • I have included the imports. For an example of findBy (read) please clone spring-data-neo4j repository on the master branch and run `RestaurantTests`. The version of neo4j-ogm used there appears to be 3.1.4 (latest). . . incidentally, could you also share the versions of spring-data-neo4j, spring-data-commons and neo4j-ogm that you are using? – Jasper Blues Nov 21 '18 at 22:06
  • Thank you!! that absolutely helped! So seems like that the only way to search is by distance, which is working now. the only problem now is that its impossible to search by Point Only, any idea on how it should be handle? or it maybe impossible? this is what im getting when only querying by Point (without distance) Properties with a CompositeAttributeConverter are not supported by Filters in this version of OGM. Consider implementing a custom FilterFunction. – USer22999299 Nov 22 '18 at 14:27
  • From memory, point is not supported. It would be simple to add - I could show you how to send a pull request. Meantime, to search by point you can compare lat and lon properties, example findByLatAndLong(Double lat, Double lon) . . optionally you could specify a small tolerance, as these are floating point values. – Jasper Blues Nov 22 '18 at 15:16
  • 1
    let's connect : jasper@liberation-data.com . . from there we can set up a video call, and I'll run you through the code you'd need to write to support `Point` – Jasper Blues Nov 22 '18 at 23:01