Neo4j Spatial Part 1

http://www.iconarchive.com/show/gis-gps-map-icons-by-icons-land/Layers-icon.html

One of my new year resolutions is to do a project with Neo4j Spatial, so we’ll kick off my first blog post of the year with a gentle introduction to this awesome plugin. I advise you to watch this very short 15 minute video by Neo4j Spatial creator Craig Taverner. The man is a genius level developer, you’ll gain IQ points just listening, I swear.

The plan is to make a Restaurant Recommendation engine based on things you care about and your current location. Yes, this is baby level stuff, but we’ll start with this and see where else Neo4j Spatial can take us later on.

So the first thing I always do when starting a new project is look for data. Asking Google, the all knowing and all powerful, I ran into… factual

Factual has a massive amount of data accessible by API and specifically some Restaurant Data that I was interested in. Let’s take a look at it shall we:

{
  "accessible_wheelchair": true,
  "address": "5951 N Broadway St",
  "alcohol": true,
  "alcohol_bar": true,
  "alcohol_beer_wine": true,
  "alcohol_byob": true,
  "attire": "casual",
  "category_ids": [
    366
  ],
  "category_labels": [
    [
      "Social",
      "Food and Dining",
      "Restaurants",
      "Sushi"
    ]
  ],
  "country": "us",
  "cuisine": [
    "Cafe",
    "Thai",
    "Sushi",
    "Asian",
    "Japanese"
  ],
  "email": "indieincindiecafe@gmail.com",
  "factual_id": "8f4b9dff-d1e2-47d5-b761-1e63928f79ac",
  "groups_goodfor": true,
  "hours": "{\"monday\":[[\"15:00\",\"22:00\",\"Dinner\"]],\"tuesday\":[[\"17:00\",\"22:00\",\"Dinner\"]],\"wednesday\":[[\"15:00\",\"22:00\",\"Dinner\"],[\"12:00\",\"15:00\"]],\"thursday\":[[\"15:00\",\"22:00\",\"Dinner\"],[\"12:00\",\"15:00\"]],\"friday\":[[\"15:00\",\"22:30\",\"Dinner\"],[\"12:00\",\"15:00\"]],\"saturday\":[[\"15:00\",\"22:30\",\"Dinner\"],[\"12:00\",\"15:00\"]],\"sunday\":[[\"15:00\",\"22:00\",\"Dinner\"],[\"12:00\",\"15:00\"]]}",
  "hours_display": "Mon 3:00 PM-10:00 PM; Tue 5:00 PM-10:00 PM; Wed-Thu 12:00 PM-10:00 PM; Fri-Sat 12:00 PM-10:30 PM; Sun 12:00 PM-10:00 PM",
  "kids_goodfor": true,
  "latitude": 41.990326,
  "locality": "Chicago",
  "longitude": -87.672907,
  "meal_cater": true,
  "meal_deliver": true,
  "meal_dinner": true,
  "meal_lunch": true,
  "meal_takeout": true,
  "name": "Indie Cafe",
  "neighborhood": [
    "Edgewater",
    "Far North Side",
    "northside",
    "North Edgewater"
  ],
  "open_24hrs": false,
  "options_healthy": true,
  "options_vegan": true,
  "options_vegetarian": true,
  "parking": true,
  "parking_street": true,
  "payment_cashonly": false,
  "postcode": "60660",
  "price": 2,
  "rating": 5,
  "region": "IL",
  "reservations": true,
  "seating_outdoor": true,
  "smoking": false,
  "status": "1",
  "tel": "(773) 561-5577",
  "website": "http://www.indiecafe.us"
}

Look at all that beautiful data. We know their hours, whether or not they have healthy, vegan and vegetarian options. If they are a good restaurant for kids or large groups, cuisines, rating, price… as well as latitude and longitude. We also know if they take reservations, but we have no easy way to just make a reservation (other than calling them and talking to a real human being, but who wants to do that). I know how to fix this…

opentable_logo_reg

I’ve used OpenTable before, and after spending some time poking around their site looking for an API I was unable to find one. Not to fear, Dan Sosedoff was kind enough to build an unofficial OpenTable API for us.

What kind of data can we get here:

{
  "id": 68710,
  "name": "Indie Cafe",
  "address": "5951 North Broadway",
  "city": "Chicago",
  "state": "IL",
  "area": "Chicago / Illinois",
  "postal_code": "60660",
  "country": "US",
  "phone": "7735615111",
  "reserve_url": "http://www.opentable.com/single.aspx?rid=68710",
  "mobile_reserve_url": "http://mobile.opentable.com/opentable/?restId=68710"
}

Awesome, there is that magic reservation link I was looking for. Now if only I had menus and menu items…

food_genius

Ah ha! Found some, but they seem to have discontinued their data API in a recent enterprise pivot. No matter, I’ll e-mail them and ask for a sample of their data. With any luck we’ll have more data than we know what to do with.

Now, back to Neo4j Spatial. The easiest thing we can do here, is create a spatial index and add the restaurants to it, then we’ll be able to query it. Now, us Ruby developers are a lazy lot, so instead of messing around with maven and building the plugin from source, I tweeked Neography so that installing Neo4j with the Spatial plugin looks like this:

echo "require 'neography/tasks'" > Rakefile
rake neo4j:install
rake neo4j:get_spatial
rake neo4j:start

Easy right? Now let’s try creating a Restaurants Spatial Index and adding a restaurant to it:

@neo = Neography::Rest.new
@neo.create_spatial_index("restaurants")

The response we get back from the server is:

{ "template" => "http://localhost:7474/db/data/index/node/restaurants/{key}/{value}", 
  "provider" => "spatial", 
  "geometry_type" => "point", 
  "lat" => "lat", 
  "lon" => "lon" }

But what actually happened?

spatial-1

It created 4 nodes for us. A special 0th node called “Spatial Root”, node 1 which we’ll take a look at in a second, and the root of an R-Tree and another node that holds the R-Tree Metadata. Let’s take a closer look at node 1:

spatial-node1

Take a look at that name, “restaurants”… that’s right, we’re not using Lucene or some other kind of external index engine, we are building the index inside of our own graph! Alright, let’s add that restaurant.

node = @neo.create_node({:name => "Indie Cafe", :lat => 41.990326, :lon => -87.672907 })

It created node 4, not connected to anything else, just kind of hanging around…

Screen Shot 2014-01-31 at 7.28.27 PM

So let’s now add this node to our spatial index:

@neo.add_node_to_spatial_index("restaurants", node)

An additional node 5 was created, with an “id” property that points back to our restaurant “node 4″.

Screen Shot 2014-01-31 at 7.31.28 PM

To make sure this is working right, let’s try querying our index. We’ll execute a cypher query that looks to the node spatial index “restaurants” for anything that appears within 10.0 kilometers of latitude 41.99, longitude -87.67.

@neo.execute_query("start n = node:restaurants({location}) return n",
                   {:location => "withinDistance:[41.99,-87.67,10.0]"})

… and we find it:

...{"data"=>{"lon"=>-87.672907, "lat"=>41.990326, "name"=>"Indie Cafe"}...

Excellent, now let’s try adding a second restaurant.

node2 = @neo.create_node({:name => "La Ciudad", :lat => 41.964239, :lon => -87.654758 })
@neo.add_node_to_spatial_index("restaurants", node2)

Our R-Tree gets another branch, and our tree root properties change to encompass both branch nodes:

Screen Shot 2014-01-31 at 7.47.09 PM

We’ll query our graph one more time:

@neo.execute_query("start n = node:restaurants({location}) return n",
                   {:location => "withinDistance:[41.99,-87.67,10.0]"})

… and we see we get back both restaurants.

...{"data"=>{"lon"=>-87.672907, "lat"=>41.990326, "name"=>"Indie Cafe"}}...
...{"data"=>{"lon"=>-87.654758, "lat"=>41.964239, "name"=>"La Ciudad"}}...

So now you have a good handle on how the simplest part of the Neo4j Plugin works. On the next blog post, we’ll use what we have learned here to build the full application, and explore the more intricate parts of the Neo4j Spatial plugin in future blog posts.

Update: Part 2 has now been published.

Tagged , , , , , , ,

4 thoughts on “Neo4j Spatial Part 1

  1. […] part 1 of this series we looked at how to get started with Neo4j Spatial and we looked at some of the […]

  2. Hi, I have a question … In your example shows how to insert geospatial information, but what if I want to change the coordinates? Should I update the properties of the node and then add the node to the spatial index? or what should be the procedure to edit the coordinates?.

  3. awesome post! however, im looking for an example just like this, but using embedded mode. any idea where i can find it? the spatial tests seem to add ‘coordinates’ to ‘layers’ whereas this tutorial adds nodes to indexes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,579 other followers

%d bloggers like this: