Database Reference
In-Depth Information
Earth distance
The earthdistance extension offers two different ways to calculate distances in a
circle on the earth's surface. For this extension, the earth is assumed to be perfectly spheric-
al. If the calculations are inaccurate for you, it's recommended to use the PostGIS exten-
sion. You will see more about it in this chapter.
In this example, you will create a table of attractions in Brazil and you will insert their latit-
ude/longitude. After using this information, you will find the distance between the two at-
tractions. You can create the following table:
id | name | latitude |
longitude
----+------------------------------+-------------+------------
1 | Christ the Redeemer (statue) | -22.9518769 |
-43.2104991
2 | Maracana Stadium | -22.9121089 |
-43.2301558
First, you will install the earthdistance extension and then insert the two attractions:
$ heroku pg:psql --app your-app-name
CREATE EXTENSION earthdistance;
CREATE TABLE attractions (id integer, name text, latitude
float, longitude float);
INSERT INTO attractions (id, name, latitude, longitude)
VALUES (1, 'Christ the Redeemer (statue)',
-22.951876900000000000, -43.210499099999990000);
INSERT INTO attractions (id, name, latitude, longitude)
VALUES (2, 'Maracana Stadium', -22.912108900000000000,
-43.230155800000034000);
Finally, you are able to calculate the distance between the Christ the Redeemer
(statue) and Maracana Stadium . In this example, you will use two functions, the
first one is the ll_to_earth function, which transforms the latitude/longitude data on a
point on the surface of the Earth. The second function is earth_distance that calcu-
lates the distance.
Search WWH ::




Custom Search