Modelling trails that share point locations and segments

I need some help with a model.

I want to model walking trails (or paths) in Neo4j where trails can cross one another at points. So where the trails overlap the points are just shared by both. And in fact a 'run' or sequence of points can be shared between trails, as shown with the blue trail and yellow trail below. Each trail would have a name, and the user should be able to know distance to the next point of the trail she is following (the trail she started on). This model is meant to be used by a mobile app that tracks the mobile user's progress along a trail, and not accidentally switch trails, and to show opportunities to explore a different trail.

Also, a user should be able to add or record her own trails using existing points, and adding one or more new points. There are thousands of trails, and that number will be grow as users create their own. These all need to coexist in the graph.

Below is a white-board style drawing of how two trails might intersect, but the drawing does not necessarily represent how I would model it in Neo4j

What is the best way to model this?

I think I'd model it exactly how you've white-boarded it. Each node would be the distinct points along route. I'd take advantage of the latitude & longitude data types so you can use distance formulas. The relationships are building a linked list of nodes that define a trail. I'd keep the meat of the information of the trail in the node (lat & long, etc...) and keep the attributes inside the relationship light. This would allow you to, if you need to introduce a new third trail that split off, you just insert a node with it's lat & long into the linked list and branch off from that node.

You could apply secondary labels to the nodes that participate in a named trail. Nodes can have many labels, or you just apply a secondary label to the starting & ending nodes and let the match query return all the nodes in between the two points. Or you can introduce a different node type that contained the trail name, description, etc... and it could point the beginning and ending nodes. Every node doesn't have to maintain the same attributes so if you can add extra information on the starting node or make a new type of node for the trail description, that comes down to personal preference and how your coding layer handles the data model.

1 Like

Thanks That first model does basically follow what normally works well for Neo4j, namely making the nodes nouns and the relationships verbs. So in this case it would have been something like (a:Location)-IS_FOLLOWED_BY ->(b:Location). But the overlapping paths really caused problems. Just like me walking a real trail, when it overlaps with another trail there's a chance I might follow the wrong path.

So I'm trying something I never have...flipping the paradigm and making the relationships the noun, and the nodes just connect them. Take a look at the image below. It's just a different take on the one above. But now all yellow relationships share the same relationship id, so it's not a unique id, but it unique to a trail. And now that we have the Lucene index option we can index these relationships.

Blue and yellow paths cross and share part of their paths, but internally they do not share the segments.

Locations are a ‘natural resource’ even in the graph. They are not duplicated, they are owned by nobody, and shared by all. Finding a trailhead, or trailend would just be pattern matches based where the start node has no incoming relationship carrying the trail id, and similarly the trail end has no outgoing relationship carrying that id.

Opinion(s) welcomed. I will try it out and maybe report back.