GGE5402/6405: Geographic Databases Fall 2016 Neo4j Exercise 2 CREATE A GRAPH DB FOR CANADIAN CITIES Emmanuel Stefanakis estef@unb.ca Spatial Procedures CALL spatial.procedures 1
CALL spatial.procedures Spatial Procedures Spatial Procedures CALL spatial.procedures 2
Dataset (Canadian Cities) Dataset (Canadian Cities) 3
// Create a point layer (cities) CALL spatial.addpointlayer('cities'); 4
// Import cities from shapefile CALL spatial.importshapefiletolayer('cities', 'C:/Users/estef/Documents/GGE6405/2016/data/Canada_Cities.shp') 5
6
// Retrieve the layer and the cities MATCH (n) WHERE n.layer = 'cities' RETURN n 7
8
MATCH (m) WHERE m.country = 'CAN RETURN m 9
// Create a relationship between the layer (node) and the cities (nodes) [it seems that there is no connection established between layer and cities] MATCH (n) WHERE n.layer = 'cities' MATCH (m) WHERE m.country = 'CAN' CREATE (n)-[:includes]->(m) // Retrieve all cities (nodes) in layer 'cities' MATCH (n)-[r:includes]->(m) RETURN m 10
// Count all nodes MATCH (n) RETURN count(n) // Nodes that are not part of the spatial index. MATCH (n) WHERE NOT (n)-[:rtree_reference]-() RETURN count(n) 11
MATCH (n) WHERE NOT (n)- [:RTREE_REFERENCE]-() RETURN ID(n) MATCH (n) WHERE NOT (n)- [:RTREE_REFERENCE]-() RETURN n 12
13
// Nodes that are part of the spatial index. MATCH (n) WHERE (n)-[:rtree_reference]-() RETURN count(n) MATCH (n) WHERE (n)-[:rtree_reference]-() RETURN n 14
15
// Non leaf nodes in the tree (1+10) MATCH (n) WHERE (n)-[:rtree_reference]->() RETURN count(n) MATCH (n) WHERE (n)-[:rtree_reference]->() RETURN n 16
MATCH (n) WHERE (n)-[:rtree_reference]->() RETURN n.bbox (distributed across the country) R-tree nodes 17
// Retrieve ids of non-leaf nodes MATCH (n) WHERE (n)-[:rtree_reference]->() RETURN ID(n) (pick one id and find how many children) MATCH (n) WHERE ID(n)=1029 MATCH (n)-[:rtree_reference]->(m) RETURN count(m) 18
MATCH (n) WHERE ID(n)=1132 MATCH (n)-[:rtree_reference]->(m) RETURN m.name // Report city properties MATCH (n) RETURN n.name 19
(WITH is like RETURN; to use values in subsequent commands) MATCH (n) WITH distinct n RETURN n.name MATCH (n) RETURN n.name, n.capital 20
MATCH (n) RETURN n.name, n.latitude, n.longitude MATCH (n) WHERE n.name = 'Toronto' RETURN n.name, n.latitude, n.longitude 21
MATCH (n), (m) WHERE n.name = 'Toronto' AND m.name = 'Fredericton' RETURN n.capital, m.stateabb // Labels - returns all NULL (no labels) MATCH (n) RETURN n:label 22
//Assign labels (Atlantic cities) MATCH (n) where n.longitude > -70 WITH COLLECT (distinct(n)) as nn FOREACH (n in nn SET n:atlantic) MATCH (n) WHERE n:atlantic RETURN n.name as ATLANTIC_CITIES 23
// List of procedures CALL spatial.procedures // Cities within 100km from location CALL spatial.withindistance('cities',{lon:-66.0,lat:45.0},100) 24
CALL spatial.withindistance('cities',{lon:-66.0,lat:45.0},100) YIELD node as n RETURN n // Cities within 100km from Fredericton MATCH (n) WHERE n.name = 'Fredericton' CALL spatial.withindistance('cities',{lon:n.longitude,lat:n.latitude},100) YIELD node as m RETURN m 25
MATCH (n) WHERE n.name = 'Fredericton' CALL spatial.withindistance('cities',{lon:n.longitude,lat:n.latitude},100) YIELD node as m RETURN m.name // Create relationships MATCH (n) WHERE n.name = 'Fredericton' CALL spatial.withindistance('cities',{lon:n.longitude,lat:n.latitude},100) YIELD node as m CREATE (n)-[r:near_fredericton]->(m) 26
MATCH (n) WHERE n.name = 'Fredericton' RETURN n --> see the graph and expand relationships MATCH (n)-[r:near_fredericton]->(m) WHERE n.name = 'Fredericton' RETURN m.name 27
// Create relationships by iteration MATCH (n:atlantic) MATCH (m) WHERE m.name = 'Toronto' MERGE (m)-[:toronto2atlantic]->(n) MATCH (n)-[r:toronto2atlantic]->(m) RETURN m.name 28
// Create relationships by iteration MATCH (m) WHERE m.name in ["Fredericton", "Ottawa", "Calgary"] CALL spatial.withindistance('cities',{lon:m.longitude,lat:m.latitude},100) YIELD node as j MERGE (j)-[:network3cities_near]-(m) MATCH (n)-[r:network3cities_near]->(m) RETURN n.name, m.name 29
// Create network betweeen atlantic cities with a dist < 100km MATCH (m) WHERE m.longitude > -70 CALL spatial.withindistance('cities',{lon:m.longitude,lat:m.latitude},100) YIELD node as j MERGE (j)-[:networkatlantic_near]-(m) MATCH (n)-[r:networkatlantic_near]->(m) RETURN n.name, m.name 30
//A network between capital cities MATCH (n) WHERE n.capital=1 RETURN n.name MATCH (n) WHERE n.capital=1 MATCH (m) WHERE m.capital=1 MERGE (n)-[:networkcapitals]->(m) 31
MATCH (n)-[r:networkcapitals]->(m) RETURN n.name, m.name //Intersection of geometries CALL spatial.intersects('cities','polygon((-70 45, -70 53, -60 55, -60 47, -70 45))') YIELD node as m RETURN m 32
CALL spatial.intersects('cities','polygon((-70 45, -70 53, -60 55, -60 47, -70 45))') YIELD node as m RETURN m.name 33
// Remove layer CALL spatial.removelayer('cities') // Remove all nodes/relationships MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r 34