summaryrefslogtreecommitdiff
path: root/FS/FS/GeocodeCache.pm
blob: 9a30f0050412790d28cd830cd499c78f0bd1244c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package FS::GeocodeCache;

use strict;
use vars qw($conf $DEBUG);
use base qw( FS::geocode_Mixin );
use FS::Record qw( qsearch qsearchs );
use FS::Conf;
use FS::Misc::Geo;

use Data::Dumper;

FS::UID->install_callback( sub { $conf = new FS::Conf; } );

$DEBUG = 0;

=head1 NAME

FS::GeocodeCache - An address undergoing the geocode process.

=head1 SYNOPSIS

  use FS::GeocodeCache;

  $record = FS::GeocodeCache->standardize(%location_hash);

=head1 DESCRIPTION

An FS::GeocodeCache object represents a street address in the process of 
being geocoded.  FS::GeocodeCache inherits from FS::geocode_Mixin.

Most methods on this object throw an exception on error.

FS::GeocodeCache has the following fields, with the same meaning as in 
L<FS::cust_location>:

=over 4

=item address1

=item address2

=item city

=item county

=item state

=item zip

=item latitude

=item longitude

=item addr_clean

=item country

=item censustract

=item geocode

=item district

=back

=head1 METHODS

=over 4

=item new HASHREF

Creates a new cache object.  For internal use.  See C<standardize>.

=cut

# minimalist constructor
sub new {
  my $class = shift;
  my $self = {
    company     => '',
    address1    => '',
    address2    => '',
    city        => '',
    state       => '',
    zip         => '',
    country     => '',
    latitude    => '',
    longitude   => '',
    addr_clean  => '',
    censustract => '',
    @_
  };
  bless $self, $class;
}

# minimalist accessor, for compatibility with geocode_Mixin
sub get {
  $_[0]->{$_[1]}
}

sub set {
  $_[0]->{$_[1]} = $_[2];
}

sub location_hash { %{$_[0]} };

=item set_censustract

Look up the censustract, if it's not already filled in, and return it.
On error, sets 'error' and returns nothing.

This uses the "get_censustract_*" methods in L<FS::Misc::Geo>; currently
available are 'uscensus' (default) or 'ffiec' (legacy, used if the
census_legacy configuration option is set).

=cut

sub set_censustract {
  my $self = shift;

  if ( $self->get('censustract') =~ /^\d{9}(\.\d{2}|\d{6})$/ ) {
    return $self->get('censustract');
  }

  my $year = $conf->config('census_legacy') || 2020;
  my $method = ($year==2020) ? 'uscensus' : 'ffiec';

  $method = "get_censustract_$method";
  my $censustract = eval { FS::Misc::Geo->$method($self, $year) };
  $self->set("censustract_error", $@);
  $self->set("censustract", $censustract);
}

=item set_coord

Set the latitude and longitude fields if they're not already set.  Returns
those values, in order.

=cut

sub set_coord { # the one in geocode_Mixin will suffice
  my $self = shift;
  if ( !$self->get('latitude') || !$self->get('longitude') ) {
    $self->SUPER::set_coord;
    $self->set('coord_error', $@);
  }
  return $self->get('latitude'), $self->get('longitude');
}

=head1 CLASS METHODS

=over 4

=item standardize LOCATION

Given a location hash or L<FS::geocode_Mixin> object, standardize the 
address using the configured method and return an L<FS::GeocodeCache> 
object.

The methods are the "standardize_*" functions in L<FS::Geo::Misc>.

=cut

sub standardize {
  my $class = shift;
  my $location = shift;
  $location = { $location->location_hash }
    if UNIVERSAL::can($location, 'location_hash');

  local $Data::Dumper::Terse = 1;
  warn "standardizing location:\n".Dumper($location) if $DEBUG;

  my $method = $conf->config('address_standardize_method');

  if ( $method ) {
    $method = "standardize_$method";
    my $new_location = eval { FS::Misc::Geo->$method( $location ) };
    if ( $new_location ) {
      $location = {
        addr_clean => 'Y',
        %$new_location
        # standardize_* can return an address with addr_clean => '' if
        # the address is somehow questionable
      }
    }
    else {
      # XXX need an option to decide what to do on error
      $location->{'addr_clean'} = '';
      $location->{'error'} = $@;
    }
    warn "result:\n".Dumper($location) if $DEBUG;
  }
  # else $location = $location
  my $cache = $class->new(%$location);
  return $cache;
}

=back

=head1 BUGS

=head1 SEE ALSO

L<FS::Record>, schema.html from the base documentation.

=cut

1;