neo4jrb / activegraph

An active model wrapper for the Neo4j Graph Database for Ruby.
http://neo4jrb.io
MIT License
1.4k stars 276 forks source link

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

Open nvtin opened 6 years ago

nvtin commented 6 years ago

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

jorroll commented 6 years ago

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.
nvtin commented 6 years ago

@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.

iloveivyxuan commented 5 years ago

@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?

nvtin commented 5 years ago

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