Public transportation of São Paulo in a graph database IGOR ROZANI Igor Rozani

IgorRozani IgorRozani

@IgorRozani IgorRozani Table of contents ▪What’s this project? ▪The database ▪Query examples ▪Q&A ▪The Hunger Games What’s this project?

What’s this project?

Query examples • Which lines does a company own? • What station is part of which line? • How many stations have each line? • What is the shortest way between stations? Which lines does a company own?

Company Lines MATCH (c:Company)-[:OWN]-(l:Line) "CPTM" ["7 - Ruby", "8 - Diamond", WITH c, l "9 - Emerald", "10 - Turquoise", "11 - Coral", "12 ORDER BY l.number, l.name - Sapphire", "13 - Jade", WITH c, collect(CASE WHEN l.number IS NULL "Airport Connect", "Airport THEN l.name ELSE l.number + ' - ' + l.name Express", "Touristic END) as lines Express"] RETURN c.name AS Company, lines AS Lines ...... "ViaMobilidade" ["5 - Lilac"] ORDER BY c.name "ViaQuatro" ["4 - Yellow"] What station is part of which line? MATCH (l:Line)-[:PART_OF]-(s) RETURN l.name AS Line, collect(s.name) AS Stations

Line Stations "Blue" ["Jabaquara", "Tiradentes", "Praça da Árvore", "Portuguesa - Tietê", "Luz", "São Bento", "Carandiru", "Parada Inglesa", "São Judas", "Saúde", "Paraíso", "Conceição", "Liberdade", "Jardim São Paulo - Ayrton Senna", "São Joaquim", "Santa Cruz", "Vergueiro", "Armênia", "Sé", "Ana Rosa", "Santana", "Tucuruvi", "Vila Mariana"] "Green" ["Trianon-Masp", "Vila Prudente", "Chácara Klabin", "Santuário Nossa senhora de Fátima - Sumaré", "Ana Rosa", "Clínicas", "Santos Imigrantes", "Vila Madalena", "Paraíso", "Brigadeiro", "Consoloação", "Alto do Ipiranga", "Tamanduateí", "Sacomã"] "Red" ["Anhangabaú", "Artur Alvim", "Bresser - Mooca", "Guilermina - Esperança", "Mal. Deodoro", "Belém", "Pedro II", "Tatuapé", "República", "Patriarca", "Carrão", "Sé", "Penha", "Corinthians - Itaquera", "Vila Matilde", "Brás", "Palmeiras - Barra Funda", "Santa Cecília"] How many stations have each line?

Line qtd "Blue" 23 MATCH (l:Line)-[:PART_OF]-(s) "Diamond" 20 WITH l, count(s) as qtd "Red" 18 "Emerald" 18 RETURN l.name as Line, qtd "Lilac" 17 ORDER BY qtd DESC ...... "Diamond - Operational Extension" 3 "Jade" 3 "Orca Shuttle Service" 2 "Airport Express" 2 What is the shortest way between stations? What is the shortest way between stations?

MATCH x = shortestPath((s1{name:"Grajaú"})-[:CONNECT*]-(s2{name:"Rio Grande da Serra"})) RETURN EXTRACT(n IN NODES(x) | n.name) AS Directions

Directions ["Grajaú", "Primavera - Interlagos", "Autódromo", "Jurubatuba", "Socorro", "Santo Amaro", "Granja Julieta", "Morumbi", "Brooklin“, "Diadema", "Piraporinha", "São Bernardo", "Santo André", "Capuava", "Mauá", "Guapituba", "Ribeirão Pires", "Rio Grande da Serra"] What is the shortest way between stations? What is the shortest way between stations? MATCH (s1{name:"Grajaú"}), (s2{name:"Rio Grande da Serra"}), p = shortestPath((s1)-[:CONNECT*]-(s2)) WHERE ALL (x IN RELATIONSHIPS(p) WHERE x.transport='train' OR x.transport='metro') RETURN EXTRACT(n IN NODES(p) | n.name) AS Directions

Directions ["Grajaú", "Primavera - Interlagos", "Autódromo", "Jurubatuba", "Socorro", "Santo Amaro", "Largo Treze", "Adolpho Pinheiro", "Alto da Boa Vista", "Borba Gato", "Brooklin", "Campo Belo", "Eucaliptos", "Moema", "AACD - Servidor", "Hospital São Paulo", "Vila Mariana", "Chácara Klabin", "Santos Imigrantes", "Alto do Ipiranga", "Sacomã", "Tamanduateí", "São Caetano do Sul", "Utinga", "Prefeito Saladino", "Santo André", "Capuava", "Mauá", "Guapituba", "Ribeirão Pires", "Rio Grande da Serra"] What is the shortest way between stations? Learn more about it

This project is available on GitHub. https://github.com/IgorRozani/Public-Transport-SP-Graph-Database Q&A Thank you! The Hunger Games

1 - Which of the following options is not a node label in 3 - Which of the queries can get the shortest path between the project? two stations, using only train transport? A) MetroStation B) PointOfInterest C) Connect A) MATCH (s1{name:"Grajaú"}), (s2{name:"Rio Grande da 2 - What does the following query do? Serra"}), MATCH (n) WHERE n.hasBikeAttachingPost = true OR p = shortestPath((s1)-[:CONNECT*]-(s2)) n.hasBikeParkingTerminal = true WHERE ALL (x IN RELATIONSHIPS(p) WHERE x.transport='train') RETURN n.name ORDER BY n.name DESC RETURN EXTRACT(n IN NODES(p) | n.name) AS Directions A) Get all nodes that have true in the property "hasBikeAttachingPost" or "hasBikeParkingTerminal" and B) MATCH (s1{name:"Grajaú"}), (s2{name:"Rio Grande da return the property "name" sorted by descending order Serra"}), p = shortestPath((s1)-[:CONNECT*]-(s2)) B) Get all nodes that have true in the property WHERE ALL (x IN RELATIONSHIPS(p) WHERE x.transport='train' "hasBikeAttachingPost" and "hasBikeParkingTerminal" OR x.transport='metro') and return the property "name" sorted by descending RETURN EXTRACT(n IN NODES(p) | n.name) AS Directions order C) Get all nodes that have true in the property C) MATCH x = shortestPath((s1{name:"Grajaú"})-[:CONNECT*]- "hasBikeAttachingPost" or "hasBikeParkingTerminal" and (s2{name:"Rio Grande da Serra"})) return the property "name" sorted by ascending order RETURN EXTRACT(n IN NODES(x) | n.name) AS Directions

Answer here: http://r.neo4j.com/hunger-games