Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save johnymontana/45009185d59c24e08cb4f3f8053546e5 to your computer and use it in GitHub Desktop.
Save johnymontana/45009185d59c24e08cb4f3f8053546e5 to your computer and use it in GitHub Desktop.
Load Airports into Neo4j Spatial
// Load airports from Openflights.org dataset
CREATE CONSTRAINT ON (c:Country) ASSERT c.name IS UNIQUE;
CREATE CONSTRAINT ON (c:City) ASSERT c.name IS UNIQUE;
CREATE CONSTRAINT ON (a:Airport) ASSERT a.id IS UNIQUE;
LOAD CSV FROM "https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat" AS row
WITH toInt(row[0]) AS id,
row[1] AS name,
row[2] AS city,
row[3] AS country,
row[4] AS code,
row[5] AS ICAO,
toFloat(row[6]) AS latitude,
toFloat(row[7]) AS longitude,
toFloat(row[8]) AS altitude,
toInt(row[9]) AS timezone
MERGE (a:Airport {id: id})
ON CREATE SET a.name = name,
a.city = city,
a.country = country,
a.code = code,
a.ICAO = ICAO,
a.latitude = latitude,
a.longitude = longitude,
a.altitude = altitude,
a.timezone = timezone
MERGE (c:City {name: city})
MERGE (t:Country {name: country})
CREATE UNIQUE (a)-[:IS_IN]->(c)
CREATE UNIQUE (c)-[:IS_IN]->(t)
// Create Neo4j spatial layer
// Ensure Neo4j spatial extension is installed (see http://gist.asciidoctor.org/?dropbox-14493611%2Fcypher_spatial.adoc#_spatial_procedures)
call spatial.addPointLayer("airports");
// Add all Airport nodes to spatial layer
MATCH (a:Airport)
WITH collect(a) AS airports
CALL spatial.addNodes('airports',airports) YIELD node
RETURN count(*);
// What airports are within 200km of Heathrow?
MATCH (c:Airport {code:"LHR"}) WITH c
CALL spatial.withinDistance('airports', c , 200) YIELD node, distance
RETURN node.name AS name, round(distance) AS dist;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment