Skip to content
Alex-Kent edited this page Apr 10, 2019 · 13 revisions

Geo::Index - Geographic indexer for Perl

Geo::Index is a Perl module for creating in-memory geographic points indices. Once indexed, fast searches can be run.

Efficient search methods include Search(…) to get all points within a distance from a given point, SearchByBounds(…) to get all points in an given area, Closest(…) to get the closest points to a given point, and Farthest(…) to get the farthest points from a given point.

Additional methods are provided to compute distances between arbitrary points (Distance(…), DistanceFrom(…), and DistanceTo(…)) and to get the size in meters of one degree or the size in degrees of one meter at a given point (OneDegreeInMeters(…) and OneMeterInDegrees(…), respectively).

While by default computations are done for the Earth, other bodies can be used by supplying appropriates radii and circumferences to new(…).

This documentation describes Geo::Index version 0.0.8

Synopsis

# Create and populate a geographic index

use Geo::Index;

@points = (
            { lat =>   1.0, lon =>   2.0 },
            { lat => -90.0, lon =>   0.0, name => 'South Pole' },
            { lat =>  30.0, lon => -20.0, ele => 123.4 }
          );
$point = { lat=>10.0, lon=>20.0 };

$index = Geo::Index->new();
$index->IndexPoints( \@points );
$index->Index( $point );
$index->Index( [ 30, 40 ] );


# Search index

%search_options = ( sort_results => 1, radius=>5_000_000 );
$results = $index->Search( [ -80, 20 ], \%search_options );
print "$$results[0]{name}\n";  # Prints 'South Pole'

# Get all points in the southern hemisphere
$results = $index->SearchByBounds( [ -180, -90, 180, 0 ] );
print "$$results[0]{name}\n";  # Also prints 'South Pole'

($closest) = $index->Closest( [ -80, 20 ] );
print "$$closest{name}\n";     # Also prints 'South Pole'

($closest) = $index->Closest( $points[1], { post_condition=>'NONE' } );
print "$$closest{name}\n";     # Also prints 'South Pole'

($farthest) = $index->Farthest( [ 90, 0 ] );
print "$$farthest{name}\n";    # Also prints 'South Pole'


# Compute distance in meters between two points (using haversine formula)

$m = $index->Distance( { lat=>51.507222, lon=>-0.1275 }, [ -6.2, 106.816667 ] );
printf("London to Jakarta: %i km\n", $m / 1000);

$index->DistanceFrom( [ 90, 0 ] );
$m = $index->DistanceTo( $points[1] );
printf("Pole to pole:      %i km\n", $m / 1000);

Further sample code can be found in the examples/ directory.