Questions about Neo4j spatial

31 views
Skip to first unread message

fede martinez

unread,
Feb 11, 2015, 5:06:47 PM2/11/15
to ne...@googlegroups.com
Hello,
I'm working on a school project which consists of comparing Neo4j spatial capabilities vs ArangoDB.

My idea is to load in Neo4j a list of airports with latitude and long and then a list of countries which would be polygons. The airports have edges if there is a route between them. I'd like to able to extract data such as:

* From what airports in Argentina is it possible to arrive to England
* A list of route options that leave a US city, have one stop somewhere else and then arrive in Germany

What I would like to know is if I should create a layer for the airports and another for the countries or if I should use only one, because I don't know if can do "cross layers" queries. Also I'd like to know, because so far I haven't been able to find the information, if I can use all the operations from the jts using Cypher, and if not, which ones are the operations I can do using Cypher?

Thanks in advance

Federico Martinez

Michael Hunger

unread,
Feb 11, 2015, 7:34:28 PM2/11/15
to ne...@googlegroups.com, Craig Taverner
you can use withindistance, bbox and within geometry queries from cypher,

see the neo4j-spatial docs:


HTH Michael

--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

fede martinez

unread,
Feb 12, 2015, 7:27:45 AM2/12/15
to ne...@googlegroups.com, craig.t...@neotechnology.com
Thanks Michael,

I had already read that link but I wasn't sure if the other operations from jts were available or not. Aslo, in all the examples the geo functions are used with constants. Is it possible to use them with attributes from other nodes? I mean something like:
'withinDistance:[another_node.x, another_node.y, 100]' instead of 'withinDistance:[60.0,15.0, 100.0]'

Fede

Craig Taverner

unread,
Feb 12, 2015, 9:17:35 AM2/12/15
to ne...@googlegroups.com
Hi,

In current Neo4j releases the cypher support is limited to only the legacy indexes, and as Michael pointed out, the 'withinDistance' type queries you can use with the legacy index. However, your use case sounds like something entirely separate from this. I see no distance calculation need at all. You are only asking about connected edges between airports, correlated to knowledge about which countries those airports are in. In fact there is no need for Neo4j Spatial at all, because the knowledge of the country an airport belongs to is always known about all airports.

Your graph model would have country nodes (no polygons required) and the airport nodes would be attached to the country nodes to which they belong. Edges between airports indicate routes. Then the two use cases you asked about would be solved like this:
  • From what airports in Argentina is it possible to arrive to England
  • MATCH (:Country {name:"Argentina"})<-[:IN]-(a:Airport)-[:ROUTE*1..2]-(:Airport)-[:IN]->(:Country {name:"England")"})
    RETURN a.name;
  • A list of route options that leave a US city, have one stop somewhere else and then arrive in Germany
  • MATCH (:Country {name:"USA"})<-[:IN]-(a:Airport), (b:Airport)-[:IN]->(:Country {name:"Germany")"})
    MATCH (a)
    -[:ROUTE]->(x)-[:ROUTE]->(b)
    RETURN a.name as depart, x.name as transit, b.name as arrive;

None of the above requires Neo4j Spatial, or the import of any spatial information into the database at all.

However, if you are especially interested in using Neo4j Spatial, and want to have queries that make use of country polygons, you will need to use the Java embedded API, which allows you to import polygons into the spatial index and perform queries on those. For your use case I do not see the need, so I will not describe that option further, unless you also suggest some use cases that do.

Regards, Craig

Fede Martinez

unread,
Feb 12, 2015, 9:20:53 AM2/12/15
to Craig Taverner, ne...@googlegroups.com
Craig,
I know the model I presented is not the best and that using edges between countries and airports is more sensible, but the idea was to apply spatial, because my essay is about that. It's like a toy example

I think I'll go with java then. I wanted to use Cypher because ArangoDB, the other database I'm using, exposes all its spatial functions (way more basic than the ones that Neo4j supports through java) in its query language AQL.

Thank you!

2015-02-12 11:10 GMT-03:00 Craig Taverner <craig.t...@neotechnology.com>:
Hi,

In current Neo4j releases the cypher support is limited to only the legacy indexes, and as Michael pointed out, the 'withinDistance' type queries you can use with the legacy index. However, your use case sounds like something entirely separate from this. I see no distance calculation need at all. You are only asking about connected edges between airports, correlated to knowledge about which countries those airports are in. In fact there is no need for Neo4j Spatial at all, because the knowledge of the country an airport belongs to is always known about all airports.

Your graph model would have country nodes (no polygons required) and the airport nodes would be attached to the country nodes to which they belong. Edges between airports indicate routes. Then the two use cases you asked about would be solved like this:
  • From what airports in Argentina is it possible to arrive to England
  • MATCH (:Country {name:"Argentina"})<-[:IN]-(a:Airport)-[:ROUTE*1..2]-(:Airport)-[:IN]->(:Country {name:"England")"})
    RETURN a.name;
  • A list of route options that leave a US city, have one stop somewhere else and then arrive in Germany
  • MATCH (:Country {name:"USA"})<-[:IN]-(a:Airport), (b:Airport)-[:IN]->(:Country {name:"Germany")"})
    MATCH (a)
    -[:ROUTE]->(x)-[:ROUTE]->(b)
    RETURN a.name as depart, x.name as transit, b.name as arrive;

None of the above requires Neo4j Spatial, or the import of any spatial information into the database at all.

However, if you are especially interested in using Neo4j Spatial, and want to have queries that make use of country polygons, you will need to use the Java embedded API, which allows you to import polygons into the spatial index and perform queries on those. For your use case I do not see the need, so I will not describe that option further, unless you also suggest some use cases that do.

Regards, Craig
On Thu, Feb 12, 2015 at 1:27 PM, fede martinez <federico...@gmail.com> wrote:

Craig Taverner

unread,
Feb 12, 2015, 9:24:53 AM2/12/15
to ne...@googlegroups.com
One compromise you could go for is to use both Java and Cypher. Use spatial and the Java API to load country polygons and airport locations. Use Java to perform spatial queries to locate airports in countries, and persist this as :IN relationships, and then use Cypher (as described in the previous email) to query this structure.

You could call the cypher from Java if you want to make the entire thing a Java app. Alternatively, an approach I think is nice is to write a Java unmanaged extension that focuses on data import, does the spatial stuff I described in Java during the import of country polygons and city locations. And then you can access the server remotely with cypher (through the browser, or any client you prefer) and run the cypher queries from there.

Fede Martinez

unread,
Feb 12, 2015, 9:26:33 AM2/12/15
to ne...@googlegroups.com
oh, that's actually a good idea. Thanks!

You received this message because you are subscribed to a topic in the Google Groups "Neo4j" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/neo4j/dbbFZ-R78DI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to neo4j+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages