activegraph icon indicating copy to clipboard operation
activegraph copied to clipboard

[Help] How can I store latitude and longitude as point type in neo4j.

Open nvtin opened this issue 5 years ago • 4 comments

Hey guys, I want to store location with latitude and longitude is a point type on Database to use spatial functions to get the distance between location. https://neo4j.com/docs/developer-manual/current/cypher/functions/spatial/#functions-distance

I know I can store latitude, longitude on model as String type like below code:

Class ABC
  include Neo4j::ActiveNode

  property :latitude, type: String
  property :longitude, type: String
end

But for some reason, we want to store 2 that properties in one field called location with Point type. Maybe it look like on this article https://medium.com/neo4j/whats-new-in-neo4j-spatial-features-586d69cda8d0 So the location should be like this: point({longitude: 56.7, latitude: 12.78, crs: 'wgs-84'})

I have tried to google but I have not found solution to do that or it might have not support by neo4j yet. Any one know about this, can you help me?

Runtime information:

Neo4j database version: neo4j gem version: 9.3.0 neo4j-core gem version: 8.1.4 Ruby gem version : 2.5.1 Rails gem version: 5.2.0

nvtin avatar Aug 06 '18 10:08 nvtin

There are a few ways you might be able to accomplish this.

  1. Neo4j supports ActiveModel serialization, which you could use to save a hash as json in a single field
  2. Neo4j (the database) supports arrays as a native property type. Arrays can only store a subset of the available primitive types, such as string, int, and float. You can natively save an array of these types in a single field.
    • i.e. new ABC(lang_long: [56.7, 12.78, 'wgs-84'])
  3. Neo4j (the gem) supports creating custom data serializers, which will convert a ruby object to a neo4j database compatible form before saving, and then reverse the process when an activenode model is loaded.

jorroll avatar Aug 07 '18 18:08 jorroll

@thefliik Thanks for your reply. I have stored it as string to resolve this issue.. But I will back to find a good solution to store it with Point type. Thanks so much.

nvtin avatar Aug 09 '18 04:08 nvtin

@nvtin Have you found a solution to save latitude and longitude as point type? I've been trying to define custom converters for point type, but I did not make it. Have you got any idea about how to do this?

iloveivyxuan avatar Jun 11 '19 05:06 iloveivyxuan

@iloveivyxuan Not yet. I have stored it as string and leave it until now.

nvtin avatar Jun 12 '19 03:06 nvtin