From the course: Advanced PostgreSQL

Unlock this course with a free trial

Join today to access over 24,700 courses taught by industry experts.

Computing the distance between locations

Computing the distance between locations - PostgreSQL Tutorial

From the course: Advanced PostgreSQL

Computing the distance between locations

It's pretty exciting to have some real cities or places data for the UK. Let's run some queries on this data. The first thing I'm going to do here is look at the two sensors that we have located in Wells and Truro. So I'm going to select the sensor location from places where name is equal to Wells or name is equal to Truro. And I get two sensor locations here. Now let's visualize this on a map. I'm going to select this select the option to view this on a map. And there you can see the two cities. They don't seem very far apart. Now, I'm curious about how far apart these two sensors are, how far apart these locations are. So I'm going to run a simple ST distance query. So I select the ST_Distance between a.sensor location and b.sensor location, where A is Wells and B is Truro. An important detail to observe here in this query that I'm representing the sensor location as geometry coordinates. Remember, geometry does not take into account the spherical nature of the Earth, the Earth's…

Contents