NoSQL Zone is brought to you in partnership with:

Mark is a graph advocate and field engineer for Neo Technology, the company behind the Neo4j graph database. As a field engineer, Mark helps customers embrace graph data and Neo4j building sophisticated solutions to challenging data problems. When he's not with customers Mark is a developer on Neo4j and writes his experiences of being a graphista on a popular blog at http://markhneedham.com/blog. He tweets at @markhneedham. Mark is a DZone MVB and is not an employee of DZone and has posted 534 posts at DZone. You can read more from them at their website. View Full User Profile

Neo4j Spatial: Indexing Football Stadiums Using the REST API

06.24.2013
| 2380 views |
  • submit to reddit

Late last week my colleague Peter wrote up some documentation about creating spatial indexes in neo4j via HTTP, something I hadn’t realised was possible until then.

I previously wrote about indexing football stadiums using neo4j spatial but the annoying thing about the approach I described was that I was using neo4j in embedded mode which restricts you to using a JVM language.

The rest of my code is in Ruby so I thought I’d translate that code.

To recap, I’m parsing a CSV file of football stadiums that I downloaded from Chris Bell’s blog which looks like this:

Name,Team,Capacity,Latitude,Longitude
"Adams Park","Wycombe Wanderers",10284,51.6306,-0.800299
"Almondvale Stadium","Livingston",10122,55.8864,-3.52207
"Amex Stadium","Brighton and Hove Albion",22374,50.8609,-0.08014

The code to process the file and index the stadiums in neo4j is as follows (and is essentially a translation of the find_geometries_within_distance_using_cypher test):

require 'csv'
require 'httparty'
require 'json'
 
HTTParty.post("http://localhost:7474/db/data/ext/SpatialPlugin/graphdb/addSimplePointLayer", 
  :body => { :layer => 'geom', :lat => 'lat', :lon => 'lon' }.to_json,
  :headers => { 'Content-Type' => 'application/json' } )
 
HTTParty.post("http://localhost:7474/db/data/index/node", 		
  :body => { :name => 'geom', :config => { :provider => 'spatial', :geometry_type => 'point', :lat => 'lat', :lon => 'lon'  } }.to_json,
  :headers => { 'Content-Type' => 'application/json' } )
 
contents = CSV.read(File.join(File.dirname(__FILE__), 'data', 'stadiums.csv'))
contents.shift
contents.each do |row|
  name, team, capacity, lat, long = row
 
  node_id = HTTParty.post("http://localhost:7474/db/data/node", 		
    :body => { :lat => lat.to_f, :lon => long.to_f, :name => name, :team => team, :capacity => capacity }.to_json,
    :headers => { 'Content-Type' => 'application/json' } )['self'].split("/")[-1]
 
  HTTParty.post("http://localhost:7474/db/data/index/node/geom", 		
    :body => { :key => 'dummy', :value => 'dummy', :uri => "http://localhost:7474/db/data/node/#{node_id}"}.to_json,
    :headers => { 'Content-Type' => 'application/json' } )
end

One change from the previous version is that I’m not indexing the stadiums using point based geometry rather than wkt.

If we want to find the number of stadiums within 10 km of Centre Point in London we’d write the following query:

START node = node:geom('withinDistance:[51.521348,-0.128113, 10.0]') 
RETURN node.name, node.team;
==> +--------------------------------------------+
==> | node.name          | node.team             |
==> +--------------------------------------------+
==> | "Emirates Stadium" | "Arsenal"             |
==> | "Stamford Bridge"  | "Chelsea"             |
==> | "The Den"          | "Millwall"            |
==> | "Loftus Road"      | "Queens Park Rangers" |
==> | "Craven Cottage"   | "Fulham"              |
==> | "Brisbane Road"    | "Leyton Orient"       |
==> +--------------------------------------------+
==> 6 rows







Published at DZone with permission of Mark Needham, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)