Reader small image

You're reading from  Learning Neo4j 3.x - Second Edition

Product typeBook
Published inOct 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781786466143
Edition2nd Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Jerome Baton
Jerome Baton
author image
Jerome Baton

Jérôme Baton started hacking computers at the age of skin problems, gaming first then continued his trip by self-learning Basic on Amstrad CPC, peaking on coding a full screen horizontal starfield, and messing the interlace of the video controller so that sprites appeared twice as high in horizontal beat'em up games. Disks were three inches for 178 Kb then. Then, for gaming reasons, he switched to Commodore Amiga and its fantastic AMOS Basic. Later caught by seriousness and studies, he wrote Turbo Pascal, C, COBOL, Visual C++, and Java on PCs and mainframes at university, and even Logo in high school. Then, Java happened and he became a consultant, mostly on backend code of websites in many different businesses. Jérôme authored several articles in French on Neo4j, JBoss Forge, an Arduino workshop for Devoxx4Kids, and reviewed kilos of books on Android. He has a weakness for wordplay, puns, spoonerisms, and Neo4j that relieves him from join(t) pains. Jérôme also has the joy to teach in French universities, currently at I.U.T de Paris, Université Paris V - René Descartes (Neo4j, Android), and Université de Troyes (Neo4j), where he does his best to enterTRain the students. When not programming, Jérôme enjoys photography, doing electronics, everything DIY, understanding how things work, trying to be clever or funny on Twitter, and spends a lot of time trying to understand his kids and life in general.
Read more about Jerome Baton

Right arrow

Chapter 9. Going Spatial

No, this chapter will not train you to become a cosmonaut. Sorry.

However, as you have seen so far, using a graph database is not rocket science. You cannot say it is well beyond your understanding. Enough with the puns and let's deal with this spatial aspect. First, we have to agree that Earth's shape is close to a sphere; this world is not a disc wandering through space on the back of a tortoise. So say we all? So let's start our trek toward a new frontier for our skills.

We will cover the following topics in this chapter:

  • Revise the geo concepts
  • See the geo features of Neo4j and of APOC 
  •  Neo4j spatial

What is spatial?


Spatial refers to giving a location to nodes, like answering the question, Where in the world is the new Dennis Ritchie?

Refresher

Every location (or point) on Earth can be localized via its spatial coordinates--latitude and longitude. Both are angular values; they measure an angle from the center of Earth. Latitude zero is shared by all points on the equator. Positive latitude is for all points north of the equator. Negative latitude is for all points located south of the equator.

All points with the same latitude form a virtual horizontal line (circle); whereas all points with the same longitude form a line (circle) called meridian. Longitude zero is currently defined by the meridian going through Greenwich near London in the United Kingdom, where the royal observatory (not a people's magazine) is located. The legend says that the Greenwich meridian replaced the Paris meridian as longitude zero in exchange of England adopting the metric system.

Unfortunately, it has not worked...

What is so spatial then?


The answer is:  

Neo4j is spatial, APOC is spatial too, and of course, Neo4j Spatial is more spatial. Let me explain.

Spatial capabilities are present in these three pieces of software. Their spatialness goes from basic to advanced in that order.

However, as the last two (APOC and Neo4j Spatial) are plugins that you install in the first software (Neo4j), spatial features add up. The package names change in order to call the procedures. We will see this now.

Neo4j's spatial features 

Neo4j has a limited built-in spatial support since Neo4j 3.0. There is default support for point and distance. This support assumes that you will set property keys named latitude and longitude for the nodes you want to use as points.

You can use them to calculate the distance between two nodes, but first let's build the towers, starting with the good habit of creating a constraint:

CREATE CONSTRAINT ON (t:Tower) ASSERT t.name IS UNIQUE
CREATE (paris:Tower {name: "Eiffel Tower",country:"FRA"...

Neo4j spatial


Neo4j Spatial (later NS) is a plugin, like APOC, discussed in Chapter 5, Awesome Procedures on Cypher - APOC. The homepage of this plugin is https://github.com/neo4j-contrib/spatial.

Just like APOC, you should take care to install a version compatible with the version of your Neo4j server. See the releases tab on their GitHub landing page. The release notes and the release filenames are self-explanatory. (Thank the project leader, Craig Taverner.)

Online demo

William Lyon (https://twitter.com/lyonwj) from Neo4j has made an online demo available at this address, http://www.lyonwj.com/scdemo/index.html.

Use the polygon tool in the toolbar on the left to define a shape, double-click inside it, and you will get markers on the map to indicate restaurant locations. Let's find some restaurants in Phoenix, in an almost successfully drawn butterfly shape:

  Restaurant butterfly

While we are in the lexical field of food, let's say the demo was an amuse-bouche (appetizers) or a starter, and...

Removing all the geo data


You may want to remove all the indexed nodes from the spatial tree. You can do this by removing all the layers, which is a rather expeditive method indeed.

Try this code, which should return an empty list :

// Removing all spatial layers
CALL spatial.layers() YIELD name AS name
WITH collect(name) AS names
UNWIND names AS nameL
CALL spatial.removeLayer(nameL)  // enamel layer :)
call spatial.layers() YIELD name AS name2
WITH collect(name2) AS emptyNamesList
RETURN emptyNamesList

I hope you enjoyed this chapter, particularly rich in puns and wordplays.

Summary


In this chapter, we saw how to use geographical coordinates in Neo4j, be it without or with APOC, or with the Neo4j Spatial plugin. We saw what is geocoding and how to import geographic data provided by OpenStreetMap. Consequently, we can now imagine queries based on locations, like what restaurants are around me? or Where can I find a shared bicycle?

We just need to import the right datasets with the skills gained in Chapter 8, Importing data into Neo4j.

Now, let's see how to deal with the security of our data in the next chapter.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning Neo4j 3.x - Second Edition
Published in: Oct 2017Publisher: PacktISBN-13: 9781786466143
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Jerome Baton

Jérôme Baton started hacking computers at the age of skin problems, gaming first then continued his trip by self-learning Basic on Amstrad CPC, peaking on coding a full screen horizontal starfield, and messing the interlace of the video controller so that sprites appeared twice as high in horizontal beat'em up games. Disks were three inches for 178 Kb then. Then, for gaming reasons, he switched to Commodore Amiga and its fantastic AMOS Basic. Later caught by seriousness and studies, he wrote Turbo Pascal, C, COBOL, Visual C++, and Java on PCs and mainframes at university, and even Logo in high school. Then, Java happened and he became a consultant, mostly on backend code of websites in many different businesses. Jérôme authored several articles in French on Neo4j, JBoss Forge, an Arduino workshop for Devoxx4Kids, and reviewed kilos of books on Android. He has a weakness for wordplay, puns, spoonerisms, and Neo4j that relieves him from join(t) pains. Jérôme also has the joy to teach in French universities, currently at I.U.T de Paris, Université Paris V - René Descartes (Neo4j, Android), and Université de Troyes (Neo4j), where he does his best to enterTRain the students. When not programming, Jérôme enjoys photography, doing electronics, everything DIY, understanding how things work, trying to be clever or funny on Twitter, and spends a lot of time trying to understand his kids and life in general.
Read more about Jerome Baton