+=head1 CLASS METHODS
+
+=over 4
+
+=item part_export
+
+Returns all sector exports. Eventually this may be refined to the level
+of enabling exports on specific sectors.
+
+=cut
+
+sub part_export {
+ my $info = $FS::part_export::exports{'tower_sector'} or return;
+ my @exporttypes = map { dbh->quote($_) } keys %$info or return;
+ qsearch({
+ 'table' => 'part_export',
+ 'extra_sql' => 'WHERE exporttype IN(' . join(',', @exporttypes) . ')'
+ });
+}
+
+=item part_export_svc_broadband
+
+Returns all svc_broadband exports.
+
+=cut
+
+sub part_export_svc_broadband {
+ my $info = $FS::part_export::exports{'svc_broadband'} or return;
+ my @exporttypes = map { dbh->quote($_) } keys %$info or return;
+ qsearch({
+ 'table' => 'part_export',
+ 'extra_sql' => 'WHERE exporttype IN(' . join(',', @exporttypes) . ')'
+ });
+}
+
+=back
+
+=head1 SUBROUTINES
+
+=over 4
+
+=item process_generate_coverage JOB, PARAMS
+
+Queueable routine to fetch the sector coverage map from the tower mapping
+server and store it. Highly experimental. Requires L<Map::Splat> to be
+installed.
+
+PARAMS must include 'sectornum'.
+
+=cut
+
+sub process_generate_coverage {
+ my $job = shift;
+ my $param = shift;
+ $job->update_statustext('0,generating map') if $job;
+ my $sectornum = $param->{sectornum};
+ my $sector = FS::tower_sector->by_key($sectornum)
+ or die "sector $sectornum does not exist";
+ $sector->set('no_regen', 1); # avoid recursion
+ my $tower = $sector->tower;
+
+ load_class('Map::Splat');
+
+ # since this is still experimental, put it somewhere we can find later
+ my $workdir = "$FS::UID::cache_dir/cache.$FS::UID::datasrc/" .
+ "generate_coverage/sector$sectornum-". time;
+ make_path($workdir);
+ my $splat = Map::Splat->new(
+ lon => $tower->longitude,
+ lat => $tower->latitude,
+ height => ($sector->height || $tower->height || 0),
+ freq => $sector->freq_mhz,
+ azimuth => $sector->direction,
+ h_width => $sector->width,
+ tilt => $sector->downtilt,
+ v_width => $sector->v_width,
+ db_levels => [ $sector->db_low, $sector->db_high ],
+ dir => $workdir,
+ #simplify => 0.0004, # remove stairstepping in SRTM3 data?
+ );
+ $splat->calculate;
+
+ my $box = $splat->box;
+ foreach (qw(west east south north)) {
+ $sector->set($_, $box->{$_});
+ }
+ $sector->set('image', $splat->png);
+ my $error = $sector->replace;
+ die $error if $error;
+
+ foreach ($sector->sector_coverage) {
+ $error = $_->delete;
+ die $error if $error;
+ }
+ # XXX undecided whether Map::Splat should even do this operation
+ # or how to store it
+ # or anything else
+ $DB::single = 1;
+ my $data = decode_json( $splat->polygonize_json );
+ for my $feature (@{ $data->{features} }) {
+ my $db = $feature->{properties}{level};
+ my $coverage = FS::sector_coverage->new({
+ sectornum => $sectornum,
+ db_loss => $db,
+ geometry => encode_json($feature->{geometry})
+ });
+ $error = $coverage->insert;
+ }
+
+ die $error if $error;
+}
+
+sub _upgrade_data {
+
+ require FS::Misc::FixIPFormat;
+ FS::Misc::FixIPFormat::fix_bad_addresses_in_table(
+ 'tower_sector', 'sectornum', 'ip_addr',
+ );
+
+ '';
+
+}
+