From eb4495ed186bcb8f4a7bf8a70fff843f0de1d128 Mon Sep 17 00:00:00 2001 From: Nigel Horne Date: Fri, 3 Jan 2025 09:15:06 -0500 Subject: [PATCH] Croak when input is just a set of numbers --- Changes | 1 + lib/Geo/Coder/Free.pm | 15 ++++++++++++++- lib/Geo/Coder/Free/OpenAddresses.pm | 14 ++++++++++---- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Changes b/Changes index 010874f..e374934 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,7 @@ Revision history for Geo-Coder-Free 0.38 Latest as_string return code and Utils.pm from VWF Added t/config.t, t/utils.t and t/display.t + Croak when input is just a set of numbers 0.37 Wed Oct 23 10:09:54 EDT 2024 Allow new() to take HASH ref diff --git a/lib/Geo/Coder/Free.pm b/lib/Geo/Coder/Free.pm index fc0b910..010df05 100644 --- a/lib/Geo/Coder/Free.pm +++ b/lib/Geo/Coder/Free.pm @@ -3,6 +3,7 @@ package Geo::Coder::Free; # TODO: Don't have Maxmind as a separate database # TODO: Rename openaddresses.sql as geo_coder_free.sql # TODO: Consider Data::Dumper::Names instead of Data::Dumper +# TODO: use the cache to store common queries use strict; use warnings; @@ -56,7 +57,9 @@ sub _normalize($); =head1 DESCRIPTION -Geo::Coder::Free provides an interface to free databases by acting as a front-end to +Geo::Coder::Free provides an interface +to translate addresses into latitude and longitude +by using to free databases such as L and L. The cgi-bin directory contains a simple DIY Geo-Coding website. @@ -68,6 +71,8 @@ When it's back up you will be able to use this to test it. curl 'https://geocode.nigelhorne.com/cgi-bin/page.fcgi?page=query&q=1600+Pennsylvania+Avenue+NW+Washington+DC+USA' +Includes functionality for running the module via the command line for testing or ad-hoc geocoding tasks. + =head1 METHODS =head2 new @@ -209,6 +214,12 @@ sub geocode { $params{'location'} = shift; } + # Fail when the input is just a set of numbers + if($params{'location'} !~ /D/) { + Carp::croak('Usage: ', __PACKAGE__, ": invalid input to geocode(), $params{location}"); + return; + } + if($self->{'openaddr'}) { if(wantarray) { my @rc = $self->{'openaddr'}->geocode(\%params); @@ -525,6 +536,8 @@ The OpenAddresses data doesn't cover the globe. Can't parse and handle "London, England". +It would be great to have a set-up wizard to create the database. + The various scripts in NJH-Snippets ought to be in this module. =head1 SEE ALSO diff --git a/lib/Geo/Coder/Free/OpenAddresses.pm b/lib/Geo/Coder/Free/OpenAddresses.pm index 778702c..9687975 100644 --- a/lib/Geo/Coder/Free/OpenAddresses.pm +++ b/lib/Geo/Coder/Free/OpenAddresses.pm @@ -89,7 +89,7 @@ This process will take some time. $geocoder = Geo::Coder::Free::OpenAddresses->new(openaddr => $ENV{'OPENADDR_HOME'}); -Takes an optional parameter directory, which is the directory of the file +Takes an optional parameter "openaddr", which is the directory of the file openaddresses.sql. Takes an optional parameter cache, which points to an object that understands get() and set() messages to store data in @@ -104,11 +104,11 @@ sub new { return unless($class); if(my $openaddr = $param{'openaddr'}) { - Carp::croak("Can't find the directory $openaddr") + Carp::croak(__PACKAGE__, ": Can't find the directory $openaddr") if((!-d $openaddr) || (!-r $openaddr)); return bless { openaddr => $openaddr, cache => $param{'cache'} }, $class; } - Carp::croak(__PACKAGE__, ": usage: new(openaddr => '/path/to/openaddresses')"); + Carp::croak(__PACKAGE__, ": Usage: new(openaddr => '/path/to/openaddresses')"); } =head2 geocode @@ -1013,13 +1013,19 @@ data from both sources. The database shouldn't be called $OPENADDR_HOME/openaddresses.sql, since the datbase now also includes data from WhosOnFirst. +The name openaddresses.sql shouldn't be hardcoded, +add support to "new" for the parameter "dbname". + +The argument "openaddr", +would be less confusing if it were called "directory", + =head1 SEE ALSO VWF, openaddresses. =head1 LICENSE AND COPYRIGHT -Copyright 2017-2023 Nigel Horne. +Copyright 2017-2025 Nigel Horne. The program code is released under the following licence: GPL for personal use on a single computer. All other users (including Commercial, Charity, Educational, Government)