This perl library aims to provide as many tools to make it as simple as possible to calculate distances between geographic points, and anything that can be derived from that. Currently there is support for finding the closest locations within a specified distance, to find the closest number of points to a specified point, and to do basic point-to-point distance calculations.

The GIS::Distance module is being worked on as a replacement for this module. In the near future Geo::Distance will become a lightweight wrapper around GIS::Distance so that legacy code benefits from fixes to GIS::Distance through the old Geo::Distance API. For any new developement I suggest that you look in to GIS::Distance.

All functions accept a unit type to do the computations of distance with. By default no units are defined in a Geo::Distance object. You can add units with reg_unit() or create some default units with default_units().

Allows you to retrieve and set the formula that is currently being used to calculate distances. The available formulas are hsin, polar, cos and mt. hsin is the default and mt/cos are deprecated in favor of hsin. Polar should be used when calculating coordinates near the poles.

This method is used to create custom unit types. There are several ways of calling it, depending on if you are defining the unit from scratch, or if you are basing it off of an existing unit (such as saying 12 inches = 1 foot ). When defining a unit from scratch you pass the name and rho (radius of the earth in that unit) value.

So, if you wanted to do your calculations in human adult steps you would have to have an average human adult walk from the crust of the earth to the core (ignore the fact that this is impossible). So, assuming we did this and we came up with 43,200 steps, you'd do something like the following.

Now, if you also wanted to do distances in baby steps you might think "well, now I gotta get a baby to walk to the center of the earth". But, you don't have to! If you do some research you'll find (no research was actually conducted) that there are, on average, 4.7 baby steps in each adult step.

# Define baby step unit.
$geo->reg_unit( 4.7, 'baby step' => 'adult step' );
# This can be read as "4.7 baby steps is the same as one adult step".

And if we were doing this in reverse and already had the baby step unit but not the adult step, you would still use the exact same syntax as above.

This method finds the closest locations within a certain distance and returns an array reference with a hash for each location matched.

The closest method requires the following arguments:

dbh - a DBI database handle
table - a table within dbh that contains the locations to search
lon - the longitude of the center point
lat - the latitude of the center point
unit - the unit of measurement to use, such as "meter"
distance - the distance, in units, from the center point to find locations

The following arguments are optional:

lon_field - the name of the field in the table that contains the longitude, defaults to "lon"
lat_field - the name of the field in the table that contains the latitude, defaults to "lat"
fields - an array reference of extra field names that you would like returned with each location
where - additional rules for the where clause of the sql
bind - an array reference of bind variables to go with the placeholders in where
sort - whether to sort the locations by their distance, making the closest location the first returned
count - return at most these number of locations (implies sort => 1)

This method uses some very simplistic calculations to SQL select out of the dbh. This means that the SQL should work fine on almost any database (only tested on MySQL and SQLite so far) and this also means that it is fast. Once this sub set of locations has been retrieved then more precise calculations are made to narrow down the result set. Remember, though, that the farther out your distance is, and the more locations in the table, the slower your searches will be.

This formula uses Meth::Trig's great_circle_distance function which at this time uses math almost exactly the same as the cos formula. If you want to use the cos formula you may find that mt will calculate faster (untested assumption). For some reason mt and cos return slight differences at very close distances. The mt formula has the same drawbacks as the cos formula.

This is the same formula that was previously the only one used by Geo::Distance (ending at version 0.06) and was wrongly called the "gcd" formula.