0

I am trying to use Apache AGE to store geographical information. I have a table called locations that contains the following columns:

id: The unique identifier of the location name: The name of the location latitude: The latitude of the location longitude: The longitude of the location I want to be able to query the table to find all locations within a certain radius of a given location. I have tried using the following Cypher query, but it is not working:

MATCH (location)
WHERE location.latitude > latitude - radius AND location.latitude < latitude + radius AND location.longitude > longitude - radius AND location.longitude < longitude + radius
RETURN location

The query is not returning any results. What am I doing wrong?

cybersam
  • 63,203
  • 6
  • 53
  • 76

7 Answers7

0

I believe you forgot to properly reference the property, you should use the following query:

MATCH (givenLocation {id: 1}), (location)
WHERE location.latitude > givenLocation.latitude - radius 
  AND location.latitude < givenLocation.latitude + radius 
  AND location.longitude > givenLocation.longitude - radius 
  AND location.longitude < givenLocation.longitude + radius
RETURN location

In the corrected query, I've introduced a new node givenLocation with an id property set to 1 to represent some given location. You can replace 1 with the desired ID value or use other property to get the desired location.

Please note that radius should be a specific value representing the distance from the given location within which you want to find other locations.

But, I don't think it works this way, I recommend looking into this answer, which provides a calculation for determining the desired distance. You can use the same calculation mentioned in that answer.

Wendel
  • 763
  • 1
  • 12
  • This "correction" does not make sense. The proposed `WHERE` would fail unless `radius` is negative, and in that case all locations would match (making the `WHERE` superfluous). – cybersam Jun 30 '23 at 17:56
0

if radius is less than or equal to 0, then the query will return nothing and if the radius is positive then the query should return all nodes because any quantity is always less than quantity + a positive number and greater than quantity - a positive number.

to find all nodes in the radius of a location first you need to find or provide the required location and the find other nodes that comes within the radius of that node.

0

Everything is not referenced properly.

MATCH (location)
WHERE location.latitude > latitude - radius AND location.latitude < latitude + 
radius AND location.longitude > longitude - radius AND location.longitude < 
longitude + radius
RETURN location

In the query you have written, there is no reference to latitude, or radius and the compiler cannot interpret without it. You have to tell it explicitly from where it has to take those values whether from the location table in this form(location.radius) or is there some other table from which these values can be taken.

So, check those things and rewrite your query and overcoming those weaknesses and it will work fine.

0

The Cypher query you gave appears to be lacking reference to a few crucial elements that could be the root of the problem, Latitude and radius are not mentioned in the query you have written. The compiler cannot read the question without them.

0

As per my understanding, the provided query lacks proper referencing for the variables latitude, longitude, and radius. Without explicit instructions on where to obtain these values, the compiler cannot interpret the query correctly. To address this issue, you need to specify the source of these variables, whether they are from the location table in the form of location.radius or obtained from another table.

To make the query work correctly, you should rewrite it by clearly indicating the source of these variables and providing the appropriate references. This way, the compiler can interpret the query properly and execute it without issues.

I hope this will solve the issue!

0

You can use the Haversine formula to find the locations in a given radius using longitude and latitude. Here's the modified query:

MATCH (location)
WHERE distance(point({latitude: location.latitude, longitude: location.longitude}), point({latitude: targetLatitude, longitude: targetLongitude})) <= radius
RETURN location

Here, replace targetLatitude, targetLongitude and radius with your values.

adil shahid
  • 125
  • 4
0
MATCH (loc:Location)
WHERE distance(loc.latitude, loc.longitude, {latitude}, {longitude}) <= {radius}
RETURN loc

Note: distance function is a user-defined function you would need to implement.

The Haversine formula can be used within this function to calculate the distance between two sets of latitude and longitude coordinates.