summaryrefslogtreecommitdiff
path: root/FS/FS/deploy_zone.pm
blob: c618fb9873fce4377e656c19374d157ad1d0c512 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
package FS::deploy_zone;

use strict;
use base qw( FS::o2m_Common FS::Record );
use FS::Record qw( qsearch qsearchs dbh );
use Storable qw(thaw);
use MIME::Base64;

use Cpanel::JSON::XS;
use LWP::UserAgent;
use HTTP::Request::Common;

use Geo::JSON::Polygon;
use Geo::JSON::Feature;

our $CENSUS_YEAR = 2020;

our $tech_label  = FS::part_pkg_fcc_option->technology_labels;

=head1 NAME

FS::deploy_zone - Object methods for deploy_zone records

=head1 SYNOPSIS

  use FS::deploy_zone;

  $record = new FS::deploy_zone \%hash;
  $record = new FS::deploy_zone { 'column' => 'value' };

  $error = $record->insert;

  $error = $new_record->replace($old_record);

  $error = $record->delete;

  $error = $record->check;

=head1 DESCRIPTION

An FS::deploy_zone object represents a geographic zone where a certain kind
of service is available.  Currently we store this information to generate
the FCC Form 477 deployment reports, but it may find other uses later.

FS::deploy_zone inherits from FS::Record.  The following fields are currently
supported:

=over 4

=item zonenum

primary key

=item description

Optional text describing the zone.

=item agentnum

The agent that serves this zone.

=item censusyear

The census map year for which this zone was last updated. May be null for
zones that contain no census blocks (mobile zones, or fixed zones that haven't
had their block lists filled in yet).

=item dbaname

The name under which service is marketed in this zone.  If null, will 
default to the agent name.

=item zonetype

The way the zone geography is defined: "B" for a list of census blocks
(used by the FCC for fixed broadband service), "P" for a polygon (for 
mobile services).  See L<FS::deploy_zone_block> and L<FS::deploy_zone_vertex>.
Note that block-type zones are still allowed to have a vertex list, for
use by the map editor.

=item technology

The FCC technology code for the type of service available.

=item spectrum

For mobile service zones, the FCC code for the RF band.

=item adv_speed_up

For broadband, the advertised upstream bandwidth in the zone.  If multiple
speed tiers are advertised, use the highest.

=item adv_speed_down

For broadband, the advertised downstream bandwidth in the zone.

=item cir_speed_up

For broadband, the contractually guaranteed upstream bandwidth, if that type
of service is sold.

=item cir_speed_down

For broadband, the contractually guaranteed downstream bandwidth, if that 
type of service is sold.

=item is_consumer

'Y' if this service is sold for consumer/household use.

=item is_business

'Y' if this service is sold to business or institutional use.  Not mutually
exclusive with is_consumer.

=item is_broadband

'Y' if this service includes broadband Internet.

=item is_voice

'Y' if this service includes voice communication.

=item active_date

The date this zone became active.

=item expire_date

The date this zone became inactive, if any.

=back

=head1 METHODS

=over 4

=item new HASHREF

Creates a new zone.  To add the zone to the database, see L<"insert">.

=cut

# the new method can be inherited from FS::Record, if a table method is defined

sub table { 'deploy_zone'; }

=item insert ELEMENTS

Adds this record to the database.  If there is an error, returns the error,
otherwise returns false.

=cut

# the insert method can be inherited from FS::Record

=item delete

Delete this record from the database.

=cut

sub delete {
  my $oldAutoCommit = $FS::UID::AutoCommit;
  local $FS::UID::AutoCommit = 0;
  # clean up linked records
  my $self = shift;
  my $error;
  foreach (qw(deploy_zone_block deploy_zone_vertex)) {
    $error ||= $self->process_o2m(
      'table'   => $_,
      'num_col' => 'zonenum',
      'fields'  => 'zonenum',
      'params'  => {},
    );
  }
  $error ||= $self->SUPER::delete(@_);
  
  if ($error) {
    dbh->rollback if $oldAutoCommit;
    return $error;
  }
  '';
}

=item replace OLD_RECORD

Replaces the OLD_RECORD with this one in the database.  If there is an error,
returns the error, otherwise returns false.

=cut

sub replace {
  my $self = shift;
  my $old = shift || $self->replace_old;

  $self->expire_date(time)
    if $self->disabled eq 'Y' && ! $old->disabled && ! $self->expire_date;

  $self->SUPER::replace($old, @_);
}
=item check

Checks all fields to make sure this is a valid zone record.  If there is
an error, returns the error, otherwise returns false.  Called by the insert
and replace methods.

=cut

sub check {
  my $self = shift;

  my $error = 
    $self->ut_numbern('zonenum')
    || $self->ut_text('description')
    || $self->ut_number('agentnum')
    || $self->ut_numbern('censusyear')
    || $self->ut_foreign_key('agentnum', 'agent', 'agentnum')
    || $self->ut_textn('dbaname')
    || $self->ut_enum('zonetype', [ 'B', 'P' ])
    || $self->ut_number('technology')
    || $self->ut_numbern('spectrum')
    || $self->ut_decimaln('adv_speed_up', 3)
    || $self->ut_decimaln('adv_speed_down', 3)
    || $self->ut_decimaln('cir_speed_up', 3)
    || $self->ut_decimaln('cir_speed_down', 3)
    || $self->ut_flag('is_consumer')
    || $self->ut_flag('is_business')
    || $self->ut_flag('is_broadband')
    || $self->ut_flag('is_voice')
    || $self->ut_numbern('active_date')
    || $self->ut_numbern('expire_date')
  ;
  return $error if $error;

  foreach(qw(adv_speed_down adv_speed_up cir_speed_down cir_speed_up)) {
    if ($self->get('is_broadband')) {
      if (!$self->get($_)) {
        $self->set($_, 0);
      }
    } else {
      $self->set($_, '');
    }
  }
  if (!$self->get('active_date')) {
    $self->set('active_date', time);
  }

  $self->SUPER::check;
}

=item deploy_zone_block

Returns the census block records in this zone, in order by census block
number.  Only appropriate to block-type zones.

=item deploy_zone_vertex

Returns the vertex records for this zone, in order by sequence number.

=cut

sub deploy_zone_block {
  my $self = shift;
  qsearch({
      table     => 'deploy_zone_block',
      hashref   => { zonenum => $self->zonenum },
      order_by  => ' ORDER BY censusblock',
  });
}

sub deploy_zone_vertex {
  my $self = shift;
  qsearch({
      table     => 'deploy_zone_vertex',
      hashref   => { zonenum => $self->zonenum },
      order_by  => ' ORDER BY vertexnum',
  });
}

=item shapefile_add SHAPEFILE

Adds this deployment zone to the supplied Geo::Shapelib shapefile.

=cut

sub shapefile_add {
  my( $self, $shapefile ) = @_;

  my @coordinates = map { [ $_->longitude, $_->latitude, 0, 0 ] }
                      $self->deploy_zone_vertex;
  push @coordinates, $coordinates[0];

  push @{$shapefile->{Shapes}}, { 'Vertices' => \@coordinates };
  push @{$shapefile->{ShapeRecords}}, [ $tech_label->{$self->technology},
                                        $self->adv_speed_down,
                                        $self->adv_speed_up,
                                      ];
  '';
}

=item vertices_json

Returns the vertex list for this zone, as a JSON string of

[ [ latitude0, longitude0 ], [ latitude1, longitude1 ] ... ]

=cut

sub vertices_json {
  my $self = shift;
  my @vertices = map { [ $_->latitude, $_->longitude ] } $self->deploy_zone_vertex;
  encode_json(\@vertices);
}

=item geo_json_feature

Returns this zone as a Geo::JSON::Feature object

=cut

sub geo_json_feature {
  my $self = shift;

  my @coordinates = map { [ $_->longitude, $_->latitude ] }
                      $self->deploy_zone_vertex;
  push @coordinates, $coordinates[0];

  Geo::JSON::Feature->new({
    geometry   => Geo::JSON::Polygon->new({ coordinates => [ \@coordinates ] }),
    properties => { 'Technology' => $tech_label->{$self->technology},
                    'Down'       => $self->adv_speed_down,
                    'Up'         => $self->adv_speed_up,
                  },
  })
}

=item kml_add

Adds this deployment zone to the supplied Geo::GoogleEarth::Pluggable object.

=cut

sub kml_polygon {
  my( $self, $kml ) = @_;

  my $name = $self->description. ' ('. $self->adv_speed_down. '/'.
                                       $self->adv_speed_up. ')';

  $kml->Polygon( 'name'        => $name,
                 'coordinates' => [ [ #outerBoundary
                                      map { [ $_->longitude, $_->latitude, 0 ] }
                                        $self->deploy_zone_vertex
                                    ],
                                    #[ #innerBoundary
                                    #]
                                  ]
               );
}

=head2 SUBROUTINES

=over 4

=item process_batch_import JOB, PARAMS

=cut

sub process_batch_import {
  eval {
    use FS::deploy_zone_block;
    use FS::deploy_zone_vertex;
  };
  my $job = shift;
  my $param = shift;
  if (!ref($param)) {
    $param = thaw(decode_base64($param));
  }

  # even if creating a new zone, the deploy_zone object should already
  # be inserted by this point
  my $zonenum = $param->{zonenum}
    or die "zonenum required";
  my $zone = FS::deploy_zone->by_key($zonenum)
    or die "deploy_zone #$zonenum not found";
  my $opt;
  if ( $zone->zonetype eq 'B' ) {
    $opt = { 'table'    => 'deploy_zone_block',
             'params'   => [ 'zonenum', 'censusyear' ],
             'formats'  => { 'plain' => [ 'censusblock' ] },
             'default_csv' => 1,
           };
    $job->update_statustext('1,Inserting census blocks');
  } elsif ( $zone->zonetype eq 'P' ) {
    $opt = { 'table'    => 'deploy_zone_vertex',
             'params'   => [ 'zonenum' ],
             'formats'  => { 'plain' => [ 'latitude', 'longitude' ] },
             'default_csv' => 1,
           };
  } else {
    die "don't know how to import to zonetype ".$zone->zonetype;
  }

  FS::Record::process_batch_import( $job, $opt, $param );

}

=item process_block_lookup JOB, ZONENUM

Look up all the census blocks in the zone's footprint, and insert them.
This will replace any existing block list.

=cut

sub process_block_lookup {
  my $job = shift;
  my $param = shift;
  if (!ref($param)) {
    $param = thaw(decode_base64($param));
  }
  my $zonenum = $param->{zonenum};
  my $zone = FS::deploy_zone->by_key($zonenum)
    or die "zone $zonenum not found\n";

  # wipe the existing list of blocks
  my $error = $zone->process_o2m(
    'table'   => 'deploy_zone_block',
    'num_col' => 'zonenum', 
    'fields'  => 'zonenum',
    'params'  => {},
  );
  die $error if $error;

  $job->update_statustext('0,querying census database') if $job;

  # negotiate the rugged jungle trails of the ArcGIS REST protocol:
  # 1. unlike most places, longitude first.
  my @zone_vertices = map { [ $_->longitude, $_->latitude ] }
    $zone->deploy_zone_vertex;

  return if scalar(@zone_vertices) < 3; # then don't bother

  # 2. package this as "rings", inside a JSON geometry object
  # 3. announce loudly and frequently that we are using spatial reference 
  #    4326, "true GPS coordinates"
  my $geometry = encode_json({
      'rings' => [ \@zone_vertices ],
      'wkid'  => 4326,
  });

  my %query = (
    f               => 'json', # duh
    geometry        => $geometry,
    geometryType    => 'esriGeometryPolygon', # as opposed to a bounding box
    inSR            => 4326,
    outSR           => 4326,
    spatialRel      => 'esriSpatialRelIntersects', # the test to perform
    outFields       => 'GEOID',
    returnGeometry  => 'false',
    orderByFields   => 'OID',
  );
  my $url = 'https://tigerweb.geo.census.gov/arcgis/rest/services/TIGERweb/Tracts_Blocks/MapServer/12/query';
  my $ua = LWP::UserAgent->new;

  # first find out how many of these we're dealing with
  my $response = $ua->request(
    POST $url, Content => [
      %query,
      returnCountOnly => 1,
    ]
  );
  die $response->status_line unless $response->is_success;
  my $data = decode_json($response->content);
  # their error messages are mostly useless, but don't just blindly continue
  die $data->{error}{message} if $data->{error};

  my $count = $data->{count};
  my $inserted = 0;

  #warn "Census block lookup: $count\n";

  my $done = 0;
  while (!$done) {
    $response = $ua->request(
      POST $url, Content => [
        %query,
        resultOffset => $inserted,
      ]
    );
    die $response->status_line unless $response->is_success;
    $data = decode_json($response->content);
    die $data->{error}{message} if $data->{error};
    last unless scalar @{$data->{features}}; #Nothing to insert

    foreach my $feature (@{ $data->{features} }) {
      my $geoid = $feature->{attributes}{GEOID}; # the prize
      my $block = FS::deploy_zone_block->new({
          zonenum     => $zonenum,
          censusblock => $geoid
      });
      $error = $block->insert;
      die "$error (inserting census block $geoid)" if $error;

      $inserted++;
      if ($job and $inserted % 100 == 0) {
        my $percent = sprintf('%.0f', $inserted / $count * 100);
        $job->update_statustext("$percent,creating block records");
      }
    }

    #warn "Inserted $inserted records\n";
    $done = 1 unless $data->{exceededTransferLimit};
  }

  $zone->set('censusyear', $CENSUS_YEAR);  
  $error = $zone->replace;
  warn "$error (updating zone census year)" if $error; # whatever, continue

  return;
}

=head1 BUGS

=head1 SEE ALSO

L<FS::Record>

=cut

1;