1 package FS::GeocodeCache;
4 use vars qw($conf $DEBUG);
5 use base qw( FS::geocode_Mixin );
6 use FS::Record qw( qsearch qsearchs );
12 FS::UID->install_callback( sub { $conf = new FS::Conf; } );
18 FS::GeocodeCache - An address undergoing the geocode process.
24 $record = FS::GeocodeCache->standardize(%location_hash);
28 An FS::GeocodeCache object represents a street address in the process of
29 being geocoded. FS::GeocodeCache inherits from FS::geocode_Mixin.
31 Most methods on this object throw an exception on error.
33 FS::GeocodeCache has the following fields, with the same meaning as in
72 Creates a new cache object. For internal use. See C<standardize>.
76 # minimalist constructor
96 # minimalist accessor, for compatibility with geocode_Mixin
102 $_[0]->{$_[1]} = $_[2];
105 sub location_hash { %{$_[0]} };
107 =item set_censustract
109 Look up the censustract, if it's not already filled in, and return it.
110 On error, sets 'error' and returns nothing.
112 This uses the "get_censustract_*" methods in L<FS::Misc::Geo>; currently
113 the only one is 'ffiec'.
117 sub set_censustract {
120 if ( $self->get('censustract') =~ /^\d{9}\.\d{2}$/ ) {
121 return $self->get('censustract');
123 my $censusyear = $conf->config('census_year');
124 return if !$censusyear;
126 my $method = 'ffiec';
127 # configurable censustract-only lookup goes here if it's ever needed.
128 $method = "get_censustract_$method";
129 my $censustract = eval { FS::Misc::Geo->$method($self, $censusyear) };
130 $self->set("censustract_error", $@);
131 $self->set("censustract", $censustract);
136 Set the latitude and longitude fields if they're not already set. Returns
137 those values, in order.
141 sub set_coord { # the one in geocode_Mixin will suffice
143 if ( !$self->get('latitude') || !$self->get('longitude') ) {
144 $self->SUPER::set_coord;
145 $self->set('coord_error', $@);
147 return $self->get('latitude'), $self->get('longitude');
154 =item standardize LOCATION
156 Given a location hash or L<FS::geocode_Mixin> object, standardize the
157 address using the configured method and return an L<FS::GeocodeCache>
160 The methods are the "standardize_*" functions in L<FS::Geo::Misc>.
166 my $location = shift;
167 $location = { $location->location_hash }
168 if UNIVERSAL::can($location, 'location_hash');
170 local $Data::Dumper::Terse = 1;
171 warn "standardizing location:\n".Dumper($location) if $DEBUG;
173 my $method = $conf->config('address_standardize_method');
176 $method = "standardize_$method";
177 my $new_location = eval { FS::Misc::Geo->$method( $location ) };
178 if ( $new_location ) {
182 # standardize_* can return an address with addr_clean => '' if
183 # the address is somehow questionable
187 # XXX need an option to decide what to do on error
188 $location->{'addr_clean'} = '';
189 $location->{'error'} = $@;
191 warn "result:\n".Dumper($location) if $DEBUG;
193 # else $location = $location
194 my $cache = $class->new(%$location);
204 L<FS::Record>, schema.html from the base documentation.