-
Notifications
You must be signed in to change notification settings - Fork 1
/
uls-57.r
2425 lines (1842 loc) · 79.6 KB
/
uls-57.r
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# uls-xx.r
# R file to extract, transform, and load (ETL) the Weekly FCC Universal Licensing System (ULS) files, and manage/convert them appropriately for end-users
# created: Jul 22, 2002
# updated: Jul 23, 2024
# prepended object identifiers
# con = connection
# df = data frame (list)
# f = function
# l = list (recursive vector)
# s = scalar (one-element vector)
# s4 = generic s4 object
# time = time (POSIXct)
# v = vector (multi-element vector)
# we need the 'DBI' package (to provide a consistent, DBMS-neutral Databse access environment) -- '> install.packages( "DBI" )'
require( "DBI" )
# we need the 'RSQLite' package (to manage the row-oriented relational DBMS structure) -- '> install.packages( "RSQLite" )'
require( "RSQLite" )
# we need the 'stringr' package (to help with string pattern matching without resorting to Regular Expressions) -- '> install.packages( "stringr" )'
require( "stringr" )
# we need the 'data.table' package (to help write .csv files faster) -- '> install.packages( "data.table" )'
# but it writes a file that can't be imported correctly...e.g., into MS-Access (so I'm temporarily not using it)
require( "data.table" )
# we need the "arrow" package -- '> install.packages( "arrow" )'
# this helps with writing '.parquet' files (columar, compressed, fast, portable)
require( "arrow" )
# we need the "duckdb" package -- '> install.packages( "duckdb" )'
# this helps with writing '.duckdb' files (columar, compressed, fast, portable)
require( "duckdb" )
# we need the "zip" package -- '> install.packages( "zip" )'
# this helps with reading and writing '.zip' files (it has not external dependencies which makes it more portable across environments)
require( "zip" )
# we need the "openxlsx" package -- '> install.packages( "openxlsx" )'
# this helps with writing Excel files (deprecated...it doesn't scale to big tables)
#library( "openxlsx" )
# Post-Join Queries (State)
# --------------------------------------------------
f.dbmspostjoinqueriesState <- function( s4.db, s.state = s.state ) {
# document the progress to the user
time.tic <- Sys.time()
print( paste0( "indexing the final '", s.state, "' table", "..." ))
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX frequency_assigned_tblGeoState_index ON tblGeoState (frequency_assigned)" )
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX call_sign_tblGeoState_index ON tblGeoState (call_sign)" )
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX radio_service_code_tblGeoState_index ON tblGeoState (radio_service_code)" )
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX class_station_code_tblGeoState_index ON tblGeoState (class_station_code)" )
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX location_county_tblGeoState_index ON tblGeoState (location_county)" )
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX location_city_tblGeoState_index ON tblGeoState (location_city)" )
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX location_address_tblGeoState_index ON tblGeoState (location_address)" )
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX entity_name_tblGeoState_index ON tblGeoState (entity_name)" )
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX control_county_tblGeoState_index ON tblGeoState (control_county)" )
print( Sys.time() - time.tic )
# document the progress to the user
time.tic <- Sys.time()
print( paste0( "generating the 'SoCal' (subsetted from '", s.state, "') table", "..." ))
s.rows <- dbExecute( conn = s4.db,
"CREATE TABLE tblGeoCountiesSoCal
AS SELECT DISTINCT *
FROM tblGeoState
WHERE (
location_county='IMPERIAL' OR
location_county='KERN' OR
location_county='LOS ANGELES' OR
location_county='ORANGE' OR
location_county='RIVERSIDE' OR
location_county='SAN BERNARDINO' OR
location_county='SAN DIEGO' OR
location_county='SAN LUIS OBISPO' OR
location_county='SANTA BARBARA' OR
location_county='VENTURA'
)
OR
(
(location_county='' OR location_county IS NULL) AND (
control_county='IMPERIAL' OR
control_county='KERN' OR
control_county='LOS ANGELES' OR
control_county='ORANGE' OR
control_county='RIVERSIDE' OR
control_county='SAN BERNARDINO' OR
control_county='SAN DIEGO' OR
control_county='SAN LUIS OBISPO' OR
control_county='SANTA BARBARA' OR
control_county='VENTURA')
)
ORDER BY frequency_assigned,
call_sign,
radio_service_code,
class_station_code,
location_state,
location_county,
location_city,
location_address
"
)
print( Sys.time() - time.tic )
# document the progress to the user
time.tic <- Sys.time()
print( paste0( "setting up to export the '", s.state, "' table", "..." ))
# old way (works)
# df.res <- dbGetQuery( conn = s4.db,
# "SELECT * FROM tblGeoState
# "
# )
# new way (also works, slightly faster)
df.res <- dbReadTable( conn = s4.db, name = "tblGeoState" )
# make sure it is ordered correctly
df.res <- df.res[
order(
df.res$frequency_assigned,
df.res$call_sign,
df.res$radio_service_code,
df.res$class_station_code,
df.res$location_state,
df.res$location_county,
df.res$location_city,
df.res$location_address
),
]
# document the progress to the user
time.tic <- Sys.time()
print( paste0( "exporting the '", s.state, "'table to a '.csv' file", "..." ))
# fwrite( x = df.res, file = paste0( tolower( s.state ), ".csv" ), sep = "|", eol = "\r\n", row.names = FALSE )
# write.table( x = df.res, file = paste0( tolower( s.state ), ".csv" ), sep = "|", eol = "\r\n", row.names = FALSE )
if( Sys.info()[ "sysname" ] == "Windows" ) {
write.csv( x = df.res, file = paste0( tolower( s.state ), ".csv" ))
} else {
write.csv( x = df.res, file = paste0( tolower( s.state ), ".csv" ), eol = "\r\n" )
}
print( Sys.time() - time.tic )
# document the progress to the user
time.tic <- Sys.time()
print( paste0( "zipping the '.csv' file to a '.zip' file", "..." ))
# ensure that a zip executable can be found somewhere on the system (e.g., "R_ZIPCMD" environment variable)
zip( zipfile = "ca-csv.zip", files = "ca.csv" )
print( Sys.time() - time.tic )
# document the progress to the user
time.tic <- Sys.time()
print( paste0( "exporting the 'CA' table to a '.parquet' file", "..." ))
# s.tempfile <- tempfile( fileext = ".parquet" )
write_parquet( x = df.res, sink = "ca.parquet" )
print( Sys.time() - time.tic )
# document the progress to the user
time.tic <- Sys.time()
print( paste0( "exporting the 'CA' table to a '.arrow' file", "..." ))
# s.tempfile <- tempfile( fileext = ".arrow" )
write_feather( x = df.res, sink = "ca.arrow" )
print( Sys.time() - time.tic )
# document the progress to the user
time.tic <- Sys.time()
print( paste0( "exporting the 'CA' table to a '.duckdb' file", "..." ))
# s.tempfile <- tempfile( fileext = ".duckdb" )
# s4.db.duckdb <- dbConnect( duckdb(), dbdir = "ca.duckdb", read_only = FALSE )
s4.db.duckdb <- dbConnect( duckdb(), "ca.duckdb" )
dbWriteTable( conn = s4.db.duckdb, name = "ca", value = df.res, overwrite = TRUE )
# works but throws a warning
# dbDisconnect( conn = s4.db.duckdb, shutdown = TRUE )
# trying...
dbDisconnect( conn = s4.db.duckdb )
duckdb_shutdown( duckdb() )
# rm( s4.db.duckdb )
print( Sys.time() - time.tic )
rm( df.res )
# document the progress to the user
time.tic <- Sys.time()
print( paste0( "exporting the '", s.state, " SoCal' table", "..." ))
# old way (works)
# df.res <- dbGetQuery( conn = s4.db,
# "SELECT * FROM tblGeoCountiesSoCal
# "
# )
# new way (also works, slightly faster)
df.res <- dbReadTable( conn = s4.db, name = "tblGeoCountiesSoCal" )
# make sure it is ordered correctly
df.res <- df.res[
order(
df.res$frequency_assigned,
df.res$call_sign,
df.res$radio_service_code,
df.res$class_station_code,
df.res$location_state,
df.res$location_county,
df.res$location_city,
df.res$location_address
),
]
# fwrite( x = df.res, file = "socal.csv", sep = "|", eol = "\r\n", row.names = FALSE )
# write.table( x = df.res, file = "socal.csv", sep = "|", eol = "\r\n", row.names = FALSE )
if( Sys.info()[ "sysname" ] == "Windows" ) {
write.csv( x = df.res, file = "socal.csv" )
} else {
write.csv( x = df.res, file = "socal.csv", eol = "\r\n" )
}
print( Sys.time() - time.tic )
# generate files for individual CA SoCal counties
# set which counties we want (currently, CA SoCal counties only)
v.socal.counties <- c(
"IMPERIAL", "KERN", "LOS ANGELES", "ORANGE", "RIVERSIDE",
"SAN BERNARDINO", "SAN DIEGO", "SAN LUIS OBISPO", "SANTA BARBARA", "VENTURA"
)
time.tic <- Sys.time()
# loop through each CA SoCal county
for( s.counter in 1: length( v.socal.counties )) {
# get the current county
s.socal.county <- v.socal.counties[ s.counter ]
# document the progress to the user
print( paste0( "exporting the 'CA SoCal'", " -- ", s.socal.county, " (subsetted from 'CA SoCal')", " table", "..." ))
# extract the data just for that county (from the SoCalCounties dataframe)
# df.res.county <- df.res[ location_county == s.socal.county | (( location_county == "" | location_county == NULL ) & control_county == s.socal.county ), ]
# df.res.county <- df.res[ location_county == s.socal.county | (( location_county == "" | is.na( location_county )) & control_county == s.socal.county ), ]
# df.res.county <- df.res[ df.res$location_county == s.socal.county | df.res$control_county == s.socal.county, ]
df.res.county <- subset( x = df.res, subset = ( location_county == s.socal.county | control_county == s.socal.county ))
# we only want the "Active" licenses
df.res.county <- subset( x = df.res.county, subset = license_status == "A" )
# we don't want a few columns
df.res.county <- subset( x = df.res.county, select = -c( license_status, frequency_upper_band, frequency_carrier ))
# remove any spaces within the county name
s.socal.county.nospaces <- gsub( pattern = " ", replacement = "", x = s.socal.county )
# and convert the county name to lower case
s.socal.county.nospaces <- tolower( x = s.socal.county.nospaces )
# write the file out as a .csv
# fwrite( x = df.res.county, file = paste0( tolower( s.state ), "-", s.socal.county.nospaces, ".csv" ), sep = "|", eol = "\r\n", row.names = FALSE )
# write.table( x = df.res.county, file = paste0( tolower( s.state ), "-", s.socal.county.nospaces, ".csv" ), sep = "|", eol = "\r\n", row.names = FALSE )
if( Sys.info()[ "sysname" ] == "Windows" ) {
write.csv( x = df.res.county, file = paste0( tolower( s.state ), "-", s.socal.county.nospaces, ".csv" ))
} else {
write.csv( x = df.res.county, file = paste0( tolower( s.state ), "-", s.socal.county.nospaces, ".csv" ), eol = "\r\n" )
}
# create an Excel Workbook
# s4.workbook <- createWorkbook()
# create a Worksheet within the Excel Workbook
# s.sheetname <- s.socal.county.nospaces
# s4.worksheet <- addWorksheet( wb = s4.workbook, sheetName = s.sheetname )
# add data to the Worksheet within the Excel Workbook
# s4.writedata <- writeData( wb = s4.workbook, sheet = s.sheetname, x = df.res.county, rowNames = FALSE )
# save the workbook
# s4.saveworkbook <- saveWorkbook( wb = s4.workbook, file = paste0( tolower( s.state ), "-", s.socal.county.nospaces, ".xlsx" ), overwrite = TRUE )
# rm( s4.worksheet )
# rm( s4.workbook )
rm( df.res.county )
}
rm( df.res )
print( Sys.time() - time.tic )
return()
}
# Open the DB
# --------------------------------------------------
f.dbmsopen <- function( s.dbname ) {
# document the progress to the user
print( paste0( "opening the database", "..." ))
# open the SQLite Database connection handle via RSQLite
s4.db <- dbConnect( drv = SQLite(), dbname = s.dbname )
return( s4.db )
}
# Write various files (SoCal Counties and 'all of CA')
# ----------------------------------------------------
f.write.files <- function( v.zip.file.prefixes, v.data.file.prefixes, s.dbname ) {
# open a Database
s4.db <- f.dbmsopen( s.dbname )
# post-join queries (CA)
s.state <- "CA"
f.dbmspostjoinqueriesState( s4.db, s.state )
# document the progress to the user
print( paste0( "disconnecting the database", "..." ))
# close the Database connection handle
dbDisconnect( conn = s4.db, shutdown = TRUE )
return()
}
# Join Query (State)
# --------------------------------------------------
f.dbmsjoinqueryState <- function( s4.db, s.state ) {
# set important constants
# document the progress to the user
print( paste0( "generating the initial '", s.state, "' (joined) table", "..." ))
s.rows <- dbExecute( conn = s4.db,
"DROP TABLE IF EXISTS tblGeoState1
"
)
s.rows <- dbExecute( conn = s4.db,
"DROP TABLE IF EXISTS tblGeoState2
"
)
s.rows <- dbExecute( conn = s4.db,
"DROP TABLE IF EXISTS tblGeoState3
"
)
s.rows <- dbExecute( conn = s4.db,
"CREATE TABLE tblGeoState1
AS SELECT DISTINCT
PUBACC_EN.call_sign ,
PUBACC_EN.entity_name AS 'contact_licensee'
FROM PUBACC_EN
WHERE (PUBACC_EN.entity_type = 'CL')
"
)
s.rows <- dbExecute( conn = s4.db,
"CREATE INDEX call_sign_tblGeoState1_index ON PUBACC_CP (call_sign)
"
)
s.rows <- dbExecute( conn = s4.db,
"DROP TABLE IF EXISTS tblGeoState
"
)
# do the main join
# it appears that there is a hard max. of 6 tables (so we need to do two joins in sequence rather than just one big join)
time.tic <- Sys.time()
print( paste0( "generating the main '", s.state, "' join", "..." ))
s.rows <- dbExecute( conn = s4.db,
"CREATE TABLE tblGeoState2
AS SELECT DISTINCT
PUBACC_FR.frequency_assigned ,
PUBACC_FR.call_sign ,
PUBACC_HD.license_status ,
PUBACC_HD.radio_service_code ,
PUBACC_FR.class_station_code ,
PUBACC_LO.location_state ,
PUBACC_LO.location_county ,
PUBACC_LO.location_city ,
PUBACC_LO.location_address ,
PUBACC_LO.location_name ,
tblGeoState1.contact_licensee ,
PUBACC_EN.entity_name ,
PUBACC_EN.state ,
PUBACC_EN.city ,
PUBACC_EN.street_address ,
PUBACC_CP.state_code ,
PUBACC_CP.control_county ,
PUBACC_CP.control_city ,
PUBACC_CP.control_address ,
PUBACC_CP.control_phone ,
PUBACC_FR.cnt_mobile_units ,
PUBACC_HD.grant_date ,
PUBACC_HD.expired_date ,
PUBACC_HD.cancellation_date ,
PUBACC_HD.effective_date ,
PUBACC_HD.last_action_date ,
PUBACC_FR.unique_system_identifier ,
-- PUBACC_FR.ULS_File_Number ,
PUBACC_LO.ground_elevation ,
PUBACC_LO.lat_degrees ,
PUBACC_LO.lat_minutes ,
PUBACC_LO.lat_seconds ,
PUBACC_LO.lat_direction ,
PUBACC_LO.long_degrees ,
PUBACC_LO.long_minutes ,
PUBACC_LO.long_seconds ,
PUBACC_LO.long_direction ,
PUBACC_FR.frequency_upper_band ,
PUBACC_FR.frequency_carrier
-- PUBACC_FR.db_id
FROM PUBACC_LO
INNER JOIN PUBACC_CP ON PUBACC_LO.call_sign=PUBACC_CP.call_sign
INNER JOIN PUBACC_EN ON PUBACC_LO.call_sign=PUBACC_EN.call_sign
INNER JOIN PUBACC_HD ON PUBACC_LO.call_sign=PUBACC_HD.call_sign
INNER JOIN PUBACC_FR ON PUBACC_LO.call_sign=PUBACC_FR.call_sign
INNER JOIN tblGeoState1 ON PUBACC_LO.call_sign=tblGeoState1.call_sign
WHERE (PUBACC_EN.entity_type = 'L') AND
(location_state = 'CA' OR state = 'CA' OR state_code = 'CA')
-- ORDER BY PUBACC_FR.frequency_assigned,
-- PUBACC_FR.call_sign,
-- PUBACC_HD.license_status,
-- PUBACC_HD.radio_service_code,
-- PUBACC_FR.class_station_code,
-- PUBACC_LO.location_state,
-- PUBACC_LO.location_county,
-- PUBACC_LO.location_city,
-- PUBACC_LO.location_address
"
)
print( Sys.time() - time.tic )
# create indexes (tblGeoState2)
print( paste0( "indexing ", "tblGeoState2", "..." ))
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX call_sign_tblGeoState2_index ON tblGeoState2 (call_sign, frequency_assigned)" )
time.tic <- Sys.time()
print( paste0( "generating the 'Emissions' join", "..." ))
s.rows <- dbExecute( conn = s4.db,
# "EXPLAIN QUERY PLAN CREATE TABLE tblGeoState3
"CREATE TABLE tblGeoState3
AS SELECT DISTINCT
tblGeoState2.frequency_assigned ,
tblGeoState2.call_sign ,
tblGeoState2.license_status ,
tblGeoState2.radio_service_code ,
tblGeoState2.class_station_code ,
tblGeoState2.location_state ,
tblGeoState2.location_county ,
tblGeoState2.location_city ,
tblGeoState2.location_address ,
tblGeoState2.location_name ,
tblGeoState2.contact_licensee ,
tblGeoState2.entity_name ,
tblGeoState2.state ,
tblGeoState2.city ,
tblGeoState2.street_address ,
tblGeoState2.state_code ,
tblGeoState2.control_county ,
tblGeoState2.control_city ,
tblGeoState2.control_address ,
tblGeoState2.control_phone ,
tblGeoState2.cnt_mobile_units ,
tblGeoState2.grant_date ,
tblGeoState2.expired_date ,
tblGeoState2.cancellation_date ,
tblGeoState2.effective_date ,
tblGeoState2.last_action_date ,
tblGeoState2.unique_system_identifier ,
-- tblGeoState2.ULS_File_Number ,
tblGeoState2.ground_elevation ,
tblGeoState2.lat_degrees ,
tblGeoState2.lat_minutes ,
tblGeoState2.lat_seconds ,
tblGeoState2.lat_direction ,
tblGeoState2.long_degrees ,
tblGeoState2.long_minutes ,
tblGeoState2.long_seconds ,
tblGeoState2.long_direction ,
PUBACC_EM.emission_code ,
tblGeoState2.frequency_upper_band ,
tblGeoState2.frequency_carrier
-- PUBACC_FR.db_id
FROM tblGeoState2
INNER JOIN PUBACC_EM ON tblGeoState2.call_sign=PUBACC_EM.call_sign AND tblGeoState2.frequency_assigned=PUBACC_EM.frequency_assigned
-- ORDER BY tblGeoState2.frequency_assigned,
-- tblGeoState2.call_sign,
-- tblGeoState2.license_status,
-- tblGeoState2.radio_service_code,
-- tblGeoState2.class_station_code,
-- tblGeoState2.location_state,
-- tblGeoState2.location_county,
-- tblGeoState2.location_city,
-- tblGeoState2.location_address
"
)
#g.res <<- res
#stop()
print( Sys.time() - time.tic )
time.tic <- Sys.time()
print( paste0( "sorting the final '", s.state, "' table", "..." ))
s.rows <- dbExecute( conn = s4.db,
"CREATE TABLE tblGeoState
AS SELECT DISTINCT
tblGeoState3.frequency_assigned ,
tblGeoState3.call_sign ,
tblGeoState3.license_status ,
tblGeoState3.radio_service_code ,
tblGeoState3.class_station_code ,
tblGeoState3.location_state ,
tblGeoState3.location_county ,
tblGeoState3.location_city ,
tblGeoState3.location_address ,
tblGeoState3.location_name ,
tblGeoState3.contact_licensee ,
tblGeoState3.entity_name ,
tblGeoState3.state ,
tblGeoState3.city ,
tblGeoState3.street_address ,
tblGeoState3.state_code ,
tblGeoState3.control_county ,
tblGeoState3.control_city ,
tblGeoState3.control_address ,
tblGeoState3.control_phone ,
tblGeoState3.cnt_mobile_units ,
tblGeoState3.grant_date ,
tblGeoState3.expired_date ,
tblGeoState3.cancellation_date ,
tblGeoState3.effective_date ,
tblGeoState3.last_action_date ,
tblGeoState3.unique_system_identifier ,
-- tblGeoState3.ULS_File_Number ,
tblGeoState3.ground_elevation ,
tblGeoState3.lat_degrees ,
tblGeoState3.lat_minutes ,
tblGeoState3.lat_seconds ,
tblGeoState3.lat_direction ,
tblGeoState3.long_degrees ,
tblGeoState3.long_minutes ,
tblGeoState3.long_seconds ,
tblGeoState3.long_direction ,
tblGeoState3.emission_code ,
tblGeoState3.frequency_upper_band ,
tblGeoState3.frequency_carrier
-- PUBACC_FR.db_id
FROM tblGeoState3
-- LEFT JOIN PUBACC_EM ON tblGeoState2.call_sign=PUBACC_EM.call_sign
ORDER BY tblGeoState3.frequency_assigned,
tblGeoState3.call_sign,
tblGeoState3.license_status,
tblGeoState3.radio_service_code,
tblGeoState3.class_station_code,
tblGeoState3.location_state,
tblGeoState3.location_county,
tblGeoState3.location_city,
tblGeoState3.location_address
"
)
print( Sys.time() - time.tic )
s.rows <- dbExecute( conn = s4.db,
"DROP TABLE tblGeoState3
"
)
s.rows <- dbExecute( conn = s4.db,
"DROP TABLE tblGeoState2
"
)
s.rows <- dbExecute( conn = s4.db,
"DROP TABLE tblGeoState1
"
)
return()
}
# Pre-Join Queries (State)
# --------------------------------------------------
f.dbmsprejoinqueriesState <- function( s4.db, s.state ) {
# deleting records *not* for this state by location_state
print( paste0( "deleting non-'", s.state, "' records by location_state", "..." ))
s.rows <- dbExecute( conn = s4.db,
"DELETE FROM PUBACC_LO
WHERE NOT ( location_state = 'CA' OR location_state = '' )
"
)
# Re-do the PUBACC_LO index
# print( paste0( "reindexing", "PUBACC_LO (callsign) index", ..." ))
# s.rows <- dbExecute( conn = s4.db,
# "REINDEX call_sign_LO_index
# "
# )
# deleting records *not* for this state by state_code
print( paste0( "deleting non-'", s.state, "' records by state_code", "..." ))
s.rows <- dbExecute( conn = s4.db,
"DELETE FROM PUBACC_CP
WHERE NOT ( state_code = 'CA' )
"
)
# Re-do the PUBACC_CP index
# print( paste0( "reindexing", "PUBACC_CP (callsign) index", ..." ))
# s.rows <- dbExecute( conn = s4.db,
# "REINDEX call_sign_CP_index
# "
# )
return()
}
# Post-Join Queries (US)
# --------------------------------------------------
f.dbmspostjoinqueriesUS <- function( s4.db ) {
# document the progress to the user
time.tic <- Sys.time()
print( paste0( "indexing the 'US' table", "..." ))
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX frequency_assigned_tblGeoUS_index ON tblGeoUS (frequency_assigned)" )
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX call_sign_tblGeoUS_index ON tblGeoUS (call_sign)" )
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX radio_service_code_tblGeoUS_index ON tblGeoUS (radio_service_code)" )
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX class_station_code_tblGeoUS_index ON tblGeoUS (class_station_code)" )
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX location_state_tblGeoUS_index ON tblGeoUS (location_state)" )
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX location_county_tblGeoUS_index ON tblGeoUS (location_county)" )
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX location_city_tblGeoUS_index ON tblGeoUS (location_city)" )
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX location_address_tblGeoUS_index ON tblGeoUS (location_address)" )
# s.rows <- dbExecute( conn = s4.db, "CREATE INDEX entity_name_tblGeoUS_index ON tblGeoUS (entity_name)" )
# s.rows <- dbExecute( conn = s4.db, "CREATE INDEX control_county_tblGeoUS_index ON tblGeoUS (control_county)" )
print( Sys.time() - time.tic )
# document the progress to the user
time.tic <- Sys.time()
print( paste0( "setting up to export the 'US' table", "..." ))
# old way (works)
# df.res <- dbGetQuery( conn = s4.db,
# "SELECT * FROM tblGeoUS
# "
# )
# new way (also works, slightly faster)
df.res <- dbReadTable( conn = s4.db, name = "tblGeoUS" )
# make sure it is ordered correctly
df.res <- df.res[
order(
df.res$frequency_assigned,
df.res$call_sign,
df.res$radio_service_code,
df.res$class_station_code,
df.res$location_state,
df.res$location_county,
df.res$location_city,
df.res$location_address
),
]
print( Sys.time() - time.tic )
# document the progress to the user
time.tic <- Sys.time()
print( paste0( "exporting the 'US' table to a '.csv' file", "..." ))
# fwrite( x = df.res, file = "us.csv", sep = "|", eol = "\r\n", row.names = FALSE )
# write.table( x = df.res, file = "us.csv", sep = "|", eol = "\r\n", row.names = FALSE )
if( Sys.info()[ "sysname" ] == "Windows" ) {
write.csv( x = df.res, file = "us.csv" )
} else {
write.csv( x = df.res, file = "us.csv", eol = "\r\n" )
}
print( Sys.time() - time.tic )
# document the progress to the user
time.tic <- Sys.time()
print( paste0( "zipping the '.csv' file to a '.zip' file", "..." ))
# ensure that a zip executable can be found somewhere on the system (e.g., "R_ZIPCMD" environment variable)
zip( zipfile = "us-csv.zip", files = "us.csv" )
print( Sys.time() - time.tic )
# document the progress to the user
time.tic <- Sys.time()
print( paste0( "exporting the 'US' table to a '.parquet' file", "..." ))
# s.tempfile <- tempfile( fileext = ".parquet" )
write_parquet( x = df.res, sink = "us.parquet" )
print( Sys.time() - time.tic )
# document the progress to the user
time.tic <- Sys.time()
print( paste0( "exporting the 'US' table to a '.arrow' file", "..." ))
# s.tempfile <- tempfile( fileext = ".arrow" )
write_feather( x = df.res, sink = "us.arrow" )
print( Sys.time() - time.tic )
# document the progress to the user
time.tic <- Sys.time()
print( paste0( "exporting the 'US' table to a '.duckdb' file", "..." ))
# s.tempfile <- tempfile( fileext = ".duckdb" )
# s4.db.duckdb <- dbConnect( duckdb(), dbdir = "us.duckdb", read_only = FALSE )
s4.db.duckdb <- dbConnect( duckdb(), "us.duckdb" )
dbWriteTable( conn = s4.db.duckdb, name = "us", value = df.res, overwrite = TRUE )
# works but throws a warning
# dbDisconnect( conn = s4.db.duckdb, shutdown = TRUE )
# trying...
dbDisconnect( conn = s4.db.duckdb )
duckdb_shutdown( duckdb() )
# rm( s4.db.duckdb )
print( Sys.time() - time.tic )
rm( df.res )
return()
}
# Join Query (US)
# --------------------------------------------------
f.dbmsjoinqueryUS <- function( s4.db ) {
# set important constants
s.state <- "US"
# document the progress to the user
print( paste0( "generating the initial '", s.state, "' (joined) table", "..." ))
s.rows <- dbExecute( conn = s4.db,
"DROP TABLE IF EXISTS tblGeoUS1
"
)
s.rows <- dbExecute( conn = s4.db,
"DROP TABLE IF EXISTS tblGeoUS2
"
)
s.rows <- dbExecute( conn = s4.db,
"DROP TABLE IF EXISTS tblGeoUS3
"
)
s.rows <- dbExecute( conn = s4.db,
"DROP TABLE IF EXISTS tblGeoUS4
"
)
s.rows <- dbExecute( conn = s4.db,
"DROP TABLE IF EXISTS tblGeoUS5
"
)
# s.rows <- dbExecute( conn = s4.db,
# "CREATE TABLE tblGeoState1
# AS SELECT DISTINCT
# PUBACC_EN.call_sign ,
# PUBACC_EN.entity_name AS 'contact_licensee'
# FROM PUBACC_EN
# WHERE (PUBACC_EN.entity_type = 'CL')
# "
# )
# s.rows <- dbExecute( conn = s4.db,
# "CREATE INDEX call_sign_tblGeoState1_index ON PUBACC_CP (call_sign)
# "
# )
# s.rows <- dbExecute( conn = s4.db,
# "DROP TABLE IF EXISTS tblGeoState
# "
# )
# it appears that there is a hard max. of 6 tables (so we need to do multiple joins in sequence rather than just one big join)
# 1 (takes ~59 minutes...why?)
time.tic <- Sys.time()
print( paste0( "generating the '", "tblGeoUS1 (FR<-->EM)", "' join", "..." ))
s.rows <- dbExecute( conn = s4.db,
"CREATE TABLE tblGeoUS1
AS SELECT DISTINCT
PUBACC_FR.call_sign ,
PUBACC_FR.frequency_assigned ,
PUBACC_FR.class_station_code ,
PUBACC_FR.cnt_mobile_units ,
PUBACC_FR.unique_system_identifier ,
PUBACC_FR.frequency_upper_band ,
PUBACC_FR.frequency_carrier ,
PUBACC_EM.emission_code
FROM PUBACC_FR
INNER JOIN PUBACC_EM ON PUBACC_FR.call_sign=PUBACC_EM.call_sign AND PUBACC_FR.frequency_assigned=PUBACC_EM.frequency_assigned
"
)
# create indexes (tblGeoUS1)
print( paste0( "indexing ", "tblGeoUS1", "..." ))
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX call_sign_tblGeoUS1_index ON tblGeoUS1 (call_sign)" )
print( Sys.time() - time.tic )
# 2 (takes ~2.5 minutes)
time.tic <- Sys.time()
print( paste0( "generating the '", "tblGeoUS2 (tblGeoUS1<-->CP)", "' join", "..." ))
s.rows <- dbExecute( conn = s4.db,
"CREATE TABLE tblGeoUS2
AS SELECT DISTINCT
tblGeoUS1.call_sign ,
tblGeoUS1.frequency_assigned ,
tblGeoUS1.class_station_code ,
tblGeoUS1.cnt_mobile_units ,
tblGeoUS1.unique_system_identifier ,
tblGeoUS1.frequency_upper_band ,
tblGeoUS1.frequency_carrier ,
tblGeoUS1.emission_code ,
PUBACC_CP.state_code ,
PUBACC_CP.control_county ,
PUBACC_CP.control_city ,
PUBACC_CP.control_address ,
PUBACC_CP.control_phone
FROM tblGeoUS1
INNER JOIN PUBACC_CP ON tblGeoUS1.call_sign=PUBACC_CP.call_sign
"
)
# create indexes (tblGeoUS2)
print( paste0( "indexing ", "tblGeoUS2", "..." ))
s.rows <- dbExecute( conn = s4.db, "CREATE INDEX call_sign_tblGeoUS2_index ON tblGeoUS2 (call_sign)" )
print( Sys.time() - time.tic )
# 3
time.tic <- Sys.time()
print( paste0( "generating the '", "tblGeoUS3 (tblGeoUS2<-->EN)", "' join", "..." ))
s.rows <- dbExecute( conn = s4.db,
"CREATE TABLE tblGeoUS3
AS SELECT DISTINCT
tblGeoUS2.call_sign ,
tblGeoUS2.frequency_assigned ,
tblGeoUS2.class_station_code ,
tblGeoUS2.cnt_mobile_units ,
tblGeoUS2.unique_system_identifier ,
tblGeoUS2.frequency_upper_band ,
tblGeoUS2.frequency_carrier ,
tblGeoUS2.emission_code ,
tblGeoUS2.state_code ,
tblGeoUS2.control_county ,
tblGeoUS2.control_city ,
tblGeoUS2.control_address ,
tblGeoUS2.control_phone ,
PUBACC_EN.entity_name AS 'contact_licensee',
PUBACC_EN.state ,
PUBACC_EN.city ,
PUBACC_EN.street_address
FROM tblGeoUS2
INNER JOIN PUBACC_EN ON tblGeoUS2.call_sign=PUBACC_EN.call_sign