Geo-Distance

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

# NAME

Geo::Distance - Calculate distances and closest locations. (DEPRECATED)

# SYNOPSIS

```perl
use Geo::Distance;

my $geo = new Geo::Distance;
$geo->formula('hsin');

$geo->reg_unit( 'toad_hop', 200120 );
$geo->reg_unit( 'frog_hop' => 6 => 'toad_hop' );

my $distance = $geo->distance( 'unit_type', $lon1,$lat1 => $lon2,$lat2 );

my $locations = $geo->closest(
    dbh => $dbh,
    table => $table,
    lon => $lon,
    lat => $lat,
    unit => $unit_type,
    distance => $dist_in_unit
);
```

# DESCRIPTION

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.

# DEPRECATED

This module has been gutted and is now a wrapper around [GIS::Distance](https://metacpan.org/pod/GIS%3A%3ADistance), please
use that module instead.

When switching from this module to [GIS::Distance](https://metacpan.org/pod/GIS%3A%3ADistance) make sure you reverse the
coordinates when passing them to ["distance" in GIS::Distance](https://metacpan.org/pod/GIS%3A%3ADistance#distance).  GIS::Distance takes
lat/lon pairs while Geo::Distance takes lon/lat pairs.

# ARGUMENTS

## no\_units

Set this to disable the loading of the default units as described in ["UNITS"](#units).

# ACCESSORS

## formula

```
if ($geo->formula() eq 'hsin') { ... }
$geo->formula('cos');
```

Set and get the formula that is currently being used to calculate distances.
See the available ["FORMULAS"](#formulas).

`hsin` is the default.

# METHODS

## distance

```perl
my $distance = $geo->distance( 'unit_type', $lon1,$lat1 => $lon2,$lat2 );
```

Calculates the distance between two lon/lat points.

## closest

```perl
my $locations = $geo->closest(
    dbh => $dbh,
    table => $table,
    lon => $lon,
    lat => $lat,
    unit => $unit_type,
    distance => $dist_in_unit
);
```

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:

```perl
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:

```perl
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.

## reg\_unit



( run in 4.221 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )