@@ -28,6 +28,7 @@ use DateTime::Format::ISO8601;
28
28
use DateTime::Format::W3CDTF;
29
29
use Fcntl qw( :flock) ;
30
30
use File::Temp qw( tempfile) ;
31
+ use Geocode::SinglePoint;
31
32
use Integrations::Jadu;
32
33
use JSON::MaybeXS qw( decode_json encode_json) ;
33
34
use LWP::Simple;
@@ -46,6 +47,11 @@ has jurisdiction_id => (
46
47
default => ' centralbedfordshire_jadu' ,
47
48
);
48
49
50
+ has singlepoint => (
51
+ is => ' lazy' ,
52
+ default => sub { Geocode::SinglePoint-> new(config_filename => $_ [0]-> jurisdiction_id) }
53
+ );
54
+
49
55
has jadu => (
50
56
is => ' lazy' ,
51
57
default => sub { Integrations::Jadu-> new(config_filename => $_ [0]-> jurisdiction_id) }
@@ -68,6 +74,17 @@ sub get_integration {
68
74
return $_ [0]-> jadu;
69
75
}
70
76
77
+ =head2 reverse_geocode_radius_meters
78
+
79
+ This is the radius in meters of the area around the report location to search for addresses.
80
+
81
+ =cut
82
+
83
+ has reverse_geocode_radius_meters => (
84
+ is => ' lazy' ,
85
+ default => sub { $_ [0]-> endpoint_config-> {reverse_geocode_radius_meters } }
86
+ );
87
+
71
88
=head2 sys_channel
72
89
73
90
This is the value to set for the 'sys-channel' field when creating a new Fly Tipping case.
@@ -94,6 +111,7 @@ has case_type => (
94
111
95
112
This is a mapping from any town associated with an address in Central Bedfordshire to
96
113
the value that should be set in the 'eso-officer' field when creating a new Fly Tipping case.
114
+ Towns must be specified in lowercase.
97
115
98
116
=cut
99
117
@@ -204,9 +222,45 @@ sub post_service_request {
204
222
my ($self , $service , $args ) = @_ ;
205
223
my $attributes = $args -> {attributes };
206
224
207
- my $officer = $self -> town_to_officer-> {$attributes -> {town }};
208
- if (!$officer ) {
209
- die " No officer found for town " . $attributes -> {town };
225
+ my $addresses = $self -> singlepoint-> get_nearest_addresses(
226
+ $attributes -> {easting },
227
+ $attributes -> {northing },
228
+ $self -> reverse_geocode_radius_meters,
229
+ [' STREET' , ' TOWN' , ' USRN' ],
230
+ );
231
+
232
+ if ($addresses == 0) {
233
+ die sprintf (
234
+ " No addresses found within %dm of easting: %d northing: %d " ,
235
+ $self -> reverse_geocode_radius_meters,
236
+ $attributes -> {easting },
237
+ $attributes -> {northing },
238
+ );
239
+ }
240
+
241
+ my $officer ;
242
+ my $nearest_valid_address ;
243
+ foreach my $address (@$addresses ) {
244
+ my $usrn = $address -> {USRN };
245
+ my $street = $address -> {STREET };
246
+ my $town = $address -> {TOWN };
247
+
248
+ unless ($usrn && $street && $town ) {
249
+ $self -> logger-> warn (" Skipping address missing one or more of USRN, STREET and TOWN" );
250
+ next ;
251
+ }
252
+
253
+ $officer = $self -> town_to_officer-> {lc $town };
254
+ if (!$officer ) {
255
+ $self -> logger-> warn (" Skipping address with unmapped town: " . $town );
256
+ next ;
257
+ }
258
+ $nearest_valid_address = $address ;
259
+ last ;
260
+ }
261
+
262
+ if (!$nearest_valid_address ) {
263
+ die " None of the addresses found were valid." ;
210
264
}
211
265
212
266
my $google_street_view_url = sprintf (
@@ -226,9 +280,9 @@ sub post_service_request {
226
280
' ens-latitude' => $args -> {lat },
227
281
' ens-longitude' => $args -> {long },
228
282
' ens-google-street-view-url' => $google_street_view_url ,
229
- ' usrn' => $attributes -> {usrn },
230
- ' ens-street' => $attributes -> {street },
231
- ' sys-town' => $attributes -> {town },
283
+ ' usrn' => $nearest_valid_address -> {USRN },
284
+ ' ens-street' => $nearest_valid_address -> {STREET },
285
+ ' sys-town' => $nearest_valid_address -> {TOWN },
232
286
' eso-officer' => $officer ,
233
287
' ens-location-description' => $attributes -> {title },
234
288
' ens-land-type' => $attributes -> {land_type },
0 commit comments