1 package FS::geocode_cache;
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::geocode_cache - An address undergoing the geocode process.
22 use FS::geocode_cache;
24 $record = FS::geocode_cache->standardize(%location_hash);
28 An FS::geocode_cache object represents a street address in the process of
29 being geocoded. FS::geocode_cache inherits from FS::geocode_Mixin.
31 Most methods on this object throw an exception on error.
33 FS::geocode_cache has the following fields, with the same meaning as in
38 All other fields have the same meaning as in L<FS::cust_main> and
75 Creates a new cache object. For internal use. See C<standardize>.
79 # minimalist constructor
99 # minimalist accessor, for compatibility with geocode_Mixin
105 $_[0]->{$_[1]} = $_[2];
108 sub location_hash { %{$_[0]} };
110 =item set_censustract
112 Look up the censustract, if it's not already filled in, and return it.
113 On error, sets 'error' and returns nothing.
115 This uses the "get_censustract_*" methods in L<FS::Misc::Geo>; currently
116 the only one is 'ffiec'.
120 sub set_censustract {
123 if ( $self->get('censustract') =~ /^\d{9}\.\d{2}$/ ) {
124 return $self->get('censustract');
126 my $censusyear = $conf->config('census_year');
127 return if !$censusyear;
129 my $method = 'ffiec';
130 # configurable censustract-only lookup goes here if it's ever needed.
131 $method = "get_censustract_$method";
132 my $censustract = eval { FS::Misc::Geo->$method($self, $censusyear) };
133 $self->set("censustract_error", $@);
134 $self->set("censustract", $censustract);
139 Set the latitude and longitude fields if they're not already set. Returns
140 those values, in order.
144 sub set_coord { # the one in geocode_Mixin will suffice
146 if ( !$self->get('latitude') || !$self->get('longitude') ) {
147 $self->SUPER::set_coord;
148 $self->set('coord_error', $@);
150 return $self->get('latitude'), $self->get('longitude');
157 =item standardize LOCATION
159 Given a location hash or L<FS::geocode_Mixin> object, standardize the
160 address using the configured method and return an L<FS::geocode_cache>
163 The methods are the "standardize_*" functions in L<FS::Geo::Misc>.
169 my $location = shift;
170 $location = { $location->location_hash }
171 if UNIVERSAL::can($location, 'location_hash');
173 local $Data::Dumper::Terse = 1;
174 warn "standardizing location:\n".Dumper($location) if $DEBUG;
176 my $method = $conf->config('address_standardize_method');
179 $method = "standardize_$method";
180 my $new_location = eval { FS::Misc::Geo->$method( $location ) };
181 if ( $new_location ) {
185 # standardize_* can return an address with addr_clean => '' if
186 # the address is somehow questionable
190 # XXX need an option to decide what to do on error
191 $location->{'addr_clean'} = '';
192 $location->{'error'} = $@;
194 warn "result:\n".Dumper($location) if $DEBUG;
196 # else $location = $location
197 my $cache = $class->new(%$location);
207 L<FS::Record>, schema.html from the base documentation.