Class: GeographicItem

Inherits:
ApplicationRecord show all
Includes:
Housekeeping::Timestamps, Housekeeping::Users, Shared::HasPapertrail, Shared::IsData, Shared::SharedAcrossProjects
Defined in:
app/models/geographic_item.rb

Overview

A GeographicItem is one and only one of [point, line_string, polygon, multi_point, multi_line_string, multi_polygon, geometry_collection] which describes a position, path, or area on the globe, generally associated with a geographic_area (through a geographic_area_geographic_item entry), and sometimes only with a georeference.

Key methods in this giant library

‘#geo_object` - return a RGEO object representation

Defined Under Namespace

Classes: GeometryCollection, LineString, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon

Constant Summary collapse

DATA_TYPES =
[
  :point,
  :line_string,
  :polygon,
  :multi_point,
  :multi_line_string,
  :multi_polygon,
  :geometry_collection
].freeze
GEOMETRY_SQL =
Arel::Nodes::Case.new(arel_table[:type])
.when('GeographicItem::MultiPolygon').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:multi_polygon].as('geometry')]))
.when('GeographicItem::Point').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:point].as('geometry')]))
.when('GeographicItem::LineString').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:line_string].as('geometry')]))
.when('GeographicItem::Polygon').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:polygon].as('geometry')]))
.when('GeographicItem::MultiLineString').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:multi_line_string].as('geometry')]))
.when('GeographicItem::MultiPoint').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:multi_point].as('geometry')]))
.when('GeographicItem::GeometryCollection').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:geometry_collection].as('geometry')]))
.freeze
GEOGRAPHY_SQL =
"CASE geographic_items.type
WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon
WHEN 'GeographicItem::Point' THEN point
WHEN 'GeographicItem::LineString' THEN line_string
WHEN 'GeographicItem::Polygon' THEN polygon
WHEN 'GeographicItem::MultiLineString' THEN multi_line_string
WHEN 'GeographicItem::MultiPoint' THEN multi_point
WHEN 'GeographicItem::GeometryCollection' THEN geometry_collection
END".freeze
ANTI_MERIDIAN =

ANTI_MERIDIAN = ‘0X0102000020E61000000200000000000000008066400000000000405640000000000080664000000000004056C0’

'LINESTRING (180 89.0, 180 -89)'.freeze

Instance Attribute Summary collapse

Attributes included from Housekeeping::Users

#by

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar

Methods included from Shared::HasPapertrail

#attribute_updated, #attribute_updater

Methods included from Housekeeping::Timestamps

#data_breakdown_for_chartkick_recent

Methods included from Housekeeping::Users

#set_created_by_id, #set_updated_by_id

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#geometryHash?

Returns An internal variable for use in super calls, holds a Hash in GeoJSON format (temporarily).

Returns:

  • (Hash, nil)

    An internal variable for use in super calls, holds a Hash in GeoJSON format (temporarily)



43
44
45
# File 'app/models/geographic_item.rb', line 43

def geometry
  @geometry
end

#line_stringRGeo::Geographic::ProjectedLineStringImpl

Returns:

  • (RGeo::Geographic::ProjectedLineStringImpl)


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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
# File 'app/models/geographic_item.rb', line 34

class GeographicItem < ApplicationRecord
 include Housekeeping::Users
 include Housekeeping::Timestamps
 include Shared::HasPapertrail
 include Shared::IsData
 include Shared::SharedAcrossProjects

 # @return [Hash, nil]
 #   An internal variable for use in super calls, holds a Hash in GeoJSON format (temporarily)
 attr_accessor :geometry

 # @return [Boolean, RGeo object]
 # @params value [Hash in GeoJSON format] ?!
 # TODO: WHY! boolean not nil, or object
 # Used to build geographic items from a shape [ of what class ] !?
 attr_accessor :shape

 # @return [Boolean]
 #   When true cached values are not built
 attr_accessor :no_cached

 DATA_TYPES = [
   :point,
   :line_string,
   :polygon,
   :multi_point,
   :multi_line_string,
   :multi_polygon,
   :geometry_collection
 ].freeze

 GEOMETRY_SQL = Arel::Nodes::Case.new(arel_table[:type])
   .when('GeographicItem::MultiPolygon').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:multi_polygon].as('geometry')]))
   .when('GeographicItem::Point').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:point].as('geometry')]))
   .when('GeographicItem::LineString').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:line_string].as('geometry')]))
   .when('GeographicItem::Polygon').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:polygon].as('geometry')]))
   .when('GeographicItem::MultiLineString').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:multi_line_string].as('geometry')]))
   .when('GeographicItem::MultiPoint').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:multi_point].as('geometry')]))
   .when('GeographicItem::GeometryCollection').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:geometry_collection].as('geometry')]))
   .freeze

 GEOGRAPHY_SQL = "CASE geographic_items.type
   WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon
   WHEN 'GeographicItem::Point' THEN point
   WHEN 'GeographicItem::LineString' THEN line_string
   WHEN 'GeographicItem::Polygon' THEN polygon
   WHEN 'GeographicItem::MultiLineString' THEN multi_line_string
   WHEN 'GeographicItem::MultiPoint' THEN multi_point
   WHEN 'GeographicItem::GeometryCollection' THEN geometry_collection
   END".freeze

   # ANTI_MERIDIAN = '0X0102000020E61000000200000000000000008066400000000000405640000000000080664000000000004056C0'
 ANTI_MERIDIAN = 'LINESTRING (180 89.0, 180 -89)'.freeze

 has_many :cached_map_items, inverse_of: :geographic_item

 has_many :geographic_areas_geographic_items, dependent: :destroy, inverse_of: :geographic_item
 has_many :geographic_areas, through: :geographic_areas_geographic_items
 has_many :asserted_distributions, through: :geographic_areas
 has_many :geographic_area_types, through: :geographic_areas
 has_many :parent_geographic_areas, through: :geographic_areas, source: :parent

 has_many :georeferences, inverse_of: :geographic_item
 has_many :georeferences_through_error_geographic_item,
   class_name: 'Georeference', foreign_key: :error_geographic_item_id, inverse_of: :error_geographic_item
 has_many :collecting_events_through_georeferences, through: :georeferences, source: :collecting_event
 has_many :collecting_events_through_georeference_error_geographic_item,
   through: :georeferences_through_error_geographic_item, source: :collecting_event

   # TODO: THIS IS NOT GOOD
 before_validation :set_type_if_geography_present

 validate :some_data_is_provided
 validates :type, presence: true

 scope :include_collecting_event, -> { includes(:collecting_events_through_georeferences) }
 scope :geo_with_collecting_event, -> { joins(:collecting_events_through_georeferences) }
 scope :err_with_collecting_event, -> { joins(:georeferences_through_error_geographic_item) }

 after_save :set_cached, unless: Proc.new {|n| n.no_cached || errors.any? }

 class << self

   def aliased_geographic_sql(name = 'a')
     "CASE #{name}.type \
        WHEN 'GeographicItem::MultiPolygon' THEN #{name}.multi_polygon \
        WHEN 'GeographicItem::Point' THEN #{name}.point \
        WHEN 'GeographicItem::LineString' THEN #{name}.line_string \
        WHEN 'GeographicItem::Polygon' THEN #{name}.polygon \
        WHEN 'GeographicItem::MultiLineString' THEN #{name}.multi_line_string \
        WHEN 'GeographicItem::MultiPoint' THEN #{name}.multi_point \
        WHEN 'GeographicItem::GeometryCollection' THEN #{name}.geometry_collection \
        END"
   end

   def st_union(geographic_item_scope)
     GeographicItem.select("ST_Union(#{GeographicItem::GEOMETRY_SQL.to_sql}) as collection")
       .where(id: geographic_item_scope.pluck(:id))
   end

   def st_collect(geographic_item_scope)
     GeographicItem.select("ST_Collect(#{GeographicItem::GEOMETRY_SQL.to_sql}) as collection")
       .where(id: geographic_item_scope.pluck(:id))
   end

   # @return [GeographicItem::ActiveRecord_Relation]
   # @params [Array] array of geographic area ids
   def default_by_geographic_area_ids(geographic_area_ids = [])
     GeographicItem.
       joins(:geographic_areas_geographic_items).
       merge(::GeographicAreasGeographicItem.default_geographic_item_data).
       where(geographic_areas_geographic_items: {geographic_area_id: geographic_area_ids})
   end

   # @param [String] wkt
   # @return [Boolean]
   #   whether or not the wtk intersects with the anti-meridian
   #   !! StrongParams security considerations
   def crosses_anti_meridian?(wkt)
     GeographicItem.find_by_sql(
       ['SELECT ST_Intersects(ST_GeogFromText(?), ST_GeogFromText(?)) as r;', wkt, ANTI_MERIDIAN]
     ).first.r
   end

   # @param [Integer] ids
   # @return [Boolean]
   #   whether or not any GeographicItem passed intersects the anti-meridian
   #   !! StrongParams security considerations
   #   This is our first line of defense against queries that define multiple shapes, one or
   #   more of which crosses the anti-meridian.  In this case the current TW strategy within the
   #   UI is to abandon the search, and prompt the user to refactor the query.
   def crosses_anti_meridian_by_id?(*ids)
     q1 = ["SELECT ST_Intersects((SELECT single_geometry FROM (#{GeographicItem.single_geometry_sql(*ids)}) as " \
           'left_intersect), ST_GeogFromText(?)) as r;', ANTI_MERIDIAN]
     _q2 = ActiveRecord::Base.send(:sanitize_sql_array, ['SELECT ST_Intersects((SELECT single_geometry FROM (?) as ' \
                                                         'left_intersect), ST_GeogFromText(?)) as r;', GeographicItem.single_geometry_sql(*ids), ANTI_MERIDIAN])
     GeographicItem.find_by_sql(q1).first.r
   end

   #
   # SQL fragments
   #

   # @param [Integer, String]
   # @return [String]
   #   a SQL select statement that returns the *geometry* for the geographic_item with the specified id
   def select_geometry_sql(geographic_item_id)
     "SELECT #{GeographicItem::GEOMETRY_SQL.to_sql} from geographic_items where geographic_items.id = #{geographic_item_id}"
   end

   # @param [Integer, String]
   # @return [String]
   #   a SQL select statement that returns the geography for the geographic_item with the specified id
   def select_geography_sql(geographic_item_id)
     ActiveRecord::Base.send(:sanitize_sql_for_conditions, [
       "SELECT #{GeographicItem::GEOMETRY_SQL.to_sql} from geographic_items where geographic_items.id = ?",
       geographic_item_id])
   end

   # @param [Symbol] choice
   # @return [String]
   #   a fragment returning either latitude or longitude columns
   def lat_long_sql(choice)
     return nil unless [:latitude, :longitude].include?(choice)
     f = "'D.DDDDDD'" # TODO: probably a constant somewhere
     v = (choice == :latitude ? 1 : 2)
     "CASE type
     WHEN 'GeographicItem::GeometryCollection' THEN split_part(ST_AsLatLonText(ST_Centroid" \
   "(geometry_collection::geometry), #{f}), ' ', #{v})
     WHEN 'GeographicItem::LineString' THEN split_part(ST_AsLatLonText(ST_Centroid(line_string::geometry), " \
   "#{f}), ' ', #{v})
     WHEN 'GeographicItem::MultiPolygon' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(multi_polygon::geometry), #{f}), ' ', #{v})
     WHEN 'GeographicItem::Point' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(point::geometry), #{f}), ' ', #{v})
     WHEN 'GeographicItem::Polygon' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(polygon::geometry), #{f}), ' ', #{v})
     WHEN 'GeographicItem::MultiLineString' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(multi_line_string::geometry), #{f} ), ' ', #{v})
     WHEN 'GeographicItem::MultiPoint' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(multi_point::geometry), #{f}), ' ', #{v})
   END as #{choice}"
   end

   # @param [Integer] geographic_item_id
   # @param [Integer] distance
   # @return [String]
   def within_radius_of_item_sql(geographic_item_id, distance)
     "ST_DWithin((#{GeographicItem::GEOGRAPHY_SQL}), (#{select_geography_sql(geographic_item_id)}), #{distance})"
   end

   # TODO: 3D is overkill here
   # @param [String] wkt
   # @param [Integer] distance (meters)
   # @return [String]
   # !! This is intersecting
   def intersecting_radius_of_wkt_sql(wkt, distance)
     "ST_DWithin((#{GeographicItem::GEOGRAPHY_SQL}), ST_Transform( ST_GeomFromText('#{wkt}', " \
       "4326), 4326), #{distance})"
   end

   # @param [String] wkt
   # @param [Integer] distance (meters)
   # @return [String]
   # !! This is fully covering
   def within_radius_of_wkt_sql(wkt, distance)
     "ST_Covers( ST_Buffer(ST_SetSRID( ST_GeomFromText('#{wkt}'), 4326)::geography, #{distance}), (#{GeographicItem::GEOGRAPHY_SQL}))"
   end

   # @param [String, Integer, String]
   # @return [String]
   #   a SQL fragment for ST_Contains() function, returns
   #   all geographic items which are contained in the item supplied
   def containing_sql(target_column_name = nil, geographic_item_id = nil, source_column_name = nil)
     return 'false' if geographic_item_id.nil? || source_column_name.nil? || target_column_name.nil?
     "ST_Contains(#{target_column_name}::geometry, (#{geometry_sql(geographic_item_id, source_column_name)}))"
   end

   # @param [String, Integer, String]
   # @return [String]
   #   a SQL fragment for ST_Contains(), returns
   #   all geographic_items which contain the supplied geographic_item
   def reverse_containing_sql(target_column_name = nil, geographic_item_id = nil, source_column_name = nil)
     return 'false' if geographic_item_id.nil? || source_column_name.nil? || target_column_name.nil?
     "ST_Contains((#{geometry_sql(geographic_item_id, source_column_name)}), #{target_column_name}::geometry)"
   end

   # @param [Integer, String]
   # @return [String]
   #   a SQL fragment that represents the geometry of the geographic item specified (which has data in the
   # source_column_name, i.e. geo_object_type)
   def geometry_sql(geographic_item_id = nil, source_column_name = nil)
     return 'false' if geographic_item_id.nil? || source_column_name.nil?
     "select geom_alias_tbl.#{source_column_name}::geometry from geographic_items geom_alias_tbl " \
       "where geom_alias_tbl.id = #{geographic_item_id}"
   end

   # rubocop:disable Metrics/MethodLength
   # @param [String] column_name
   # @param [GeographicItem] geographic_item
   # @return [String] of SQL
   def is_contained_by_sql(column_name, geographic_item)
     geo_id = geographic_item.id
     geo_type = geographic_item.geo_object_type
     template = '(ST_Contains((select geographic_items.%s::geometry from geographic_items where ' \
       'geographic_items.id = %d), %s::geometry))'
     retval = []
     column_name.downcase!
     case column_name
     when 'any'
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           retval.push(template % [geo_type, geo_id, column])
         end
       }
     when 'any_poly', 'any_line'
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           if column.to_s.index(column_name.gsub('any_', ''))
             retval.push(template % [geo_type, geo_id, column])
           end
         end
       }
     else
       retval = template % [geo_type, geo_id, column_name]
     end
     retval = retval.join(' OR ') if retval.instance_of?(Array)
     retval
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   #   a select query that returns a single geometry (column name 'single_geometry' for the collection of ids
   # provided via ST_Collect)
   def st_collect_sql(*geographic_item_ids)
     geographic_item_ids.flatten!
     ActiveRecord::Base.send(:sanitize_sql_for_conditions, [
       "SELECT ST_Collect(f.the_geom) AS single_geometry
    FROM (
       SELECT (ST_DUMP(#{GeographicItem::GEOMETRY_SQL.to_sql})).geom as the_geom
       FROM geographic_items
       WHERE id in (?))
     AS f", geographic_item_ids])
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   #    returns one or more geographic items combined as a single geometry in column 'single'
   def single_geometry_sql(*geographic_item_ids)
     a = GeographicItem.st_collect_sql(geographic_item_ids)
     '(SELECT single.single_geometry FROM (' + a + ' ) AS single)'
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   #   returns a single geometry "column" (paren wrapped) as "single" for multiple geographic item ids, or the
   # geometry as 'geometry' for a single id
   def geometry_sql2(*geographic_item_ids)
     geographic_item_ids.flatten! # *ALWAYS* reduce the pile to a single level of ids
     if geographic_item_ids.count == 1
       "(#{GeographicItem.geometry_for_sql(geographic_item_ids.first)})"
     else
       GeographicItem.single_geometry_sql(geographic_item_ids)
     end
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   def containing_where_sql(*geographic_item_ids)
     "ST_CoveredBy(
   #{GeographicItem.geometry_sql2(*geographic_item_ids)},
    CASE geographic_items.type
      WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon::geometry
      WHEN 'GeographicItem::Point' THEN point::geometry
      WHEN 'GeographicItem::LineString' THEN line_string::geometry
      WHEN 'GeographicItem::Polygon' THEN polygon::geometry
      WHEN 'GeographicItem::MultiLineString' THEN multi_line_string::geometry
      WHEN 'GeographicItem::MultiPoint' THEN multi_point::geometry
   END)"
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   def containing_where_sql_geog(*geographic_item_ids)
     "ST_CoveredBy(
   #{GeographicItem.geometry_sql2(*geographic_item_ids)},
    CASE geographic_items.type
      WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon::geography
      WHEN 'GeographicItem::Point' THEN point::geography
      WHEN 'GeographicItem::LineString' THEN line_string::geography
      WHEN 'GeographicItem::Polygon' THEN polygon::geography
      WHEN 'GeographicItem::MultiLineString' THEN multi_line_string::geography
      WHEN 'GeographicItem::MultiPoint' THEN multi_point::geography
   END)"
   end

   # @param [Interger, Array of Integer] ids
   # @return [Array]
   #   If we detect that some query id has crossed the meridian, then loop through
   #   and "manually" build up a list of results.
   #   Should only be used if GeographicItem.crosses_anti_meridian_by_id? is true.
   #   Note that this does not return a Scope, so you can't chain it like contained_by?
   # TODO: test this
   def contained_by_with_antimeridian_check(*ids)
     ids.flatten! # make sure there is only one level of splat (*)
     results = []

     crossing_ids = []

     ids.each do |id|
       # push each which crosses
       crossing_ids.push(id) if GeographicItem.crosses_anti_meridian_by_id?(id)
     end

     non_crossing_ids = ids - crossing_ids
     results.push GeographicItem.contained_by(non_crossing_ids).to_a if non_crossing_ids.any?

     crossing_ids.each do |id|
       # [61666, 61661, 61659, 61654, 61639]
       q1 = ActiveRecord::Base.send(:sanitize_sql_array, ['SELECT ST_AsText((SELECT polygon FROM geographic_items ' \
                                                          'WHERE id = ?))', id])
       r = GeographicItem.where(
         # GeographicItem.contained_by_wkt_shifted_sql(GeographicItem.find(id).geo_object.to_s)
         GeographicItem.contained_by_wkt_shifted_sql(
           ApplicationRecord.connection.execute(q1).first['st_astext'])
       ).to_a
       results.push(r)
     end

     results.flatten.uniq
   end

   # @params [String] well known text
   # @return [String] the SQL fragment for the specific geometry type, shifted by longitude
   # Note: this routine is called when it is already known that the A argument crosses anti-meridian
   def contained_by_wkt_shifted_sql(wkt)
     "ST_Contains(ST_ShiftLongitude(ST_GeomFromText('#{wkt}', 4326)), (
       CASE geographic_items.type
          WHEN 'GeographicItem::MultiPolygon' THEN ST_ShiftLongitude(multi_polygon::geometry)
          WHEN 'GeographicItem::Point' THEN ST_ShiftLongitude(point::geometry)
          WHEN 'GeographicItem::LineString' THEN ST_ShiftLongitude(line_string::geometry)
          WHEN 'GeographicItem::Polygon' THEN ST_ShiftLongitude(polygon::geometry)
          WHEN 'GeographicItem::MultiLineString' THEN ST_ShiftLongitude(multi_line_string::geometry)
          WHEN 'GeographicItem::MultiPoint' THEN ST_ShiftLongitude(multi_point::geometry)
       END
       )
     )"
   end

   # TODO: Remove the hard coded 4326 reference
   # @params [String] wkt
   # @return [String] SQL fragment limiting geographics items to those in this WKT
   def contained_by_wkt_sql(wkt)
     if crosses_anti_meridian?(wkt)
       retval = contained_by_wkt_shifted_sql(wkt)
     else
       retval = "ST_Contains(ST_GeomFromText('#{wkt}', 4326), (
       CASE geographic_items.type
          WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon::geometry
          WHEN 'GeographicItem::Point' THEN point::geometry
          WHEN 'GeographicItem::LineString' THEN line_string::geometry
          WHEN 'GeographicItem::Polygon' THEN polygon::geometry
          WHEN 'GeographicItem::MultiLineString' THEN multi_line_string::geometry
          WHEN 'GeographicItem::MultiPoint' THEN multi_point::geometry
       END
       )
     )"
     end
     retval
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String] sql for contained_by via ST_ContainsProperly
   # Note: Can not use GEOMETRY_SQL because geometry_collection is not supported in ST_ContainsProperly
   # Note: !! If the target GeographicItem#id crosses the anti-meridian then you may/will get unexpected results.
   def contained_by_where_sql(*geographic_item_ids)
     "ST_Contains(
       #{GeographicItem.geometry_sql2(*geographic_item_ids)},
       CASE geographic_items.type
         WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon::geometry
         WHEN 'GeographicItem::Point' THEN point::geometry
         WHEN 'GeographicItem::LineString' THEN line_string::geometry
         WHEN 'GeographicItem::Polygon' THEN polygon::geometry
         WHEN 'GeographicItem::MultiLineString' THEN multi_line_string::geometry
         WHEN 'GeographicItem::MultiPoint' THEN multi_point::geometry
       END)"
   end

   # @param [RGeo:Point] rgeo_point
   # @return [String] sql for containing via ST_CoveredBy
   # TODO: Remove the hard coded 4326 reference
   # TODO: should this be wkt_point instead of rgeo_point?
   def containing_where_for_point_sql(rgeo_point)
     "ST_CoveredBy(
     ST_GeomFromText('#{rgeo_point}', 4326),
     #{GeographicItem::GEOMETRY_SQL.to_sql}
    )"
   end

   # @param [Interger] geographic_item_id
   # @return [String] SQL for geometries
   # example, not used
   def geometry_for_sql(geographic_item_id)
     'SELECT ' + GeographicItem::GEOMETRY_SQL.to_sql + ' AS geometry FROM geographic_items WHERE id = ' \
       "#{geographic_item_id} LIMIT 1"
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String] SQL for geometries
   # example, not used
   def geometry_for_collection_sql(*geographic_item_ids)
     'SELECT ' + GeographicItem::GEOMETRY_SQL.to_sql + ' AS geometry FROM geographic_items WHERE id IN ' \
       "( #{geographic_item_ids.join(',')} )"
   end

   #
   # Scopes
   #

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [Scope]
   #    the geographic items containing these collective geographic_item ids, not including self
   def containing(*geographic_item_ids)
     where(GeographicItem.containing_where_sql(geographic_item_ids)).not_ids(*geographic_item_ids)
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [Scope]
   #    the geographic items contained by any of these geographic_item ids, not including self
   # (works via ST_ContainsProperly)
   def contained_by(*geographic_item_ids)
     where(GeographicItem.contained_by_where_sql(geographic_item_ids))
   end

   # @param [RGeo::Point] rgeo_point
   # @return [Scope]
   #    the geographic items containing this point
   # TODO: should be containing_wkt ?
   def containing_point(rgeo_point)
     where(GeographicItem.containing_where_for_point_sql(rgeo_point))
   end

   # @return [Scope]
   #   adds an area_in_meters field, with meters
   def with_area
     select("ST_Area(#{GeographicItem::GEOGRAPHY_SQL}, false) as area_in_meters")
   end

   # return [Scope]
   #   A scope that limits the result to those GeographicItems that have a collecting event
   #   through either the geographic_item or the error_geographic_item
   #
   # A raw SQL join approach for comparison
   #
   # GeographicItem.joins('LEFT JOIN georeferences g1 ON geographic_items.id = g1.geographic_item_id').
   #   joins('LEFT JOIN georeferences g2 ON geographic_items.id = g2.error_geographic_item_id').
   #   where("(g1.geographic_item_id IS NOT NULL OR g2.error_geographic_item_id IS NOT NULL)").uniq

   # @return [Scope] GeographicItem
   # This uses an Arel table approach, this is ultimately more decomposable if we need. Of use:
   #  http://danshultz.github.io/talks/mastering_activerecord_arel  <- best
   #  https://github.com/rails/arel
   #  http://stackoverflow.com/questions/4500629/use-arel-for-a-nested-set-join-query-and-convert-to-activerecordrelation
   #  http://rdoc.info/github/rails/arel/Arel/SelectManager
   #  http://stackoverflow.com/questions/7976358/activerecord-arel-or-condition
   #
   def with_collecting_event_through_georeferences
     geographic_items = GeographicItem.arel_table
     georeferences = Georeference.arel_table
     g1 = georeferences.alias('a')
     g2 = georeferences.alias('b')

     c = geographic_items.join(g1, Arel::Nodes::OuterJoin).on(geographic_items[:id].eq(g1[:geographic_item_id]))
       .join(g2, Arel::Nodes::OuterJoin).on(geographic_items[:id].eq(g2[:error_geographic_item_id]))

     GeographicItem.joins(# turn the Arel back into scope
                          c.join_sources # translate the Arel join to a join hash(?)
                         ).where(
                           g1[:id].not_eq(nil).or(g2[:id].not_eq(nil)) # returns a Arel::Nodes::Grouping
                         ).distinct
   end

   # @return [Scope] include a 'latitude' column
   def with_latitude
     select(lat_long_sql(:latitude))
   end

   # @return [Scope] include a 'longitude' column
   def with_longitude
     select(lat_long_sql(:longitude))
   end

   # @param [String, GeographicItems]
   # @return [Scope]
   def intersecting(column_name, *geographic_items)
     if column_name.downcase == 'any'
       pieces = []
       DATA_TYPES.each { |column|
         pieces.push(GeographicItem.intersecting(column.to_s, geographic_items).to_a)
       }

       # @TODO change 'id in (?)' to some other sql construct

       GeographicItem.where(id: pieces.flatten.map(&:id))
     else
       q = geographic_items.flatten.collect { |geographic_item|
         "ST_Intersects(#{column_name}, '#{geographic_item.geo_object}'    )" # seems like we want this: http://danshultz.github.io/talks/mastering_activerecord_arel/#/15/2
       }.join(' or ')

       where(q)
     end
   end

   # @param [GeographicItem#id] geographic_item_id
   # @param [Float] distance in meters ?!?!
   # @return [ActiveRecord::Relation]
   # !! should be distance, not radius?!
   def within_radius_of_item(geographic_item_id, distance)
     where(within_radius_of_item_sql(geographic_item_id, distance))
   end

   # @param [String, GeographicItem]
   # @return [Scope]
   #   a SQL fragment for ST_DISJOINT, specifies all geographic_items that have data in column_name
   #   that are disjoint from the passed geographic_items
   def disjoint_from(column_name, *geographic_items)
     q = geographic_items.flatten.collect { |geographic_item|
       "ST_DISJOINT(#{column_name}::geometry, (#{geometry_sql(geographic_item.to_param,
                                                            geographic_item.geo_object_type)}))"
     }.join(' and ')

     where(q)
   end

   # @return [Scope]
   #   see are_contained_in_item_by_id
   # @param [String] column_name
   # @param [GeographicItem, Array] geographic_items
   def are_contained_in_item(column_name, *geographic_items)
     are_contained_in_item_by_id(column_name, geographic_items.flatten.map(&:id))
   end

   # rubocop:disable Metrics/MethodLength
   # @param [String] column_name to search
   # @param [GeographicItem] geographic_item_ids or array of geographic_item_ids to be tested.
   # @return [Scope] of GeographicItems
   #
   # If this scope is given an Array of GeographicItems as a second parameter,
   # it will return the 'OR' of each of the objects against the table.
   # SELECT COUNT(*) FROM "geographic_items"
   #        WHERE (ST_Contains(polygon::geometry, GeomFromEWKT('srid=4326;POINT (0.0 0.0 0.0)'))
   #               OR ST_Contains(polygon::geometry, GeomFromEWKT('srid=4326;POINT (-9.8 5.0 0.0)')))
   #
   def are_contained_in_item_by_id(column_name, *geographic_item_ids) # = containing
     geographic_item_ids.flatten! # in case there is a array of arrays, or multiple objects
     column_name.downcase!
     case column_name
     when 'any'
       part = []
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           part.push(GeographicItem.are_contained_in_item_by_id(column.to_s, geographic_item_ids).to_a)
         end
       }
       # TODO: change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten.map(&:id))
     when 'any_poly', 'any_line'
       part = []
       DATA_TYPES.each { |column|
         if column.to_s.index(column_name.gsub('any_', ''))
           part.push(GeographicItem.are_contained_in_item_by_id("#{column}", geographic_item_ids).to_a)
         end
       }
       # TODO: change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten.map(&:id))
     else
       q = geographic_item_ids.flatten.collect { |geographic_item_id|
         # discover the item types, and convert type to database type for 'multi_'
         b = GeographicItem.where(id: geographic_item_id)
           .pluck(:type)[0].split(':')[2].downcase.gsub('lti', 'lti_')
         # a = GeographicItem.find(geographic_item_id).geo_object_type
         GeographicItem.containing_sql(column_name, geographic_item_id, b)
       }.join(' or ')
       q = 'FALSE' if q.blank? # this will prevent the invocation of *ALL* of the GeographicItems, if there are
       # no GeographicItems in the request (see CollectingEvent.name_hash(types)).
       where(q) # .not_including(geographic_items)
     end
   end

   # rubocop:enable Metrics/MethodLength

   # @param [String] column_name
   # @param [String] geometry of WKT
   # @return [Scope]
   # a single WKT geometry is compared against column or columns (except geometry_collection) to find geographic_items
   # which are contained in the WKT
   def are_contained_in_wkt(column_name, geometry)
     column_name.downcase!
     # column_name = 'point'
     case column_name
     when 'any'
       part = []
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           part.push(GeographicItem.are_contained_in_wkt(column.to_s, geometry).pluck(:id).to_a)
         end
       }
       # TODO: change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten)
     when 'any_poly', 'any_line'
       part = []
       DATA_TYPES.each { |column|
         if column.to_s.index(column_name.gsub('any_', ''))
           part.push(GeographicItem.are_contained_in_wkt("#{column}", geometry).pluck(:id).to_a)
         end
       }
       # TODO: change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten)
     else
       # column = points, geometry = square
       q = "ST_Contains(ST_GeomFromEWKT('srid=4326;#{geometry}'), #{column_name}::geometry)"
       where(q) # .not_including(geographic_items)
     end
   end

   # rubocop:disable Metrics/MethodLength
   #    containing the items the shape of which is contained in the geographic_item[s] supplied.
   # @param column_name [String] can be any of DATA_TYPES, or 'any' to check against all types, 'any_poly' to check
   # against 'polygon' or 'multi_polygon', or 'any_line' to check against 'line_string' or 'multi_line_string'.
   #  CANNOT be 'geometry_collection'.
   # @param geographic_items [GeographicItem] Can be a single GeographicItem, or an array of GeographicItem.
   # @return [Scope]
   def is_contained_by(column_name, *geographic_items)
     column_name.downcase!
     case column_name
     when 'any'
       part = []
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           part.push(GeographicItem.is_contained_by(column.to_s, geographic_items).to_a)
         end
       }
       # @TODO change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten.map(&:id))

     when 'any_poly', 'any_line'
       part = []
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           if column.to_s.index(column_name.gsub('any_', ''))
             part.push(GeographicItem.is_contained_by(column.to_s, geographic_items).to_a)
           end
         end
       }
       # @TODO change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten.map(&:id))

     else
       q = geographic_items.flatten.collect { |geographic_item|
         GeographicItem.reverse_containing_sql(column_name, geographic_item.to_param,
                                               geographic_item.geo_object_type)
       }.join(' or ')
       where(q) # .not_including(geographic_items)
     end
   end

   # rubocop:enable Metrics/MethodLength

   # @param [String, GeographicItem]
   # @return [Scope]
   def ordered_by_shortest_distance_from(column_name, geographic_item)
     if true # check_geo_params(column_name, geographic_item)
       select_distance_with_geo_object(column_name, geographic_item)
         .where_distance_greater_than_zero(column_name, geographic_item).order('distance')
     else
       where('false')
     end
   end

   # @param [String, GeographicItem]
   # @return [Scope]
   def ordered_by_longest_distance_from(column_name, geographic_item)
     if true # check_geo_params(column_name, geographic_item)
       q = select_distance_with_geo_object(column_name, geographic_item)
         .where_distance_greater_than_zero(column_name, geographic_item)
         .order('distance desc')
       q
     else
       where('false')
     end
   end

   # @param [String] column_name
   # @param [GeographicItem] geographic_item
   # @return [String]
   def select_distance_with_geo_object(column_name, geographic_item)
     select("*, ST_Distance(#{column_name}, GeomFromEWKT('srid=4326;#{geographic_item.geo_object}')) as distance")
   end

   # @param [String, GeographicItem]
   # @return [Scope]
   def where_distance_greater_than_zero(column_name, geographic_item)
     where("#{column_name} is not null and ST_Distance(#{column_name}, " \
           "GeomFromEWKT('srid=4326;#{geographic_item.geo_object}')) > 0")
   end

   # @param [GeographicItem]
   # @return [Scope]
   def not_including(geographic_items)
     where.not(id: geographic_items)
   end

   # @return [Scope]
   #   includes an 'is_valid' attribute (True/False) for the passed geographic_item.  Uses St_IsValid.
   # @param [RGeo object] geographic_item
   def with_is_valid_geometry_column(geographic_item)
     where(id: geographic_item.id).select("ST_IsValid(ST_AsBinary(#{geographic_item.geo_object_type})) is_valid")
   end

   #
   # Other
   #

   # @param [Integer] geographic_item_id1
   # @param [Integer] geographic_item_id2
   # @return [Float]
   def distance_between(geographic_item_id1, geographic_item_id2)
     q1 = "ST_Distance(#{GeographicItem::GEOGRAPHY_SQL}, " \
       "(#{select_geography_sql(geographic_item_id2)})) as distance"
     _q2 = ActiveRecord::Base.send(
       :sanitize_sql_array, ['ST_Distance(?, (?)) as distance',
                             GeographicItem::GEOGRAPHY_SQL,
                             select_geography_sql(geographic_item_id2)])
     GeographicItem.where(id: geographic_item_id1).pluck(Arel.sql(q1)).first
   end

   # @param [RGeo::Point] point
   # @return [Hash]
   #   as per #inferred_geographic_name_hierarchy but for Rgeo point
   def point_inferred_geographic_name_hierarchy(point)
     GeographicItem.containing_point(point).order(cached_total_area: :ASC).limit(1).first&.inferred_geographic_name_hierarchy
   end

   # @param [String] type_name ('polygon', 'point', 'line' [, 'circle'])
   # @return [String] if type
   def eval_for_type(type_name)
     retval = 'GeographicItem'
     case type_name.upcase
     when 'POLYGON'
       retval += '::Polygon'
     when 'LINESTRING'
       retval += '::LineString'
     when 'POINT'
       retval += '::Point'
     when 'MULTIPOLYGON'
       retval += '::MultiPolygon'
     when 'MULTILINESTRING'
       retval += '::MultiLineString'
     when 'MULTIPOINT'
       retval += '::MultiPoint'
     else
       retval = nil
     end
     retval
   end

   # example, not used
   # @param [Integer] geographic_item_id
   # @return [RGeo::Geographic object]
   def geometry_for(geographic_item_id)
     GeographicItem.select(GeographicItem::GEOMETRY_SQL.to_sql + ' AS geometry').find(geographic_item_id)['geometry']
   end

   # example, not used
   # @param [Integer, Array] geographic_item_ids
   # @return [Scope]
   def st_multi(*geographic_item_ids)
     GeographicItem.find_by_sql(
       "SELECT ST_Multi(ST_Collect(g.the_geom)) AS singlegeom
    FROM (
       SELECT (ST_DUMP(#{GeographicItem::GEOMETRY_SQL.to_sql})).geom AS the_geom
       FROM geographic_items
       WHERE id IN (?))
     AS g;", geographic_item_ids.flatten
     )
   end
   end # class << self

     # @return [Hash]
     #    a geographic_name_classification or empty Hash
     # This is a quick approach that works only when
     # the geographic_item is linked explicitly to a GeographicArea.
     #
     # !! Note that it is not impossible for a GeographicItem to be linked
     # to > 1 GeographicArea, in that case we are assuming that all are
     # equally refined, this might not be the case in the future because
     # of how the GeographicArea gazetteer is indexed.
 def quick_geographic_name_hierarchy
   geographic_areas.order(:id).each do |ga|
     h = ga.geographic_name_classification # not quick enough !!
     return h if h.present?
   end
   return  {}
 end

     # @return [Hash]
     #   a geographic_name_classification (see GeographicArea) inferred by
     # finding the smallest area containing this GeographicItem, in the most accurate gazetteer
     # and using it to return country/state/county. See also the logic in
     # filling in missing levels in GeographicArea.
 def inferred_geographic_name_hierarchy
   if small_area = containing_geographic_areas
     .joins(:geographic_areas_geographic_items)
     .merge(GeographicAreasGeographicItem.ordered_by_data_origin)
     .ordered_by_area
     .limit(1)
     .first
     return small_area.geographic_name_classification
   else
     {}
   end
 end

 def geographic_name_hierarchy
   a = quick_geographic_name_hierarchy # quick; almost never the case, UI not setup to do this
   return a if a.present?
   inferred_geographic_name_hierarchy # slow
 end

     # @return [Scope]
     #   the Geographic Areas that contain (gis) this geographic item
 def containing_geographic_areas
   GeographicArea.joins(:geographic_items).includes(:geographic_area_type)
     .joins("JOIN (#{GeographicItem.containing(id).to_sql}) j on geographic_items.id = j.id")
 end

     # @return [Boolean]
     #   whether stored shape is ST_IsValid
 def valid_geometry?
   GeographicItem.with_is_valid_geometry_column(self).first['is_valid']
 end

     # @return [Array of latitude, longitude]
     #    the lat, lon of the first point in the GeoItem, see subclass for #st_start_point
 def start_point
   o = st_start_point
   [o.y, o.x]
 end

     # @return [Array]
     #   the lat, long, as STRINGs for the centroid of this geographic item
     #   Meh- this: https://postgis.net/docs/en/ST_MinimumBoundingRadius.html
 def center_coords
   r = GeographicItem.find_by_sql(
     "Select split_part(ST_AsLatLonText(ST_Centroid(#{GeographicItem::GEOMETRY_SQL.to_sql}), " \
     "'D.DDDDDD'), ' ', 1) latitude, split_part(ST_AsLatLonText(ST_Centroid" \
     "(#{GeographicItem::GEOMETRY_SQL.to_sql}), 'D.DDDDDD'), ' ', 2) " \
     "longitude from geographic_items where id = #{id};")[0]

   [r.latitude, r.longitude]
 end

     # @return [RGeo::Geographic::ProjectedPointImpl]
     #    representing the centroid of this geographic item
 def centroid
   # Gis::FACTORY.point(*center_coords.reverse)
   return geo_object if type == 'GeographicItem::Point'
   return Gis::FACTORY.parse_wkt(self.st_centroid)
 end

     # @param [Integer] geographic_item_id
     # @return [Double] distance in meters (slower, more accurate)
 def st_distance(geographic_item_id) # geo_object
   q1 = "ST_Distance((#{GeographicItem.select_geography_sql(id)}), " \
     "(#{GeographicItem.select_geography_sql(geographic_item_id)})) as d"
 _q2 = ActiveRecord::Base.send(:sanitize_sql_array, ['ST_Distance((?),(?)) as d',
                                                     GeographicItem.select_geography_sql(self.id),
                                                     GeographicItem.select_geography_sql(geographic_item_id)])
 deg = GeographicItem.where(id:).pluck(Arel.sql(q1)).first
 deg * Utilities::Geo::ONE_WEST
 end

     # @param [GeographicItem] geographic_item
     # @return [Double] distance in meters
     # Like st_distance but works with changed and non persisted objects
 def st_distance_to_geographic_item(geographic_item)
   unless !persisted? || changed?
     a = "(#{GeographicItem.select_geography_sql(id)})"
   else
     a = "ST_GeographyFromText('#{geo_object}')"
   end

   unless !geographic_item.persisted? || geographic_item.changed?
     b = "(#{GeographicItem.select_geography_sql(geographic_item.id)})"
   else
     b = "ST_GeographyFromText('#{geographic_item.geo_object}')"
   end

   ActiveRecord::Base.connection.select_value("SELECT ST_Distance(#{a}, #{b})")
 end

 alias_method :distance_to, :st_distance

     # @param [Integer] geographic_item_id
     # @return [Double] distance in meters (faster, less accurate)
 def st_distance_spheroid(geographic_item_id)
   q1 = "ST_DistanceSpheroid((#{GeographicItem.select_geometry_sql(id)})," \
     "(#{GeographicItem.select_geometry_sql(geographic_item_id)}),'#{Gis::SPHEROID}') as distance"
   _q2 = ActiveRecord::Base.send(:sanitize_sql_array,
                                 ['ST_DistanceSpheroid((?),(?),?) as distance',
                                  GeographicItem.select_geometry_sql(id),
                                  GeographicItem.select_geometry_sql(geographic_item_id),
                                  Gis::SPHEROID])
   # TODO: what is _q2?
   GeographicItem.where(id:).pluck(Arel.sql(q1)).first
 end

     # @return [String]
     #   a WKT POINT representing the centroid of the geographic item
 def st_centroid
   GeographicItem.where(id: to_param).pluck(Arel.sql("ST_AsEWKT(ST_Centroid(#{GeographicItem::GEOMETRY_SQL.to_sql}))")).first.gsub(/SRID=\d*;/, '')
 end

     # @return [Integer]
     #   the number of points in the geometry
 def st_npoints
   GeographicItem.where(id:).pluck(Arel.sql("ST_NPoints(#{GeographicItem::GEOMETRY_SQL.to_sql}) as npoints")).first
 end

     # @return [Symbol]
     #   the geo type (i.e. column like :point, :multipolygon).  References the first-found object,
     # according to the list of DATA_TYPES, or nil
 def geo_object_type
   if self.class.name == 'GeographicItem' # a proxy check for new records
     geo_type
   else
     self.class::SHAPE_COLUMN
   end
 end

     # !!TODO: migrate these to use native column calls

     # @return [RGeo instance, nil]
     #  the Rgeo shape (See http://rubydoc.info/github/dazuma/rgeo/RGeo/Feature)
 def geo_object
   begin
     if r = geo_object_type # rubocop:disable Lint/AssignmentInCondition
       send(r)
     else
       false
     end
   rescue RGeo::Error::InvalidGeometry
     return nil # TODO: we need to render proper error for this!
   end
 end

     # @param [geo_object]
     # @return [Boolean]
 def contains?(target_geo_object)
   return nil if target_geo_object.nil?
   self.geo_object.contains?(target_geo_object)
 end

     # @param [geo_object]
     # @return [Boolean]
 def within?(target_geo_object)
   self.geo_object.within?(target_geo_object)
 end

     # @param [geo_object]
     # @return [Boolean]
 def intersects?(target_geo_object)
   self.geo_object.intersects?(target_geo_object)
 end

     # @param [geo_object, Double]
     # @return [Boolean]
 def near(target_geo_object, distance)
   self.geo_object.unsafe_buffer(distance).contains?(target_geo_object)
 end

     # @param [geo_object, Double]
     # @return [Boolean]
 def far(target_geo_object, distance)
   !near(target_geo_object, distance)
 end

     # @return [GeoJSON hash]
     #    via Rgeo apparently necessary for GeometryCollection
 def rgeo_to_geo_json
   RGeo::GeoJSON.encode(geo_object).to_json
 end

     # @return [Hash]
     #   in GeoJSON format
     #   Computed via "raw" PostGIS (much faster). This
     #   requires the geo_object_type and id.
     #
 def to_geo_json
   JSON.parse(
     GeographicItem.connection.select_one(
       "SELECT ST_AsGeoJSON(#{geo_object_type}::geometry) a " \
       "FROM geographic_items WHERE id=#{id};")['a'])
 end

     # We don't need to serialize to/from JSON
 def to_geo_json_string
   GeographicItem.connection.select_one(
     "SELECT ST_AsGeoJSON(#{geo_object_type}::geometry) a " \
     "FROM geographic_items WHERE id=#{id};")['a']
 end

     # @return [Hash]
     #   the shape as a GeoJSON Feature with some item metadata
 def to_geo_json_feature
   @geometry ||= to_geo_json
   {'type' => 'Feature',
    'geometry' => geometry,
    'properties' => {
      'geographic_item' => {
        'id' => id}
    }
   }
 end

     # @param value [String] like:
     #   '{"type":"Feature","geometry":{"type":"Point","coordinates":[2.5,4.0]},"properties":{"color":"red"}}'
     #
     #   '{"type":"Feature","geometry":{"type":"Polygon","coordinates":"[[[-125.29394388198853, 48.584480409793],
     #      [-67.11035013198853, 45.09937589848195],[-80.64550638198853, 25.01924647619111],[-117.55956888198853,
     #      32.5591595028449],[-125.29394388198853, 48.584480409793]]]"},"properties":{}}'
     #
     #  '{"type":"Point","coordinates":[2.5,4.0]},"properties":{"color":"red"}}'
     #
     # @return [Boolean, RGeo object]
 def shape=(value)

   if value.present?
     geom = RGeo::GeoJSON.decode(value, json_parser: :json, geo_factory: Gis::FACTORY)
     this_type = nil

     if geom.respond_to?(:geometry_type)
       this_type = geom.geometry_type.to_s
     elsif geom.respond_to?(:geometry)
       this_type = geom.geometry.geometry_type.to_s
     else
     end

     self.type = GeographicItem.eval_for_type(this_type) unless geom.nil?

     if type.blank?
       errors.add(:base, 'type is not set from shape')
       return
     end
     # raise('GeographicItem.type not set.') if type.blank?

     object = nil

     s = geom.respond_to?(:geometry) ? geom.geometry.to_s : geom.to_s

     begin
       object = Gis::FACTORY.parse_wkt(s)
     rescue RGeo::Error::InvalidGeometry
       errors.add(:self, 'Shape value is an Invalid Geometry')
     end

     write_attribute(this_type.underscore.to_sym, object)
     geom
   end
 end

     # @return [String]
 def to_wkt
   #  10k  #<Benchmark::Tms:0x00007fb0dfd30fd0 @label="", @real=25.237487000005785, @cstime=0.0, @cutime=0.0, @stime=1.1704609999999995, @utime=5.507929999999988, @total=6.678390999999987>
   #  GeographicItem.select("ST_AsText( #{GeographicItem::GEOMETRY_SQL.to_sql}) wkt").where(id: id).first.wkt

   # 10k <Benchmark::Tms:0x00007fb0e02f7540 @label="", @real=21.619827999995323, @cstime=0.0, @cutime=0.0, @stime=0.8850890000000007, @utime=3.2958549999999605, @total=4.180943999999961>
   if a =  ApplicationRecord.connection.execute( "SELECT ST_AsText( #{GeographicItem::GEOMETRY_SQL.to_sql} ) wkt from geographic_items where geographic_items.id = #{id}").first
     return a['wkt']
   else
     return nil
   end
 end

     # return float, in meters, calculated, not from cached
 def area
   GeographicItem.where(id:).select("ST_Area(#{GeographicItem::GEOGRAPHY_SQL}, false) as area_in_meters").first['area_in_meters']
 end

     # @return [Float, false]
     #    the value in square meters of the interesecting area of this and another GeographicItem
 def intersecting_area(geographic_item_id)
   a = GeographicItem.aliased_geographic_sql('a')
   b = GeographicItem.aliased_geographic_sql('b')

   c = GeographicItem.connection.execute("SELECT ST_Area(ST_Intersection(#{a}, #{b})) as intersecting_area
     FROM geographic_items a, geographic_items b
     WHERE a.id = #{id} AND b.id = #{geographic_item_id};"
                                        ).first
                                        c && c['intersecting_area'].to_f
 end

     # TODO: This is bad, while internal use of ONE_WEST_MEAN is consistent it is in-accurate given the vast differences of radius vs. lat/long position.
     # When we strike the error-polygon from radius we should remove this
     #
     # Use case is returning the radius from a circle we calculated via buffer for error-polygon creation.
 def radius
   r = ApplicationRecord.connection.execute( "SELECT ST_MinimumBoundingRadius( ST_Transform(  #{GeographicItem::GEOMETRY_SQL.to_sql}, 4326 )  ) AS radius from geographic_items where geographic_items.id = #{id}").first['radius'].split(',').last.chop.to_f
   r = (r * Utilities::Geo::ONE_WEST_MEAN).to_i
 end

     private

 def set_cached
   update_column(:cached_total_area, area)
 end

     protected

     # @return [Symbol]
     #   returns the attribute (column name) containing data
     #   nearly all methods should use #geo_object_type, not geo_type
 def geo_type
   DATA_TYPES.each { |item|
     return item if send(item)
   }
   nil
 end

     # @return [Boolean, String] false if already set, or type to which it was set
 def set_type_if_geography_present
   if type.blank?
     column = geo_type
     self.type = "GeographicItem::#{column.to_s.camelize}" if column
   end
 end

     # @param [RGeo::Point] point
     # @return [Array] of a point
     #   Longitude |, Latitude -
 def point_to_a(point)
   data = []
   data.push(point.x, point.y)
   data
 end

     # @param [RGeo::Point] point
     # @return [Hash] of a point
 def point_to_hash(point)
   {points: [point_to_a(point)]}
 end

     # @param [RGeo::MultiPoint] multi_point
     # @return [Array] of points
 def multi_point_to_a(multi_point)
   data = []
   multi_point.each { |point|
     data.push([point.x, point.y])
   }
   data
 end

     # @return [Hash] of points
 def multi_point_to_hash(_multi_point)
   # when we encounter a multi_point type, we only stick the points into the array, NOT it's identity as a group
   {points: multi_point_to_a(multi_point)}
 end

     # @param [Reo::LineString] line_string
     # @return [Array] of points in the line
 def line_string_to_a(line_string)
   data = []
   line_string.points.each { |point|
     data.push([point.x, point.y])
   }
   data
 end

     # @param [Reo::LineString] line_string
     # @return [Hash] of points in the line
 def line_string_to_hash(line_string)
   {lines: [line_string_to_a(line_string)]}
 end

     # @param [RGeo::Polygon] polygon
     # @return [Array] of points in the polygon (exterior_ring ONLY)
 def polygon_to_a(polygon)
   # TODO: handle other parts of the polygon; i.e., the interior_rings (if they exist)
   data = []
   polygon.exterior_ring.points.each { |point|
     data.push([point.x, point.y])
   }
   data
 end

     # @param [RGeo::Polygon] polygon
     # @return [Hash] of points in the polygon (exterior_ring ONLY)
 def polygon_to_hash(polygon)
   {polygons: [polygon_to_a(polygon)]}
 end

     # @return [Array] of line_strings as arrays of points
     # @param [RGeo::MultiLineString] multi_line_string
 def multi_line_string_to_a(multi_line_string)
   data = []
   multi_line_string.each { |line_string|
     line_data = []
     line_string.points.each { |point|
       line_data.push([point.x, point.y])
     }
     data.push(line_data)
   }
   data
 end

     # @return [Hash] of line_strings as hashes of points
 def multi_line_string_to_hash(_multi_line_string)
   {lines: to_a}
 end

     # @param [RGeo::MultiPolygon] multi_polygon
     # @return [Array] of arrays of points in the polygons (exterior_ring ONLY)
 def multi_polygon_to_a(multi_polygon)
   data = []
   multi_polygon.each { |polygon|
     polygon_data = []
     polygon.exterior_ring.points.each { |point|
       polygon_data.push([point.x, point.y])
     }
     data.push(polygon_data)
   }
   data
 end

     # @return [Hash] of hashes of points in the polygons (exterior_ring ONLY)
 def multi_polygon_to_hash(_multi_polygon)
   {polygons: to_a}
 end

     # @return [Boolean] iff there is one and only one shape column set
 def some_data_is_provided
   data = []

   DATA_TYPES.each do |item|
     data.push(item) if send(item).present?
   end

   case data.count
   when 0
     errors.add(:base, 'No shape provided or provided shape is invalid')
   when 1
     return true
   else
     data.each do |object|
       errors.add(object, 'More than one shape type provided')
     end
   end
   true
 end
end

#multi_line_stringRGeo::Geographic::ProjectedMultiLineStringImpl

Returns:

  • (RGeo::Geographic::ProjectedMultiLineStringImpl)


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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
# File 'app/models/geographic_item.rb', line 34

class GeographicItem < ApplicationRecord
 include Housekeeping::Users
 include Housekeeping::Timestamps
 include Shared::HasPapertrail
 include Shared::IsData
 include Shared::SharedAcrossProjects

 # @return [Hash, nil]
 #   An internal variable for use in super calls, holds a Hash in GeoJSON format (temporarily)
 attr_accessor :geometry

 # @return [Boolean, RGeo object]
 # @params value [Hash in GeoJSON format] ?!
 # TODO: WHY! boolean not nil, or object
 # Used to build geographic items from a shape [ of what class ] !?
 attr_accessor :shape

 # @return [Boolean]
 #   When true cached values are not built
 attr_accessor :no_cached

 DATA_TYPES = [
   :point,
   :line_string,
   :polygon,
   :multi_point,
   :multi_line_string,
   :multi_polygon,
   :geometry_collection
 ].freeze

 GEOMETRY_SQL = Arel::Nodes::Case.new(arel_table[:type])
   .when('GeographicItem::MultiPolygon').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:multi_polygon].as('geometry')]))
   .when('GeographicItem::Point').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:point].as('geometry')]))
   .when('GeographicItem::LineString').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:line_string].as('geometry')]))
   .when('GeographicItem::Polygon').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:polygon].as('geometry')]))
   .when('GeographicItem::MultiLineString').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:multi_line_string].as('geometry')]))
   .when('GeographicItem::MultiPoint').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:multi_point].as('geometry')]))
   .when('GeographicItem::GeometryCollection').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:geometry_collection].as('geometry')]))
   .freeze

 GEOGRAPHY_SQL = "CASE geographic_items.type
   WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon
   WHEN 'GeographicItem::Point' THEN point
   WHEN 'GeographicItem::LineString' THEN line_string
   WHEN 'GeographicItem::Polygon' THEN polygon
   WHEN 'GeographicItem::MultiLineString' THEN multi_line_string
   WHEN 'GeographicItem::MultiPoint' THEN multi_point
   WHEN 'GeographicItem::GeometryCollection' THEN geometry_collection
   END".freeze

   # ANTI_MERIDIAN = '0X0102000020E61000000200000000000000008066400000000000405640000000000080664000000000004056C0'
 ANTI_MERIDIAN = 'LINESTRING (180 89.0, 180 -89)'.freeze

 has_many :cached_map_items, inverse_of: :geographic_item

 has_many :geographic_areas_geographic_items, dependent: :destroy, inverse_of: :geographic_item
 has_many :geographic_areas, through: :geographic_areas_geographic_items
 has_many :asserted_distributions, through: :geographic_areas
 has_many :geographic_area_types, through: :geographic_areas
 has_many :parent_geographic_areas, through: :geographic_areas, source: :parent

 has_many :georeferences, inverse_of: :geographic_item
 has_many :georeferences_through_error_geographic_item,
   class_name: 'Georeference', foreign_key: :error_geographic_item_id, inverse_of: :error_geographic_item
 has_many :collecting_events_through_georeferences, through: :georeferences, source: :collecting_event
 has_many :collecting_events_through_georeference_error_geographic_item,
   through: :georeferences_through_error_geographic_item, source: :collecting_event

   # TODO: THIS IS NOT GOOD
 before_validation :set_type_if_geography_present

 validate :some_data_is_provided
 validates :type, presence: true

 scope :include_collecting_event, -> { includes(:collecting_events_through_georeferences) }
 scope :geo_with_collecting_event, -> { joins(:collecting_events_through_georeferences) }
 scope :err_with_collecting_event, -> { joins(:georeferences_through_error_geographic_item) }

 after_save :set_cached, unless: Proc.new {|n| n.no_cached || errors.any? }

 class << self

   def aliased_geographic_sql(name = 'a')
     "CASE #{name}.type \
        WHEN 'GeographicItem::MultiPolygon' THEN #{name}.multi_polygon \
        WHEN 'GeographicItem::Point' THEN #{name}.point \
        WHEN 'GeographicItem::LineString' THEN #{name}.line_string \
        WHEN 'GeographicItem::Polygon' THEN #{name}.polygon \
        WHEN 'GeographicItem::MultiLineString' THEN #{name}.multi_line_string \
        WHEN 'GeographicItem::MultiPoint' THEN #{name}.multi_point \
        WHEN 'GeographicItem::GeometryCollection' THEN #{name}.geometry_collection \
        END"
   end

   def st_union(geographic_item_scope)
     GeographicItem.select("ST_Union(#{GeographicItem::GEOMETRY_SQL.to_sql}) as collection")
       .where(id: geographic_item_scope.pluck(:id))
   end

   def st_collect(geographic_item_scope)
     GeographicItem.select("ST_Collect(#{GeographicItem::GEOMETRY_SQL.to_sql}) as collection")
       .where(id: geographic_item_scope.pluck(:id))
   end

   # @return [GeographicItem::ActiveRecord_Relation]
   # @params [Array] array of geographic area ids
   def default_by_geographic_area_ids(geographic_area_ids = [])
     GeographicItem.
       joins(:geographic_areas_geographic_items).
       merge(::GeographicAreasGeographicItem.default_geographic_item_data).
       where(geographic_areas_geographic_items: {geographic_area_id: geographic_area_ids})
   end

   # @param [String] wkt
   # @return [Boolean]
   #   whether or not the wtk intersects with the anti-meridian
   #   !! StrongParams security considerations
   def crosses_anti_meridian?(wkt)
     GeographicItem.find_by_sql(
       ['SELECT ST_Intersects(ST_GeogFromText(?), ST_GeogFromText(?)) as r;', wkt, ANTI_MERIDIAN]
     ).first.r
   end

   # @param [Integer] ids
   # @return [Boolean]
   #   whether or not any GeographicItem passed intersects the anti-meridian
   #   !! StrongParams security considerations
   #   This is our first line of defense against queries that define multiple shapes, one or
   #   more of which crosses the anti-meridian.  In this case the current TW strategy within the
   #   UI is to abandon the search, and prompt the user to refactor the query.
   def crosses_anti_meridian_by_id?(*ids)
     q1 = ["SELECT ST_Intersects((SELECT single_geometry FROM (#{GeographicItem.single_geometry_sql(*ids)}) as " \
           'left_intersect), ST_GeogFromText(?)) as r;', ANTI_MERIDIAN]
     _q2 = ActiveRecord::Base.send(:sanitize_sql_array, ['SELECT ST_Intersects((SELECT single_geometry FROM (?) as ' \
                                                         'left_intersect), ST_GeogFromText(?)) as r;', GeographicItem.single_geometry_sql(*ids), ANTI_MERIDIAN])
     GeographicItem.find_by_sql(q1).first.r
   end

   #
   # SQL fragments
   #

   # @param [Integer, String]
   # @return [String]
   #   a SQL select statement that returns the *geometry* for the geographic_item with the specified id
   def select_geometry_sql(geographic_item_id)
     "SELECT #{GeographicItem::GEOMETRY_SQL.to_sql} from geographic_items where geographic_items.id = #{geographic_item_id}"
   end

   # @param [Integer, String]
   # @return [String]
   #   a SQL select statement that returns the geography for the geographic_item with the specified id
   def select_geography_sql(geographic_item_id)
     ActiveRecord::Base.send(:sanitize_sql_for_conditions, [
       "SELECT #{GeographicItem::GEOMETRY_SQL.to_sql} from geographic_items where geographic_items.id = ?",
       geographic_item_id])
   end

   # @param [Symbol] choice
   # @return [String]
   #   a fragment returning either latitude or longitude columns
   def lat_long_sql(choice)
     return nil unless [:latitude, :longitude].include?(choice)
     f = "'D.DDDDDD'" # TODO: probably a constant somewhere
     v = (choice == :latitude ? 1 : 2)
     "CASE type
     WHEN 'GeographicItem::GeometryCollection' THEN split_part(ST_AsLatLonText(ST_Centroid" \
   "(geometry_collection::geometry), #{f}), ' ', #{v})
     WHEN 'GeographicItem::LineString' THEN split_part(ST_AsLatLonText(ST_Centroid(line_string::geometry), " \
   "#{f}), ' ', #{v})
     WHEN 'GeographicItem::MultiPolygon' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(multi_polygon::geometry), #{f}), ' ', #{v})
     WHEN 'GeographicItem::Point' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(point::geometry), #{f}), ' ', #{v})
     WHEN 'GeographicItem::Polygon' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(polygon::geometry), #{f}), ' ', #{v})
     WHEN 'GeographicItem::MultiLineString' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(multi_line_string::geometry), #{f} ), ' ', #{v})
     WHEN 'GeographicItem::MultiPoint' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(multi_point::geometry), #{f}), ' ', #{v})
   END as #{choice}"
   end

   # @param [Integer] geographic_item_id
   # @param [Integer] distance
   # @return [String]
   def within_radius_of_item_sql(geographic_item_id, distance)
     "ST_DWithin((#{GeographicItem::GEOGRAPHY_SQL}), (#{select_geography_sql(geographic_item_id)}), #{distance})"
   end

   # TODO: 3D is overkill here
   # @param [String] wkt
   # @param [Integer] distance (meters)
   # @return [String]
   # !! This is intersecting
   def intersecting_radius_of_wkt_sql(wkt, distance)
     "ST_DWithin((#{GeographicItem::GEOGRAPHY_SQL}), ST_Transform( ST_GeomFromText('#{wkt}', " \
       "4326), 4326), #{distance})"
   end

   # @param [String] wkt
   # @param [Integer] distance (meters)
   # @return [String]
   # !! This is fully covering
   def within_radius_of_wkt_sql(wkt, distance)
     "ST_Covers( ST_Buffer(ST_SetSRID( ST_GeomFromText('#{wkt}'), 4326)::geography, #{distance}), (#{GeographicItem::GEOGRAPHY_SQL}))"
   end

   # @param [String, Integer, String]
   # @return [String]
   #   a SQL fragment for ST_Contains() function, returns
   #   all geographic items which are contained in the item supplied
   def containing_sql(target_column_name = nil, geographic_item_id = nil, source_column_name = nil)
     return 'false' if geographic_item_id.nil? || source_column_name.nil? || target_column_name.nil?
     "ST_Contains(#{target_column_name}::geometry, (#{geometry_sql(geographic_item_id, source_column_name)}))"
   end

   # @param [String, Integer, String]
   # @return [String]
   #   a SQL fragment for ST_Contains(), returns
   #   all geographic_items which contain the supplied geographic_item
   def reverse_containing_sql(target_column_name = nil, geographic_item_id = nil, source_column_name = nil)
     return 'false' if geographic_item_id.nil? || source_column_name.nil? || target_column_name.nil?
     "ST_Contains((#{geometry_sql(geographic_item_id, source_column_name)}), #{target_column_name}::geometry)"
   end

   # @param [Integer, String]
   # @return [String]
   #   a SQL fragment that represents the geometry of the geographic item specified (which has data in the
   # source_column_name, i.e. geo_object_type)
   def geometry_sql(geographic_item_id = nil, source_column_name = nil)
     return 'false' if geographic_item_id.nil? || source_column_name.nil?
     "select geom_alias_tbl.#{source_column_name}::geometry from geographic_items geom_alias_tbl " \
       "where geom_alias_tbl.id = #{geographic_item_id}"
   end

   # rubocop:disable Metrics/MethodLength
   # @param [String] column_name
   # @param [GeographicItem] geographic_item
   # @return [String] of SQL
   def is_contained_by_sql(column_name, geographic_item)
     geo_id = geographic_item.id
     geo_type = geographic_item.geo_object_type
     template = '(ST_Contains((select geographic_items.%s::geometry from geographic_items where ' \
       'geographic_items.id = %d), %s::geometry))'
     retval = []
     column_name.downcase!
     case column_name
     when 'any'
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           retval.push(template % [geo_type, geo_id, column])
         end
       }
     when 'any_poly', 'any_line'
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           if column.to_s.index(column_name.gsub('any_', ''))
             retval.push(template % [geo_type, geo_id, column])
           end
         end
       }
     else
       retval = template % [geo_type, geo_id, column_name]
     end
     retval = retval.join(' OR ') if retval.instance_of?(Array)
     retval
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   #   a select query that returns a single geometry (column name 'single_geometry' for the collection of ids
   # provided via ST_Collect)
   def st_collect_sql(*geographic_item_ids)
     geographic_item_ids.flatten!
     ActiveRecord::Base.send(:sanitize_sql_for_conditions, [
       "SELECT ST_Collect(f.the_geom) AS single_geometry
    FROM (
       SELECT (ST_DUMP(#{GeographicItem::GEOMETRY_SQL.to_sql})).geom as the_geom
       FROM geographic_items
       WHERE id in (?))
     AS f", geographic_item_ids])
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   #    returns one or more geographic items combined as a single geometry in column 'single'
   def single_geometry_sql(*geographic_item_ids)
     a = GeographicItem.st_collect_sql(geographic_item_ids)
     '(SELECT single.single_geometry FROM (' + a + ' ) AS single)'
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   #   returns a single geometry "column" (paren wrapped) as "single" for multiple geographic item ids, or the
   # geometry as 'geometry' for a single id
   def geometry_sql2(*geographic_item_ids)
     geographic_item_ids.flatten! # *ALWAYS* reduce the pile to a single level of ids
     if geographic_item_ids.count == 1
       "(#{GeographicItem.geometry_for_sql(geographic_item_ids.first)})"
     else
       GeographicItem.single_geometry_sql(geographic_item_ids)
     end
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   def containing_where_sql(*geographic_item_ids)
     "ST_CoveredBy(
   #{GeographicItem.geometry_sql2(*geographic_item_ids)},
    CASE geographic_items.type
      WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon::geometry
      WHEN 'GeographicItem::Point' THEN point::geometry
      WHEN 'GeographicItem::LineString' THEN line_string::geometry
      WHEN 'GeographicItem::Polygon' THEN polygon::geometry
      WHEN 'GeographicItem::MultiLineString' THEN multi_line_string::geometry
      WHEN 'GeographicItem::MultiPoint' THEN multi_point::geometry
   END)"
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   def containing_where_sql_geog(*geographic_item_ids)
     "ST_CoveredBy(
   #{GeographicItem.geometry_sql2(*geographic_item_ids)},
    CASE geographic_items.type
      WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon::geography
      WHEN 'GeographicItem::Point' THEN point::geography
      WHEN 'GeographicItem::LineString' THEN line_string::geography
      WHEN 'GeographicItem::Polygon' THEN polygon::geography
      WHEN 'GeographicItem::MultiLineString' THEN multi_line_string::geography
      WHEN 'GeographicItem::MultiPoint' THEN multi_point::geography
   END)"
   end

   # @param [Interger, Array of Integer] ids
   # @return [Array]
   #   If we detect that some query id has crossed the meridian, then loop through
   #   and "manually" build up a list of results.
   #   Should only be used if GeographicItem.crosses_anti_meridian_by_id? is true.
   #   Note that this does not return a Scope, so you can't chain it like contained_by?
   # TODO: test this
   def contained_by_with_antimeridian_check(*ids)
     ids.flatten! # make sure there is only one level of splat (*)
     results = []

     crossing_ids = []

     ids.each do |id|
       # push each which crosses
       crossing_ids.push(id) if GeographicItem.crosses_anti_meridian_by_id?(id)
     end

     non_crossing_ids = ids - crossing_ids
     results.push GeographicItem.contained_by(non_crossing_ids).to_a if non_crossing_ids.any?

     crossing_ids.each do |id|
       # [61666, 61661, 61659, 61654, 61639]
       q1 = ActiveRecord::Base.send(:sanitize_sql_array, ['SELECT ST_AsText((SELECT polygon FROM geographic_items ' \
                                                          'WHERE id = ?))', id])
       r = GeographicItem.where(
         # GeographicItem.contained_by_wkt_shifted_sql(GeographicItem.find(id).geo_object.to_s)
         GeographicItem.contained_by_wkt_shifted_sql(
           ApplicationRecord.connection.execute(q1).first['st_astext'])
       ).to_a
       results.push(r)
     end

     results.flatten.uniq
   end

   # @params [String] well known text
   # @return [String] the SQL fragment for the specific geometry type, shifted by longitude
   # Note: this routine is called when it is already known that the A argument crosses anti-meridian
   def contained_by_wkt_shifted_sql(wkt)
     "ST_Contains(ST_ShiftLongitude(ST_GeomFromText('#{wkt}', 4326)), (
       CASE geographic_items.type
          WHEN 'GeographicItem::MultiPolygon' THEN ST_ShiftLongitude(multi_polygon::geometry)
          WHEN 'GeographicItem::Point' THEN ST_ShiftLongitude(point::geometry)
          WHEN 'GeographicItem::LineString' THEN ST_ShiftLongitude(line_string::geometry)
          WHEN 'GeographicItem::Polygon' THEN ST_ShiftLongitude(polygon::geometry)
          WHEN 'GeographicItem::MultiLineString' THEN ST_ShiftLongitude(multi_line_string::geometry)
          WHEN 'GeographicItem::MultiPoint' THEN ST_ShiftLongitude(multi_point::geometry)
       END
       )
     )"
   end

   # TODO: Remove the hard coded 4326 reference
   # @params [String] wkt
   # @return [String] SQL fragment limiting geographics items to those in this WKT
   def contained_by_wkt_sql(wkt)
     if crosses_anti_meridian?(wkt)
       retval = contained_by_wkt_shifted_sql(wkt)
     else
       retval = "ST_Contains(ST_GeomFromText('#{wkt}', 4326), (
       CASE geographic_items.type
          WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon::geometry
          WHEN 'GeographicItem::Point' THEN point::geometry
          WHEN 'GeographicItem::LineString' THEN line_string::geometry
          WHEN 'GeographicItem::Polygon' THEN polygon::geometry
          WHEN 'GeographicItem::MultiLineString' THEN multi_line_string::geometry
          WHEN 'GeographicItem::MultiPoint' THEN multi_point::geometry
       END
       )
     )"
     end
     retval
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String] sql for contained_by via ST_ContainsProperly
   # Note: Can not use GEOMETRY_SQL because geometry_collection is not supported in ST_ContainsProperly
   # Note: !! If the target GeographicItem#id crosses the anti-meridian then you may/will get unexpected results.
   def contained_by_where_sql(*geographic_item_ids)
     "ST_Contains(
       #{GeographicItem.geometry_sql2(*geographic_item_ids)},
       CASE geographic_items.type
         WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon::geometry
         WHEN 'GeographicItem::Point' THEN point::geometry
         WHEN 'GeographicItem::LineString' THEN line_string::geometry
         WHEN 'GeographicItem::Polygon' THEN polygon::geometry
         WHEN 'GeographicItem::MultiLineString' THEN multi_line_string::geometry
         WHEN 'GeographicItem::MultiPoint' THEN multi_point::geometry
       END)"
   end

   # @param [RGeo:Point] rgeo_point
   # @return [String] sql for containing via ST_CoveredBy
   # TODO: Remove the hard coded 4326 reference
   # TODO: should this be wkt_point instead of rgeo_point?
   def containing_where_for_point_sql(rgeo_point)
     "ST_CoveredBy(
     ST_GeomFromText('#{rgeo_point}', 4326),
     #{GeographicItem::GEOMETRY_SQL.to_sql}
    )"
   end

   # @param [Interger] geographic_item_id
   # @return [String] SQL for geometries
   # example, not used
   def geometry_for_sql(geographic_item_id)
     'SELECT ' + GeographicItem::GEOMETRY_SQL.to_sql + ' AS geometry FROM geographic_items WHERE id = ' \
       "#{geographic_item_id} LIMIT 1"
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String] SQL for geometries
   # example, not used
   def geometry_for_collection_sql(*geographic_item_ids)
     'SELECT ' + GeographicItem::GEOMETRY_SQL.to_sql + ' AS geometry FROM geographic_items WHERE id IN ' \
       "( #{geographic_item_ids.join(',')} )"
   end

   #
   # Scopes
   #

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [Scope]
   #    the geographic items containing these collective geographic_item ids, not including self
   def containing(*geographic_item_ids)
     where(GeographicItem.containing_where_sql(geographic_item_ids)).not_ids(*geographic_item_ids)
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [Scope]
   #    the geographic items contained by any of these geographic_item ids, not including self
   # (works via ST_ContainsProperly)
   def contained_by(*geographic_item_ids)
     where(GeographicItem.contained_by_where_sql(geographic_item_ids))
   end

   # @param [RGeo::Point] rgeo_point
   # @return [Scope]
   #    the geographic items containing this point
   # TODO: should be containing_wkt ?
   def containing_point(rgeo_point)
     where(GeographicItem.containing_where_for_point_sql(rgeo_point))
   end

   # @return [Scope]
   #   adds an area_in_meters field, with meters
   def with_area
     select("ST_Area(#{GeographicItem::GEOGRAPHY_SQL}, false) as area_in_meters")
   end

   # return [Scope]
   #   A scope that limits the result to those GeographicItems that have a collecting event
   #   through either the geographic_item or the error_geographic_item
   #
   # A raw SQL join approach for comparison
   #
   # GeographicItem.joins('LEFT JOIN georeferences g1 ON geographic_items.id = g1.geographic_item_id').
   #   joins('LEFT JOIN georeferences g2 ON geographic_items.id = g2.error_geographic_item_id').
   #   where("(g1.geographic_item_id IS NOT NULL OR g2.error_geographic_item_id IS NOT NULL)").uniq

   # @return [Scope] GeographicItem
   # This uses an Arel table approach, this is ultimately more decomposable if we need. Of use:
   #  http://danshultz.github.io/talks/mastering_activerecord_arel  <- best
   #  https://github.com/rails/arel
   #  http://stackoverflow.com/questions/4500629/use-arel-for-a-nested-set-join-query-and-convert-to-activerecordrelation
   #  http://rdoc.info/github/rails/arel/Arel/SelectManager
   #  http://stackoverflow.com/questions/7976358/activerecord-arel-or-condition
   #
   def with_collecting_event_through_georeferences
     geographic_items = GeographicItem.arel_table
     georeferences = Georeference.arel_table
     g1 = georeferences.alias('a')
     g2 = georeferences.alias('b')

     c = geographic_items.join(g1, Arel::Nodes::OuterJoin).on(geographic_items[:id].eq(g1[:geographic_item_id]))
       .join(g2, Arel::Nodes::OuterJoin).on(geographic_items[:id].eq(g2[:error_geographic_item_id]))

     GeographicItem.joins(# turn the Arel back into scope
                          c.join_sources # translate the Arel join to a join hash(?)
                         ).where(
                           g1[:id].not_eq(nil).or(g2[:id].not_eq(nil)) # returns a Arel::Nodes::Grouping
                         ).distinct
   end

   # @return [Scope] include a 'latitude' column
   def with_latitude
     select(lat_long_sql(:latitude))
   end

   # @return [Scope] include a 'longitude' column
   def with_longitude
     select(lat_long_sql(:longitude))
   end

   # @param [String, GeographicItems]
   # @return [Scope]
   def intersecting(column_name, *geographic_items)
     if column_name.downcase == 'any'
       pieces = []
       DATA_TYPES.each { |column|
         pieces.push(GeographicItem.intersecting(column.to_s, geographic_items).to_a)
       }

       # @TODO change 'id in (?)' to some other sql construct

       GeographicItem.where(id: pieces.flatten.map(&:id))
     else
       q = geographic_items.flatten.collect { |geographic_item|
         "ST_Intersects(#{column_name}, '#{geographic_item.geo_object}'    )" # seems like we want this: http://danshultz.github.io/talks/mastering_activerecord_arel/#/15/2
       }.join(' or ')

       where(q)
     end
   end

   # @param [GeographicItem#id] geographic_item_id
   # @param [Float] distance in meters ?!?!
   # @return [ActiveRecord::Relation]
   # !! should be distance, not radius?!
   def within_radius_of_item(geographic_item_id, distance)
     where(within_radius_of_item_sql(geographic_item_id, distance))
   end

   # @param [String, GeographicItem]
   # @return [Scope]
   #   a SQL fragment for ST_DISJOINT, specifies all geographic_items that have data in column_name
   #   that are disjoint from the passed geographic_items
   def disjoint_from(column_name, *geographic_items)
     q = geographic_items.flatten.collect { |geographic_item|
       "ST_DISJOINT(#{column_name}::geometry, (#{geometry_sql(geographic_item.to_param,
                                                            geographic_item.geo_object_type)}))"
     }.join(' and ')

     where(q)
   end

   # @return [Scope]
   #   see are_contained_in_item_by_id
   # @param [String] column_name
   # @param [GeographicItem, Array] geographic_items
   def are_contained_in_item(column_name, *geographic_items)
     are_contained_in_item_by_id(column_name, geographic_items.flatten.map(&:id))
   end

   # rubocop:disable Metrics/MethodLength
   # @param [String] column_name to search
   # @param [GeographicItem] geographic_item_ids or array of geographic_item_ids to be tested.
   # @return [Scope] of GeographicItems
   #
   # If this scope is given an Array of GeographicItems as a second parameter,
   # it will return the 'OR' of each of the objects against the table.
   # SELECT COUNT(*) FROM "geographic_items"
   #        WHERE (ST_Contains(polygon::geometry, GeomFromEWKT('srid=4326;POINT (0.0 0.0 0.0)'))
   #               OR ST_Contains(polygon::geometry, GeomFromEWKT('srid=4326;POINT (-9.8 5.0 0.0)')))
   #
   def are_contained_in_item_by_id(column_name, *geographic_item_ids) # = containing
     geographic_item_ids.flatten! # in case there is a array of arrays, or multiple objects
     column_name.downcase!
     case column_name
     when 'any'
       part = []
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           part.push(GeographicItem.are_contained_in_item_by_id(column.to_s, geographic_item_ids).to_a)
         end
       }
       # TODO: change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten.map(&:id))
     when 'any_poly', 'any_line'
       part = []
       DATA_TYPES.each { |column|
         if column.to_s.index(column_name.gsub('any_', ''))
           part.push(GeographicItem.are_contained_in_item_by_id("#{column}", geographic_item_ids).to_a)
         end
       }
       # TODO: change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten.map(&:id))
     else
       q = geographic_item_ids.flatten.collect { |geographic_item_id|
         # discover the item types, and convert type to database type for 'multi_'
         b = GeographicItem.where(id: geographic_item_id)
           .pluck(:type)[0].split(':')[2].downcase.gsub('lti', 'lti_')
         # a = GeographicItem.find(geographic_item_id).geo_object_type
         GeographicItem.containing_sql(column_name, geographic_item_id, b)
       }.join(' or ')
       q = 'FALSE' if q.blank? # this will prevent the invocation of *ALL* of the GeographicItems, if there are
       # no GeographicItems in the request (see CollectingEvent.name_hash(types)).
       where(q) # .not_including(geographic_items)
     end
   end

   # rubocop:enable Metrics/MethodLength

   # @param [String] column_name
   # @param [String] geometry of WKT
   # @return [Scope]
   # a single WKT geometry is compared against column or columns (except geometry_collection) to find geographic_items
   # which are contained in the WKT
   def are_contained_in_wkt(column_name, geometry)
     column_name.downcase!
     # column_name = 'point'
     case column_name
     when 'any'
       part = []
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           part.push(GeographicItem.are_contained_in_wkt(column.to_s, geometry).pluck(:id).to_a)
         end
       }
       # TODO: change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten)
     when 'any_poly', 'any_line'
       part = []
       DATA_TYPES.each { |column|
         if column.to_s.index(column_name.gsub('any_', ''))
           part.push(GeographicItem.are_contained_in_wkt("#{column}", geometry).pluck(:id).to_a)
         end
       }
       # TODO: change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten)
     else
       # column = points, geometry = square
       q = "ST_Contains(ST_GeomFromEWKT('srid=4326;#{geometry}'), #{column_name}::geometry)"
       where(q) # .not_including(geographic_items)
     end
   end

   # rubocop:disable Metrics/MethodLength
   #    containing the items the shape of which is contained in the geographic_item[s] supplied.
   # @param column_name [String] can be any of DATA_TYPES, or 'any' to check against all types, 'any_poly' to check
   # against 'polygon' or 'multi_polygon', or 'any_line' to check against 'line_string' or 'multi_line_string'.
   #  CANNOT be 'geometry_collection'.
   # @param geographic_items [GeographicItem] Can be a single GeographicItem, or an array of GeographicItem.
   # @return [Scope]
   def is_contained_by(column_name, *geographic_items)
     column_name.downcase!
     case column_name
     when 'any'
       part = []
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           part.push(GeographicItem.is_contained_by(column.to_s, geographic_items).to_a)
         end
       }
       # @TODO change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten.map(&:id))

     when 'any_poly', 'any_line'
       part = []
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           if column.to_s.index(column_name.gsub('any_', ''))
             part.push(GeographicItem.is_contained_by(column.to_s, geographic_items).to_a)
           end
         end
       }
       # @TODO change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten.map(&:id))

     else
       q = geographic_items.flatten.collect { |geographic_item|
         GeographicItem.reverse_containing_sql(column_name, geographic_item.to_param,
                                               geographic_item.geo_object_type)
       }.join(' or ')
       where(q) # .not_including(geographic_items)
     end
   end

   # rubocop:enable Metrics/MethodLength

   # @param [String, GeographicItem]
   # @return [Scope]
   def ordered_by_shortest_distance_from(column_name, geographic_item)
     if true # check_geo_params(column_name, geographic_item)
       select_distance_with_geo_object(column_name, geographic_item)
         .where_distance_greater_than_zero(column_name, geographic_item).order('distance')
     else
       where('false')
     end
   end

   # @param [String, GeographicItem]
   # @return [Scope]
   def ordered_by_longest_distance_from(column_name, geographic_item)
     if true # check_geo_params(column_name, geographic_item)
       q = select_distance_with_geo_object(column_name, geographic_item)
         .where_distance_greater_than_zero(column_name, geographic_item)
         .order('distance desc')
       q
     else
       where('false')
     end
   end

   # @param [String] column_name
   # @param [GeographicItem] geographic_item
   # @return [String]
   def select_distance_with_geo_object(column_name, geographic_item)
     select("*, ST_Distance(#{column_name}, GeomFromEWKT('srid=4326;#{geographic_item.geo_object}')) as distance")
   end

   # @param [String, GeographicItem]
   # @return [Scope]
   def where_distance_greater_than_zero(column_name, geographic_item)
     where("#{column_name} is not null and ST_Distance(#{column_name}, " \
           "GeomFromEWKT('srid=4326;#{geographic_item.geo_object}')) > 0")
   end

   # @param [GeographicItem]
   # @return [Scope]
   def not_including(geographic_items)
     where.not(id: geographic_items)
   end

   # @return [Scope]
   #   includes an 'is_valid' attribute (True/False) for the passed geographic_item.  Uses St_IsValid.
   # @param [RGeo object] geographic_item
   def with_is_valid_geometry_column(geographic_item)
     where(id: geographic_item.id).select("ST_IsValid(ST_AsBinary(#{geographic_item.geo_object_type})) is_valid")
   end

   #
   # Other
   #

   # @param [Integer] geographic_item_id1
   # @param [Integer] geographic_item_id2
   # @return [Float]
   def distance_between(geographic_item_id1, geographic_item_id2)
     q1 = "ST_Distance(#{GeographicItem::GEOGRAPHY_SQL}, " \
       "(#{select_geography_sql(geographic_item_id2)})) as distance"
     _q2 = ActiveRecord::Base.send(
       :sanitize_sql_array, ['ST_Distance(?, (?)) as distance',
                             GeographicItem::GEOGRAPHY_SQL,
                             select_geography_sql(geographic_item_id2)])
     GeographicItem.where(id: geographic_item_id1).pluck(Arel.sql(q1)).first
   end

   # @param [RGeo::Point] point
   # @return [Hash]
   #   as per #inferred_geographic_name_hierarchy but for Rgeo point
   def point_inferred_geographic_name_hierarchy(point)
     GeographicItem.containing_point(point).order(cached_total_area: :ASC).limit(1).first&.inferred_geographic_name_hierarchy
   end

   # @param [String] type_name ('polygon', 'point', 'line' [, 'circle'])
   # @return [String] if type
   def eval_for_type(type_name)
     retval = 'GeographicItem'
     case type_name.upcase
     when 'POLYGON'
       retval += '::Polygon'
     when 'LINESTRING'
       retval += '::LineString'
     when 'POINT'
       retval += '::Point'
     when 'MULTIPOLYGON'
       retval += '::MultiPolygon'
     when 'MULTILINESTRING'
       retval += '::MultiLineString'
     when 'MULTIPOINT'
       retval += '::MultiPoint'
     else
       retval = nil
     end
     retval
   end

   # example, not used
   # @param [Integer] geographic_item_id
   # @return [RGeo::Geographic object]
   def geometry_for(geographic_item_id)
     GeographicItem.select(GeographicItem::GEOMETRY_SQL.to_sql + ' AS geometry').find(geographic_item_id)['geometry']
   end

   # example, not used
   # @param [Integer, Array] geographic_item_ids
   # @return [Scope]
   def st_multi(*geographic_item_ids)
     GeographicItem.find_by_sql(
       "SELECT ST_Multi(ST_Collect(g.the_geom)) AS singlegeom
    FROM (
       SELECT (ST_DUMP(#{GeographicItem::GEOMETRY_SQL.to_sql})).geom AS the_geom
       FROM geographic_items
       WHERE id IN (?))
     AS g;", geographic_item_ids.flatten
     )
   end
   end # class << self

     # @return [Hash]
     #    a geographic_name_classification or empty Hash
     # This is a quick approach that works only when
     # the geographic_item is linked explicitly to a GeographicArea.
     #
     # !! Note that it is not impossible for a GeographicItem to be linked
     # to > 1 GeographicArea, in that case we are assuming that all are
     # equally refined, this might not be the case in the future because
     # of how the GeographicArea gazetteer is indexed.
 def quick_geographic_name_hierarchy
   geographic_areas.order(:id).each do |ga|
     h = ga.geographic_name_classification # not quick enough !!
     return h if h.present?
   end
   return  {}
 end

     # @return [Hash]
     #   a geographic_name_classification (see GeographicArea) inferred by
     # finding the smallest area containing this GeographicItem, in the most accurate gazetteer
     # and using it to return country/state/county. See also the logic in
     # filling in missing levels in GeographicArea.
 def inferred_geographic_name_hierarchy
   if small_area = containing_geographic_areas
     .joins(:geographic_areas_geographic_items)
     .merge(GeographicAreasGeographicItem.ordered_by_data_origin)
     .ordered_by_area
     .limit(1)
     .first
     return small_area.geographic_name_classification
   else
     {}
   end
 end

 def geographic_name_hierarchy
   a = quick_geographic_name_hierarchy # quick; almost never the case, UI not setup to do this
   return a if a.present?
   inferred_geographic_name_hierarchy # slow
 end

     # @return [Scope]
     #   the Geographic Areas that contain (gis) this geographic item
 def containing_geographic_areas
   GeographicArea.joins(:geographic_items).includes(:geographic_area_type)
     .joins("JOIN (#{GeographicItem.containing(id).to_sql}) j on geographic_items.id = j.id")
 end

     # @return [Boolean]
     #   whether stored shape is ST_IsValid
 def valid_geometry?
   GeographicItem.with_is_valid_geometry_column(self).first['is_valid']
 end

     # @return [Array of latitude, longitude]
     #    the lat, lon of the first point in the GeoItem, see subclass for #st_start_point
 def start_point
   o = st_start_point
   [o.y, o.x]
 end

     # @return [Array]
     #   the lat, long, as STRINGs for the centroid of this geographic item
     #   Meh- this: https://postgis.net/docs/en/ST_MinimumBoundingRadius.html
 def center_coords
   r = GeographicItem.find_by_sql(
     "Select split_part(ST_AsLatLonText(ST_Centroid(#{GeographicItem::GEOMETRY_SQL.to_sql}), " \
     "'D.DDDDDD'), ' ', 1) latitude, split_part(ST_AsLatLonText(ST_Centroid" \
     "(#{GeographicItem::GEOMETRY_SQL.to_sql}), 'D.DDDDDD'), ' ', 2) " \
     "longitude from geographic_items where id = #{id};")[0]

   [r.latitude, r.longitude]
 end

     # @return [RGeo::Geographic::ProjectedPointImpl]
     #    representing the centroid of this geographic item
 def centroid
   # Gis::FACTORY.point(*center_coords.reverse)
   return geo_object if type == 'GeographicItem::Point'
   return Gis::FACTORY.parse_wkt(self.st_centroid)
 end

     # @param [Integer] geographic_item_id
     # @return [Double] distance in meters (slower, more accurate)
 def st_distance(geographic_item_id) # geo_object
   q1 = "ST_Distance((#{GeographicItem.select_geography_sql(id)}), " \
     "(#{GeographicItem.select_geography_sql(geographic_item_id)})) as d"
 _q2 = ActiveRecord::Base.send(:sanitize_sql_array, ['ST_Distance((?),(?)) as d',
                                                     GeographicItem.select_geography_sql(self.id),
                                                     GeographicItem.select_geography_sql(geographic_item_id)])
 deg = GeographicItem.where(id:).pluck(Arel.sql(q1)).first
 deg * Utilities::Geo::ONE_WEST
 end

     # @param [GeographicItem] geographic_item
     # @return [Double] distance in meters
     # Like st_distance but works with changed and non persisted objects
 def st_distance_to_geographic_item(geographic_item)
   unless !persisted? || changed?
     a = "(#{GeographicItem.select_geography_sql(id)})"
   else
     a = "ST_GeographyFromText('#{geo_object}')"
   end

   unless !geographic_item.persisted? || geographic_item.changed?
     b = "(#{GeographicItem.select_geography_sql(geographic_item.id)})"
   else
     b = "ST_GeographyFromText('#{geographic_item.geo_object}')"
   end

   ActiveRecord::Base.connection.select_value("SELECT ST_Distance(#{a}, #{b})")
 end

 alias_method :distance_to, :st_distance

     # @param [Integer] geographic_item_id
     # @return [Double] distance in meters (faster, less accurate)
 def st_distance_spheroid(geographic_item_id)
   q1 = "ST_DistanceSpheroid((#{GeographicItem.select_geometry_sql(id)})," \
     "(#{GeographicItem.select_geometry_sql(geographic_item_id)}),'#{Gis::SPHEROID}') as distance"
   _q2 = ActiveRecord::Base.send(:sanitize_sql_array,
                                 ['ST_DistanceSpheroid((?),(?),?) as distance',
                                  GeographicItem.select_geometry_sql(id),
                                  GeographicItem.select_geometry_sql(geographic_item_id),
                                  Gis::SPHEROID])
   # TODO: what is _q2?
   GeographicItem.where(id:).pluck(Arel.sql(q1)).first
 end

     # @return [String]
     #   a WKT POINT representing the centroid of the geographic item
 def st_centroid
   GeographicItem.where(id: to_param).pluck(Arel.sql("ST_AsEWKT(ST_Centroid(#{GeographicItem::GEOMETRY_SQL.to_sql}))")).first.gsub(/SRID=\d*;/, '')
 end

     # @return [Integer]
     #   the number of points in the geometry
 def st_npoints
   GeographicItem.where(id:).pluck(Arel.sql("ST_NPoints(#{GeographicItem::GEOMETRY_SQL.to_sql}) as npoints")).first
 end

     # @return [Symbol]
     #   the geo type (i.e. column like :point, :multipolygon).  References the first-found object,
     # according to the list of DATA_TYPES, or nil
 def geo_object_type
   if self.class.name == 'GeographicItem' # a proxy check for new records
     geo_type
   else
     self.class::SHAPE_COLUMN
   end
 end

     # !!TODO: migrate these to use native column calls

     # @return [RGeo instance, nil]
     #  the Rgeo shape (See http://rubydoc.info/github/dazuma/rgeo/RGeo/Feature)
 def geo_object
   begin
     if r = geo_object_type # rubocop:disable Lint/AssignmentInCondition
       send(r)
     else
       false
     end
   rescue RGeo::Error::InvalidGeometry
     return nil # TODO: we need to render proper error for this!
   end
 end

     # @param [geo_object]
     # @return [Boolean]
 def contains?(target_geo_object)
   return nil if target_geo_object.nil?
   self.geo_object.contains?(target_geo_object)
 end

     # @param [geo_object]
     # @return [Boolean]
 def within?(target_geo_object)
   self.geo_object.within?(target_geo_object)
 end

     # @param [geo_object]
     # @return [Boolean]
 def intersects?(target_geo_object)
   self.geo_object.intersects?(target_geo_object)
 end

     # @param [geo_object, Double]
     # @return [Boolean]
 def near(target_geo_object, distance)
   self.geo_object.unsafe_buffer(distance).contains?(target_geo_object)
 end

     # @param [geo_object, Double]
     # @return [Boolean]
 def far(target_geo_object, distance)
   !near(target_geo_object, distance)
 end

     # @return [GeoJSON hash]
     #    via Rgeo apparently necessary for GeometryCollection
 def rgeo_to_geo_json
   RGeo::GeoJSON.encode(geo_object).to_json
 end

     # @return [Hash]
     #   in GeoJSON format
     #   Computed via "raw" PostGIS (much faster). This
     #   requires the geo_object_type and id.
     #
 def to_geo_json
   JSON.parse(
     GeographicItem.connection.select_one(
       "SELECT ST_AsGeoJSON(#{geo_object_type}::geometry) a " \
       "FROM geographic_items WHERE id=#{id};")['a'])
 end

     # We don't need to serialize to/from JSON
 def to_geo_json_string
   GeographicItem.connection.select_one(
     "SELECT ST_AsGeoJSON(#{geo_object_type}::geometry) a " \
     "FROM geographic_items WHERE id=#{id};")['a']
 end

     # @return [Hash]
     #   the shape as a GeoJSON Feature with some item metadata
 def to_geo_json_feature
   @geometry ||= to_geo_json
   {'type' => 'Feature',
    'geometry' => geometry,
    'properties' => {
      'geographic_item' => {
        'id' => id}
    }
   }
 end

     # @param value [String] like:
     #   '{"type":"Feature","geometry":{"type":"Point","coordinates":[2.5,4.0]},"properties":{"color":"red"}}'
     #
     #   '{"type":"Feature","geometry":{"type":"Polygon","coordinates":"[[[-125.29394388198853, 48.584480409793],
     #      [-67.11035013198853, 45.09937589848195],[-80.64550638198853, 25.01924647619111],[-117.55956888198853,
     #      32.5591595028449],[-125.29394388198853, 48.584480409793]]]"},"properties":{}}'
     #
     #  '{"type":"Point","coordinates":[2.5,4.0]},"properties":{"color":"red"}}'
     #
     # @return [Boolean, RGeo object]
 def shape=(value)

   if value.present?
     geom = RGeo::GeoJSON.decode(value, json_parser: :json, geo_factory: Gis::FACTORY)
     this_type = nil

     if geom.respond_to?(:geometry_type)
       this_type = geom.geometry_type.to_s
     elsif geom.respond_to?(:geometry)
       this_type = geom.geometry.geometry_type.to_s
     else
     end

     self.type = GeographicItem.eval_for_type(this_type) unless geom.nil?

     if type.blank?
       errors.add(:base, 'type is not set from shape')
       return
     end
     # raise('GeographicItem.type not set.') if type.blank?

     object = nil

     s = geom.respond_to?(:geometry) ? geom.geometry.to_s : geom.to_s

     begin
       object = Gis::FACTORY.parse_wkt(s)
     rescue RGeo::Error::InvalidGeometry
       errors.add(:self, 'Shape value is an Invalid Geometry')
     end

     write_attribute(this_type.underscore.to_sym, object)
     geom
   end
 end

     # @return [String]
 def to_wkt
   #  10k  #<Benchmark::Tms:0x00007fb0dfd30fd0 @label="", @real=25.237487000005785, @cstime=0.0, @cutime=0.0, @stime=1.1704609999999995, @utime=5.507929999999988, @total=6.678390999999987>
   #  GeographicItem.select("ST_AsText( #{GeographicItem::GEOMETRY_SQL.to_sql}) wkt").where(id: id).first.wkt

   # 10k <Benchmark::Tms:0x00007fb0e02f7540 @label="", @real=21.619827999995323, @cstime=0.0, @cutime=0.0, @stime=0.8850890000000007, @utime=3.2958549999999605, @total=4.180943999999961>
   if a =  ApplicationRecord.connection.execute( "SELECT ST_AsText( #{GeographicItem::GEOMETRY_SQL.to_sql} ) wkt from geographic_items where geographic_items.id = #{id}").first
     return a['wkt']
   else
     return nil
   end
 end

     # return float, in meters, calculated, not from cached
 def area
   GeographicItem.where(id:).select("ST_Area(#{GeographicItem::GEOGRAPHY_SQL}, false) as area_in_meters").first['area_in_meters']
 end

     # @return [Float, false]
     #    the value in square meters of the interesecting area of this and another GeographicItem
 def intersecting_area(geographic_item_id)
   a = GeographicItem.aliased_geographic_sql('a')
   b = GeographicItem.aliased_geographic_sql('b')

   c = GeographicItem.connection.execute("SELECT ST_Area(ST_Intersection(#{a}, #{b})) as intersecting_area
     FROM geographic_items a, geographic_items b
     WHERE a.id = #{id} AND b.id = #{geographic_item_id};"
                                        ).first
                                        c && c['intersecting_area'].to_f
 end

     # TODO: This is bad, while internal use of ONE_WEST_MEAN is consistent it is in-accurate given the vast differences of radius vs. lat/long position.
     # When we strike the error-polygon from radius we should remove this
     #
     # Use case is returning the radius from a circle we calculated via buffer for error-polygon creation.
 def radius
   r = ApplicationRecord.connection.execute( "SELECT ST_MinimumBoundingRadius( ST_Transform(  #{GeographicItem::GEOMETRY_SQL.to_sql}, 4326 )  ) AS radius from geographic_items where geographic_items.id = #{id}").first['radius'].split(',').last.chop.to_f
   r = (r * Utilities::Geo::ONE_WEST_MEAN).to_i
 end

     private

 def set_cached
   update_column(:cached_total_area, area)
 end

     protected

     # @return [Symbol]
     #   returns the attribute (column name) containing data
     #   nearly all methods should use #geo_object_type, not geo_type
 def geo_type
   DATA_TYPES.each { |item|
     return item if send(item)
   }
   nil
 end

     # @return [Boolean, String] false if already set, or type to which it was set
 def set_type_if_geography_present
   if type.blank?
     column = geo_type
     self.type = "GeographicItem::#{column.to_s.camelize}" if column
   end
 end

     # @param [RGeo::Point] point
     # @return [Array] of a point
     #   Longitude |, Latitude -
 def point_to_a(point)
   data = []
   data.push(point.x, point.y)
   data
 end

     # @param [RGeo::Point] point
     # @return [Hash] of a point
 def point_to_hash(point)
   {points: [point_to_a(point)]}
 end

     # @param [RGeo::MultiPoint] multi_point
     # @return [Array] of points
 def multi_point_to_a(multi_point)
   data = []
   multi_point.each { |point|
     data.push([point.x, point.y])
   }
   data
 end

     # @return [Hash] of points
 def multi_point_to_hash(_multi_point)
   # when we encounter a multi_point type, we only stick the points into the array, NOT it's identity as a group
   {points: multi_point_to_a(multi_point)}
 end

     # @param [Reo::LineString] line_string
     # @return [Array] of points in the line
 def line_string_to_a(line_string)
   data = []
   line_string.points.each { |point|
     data.push([point.x, point.y])
   }
   data
 end

     # @param [Reo::LineString] line_string
     # @return [Hash] of points in the line
 def line_string_to_hash(line_string)
   {lines: [line_string_to_a(line_string)]}
 end

     # @param [RGeo::Polygon] polygon
     # @return [Array] of points in the polygon (exterior_ring ONLY)
 def polygon_to_a(polygon)
   # TODO: handle other parts of the polygon; i.e., the interior_rings (if they exist)
   data = []
   polygon.exterior_ring.points.each { |point|
     data.push([point.x, point.y])
   }
   data
 end

     # @param [RGeo::Polygon] polygon
     # @return [Hash] of points in the polygon (exterior_ring ONLY)
 def polygon_to_hash(polygon)
   {polygons: [polygon_to_a(polygon)]}
 end

     # @return [Array] of line_strings as arrays of points
     # @param [RGeo::MultiLineString] multi_line_string
 def multi_line_string_to_a(multi_line_string)
   data = []
   multi_line_string.each { |line_string|
     line_data = []
     line_string.points.each { |point|
       line_data.push([point.x, point.y])
     }
     data.push(line_data)
   }
   data
 end

     # @return [Hash] of line_strings as hashes of points
 def multi_line_string_to_hash(_multi_line_string)
   {lines: to_a}
 end

     # @param [RGeo::MultiPolygon] multi_polygon
     # @return [Array] of arrays of points in the polygons (exterior_ring ONLY)
 def multi_polygon_to_a(multi_polygon)
   data = []
   multi_polygon.each { |polygon|
     polygon_data = []
     polygon.exterior_ring.points.each { |point|
       polygon_data.push([point.x, point.y])
     }
     data.push(polygon_data)
   }
   data
 end

     # @return [Hash] of hashes of points in the polygons (exterior_ring ONLY)
 def multi_polygon_to_hash(_multi_polygon)
   {polygons: to_a}
 end

     # @return [Boolean] iff there is one and only one shape column set
 def some_data_is_provided
   data = []

   DATA_TYPES.each do |item|
     data.push(item) if send(item).present?
   end

   case data.count
   when 0
     errors.add(:base, 'No shape provided or provided shape is invalid')
   when 1
     return true
   else
     data.each do |object|
       errors.add(object, 'More than one shape type provided')
     end
   end
   true
 end
end

#multi_pointRGeo::Geographic::ProjectedMultiPointImpl

Returns:

  • (RGeo::Geographic::ProjectedMultiPointImpl)


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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
# File 'app/models/geographic_item.rb', line 34

class GeographicItem < ApplicationRecord
 include Housekeeping::Users
 include Housekeeping::Timestamps
 include Shared::HasPapertrail
 include Shared::IsData
 include Shared::SharedAcrossProjects

 # @return [Hash, nil]
 #   An internal variable for use in super calls, holds a Hash in GeoJSON format (temporarily)
 attr_accessor :geometry

 # @return [Boolean, RGeo object]
 # @params value [Hash in GeoJSON format] ?!
 # TODO: WHY! boolean not nil, or object
 # Used to build geographic items from a shape [ of what class ] !?
 attr_accessor :shape

 # @return [Boolean]
 #   When true cached values are not built
 attr_accessor :no_cached

 DATA_TYPES = [
   :point,
   :line_string,
   :polygon,
   :multi_point,
   :multi_line_string,
   :multi_polygon,
   :geometry_collection
 ].freeze

 GEOMETRY_SQL = Arel::Nodes::Case.new(arel_table[:type])
   .when('GeographicItem::MultiPolygon').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:multi_polygon].as('geometry')]))
   .when('GeographicItem::Point').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:point].as('geometry')]))
   .when('GeographicItem::LineString').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:line_string].as('geometry')]))
   .when('GeographicItem::Polygon').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:polygon].as('geometry')]))
   .when('GeographicItem::MultiLineString').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:multi_line_string].as('geometry')]))
   .when('GeographicItem::MultiPoint').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:multi_point].as('geometry')]))
   .when('GeographicItem::GeometryCollection').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:geometry_collection].as('geometry')]))
   .freeze

 GEOGRAPHY_SQL = "CASE geographic_items.type
   WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon
   WHEN 'GeographicItem::Point' THEN point
   WHEN 'GeographicItem::LineString' THEN line_string
   WHEN 'GeographicItem::Polygon' THEN polygon
   WHEN 'GeographicItem::MultiLineString' THEN multi_line_string
   WHEN 'GeographicItem::MultiPoint' THEN multi_point
   WHEN 'GeographicItem::GeometryCollection' THEN geometry_collection
   END".freeze

   # ANTI_MERIDIAN = '0X0102000020E61000000200000000000000008066400000000000405640000000000080664000000000004056C0'
 ANTI_MERIDIAN = 'LINESTRING (180 89.0, 180 -89)'.freeze

 has_many :cached_map_items, inverse_of: :geographic_item

 has_many :geographic_areas_geographic_items, dependent: :destroy, inverse_of: :geographic_item
 has_many :geographic_areas, through: :geographic_areas_geographic_items
 has_many :asserted_distributions, through: :geographic_areas
 has_many :geographic_area_types, through: :geographic_areas
 has_many :parent_geographic_areas, through: :geographic_areas, source: :parent

 has_many :georeferences, inverse_of: :geographic_item
 has_many :georeferences_through_error_geographic_item,
   class_name: 'Georeference', foreign_key: :error_geographic_item_id, inverse_of: :error_geographic_item
 has_many :collecting_events_through_georeferences, through: :georeferences, source: :collecting_event
 has_many :collecting_events_through_georeference_error_geographic_item,
   through: :georeferences_through_error_geographic_item, source: :collecting_event

   # TODO: THIS IS NOT GOOD
 before_validation :set_type_if_geography_present

 validate :some_data_is_provided
 validates :type, presence: true

 scope :include_collecting_event, -> { includes(:collecting_events_through_georeferences) }
 scope :geo_with_collecting_event, -> { joins(:collecting_events_through_georeferences) }
 scope :err_with_collecting_event, -> { joins(:georeferences_through_error_geographic_item) }

 after_save :set_cached, unless: Proc.new {|n| n.no_cached || errors.any? }

 class << self

   def aliased_geographic_sql(name = 'a')
     "CASE #{name}.type \
        WHEN 'GeographicItem::MultiPolygon' THEN #{name}.multi_polygon \
        WHEN 'GeographicItem::Point' THEN #{name}.point \
        WHEN 'GeographicItem::LineString' THEN #{name}.line_string \
        WHEN 'GeographicItem::Polygon' THEN #{name}.polygon \
        WHEN 'GeographicItem::MultiLineString' THEN #{name}.multi_line_string \
        WHEN 'GeographicItem::MultiPoint' THEN #{name}.multi_point \
        WHEN 'GeographicItem::GeometryCollection' THEN #{name}.geometry_collection \
        END"
   end

   def st_union(geographic_item_scope)
     GeographicItem.select("ST_Union(#{GeographicItem::GEOMETRY_SQL.to_sql}) as collection")
       .where(id: geographic_item_scope.pluck(:id))
   end

   def st_collect(geographic_item_scope)
     GeographicItem.select("ST_Collect(#{GeographicItem::GEOMETRY_SQL.to_sql}) as collection")
       .where(id: geographic_item_scope.pluck(:id))
   end

   # @return [GeographicItem::ActiveRecord_Relation]
   # @params [Array] array of geographic area ids
   def default_by_geographic_area_ids(geographic_area_ids = [])
     GeographicItem.
       joins(:geographic_areas_geographic_items).
       merge(::GeographicAreasGeographicItem.default_geographic_item_data).
       where(geographic_areas_geographic_items: {geographic_area_id: geographic_area_ids})
   end

   # @param [String] wkt
   # @return [Boolean]
   #   whether or not the wtk intersects with the anti-meridian
   #   !! StrongParams security considerations
   def crosses_anti_meridian?(wkt)
     GeographicItem.find_by_sql(
       ['SELECT ST_Intersects(ST_GeogFromText(?), ST_GeogFromText(?)) as r;', wkt, ANTI_MERIDIAN]
     ).first.r
   end

   # @param [Integer] ids
   # @return [Boolean]
   #   whether or not any GeographicItem passed intersects the anti-meridian
   #   !! StrongParams security considerations
   #   This is our first line of defense against queries that define multiple shapes, one or
   #   more of which crosses the anti-meridian.  In this case the current TW strategy within the
   #   UI is to abandon the search, and prompt the user to refactor the query.
   def crosses_anti_meridian_by_id?(*ids)
     q1 = ["SELECT ST_Intersects((SELECT single_geometry FROM (#{GeographicItem.single_geometry_sql(*ids)}) as " \
           'left_intersect), ST_GeogFromText(?)) as r;', ANTI_MERIDIAN]
     _q2 = ActiveRecord::Base.send(:sanitize_sql_array, ['SELECT ST_Intersects((SELECT single_geometry FROM (?) as ' \
                                                         'left_intersect), ST_GeogFromText(?)) as r;', GeographicItem.single_geometry_sql(*ids), ANTI_MERIDIAN])
     GeographicItem.find_by_sql(q1).first.r
   end

   #
   # SQL fragments
   #

   # @param [Integer, String]
   # @return [String]
   #   a SQL select statement that returns the *geometry* for the geographic_item with the specified id
   def select_geometry_sql(geographic_item_id)
     "SELECT #{GeographicItem::GEOMETRY_SQL.to_sql} from geographic_items where geographic_items.id = #{geographic_item_id}"
   end

   # @param [Integer, String]
   # @return [String]
   #   a SQL select statement that returns the geography for the geographic_item with the specified id
   def select_geography_sql(geographic_item_id)
     ActiveRecord::Base.send(:sanitize_sql_for_conditions, [
       "SELECT #{GeographicItem::GEOMETRY_SQL.to_sql} from geographic_items where geographic_items.id = ?",
       geographic_item_id])
   end

   # @param [Symbol] choice
   # @return [String]
   #   a fragment returning either latitude or longitude columns
   def lat_long_sql(choice)
     return nil unless [:latitude, :longitude].include?(choice)
     f = "'D.DDDDDD'" # TODO: probably a constant somewhere
     v = (choice == :latitude ? 1 : 2)
     "CASE type
     WHEN 'GeographicItem::GeometryCollection' THEN split_part(ST_AsLatLonText(ST_Centroid" \
   "(geometry_collection::geometry), #{f}), ' ', #{v})
     WHEN 'GeographicItem::LineString' THEN split_part(ST_AsLatLonText(ST_Centroid(line_string::geometry), " \
   "#{f}), ' ', #{v})
     WHEN 'GeographicItem::MultiPolygon' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(multi_polygon::geometry), #{f}), ' ', #{v})
     WHEN 'GeographicItem::Point' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(point::geometry), #{f}), ' ', #{v})
     WHEN 'GeographicItem::Polygon' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(polygon::geometry), #{f}), ' ', #{v})
     WHEN 'GeographicItem::MultiLineString' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(multi_line_string::geometry), #{f} ), ' ', #{v})
     WHEN 'GeographicItem::MultiPoint' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(multi_point::geometry), #{f}), ' ', #{v})
   END as #{choice}"
   end

   # @param [Integer] geographic_item_id
   # @param [Integer] distance
   # @return [String]
   def within_radius_of_item_sql(geographic_item_id, distance)
     "ST_DWithin((#{GeographicItem::GEOGRAPHY_SQL}), (#{select_geography_sql(geographic_item_id)}), #{distance})"
   end

   # TODO: 3D is overkill here
   # @param [String] wkt
   # @param [Integer] distance (meters)
   # @return [String]
   # !! This is intersecting
   def intersecting_radius_of_wkt_sql(wkt, distance)
     "ST_DWithin((#{GeographicItem::GEOGRAPHY_SQL}), ST_Transform( ST_GeomFromText('#{wkt}', " \
       "4326), 4326), #{distance})"
   end

   # @param [String] wkt
   # @param [Integer] distance (meters)
   # @return [String]
   # !! This is fully covering
   def within_radius_of_wkt_sql(wkt, distance)
     "ST_Covers( ST_Buffer(ST_SetSRID( ST_GeomFromText('#{wkt}'), 4326)::geography, #{distance}), (#{GeographicItem::GEOGRAPHY_SQL}))"
   end

   # @param [String, Integer, String]
   # @return [String]
   #   a SQL fragment for ST_Contains() function, returns
   #   all geographic items which are contained in the item supplied
   def containing_sql(target_column_name = nil, geographic_item_id = nil, source_column_name = nil)
     return 'false' if geographic_item_id.nil? || source_column_name.nil? || target_column_name.nil?
     "ST_Contains(#{target_column_name}::geometry, (#{geometry_sql(geographic_item_id, source_column_name)}))"
   end

   # @param [String, Integer, String]
   # @return [String]
   #   a SQL fragment for ST_Contains(), returns
   #   all geographic_items which contain the supplied geographic_item
   def reverse_containing_sql(target_column_name = nil, geographic_item_id = nil, source_column_name = nil)
     return 'false' if geographic_item_id.nil? || source_column_name.nil? || target_column_name.nil?
     "ST_Contains((#{geometry_sql(geographic_item_id, source_column_name)}), #{target_column_name}::geometry)"
   end

   # @param [Integer, String]
   # @return [String]
   #   a SQL fragment that represents the geometry of the geographic item specified (which has data in the
   # source_column_name, i.e. geo_object_type)
   def geometry_sql(geographic_item_id = nil, source_column_name = nil)
     return 'false' if geographic_item_id.nil? || source_column_name.nil?
     "select geom_alias_tbl.#{source_column_name}::geometry from geographic_items geom_alias_tbl " \
       "where geom_alias_tbl.id = #{geographic_item_id}"
   end

   # rubocop:disable Metrics/MethodLength
   # @param [String] column_name
   # @param [GeographicItem] geographic_item
   # @return [String] of SQL
   def is_contained_by_sql(column_name, geographic_item)
     geo_id = geographic_item.id
     geo_type = geographic_item.geo_object_type
     template = '(ST_Contains((select geographic_items.%s::geometry from geographic_items where ' \
       'geographic_items.id = %d), %s::geometry))'
     retval = []
     column_name.downcase!
     case column_name
     when 'any'
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           retval.push(template % [geo_type, geo_id, column])
         end
       }
     when 'any_poly', 'any_line'
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           if column.to_s.index(column_name.gsub('any_', ''))
             retval.push(template % [geo_type, geo_id, column])
           end
         end
       }
     else
       retval = template % [geo_type, geo_id, column_name]
     end
     retval = retval.join(' OR ') if retval.instance_of?(Array)
     retval
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   #   a select query that returns a single geometry (column name 'single_geometry' for the collection of ids
   # provided via ST_Collect)
   def st_collect_sql(*geographic_item_ids)
     geographic_item_ids.flatten!
     ActiveRecord::Base.send(:sanitize_sql_for_conditions, [
       "SELECT ST_Collect(f.the_geom) AS single_geometry
    FROM (
       SELECT (ST_DUMP(#{GeographicItem::GEOMETRY_SQL.to_sql})).geom as the_geom
       FROM geographic_items
       WHERE id in (?))
     AS f", geographic_item_ids])
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   #    returns one or more geographic items combined as a single geometry in column 'single'
   def single_geometry_sql(*geographic_item_ids)
     a = GeographicItem.st_collect_sql(geographic_item_ids)
     '(SELECT single.single_geometry FROM (' + a + ' ) AS single)'
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   #   returns a single geometry "column" (paren wrapped) as "single" for multiple geographic item ids, or the
   # geometry as 'geometry' for a single id
   def geometry_sql2(*geographic_item_ids)
     geographic_item_ids.flatten! # *ALWAYS* reduce the pile to a single level of ids
     if geographic_item_ids.count == 1
       "(#{GeographicItem.geometry_for_sql(geographic_item_ids.first)})"
     else
       GeographicItem.single_geometry_sql(geographic_item_ids)
     end
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   def containing_where_sql(*geographic_item_ids)
     "ST_CoveredBy(
   #{GeographicItem.geometry_sql2(*geographic_item_ids)},
    CASE geographic_items.type
      WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon::geometry
      WHEN 'GeographicItem::Point' THEN point::geometry
      WHEN 'GeographicItem::LineString' THEN line_string::geometry
      WHEN 'GeographicItem::Polygon' THEN polygon::geometry
      WHEN 'GeographicItem::MultiLineString' THEN multi_line_string::geometry
      WHEN 'GeographicItem::MultiPoint' THEN multi_point::geometry
   END)"
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   def containing_where_sql_geog(*geographic_item_ids)
     "ST_CoveredBy(
   #{GeographicItem.geometry_sql2(*geographic_item_ids)},
    CASE geographic_items.type
      WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon::geography
      WHEN 'GeographicItem::Point' THEN point::geography
      WHEN 'GeographicItem::LineString' THEN line_string::geography
      WHEN 'GeographicItem::Polygon' THEN polygon::geography
      WHEN 'GeographicItem::MultiLineString' THEN multi_line_string::geography
      WHEN 'GeographicItem::MultiPoint' THEN multi_point::geography
   END)"
   end

   # @param [Interger, Array of Integer] ids
   # @return [Array]
   #   If we detect that some query id has crossed the meridian, then loop through
   #   and "manually" build up a list of results.
   #   Should only be used if GeographicItem.crosses_anti_meridian_by_id? is true.
   #   Note that this does not return a Scope, so you can't chain it like contained_by?
   # TODO: test this
   def contained_by_with_antimeridian_check(*ids)
     ids.flatten! # make sure there is only one level of splat (*)
     results = []

     crossing_ids = []

     ids.each do |id|
       # push each which crosses
       crossing_ids.push(id) if GeographicItem.crosses_anti_meridian_by_id?(id)
     end

     non_crossing_ids = ids - crossing_ids
     results.push GeographicItem.contained_by(non_crossing_ids).to_a if non_crossing_ids.any?

     crossing_ids.each do |id|
       # [61666, 61661, 61659, 61654, 61639]
       q1 = ActiveRecord::Base.send(:sanitize_sql_array, ['SELECT ST_AsText((SELECT polygon FROM geographic_items ' \
                                                          'WHERE id = ?))', id])
       r = GeographicItem.where(
         # GeographicItem.contained_by_wkt_shifted_sql(GeographicItem.find(id).geo_object.to_s)
         GeographicItem.contained_by_wkt_shifted_sql(
           ApplicationRecord.connection.execute(q1).first['st_astext'])
       ).to_a
       results.push(r)
     end

     results.flatten.uniq
   end

   # @params [String] well known text
   # @return [String] the SQL fragment for the specific geometry type, shifted by longitude
   # Note: this routine is called when it is already known that the A argument crosses anti-meridian
   def contained_by_wkt_shifted_sql(wkt)
     "ST_Contains(ST_ShiftLongitude(ST_GeomFromText('#{wkt}', 4326)), (
       CASE geographic_items.type
          WHEN 'GeographicItem::MultiPolygon' THEN ST_ShiftLongitude(multi_polygon::geometry)
          WHEN 'GeographicItem::Point' THEN ST_ShiftLongitude(point::geometry)
          WHEN 'GeographicItem::LineString' THEN ST_ShiftLongitude(line_string::geometry)
          WHEN 'GeographicItem::Polygon' THEN ST_ShiftLongitude(polygon::geometry)
          WHEN 'GeographicItem::MultiLineString' THEN ST_ShiftLongitude(multi_line_string::geometry)
          WHEN 'GeographicItem::MultiPoint' THEN ST_ShiftLongitude(multi_point::geometry)
       END
       )
     )"
   end

   # TODO: Remove the hard coded 4326 reference
   # @params [String] wkt
   # @return [String] SQL fragment limiting geographics items to those in this WKT
   def contained_by_wkt_sql(wkt)
     if crosses_anti_meridian?(wkt)
       retval = contained_by_wkt_shifted_sql(wkt)
     else
       retval = "ST_Contains(ST_GeomFromText('#{wkt}', 4326), (
       CASE geographic_items.type
          WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon::geometry
          WHEN 'GeographicItem::Point' THEN point::geometry
          WHEN 'GeographicItem::LineString' THEN line_string::geometry
          WHEN 'GeographicItem::Polygon' THEN polygon::geometry
          WHEN 'GeographicItem::MultiLineString' THEN multi_line_string::geometry
          WHEN 'GeographicItem::MultiPoint' THEN multi_point::geometry
       END
       )
     )"
     end
     retval
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String] sql for contained_by via ST_ContainsProperly
   # Note: Can not use GEOMETRY_SQL because geometry_collection is not supported in ST_ContainsProperly
   # Note: !! If the target GeographicItem#id crosses the anti-meridian then you may/will get unexpected results.
   def contained_by_where_sql(*geographic_item_ids)
     "ST_Contains(
       #{GeographicItem.geometry_sql2(*geographic_item_ids)},
       CASE geographic_items.type
         WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon::geometry
         WHEN 'GeographicItem::Point' THEN point::geometry
         WHEN 'GeographicItem::LineString' THEN line_string::geometry
         WHEN 'GeographicItem::Polygon' THEN polygon::geometry
         WHEN 'GeographicItem::MultiLineString' THEN multi_line_string::geometry
         WHEN 'GeographicItem::MultiPoint' THEN multi_point::geometry
       END)"
   end

   # @param [RGeo:Point] rgeo_point
   # @return [String] sql for containing via ST_CoveredBy
   # TODO: Remove the hard coded 4326 reference
   # TODO: should this be wkt_point instead of rgeo_point?
   def containing_where_for_point_sql(rgeo_point)
     "ST_CoveredBy(
     ST_GeomFromText('#{rgeo_point}', 4326),
     #{GeographicItem::GEOMETRY_SQL.to_sql}
    )"
   end

   # @param [Interger] geographic_item_id
   # @return [String] SQL for geometries
   # example, not used
   def geometry_for_sql(geographic_item_id)
     'SELECT ' + GeographicItem::GEOMETRY_SQL.to_sql + ' AS geometry FROM geographic_items WHERE id = ' \
       "#{geographic_item_id} LIMIT 1"
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String] SQL for geometries
   # example, not used
   def geometry_for_collection_sql(*geographic_item_ids)
     'SELECT ' + GeographicItem::GEOMETRY_SQL.to_sql + ' AS geometry FROM geographic_items WHERE id IN ' \
       "( #{geographic_item_ids.join(',')} )"
   end

   #
   # Scopes
   #

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [Scope]
   #    the geographic items containing these collective geographic_item ids, not including self
   def containing(*geographic_item_ids)
     where(GeographicItem.containing_where_sql(geographic_item_ids)).not_ids(*geographic_item_ids)
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [Scope]
   #    the geographic items contained by any of these geographic_item ids, not including self
   # (works via ST_ContainsProperly)
   def contained_by(*geographic_item_ids)
     where(GeographicItem.contained_by_where_sql(geographic_item_ids))
   end

   # @param [RGeo::Point] rgeo_point
   # @return [Scope]
   #    the geographic items containing this point
   # TODO: should be containing_wkt ?
   def containing_point(rgeo_point)
     where(GeographicItem.containing_where_for_point_sql(rgeo_point))
   end

   # @return [Scope]
   #   adds an area_in_meters field, with meters
   def with_area
     select("ST_Area(#{GeographicItem::GEOGRAPHY_SQL}, false) as area_in_meters")
   end

   # return [Scope]
   #   A scope that limits the result to those GeographicItems that have a collecting event
   #   through either the geographic_item or the error_geographic_item
   #
   # A raw SQL join approach for comparison
   #
   # GeographicItem.joins('LEFT JOIN georeferences g1 ON geographic_items.id = g1.geographic_item_id').
   #   joins('LEFT JOIN georeferences g2 ON geographic_items.id = g2.error_geographic_item_id').
   #   where("(g1.geographic_item_id IS NOT NULL OR g2.error_geographic_item_id IS NOT NULL)").uniq

   # @return [Scope] GeographicItem
   # This uses an Arel table approach, this is ultimately more decomposable if we need. Of use:
   #  http://danshultz.github.io/talks/mastering_activerecord_arel  <- best
   #  https://github.com/rails/arel
   #  http://stackoverflow.com/questions/4500629/use-arel-for-a-nested-set-join-query-and-convert-to-activerecordrelation
   #  http://rdoc.info/github/rails/arel/Arel/SelectManager
   #  http://stackoverflow.com/questions/7976358/activerecord-arel-or-condition
   #
   def with_collecting_event_through_georeferences
     geographic_items = GeographicItem.arel_table
     georeferences = Georeference.arel_table
     g1 = georeferences.alias('a')
     g2 = georeferences.alias('b')

     c = geographic_items.join(g1, Arel::Nodes::OuterJoin).on(geographic_items[:id].eq(g1[:geographic_item_id]))
       .join(g2, Arel::Nodes::OuterJoin).on(geographic_items[:id].eq(g2[:error_geographic_item_id]))

     GeographicItem.joins(# turn the Arel back into scope
                          c.join_sources # translate the Arel join to a join hash(?)
                         ).where(
                           g1[:id].not_eq(nil).or(g2[:id].not_eq(nil)) # returns a Arel::Nodes::Grouping
                         ).distinct
   end

   # @return [Scope] include a 'latitude' column
   def with_latitude
     select(lat_long_sql(:latitude))
   end

   # @return [Scope] include a 'longitude' column
   def with_longitude
     select(lat_long_sql(:longitude))
   end

   # @param [String, GeographicItems]
   # @return [Scope]
   def intersecting(column_name, *geographic_items)
     if column_name.downcase == 'any'
       pieces = []
       DATA_TYPES.each { |column|
         pieces.push(GeographicItem.intersecting(column.to_s, geographic_items).to_a)
       }

       # @TODO change 'id in (?)' to some other sql construct

       GeographicItem.where(id: pieces.flatten.map(&:id))
     else
       q = geographic_items.flatten.collect { |geographic_item|
         "ST_Intersects(#{column_name}, '#{geographic_item.geo_object}'    )" # seems like we want this: http://danshultz.github.io/talks/mastering_activerecord_arel/#/15/2
       }.join(' or ')

       where(q)
     end
   end

   # @param [GeographicItem#id] geographic_item_id
   # @param [Float] distance in meters ?!?!
   # @return [ActiveRecord::Relation]
   # !! should be distance, not radius?!
   def within_radius_of_item(geographic_item_id, distance)
     where(within_radius_of_item_sql(geographic_item_id, distance))
   end

   # @param [String, GeographicItem]
   # @return [Scope]
   #   a SQL fragment for ST_DISJOINT, specifies all geographic_items that have data in column_name
   #   that are disjoint from the passed geographic_items
   def disjoint_from(column_name, *geographic_items)
     q = geographic_items.flatten.collect { |geographic_item|
       "ST_DISJOINT(#{column_name}::geometry, (#{geometry_sql(geographic_item.to_param,
                                                            geographic_item.geo_object_type)}))"
     }.join(' and ')

     where(q)
   end

   # @return [Scope]
   #   see are_contained_in_item_by_id
   # @param [String] column_name
   # @param [GeographicItem, Array] geographic_items
   def are_contained_in_item(column_name, *geographic_items)
     are_contained_in_item_by_id(column_name, geographic_items.flatten.map(&:id))
   end

   # rubocop:disable Metrics/MethodLength
   # @param [String] column_name to search
   # @param [GeographicItem] geographic_item_ids or array of geographic_item_ids to be tested.
   # @return [Scope] of GeographicItems
   #
   # If this scope is given an Array of GeographicItems as a second parameter,
   # it will return the 'OR' of each of the objects against the table.
   # SELECT COUNT(*) FROM "geographic_items"
   #        WHERE (ST_Contains(polygon::geometry, GeomFromEWKT('srid=4326;POINT (0.0 0.0 0.0)'))
   #               OR ST_Contains(polygon::geometry, GeomFromEWKT('srid=4326;POINT (-9.8 5.0 0.0)')))
   #
   def are_contained_in_item_by_id(column_name, *geographic_item_ids) # = containing
     geographic_item_ids.flatten! # in case there is a array of arrays, or multiple objects
     column_name.downcase!
     case column_name
     when 'any'
       part = []
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           part.push(GeographicItem.are_contained_in_item_by_id(column.to_s, geographic_item_ids).to_a)
         end
       }
       # TODO: change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten.map(&:id))
     when 'any_poly', 'any_line'
       part = []
       DATA_TYPES.each { |column|
         if column.to_s.index(column_name.gsub('any_', ''))
           part.push(GeographicItem.are_contained_in_item_by_id("#{column}", geographic_item_ids).to_a)
         end
       }
       # TODO: change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten.map(&:id))
     else
       q = geographic_item_ids.flatten.collect { |geographic_item_id|
         # discover the item types, and convert type to database type for 'multi_'
         b = GeographicItem.where(id: geographic_item_id)
           .pluck(:type)[0].split(':')[2].downcase.gsub('lti', 'lti_')
         # a = GeographicItem.find(geographic_item_id).geo_object_type
         GeographicItem.containing_sql(column_name, geographic_item_id, b)
       }.join(' or ')
       q = 'FALSE' if q.blank? # this will prevent the invocation of *ALL* of the GeographicItems, if there are
       # no GeographicItems in the request (see CollectingEvent.name_hash(types)).
       where(q) # .not_including(geographic_items)
     end
   end

   # rubocop:enable Metrics/MethodLength

   # @param [String] column_name
   # @param [String] geometry of WKT
   # @return [Scope]
   # a single WKT geometry is compared against column or columns (except geometry_collection) to find geographic_items
   # which are contained in the WKT
   def are_contained_in_wkt(column_name, geometry)
     column_name.downcase!
     # column_name = 'point'
     case column_name
     when 'any'
       part = []
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           part.push(GeographicItem.are_contained_in_wkt(column.to_s, geometry).pluck(:id).to_a)
         end
       }
       # TODO: change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten)
     when 'any_poly', 'any_line'
       part = []
       DATA_TYPES.each { |column|
         if column.to_s.index(column_name.gsub('any_', ''))
           part.push(GeographicItem.are_contained_in_wkt("#{column}", geometry).pluck(:id).to_a)
         end
       }
       # TODO: change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten)
     else
       # column = points, geometry = square
       q = "ST_Contains(ST_GeomFromEWKT('srid=4326;#{geometry}'), #{column_name}::geometry)"
       where(q) # .not_including(geographic_items)
     end
   end

   # rubocop:disable Metrics/MethodLength
   #    containing the items the shape of which is contained in the geographic_item[s] supplied.
   # @param column_name [String] can be any of DATA_TYPES, or 'any' to check against all types, 'any_poly' to check
   # against 'polygon' or 'multi_polygon', or 'any_line' to check against 'line_string' or 'multi_line_string'.
   #  CANNOT be 'geometry_collection'.
   # @param geographic_items [GeographicItem] Can be a single GeographicItem, or an array of GeographicItem.
   # @return [Scope]
   def is_contained_by(column_name, *geographic_items)
     column_name.downcase!
     case column_name
     when 'any'
       part = []
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           part.push(GeographicItem.is_contained_by(column.to_s, geographic_items).to_a)
         end
       }
       # @TODO change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten.map(&:id))

     when 'any_poly', 'any_line'
       part = []
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           if column.to_s.index(column_name.gsub('any_', ''))
             part.push(GeographicItem.is_contained_by(column.to_s, geographic_items).to_a)
           end
         end
       }
       # @TODO change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten.map(&:id))

     else
       q = geographic_items.flatten.collect { |geographic_item|
         GeographicItem.reverse_containing_sql(column_name, geographic_item.to_param,
                                               geographic_item.geo_object_type)
       }.join(' or ')
       where(q) # .not_including(geographic_items)
     end
   end

   # rubocop:enable Metrics/MethodLength

   # @param [String, GeographicItem]
   # @return [Scope]
   def ordered_by_shortest_distance_from(column_name, geographic_item)
     if true # check_geo_params(column_name, geographic_item)
       select_distance_with_geo_object(column_name, geographic_item)
         .where_distance_greater_than_zero(column_name, geographic_item).order('distance')
     else
       where('false')
     end
   end

   # @param [String, GeographicItem]
   # @return [Scope]
   def ordered_by_longest_distance_from(column_name, geographic_item)
     if true # check_geo_params(column_name, geographic_item)
       q = select_distance_with_geo_object(column_name, geographic_item)
         .where_distance_greater_than_zero(column_name, geographic_item)
         .order('distance desc')
       q
     else
       where('false')
     end
   end

   # @param [String] column_name
   # @param [GeographicItem] geographic_item
   # @return [String]
   def select_distance_with_geo_object(column_name, geographic_item)
     select("*, ST_Distance(#{column_name}, GeomFromEWKT('srid=4326;#{geographic_item.geo_object}')) as distance")
   end

   # @param [String, GeographicItem]
   # @return [Scope]
   def where_distance_greater_than_zero(column_name, geographic_item)
     where("#{column_name} is not null and ST_Distance(#{column_name}, " \
           "GeomFromEWKT('srid=4326;#{geographic_item.geo_object}')) > 0")
   end

   # @param [GeographicItem]
   # @return [Scope]
   def not_including(geographic_items)
     where.not(id: geographic_items)
   end

   # @return [Scope]
   #   includes an 'is_valid' attribute (True/False) for the passed geographic_item.  Uses St_IsValid.
   # @param [RGeo object] geographic_item
   def with_is_valid_geometry_column(geographic_item)
     where(id: geographic_item.id).select("ST_IsValid(ST_AsBinary(#{geographic_item.geo_object_type})) is_valid")
   end

   #
   # Other
   #

   # @param [Integer] geographic_item_id1
   # @param [Integer] geographic_item_id2
   # @return [Float]
   def distance_between(geographic_item_id1, geographic_item_id2)
     q1 = "ST_Distance(#{GeographicItem::GEOGRAPHY_SQL}, " \
       "(#{select_geography_sql(geographic_item_id2)})) as distance"
     _q2 = ActiveRecord::Base.send(
       :sanitize_sql_array, ['ST_Distance(?, (?)) as distance',
                             GeographicItem::GEOGRAPHY_SQL,
                             select_geography_sql(geographic_item_id2)])
     GeographicItem.where(id: geographic_item_id1).pluck(Arel.sql(q1)).first
   end

   # @param [RGeo::Point] point
   # @return [Hash]
   #   as per #inferred_geographic_name_hierarchy but for Rgeo point
   def point_inferred_geographic_name_hierarchy(point)
     GeographicItem.containing_point(point).order(cached_total_area: :ASC).limit(1).first&.inferred_geographic_name_hierarchy
   end

   # @param [String] type_name ('polygon', 'point', 'line' [, 'circle'])
   # @return [String] if type
   def eval_for_type(type_name)
     retval = 'GeographicItem'
     case type_name.upcase
     when 'POLYGON'
       retval += '::Polygon'
     when 'LINESTRING'
       retval += '::LineString'
     when 'POINT'
       retval += '::Point'
     when 'MULTIPOLYGON'
       retval += '::MultiPolygon'
     when 'MULTILINESTRING'
       retval += '::MultiLineString'
     when 'MULTIPOINT'
       retval += '::MultiPoint'
     else
       retval = nil
     end
     retval
   end

   # example, not used
   # @param [Integer] geographic_item_id
   # @return [RGeo::Geographic object]
   def geometry_for(geographic_item_id)
     GeographicItem.select(GeographicItem::GEOMETRY_SQL.to_sql + ' AS geometry').find(geographic_item_id)['geometry']
   end

   # example, not used
   # @param [Integer, Array] geographic_item_ids
   # @return [Scope]
   def st_multi(*geographic_item_ids)
     GeographicItem.find_by_sql(
       "SELECT ST_Multi(ST_Collect(g.the_geom)) AS singlegeom
    FROM (
       SELECT (ST_DUMP(#{GeographicItem::GEOMETRY_SQL.to_sql})).geom AS the_geom
       FROM geographic_items
       WHERE id IN (?))
     AS g;", geographic_item_ids.flatten
     )
   end
   end # class << self

     # @return [Hash]
     #    a geographic_name_classification or empty Hash
     # This is a quick approach that works only when
     # the geographic_item is linked explicitly to a GeographicArea.
     #
     # !! Note that it is not impossible for a GeographicItem to be linked
     # to > 1 GeographicArea, in that case we are assuming that all are
     # equally refined, this might not be the case in the future because
     # of how the GeographicArea gazetteer is indexed.
 def quick_geographic_name_hierarchy
   geographic_areas.order(:id).each do |ga|
     h = ga.geographic_name_classification # not quick enough !!
     return h if h.present?
   end
   return  {}
 end

     # @return [Hash]
     #   a geographic_name_classification (see GeographicArea) inferred by
     # finding the smallest area containing this GeographicItem, in the most accurate gazetteer
     # and using it to return country/state/county. See also the logic in
     # filling in missing levels in GeographicArea.
 def inferred_geographic_name_hierarchy
   if small_area = containing_geographic_areas
     .joins(:geographic_areas_geographic_items)
     .merge(GeographicAreasGeographicItem.ordered_by_data_origin)
     .ordered_by_area
     .limit(1)
     .first
     return small_area.geographic_name_classification
   else
     {}
   end
 end

 def geographic_name_hierarchy
   a = quick_geographic_name_hierarchy # quick; almost never the case, UI not setup to do this
   return a if a.present?
   inferred_geographic_name_hierarchy # slow
 end

     # @return [Scope]
     #   the Geographic Areas that contain (gis) this geographic item
 def containing_geographic_areas
   GeographicArea.joins(:geographic_items).includes(:geographic_area_type)
     .joins("JOIN (#{GeographicItem.containing(id).to_sql}) j on geographic_items.id = j.id")
 end

     # @return [Boolean]
     #   whether stored shape is ST_IsValid
 def valid_geometry?
   GeographicItem.with_is_valid_geometry_column(self).first['is_valid']
 end

     # @return [Array of latitude, longitude]
     #    the lat, lon of the first point in the GeoItem, see subclass for #st_start_point
 def start_point
   o = st_start_point
   [o.y, o.x]
 end

     # @return [Array]
     #   the lat, long, as STRINGs for the centroid of this geographic item
     #   Meh- this: https://postgis.net/docs/en/ST_MinimumBoundingRadius.html
 def center_coords
   r = GeographicItem.find_by_sql(
     "Select split_part(ST_AsLatLonText(ST_Centroid(#{GeographicItem::GEOMETRY_SQL.to_sql}), " \
     "'D.DDDDDD'), ' ', 1) latitude, split_part(ST_AsLatLonText(ST_Centroid" \
     "(#{GeographicItem::GEOMETRY_SQL.to_sql}), 'D.DDDDDD'), ' ', 2) " \
     "longitude from geographic_items where id = #{id};")[0]

   [r.latitude, r.longitude]
 end

     # @return [RGeo::Geographic::ProjectedPointImpl]
     #    representing the centroid of this geographic item
 def centroid
   # Gis::FACTORY.point(*center_coords.reverse)
   return geo_object if type == 'GeographicItem::Point'
   return Gis::FACTORY.parse_wkt(self.st_centroid)
 end

     # @param [Integer] geographic_item_id
     # @return [Double] distance in meters (slower, more accurate)
 def st_distance(geographic_item_id) # geo_object
   q1 = "ST_Distance((#{GeographicItem.select_geography_sql(id)}), " \
     "(#{GeographicItem.select_geography_sql(geographic_item_id)})) as d"
 _q2 = ActiveRecord::Base.send(:sanitize_sql_array, ['ST_Distance((?),(?)) as d',
                                                     GeographicItem.select_geography_sql(self.id),
                                                     GeographicItem.select_geography_sql(geographic_item_id)])
 deg = GeographicItem.where(id:).pluck(Arel.sql(q1)).first
 deg * Utilities::Geo::ONE_WEST
 end

     # @param [GeographicItem] geographic_item
     # @return [Double] distance in meters
     # Like st_distance but works with changed and non persisted objects
 def st_distance_to_geographic_item(geographic_item)
   unless !persisted? || changed?
     a = "(#{GeographicItem.select_geography_sql(id)})"
   else
     a = "ST_GeographyFromText('#{geo_object}')"
   end

   unless !geographic_item.persisted? || geographic_item.changed?
     b = "(#{GeographicItem.select_geography_sql(geographic_item.id)})"
   else
     b = "ST_GeographyFromText('#{geographic_item.geo_object}')"
   end

   ActiveRecord::Base.connection.select_value("SELECT ST_Distance(#{a}, #{b})")
 end

 alias_method :distance_to, :st_distance

     # @param [Integer] geographic_item_id
     # @return [Double] distance in meters (faster, less accurate)
 def st_distance_spheroid(geographic_item_id)
   q1 = "ST_DistanceSpheroid((#{GeographicItem.select_geometry_sql(id)})," \
     "(#{GeographicItem.select_geometry_sql(geographic_item_id)}),'#{Gis::SPHEROID}') as distance"
   _q2 = ActiveRecord::Base.send(:sanitize_sql_array,
                                 ['ST_DistanceSpheroid((?),(?),?) as distance',
                                  GeographicItem.select_geometry_sql(id),
                                  GeographicItem.select_geometry_sql(geographic_item_id),
                                  Gis::SPHEROID])
   # TODO: what is _q2?
   GeographicItem.where(id:).pluck(Arel.sql(q1)).first
 end

     # @return [String]
     #   a WKT POINT representing the centroid of the geographic item
 def st_centroid
   GeographicItem.where(id: to_param).pluck(Arel.sql("ST_AsEWKT(ST_Centroid(#{GeographicItem::GEOMETRY_SQL.to_sql}))")).first.gsub(/SRID=\d*;/, '')
 end

     # @return [Integer]
     #   the number of points in the geometry
 def st_npoints
   GeographicItem.where(id:).pluck(Arel.sql("ST_NPoints(#{GeographicItem::GEOMETRY_SQL.to_sql}) as npoints")).first
 end

     # @return [Symbol]
     #   the geo type (i.e. column like :point, :multipolygon).  References the first-found object,
     # according to the list of DATA_TYPES, or nil
 def geo_object_type
   if self.class.name == 'GeographicItem' # a proxy check for new records
     geo_type
   else
     self.class::SHAPE_COLUMN
   end
 end

     # !!TODO: migrate these to use native column calls

     # @return [RGeo instance, nil]
     #  the Rgeo shape (See http://rubydoc.info/github/dazuma/rgeo/RGeo/Feature)
 def geo_object
   begin
     if r = geo_object_type # rubocop:disable Lint/AssignmentInCondition
       send(r)
     else
       false
     end
   rescue RGeo::Error::InvalidGeometry
     return nil # TODO: we need to render proper error for this!
   end
 end

     # @param [geo_object]
     # @return [Boolean]
 def contains?(target_geo_object)
   return nil if target_geo_object.nil?
   self.geo_object.contains?(target_geo_object)
 end

     # @param [geo_object]
     # @return [Boolean]
 def within?(target_geo_object)
   self.geo_object.within?(target_geo_object)
 end

     # @param [geo_object]
     # @return [Boolean]
 def intersects?(target_geo_object)
   self.geo_object.intersects?(target_geo_object)
 end

     # @param [geo_object, Double]
     # @return [Boolean]
 def near(target_geo_object, distance)
   self.geo_object.unsafe_buffer(distance).contains?(target_geo_object)
 end

     # @param [geo_object, Double]
     # @return [Boolean]
 def far(target_geo_object, distance)
   !near(target_geo_object, distance)
 end

     # @return [GeoJSON hash]
     #    via Rgeo apparently necessary for GeometryCollection
 def rgeo_to_geo_json
   RGeo::GeoJSON.encode(geo_object).to_json
 end

     # @return [Hash]
     #   in GeoJSON format
     #   Computed via "raw" PostGIS (much faster). This
     #   requires the geo_object_type and id.
     #
 def to_geo_json
   JSON.parse(
     GeographicItem.connection.select_one(
       "SELECT ST_AsGeoJSON(#{geo_object_type}::geometry) a " \
       "FROM geographic_items WHERE id=#{id};")['a'])
 end

     # We don't need to serialize to/from JSON
 def to_geo_json_string
   GeographicItem.connection.select_one(
     "SELECT ST_AsGeoJSON(#{geo_object_type}::geometry) a " \
     "FROM geographic_items WHERE id=#{id};")['a']
 end

     # @return [Hash]
     #   the shape as a GeoJSON Feature with some item metadata
 def to_geo_json_feature
   @geometry ||= to_geo_json
   {'type' => 'Feature',
    'geometry' => geometry,
    'properties' => {
      'geographic_item' => {
        'id' => id}
    }
   }
 end

     # @param value [String] like:
     #   '{"type":"Feature","geometry":{"type":"Point","coordinates":[2.5,4.0]},"properties":{"color":"red"}}'
     #
     #   '{"type":"Feature","geometry":{"type":"Polygon","coordinates":"[[[-125.29394388198853, 48.584480409793],
     #      [-67.11035013198853, 45.09937589848195],[-80.64550638198853, 25.01924647619111],[-117.55956888198853,
     #      32.5591595028449],[-125.29394388198853, 48.584480409793]]]"},"properties":{}}'
     #
     #  '{"type":"Point","coordinates":[2.5,4.0]},"properties":{"color":"red"}}'
     #
     # @return [Boolean, RGeo object]
 def shape=(value)

   if value.present?
     geom = RGeo::GeoJSON.decode(value, json_parser: :json, geo_factory: Gis::FACTORY)
     this_type = nil

     if geom.respond_to?(:geometry_type)
       this_type = geom.geometry_type.to_s
     elsif geom.respond_to?(:geometry)
       this_type = geom.geometry.geometry_type.to_s
     else
     end

     self.type = GeographicItem.eval_for_type(this_type) unless geom.nil?

     if type.blank?
       errors.add(:base, 'type is not set from shape')
       return
     end
     # raise('GeographicItem.type not set.') if type.blank?

     object = nil

     s = geom.respond_to?(:geometry) ? geom.geometry.to_s : geom.to_s

     begin
       object = Gis::FACTORY.parse_wkt(s)
     rescue RGeo::Error::InvalidGeometry
       errors.add(:self, 'Shape value is an Invalid Geometry')
     end

     write_attribute(this_type.underscore.to_sym, object)
     geom
   end
 end

     # @return [String]
 def to_wkt
   #  10k  #<Benchmark::Tms:0x00007fb0dfd30fd0 @label="", @real=25.237487000005785, @cstime=0.0, @cutime=0.0, @stime=1.1704609999999995, @utime=5.507929999999988, @total=6.678390999999987>
   #  GeographicItem.select("ST_AsText( #{GeographicItem::GEOMETRY_SQL.to_sql}) wkt").where(id: id).first.wkt

   # 10k <Benchmark::Tms:0x00007fb0e02f7540 @label="", @real=21.619827999995323, @cstime=0.0, @cutime=0.0, @stime=0.8850890000000007, @utime=3.2958549999999605, @total=4.180943999999961>
   if a =  ApplicationRecord.connection.execute( "SELECT ST_AsText( #{GeographicItem::GEOMETRY_SQL.to_sql} ) wkt from geographic_items where geographic_items.id = #{id}").first
     return a['wkt']
   else
     return nil
   end
 end

     # return float, in meters, calculated, not from cached
 def area
   GeographicItem.where(id:).select("ST_Area(#{GeographicItem::GEOGRAPHY_SQL}, false) as area_in_meters").first['area_in_meters']
 end

     # @return [Float, false]
     #    the value in square meters of the interesecting area of this and another GeographicItem
 def intersecting_area(geographic_item_id)
   a = GeographicItem.aliased_geographic_sql('a')
   b = GeographicItem.aliased_geographic_sql('b')

   c = GeographicItem.connection.execute("SELECT ST_Area(ST_Intersection(#{a}, #{b})) as intersecting_area
     FROM geographic_items a, geographic_items b
     WHERE a.id = #{id} AND b.id = #{geographic_item_id};"
                                        ).first
                                        c && c['intersecting_area'].to_f
 end

     # TODO: This is bad, while internal use of ONE_WEST_MEAN is consistent it is in-accurate given the vast differences of radius vs. lat/long position.
     # When we strike the error-polygon from radius we should remove this
     #
     # Use case is returning the radius from a circle we calculated via buffer for error-polygon creation.
 def radius
   r = ApplicationRecord.connection.execute( "SELECT ST_MinimumBoundingRadius( ST_Transform(  #{GeographicItem::GEOMETRY_SQL.to_sql}, 4326 )  ) AS radius from geographic_items where geographic_items.id = #{id}").first['radius'].split(',').last.chop.to_f
   r = (r * Utilities::Geo::ONE_WEST_MEAN).to_i
 end

     private

 def set_cached
   update_column(:cached_total_area, area)
 end

     protected

     # @return [Symbol]
     #   returns the attribute (column name) containing data
     #   nearly all methods should use #geo_object_type, not geo_type
 def geo_type
   DATA_TYPES.each { |item|
     return item if send(item)
   }
   nil
 end

     # @return [Boolean, String] false if already set, or type to which it was set
 def set_type_if_geography_present
   if type.blank?
     column = geo_type
     self.type = "GeographicItem::#{column.to_s.camelize}" if column
   end
 end

     # @param [RGeo::Point] point
     # @return [Array] of a point
     #   Longitude |, Latitude -
 def point_to_a(point)
   data = []
   data.push(point.x, point.y)
   data
 end

     # @param [RGeo::Point] point
     # @return [Hash] of a point
 def point_to_hash(point)
   {points: [point_to_a(point)]}
 end

     # @param [RGeo::MultiPoint] multi_point
     # @return [Array] of points
 def multi_point_to_a(multi_point)
   data = []
   multi_point.each { |point|
     data.push([point.x, point.y])
   }
   data
 end

     # @return [Hash] of points
 def multi_point_to_hash(_multi_point)
   # when we encounter a multi_point type, we only stick the points into the array, NOT it's identity as a group
   {points: multi_point_to_a(multi_point)}
 end

     # @param [Reo::LineString] line_string
     # @return [Array] of points in the line
 def line_string_to_a(line_string)
   data = []
   line_string.points.each { |point|
     data.push([point.x, point.y])
   }
   data
 end

     # @param [Reo::LineString] line_string
     # @return [Hash] of points in the line
 def line_string_to_hash(line_string)
   {lines: [line_string_to_a(line_string)]}
 end

     # @param [RGeo::Polygon] polygon
     # @return [Array] of points in the polygon (exterior_ring ONLY)
 def polygon_to_a(polygon)
   # TODO: handle other parts of the polygon; i.e., the interior_rings (if they exist)
   data = []
   polygon.exterior_ring.points.each { |point|
     data.push([point.x, point.y])
   }
   data
 end

     # @param [RGeo::Polygon] polygon
     # @return [Hash] of points in the polygon (exterior_ring ONLY)
 def polygon_to_hash(polygon)
   {polygons: [polygon_to_a(polygon)]}
 end

     # @return [Array] of line_strings as arrays of points
     # @param [RGeo::MultiLineString] multi_line_string
 def multi_line_string_to_a(multi_line_string)
   data = []
   multi_line_string.each { |line_string|
     line_data = []
     line_string.points.each { |point|
       line_data.push([point.x, point.y])
     }
     data.push(line_data)
   }
   data
 end

     # @return [Hash] of line_strings as hashes of points
 def multi_line_string_to_hash(_multi_line_string)
   {lines: to_a}
 end

     # @param [RGeo::MultiPolygon] multi_polygon
     # @return [Array] of arrays of points in the polygons (exterior_ring ONLY)
 def multi_polygon_to_a(multi_polygon)
   data = []
   multi_polygon.each { |polygon|
     polygon_data = []
     polygon.exterior_ring.points.each { |point|
       polygon_data.push([point.x, point.y])
     }
     data.push(polygon_data)
   }
   data
 end

     # @return [Hash] of hashes of points in the polygons (exterior_ring ONLY)
 def multi_polygon_to_hash(_multi_polygon)
   {polygons: to_a}
 end

     # @return [Boolean] iff there is one and only one shape column set
 def some_data_is_provided
   data = []

   DATA_TYPES.each do |item|
     data.push(item) if send(item).present?
   end

   case data.count
   when 0
     errors.add(:base, 'No shape provided or provided shape is invalid')
   when 1
     return true
   else
     data.each do |object|
       errors.add(object, 'More than one shape type provided')
     end
   end
   true
 end
end

#multi_polygonRGeo::Geographic::ProjectedMultiPolygonImpl

Returns:

  • (RGeo::Geographic::ProjectedMultiPolygonImpl)


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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
# File 'app/models/geographic_item.rb', line 34

class GeographicItem < ApplicationRecord
 include Housekeeping::Users
 include Housekeeping::Timestamps
 include Shared::HasPapertrail
 include Shared::IsData
 include Shared::SharedAcrossProjects

 # @return [Hash, nil]
 #   An internal variable for use in super calls, holds a Hash in GeoJSON format (temporarily)
 attr_accessor :geometry

 # @return [Boolean, RGeo object]
 # @params value [Hash in GeoJSON format] ?!
 # TODO: WHY! boolean not nil, or object
 # Used to build geographic items from a shape [ of what class ] !?
 attr_accessor :shape

 # @return [Boolean]
 #   When true cached values are not built
 attr_accessor :no_cached

 DATA_TYPES = [
   :point,
   :line_string,
   :polygon,
   :multi_point,
   :multi_line_string,
   :multi_polygon,
   :geometry_collection
 ].freeze

 GEOMETRY_SQL = Arel::Nodes::Case.new(arel_table[:type])
   .when('GeographicItem::MultiPolygon').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:multi_polygon].as('geometry')]))
   .when('GeographicItem::Point').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:point].as('geometry')]))
   .when('GeographicItem::LineString').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:line_string].as('geometry')]))
   .when('GeographicItem::Polygon').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:polygon].as('geometry')]))
   .when('GeographicItem::MultiLineString').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:multi_line_string].as('geometry')]))
   .when('GeographicItem::MultiPoint').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:multi_point].as('geometry')]))
   .when('GeographicItem::GeometryCollection').then(Arel::Nodes::NamedFunction.new('CAST', [arel_table[:geometry_collection].as('geometry')]))
   .freeze

 GEOGRAPHY_SQL = "CASE geographic_items.type
   WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon
   WHEN 'GeographicItem::Point' THEN point
   WHEN 'GeographicItem::LineString' THEN line_string
   WHEN 'GeographicItem::Polygon' THEN polygon
   WHEN 'GeographicItem::MultiLineString' THEN multi_line_string
   WHEN 'GeographicItem::MultiPoint' THEN multi_point
   WHEN 'GeographicItem::GeometryCollection' THEN geometry_collection
   END".freeze

   # ANTI_MERIDIAN = '0X0102000020E61000000200000000000000008066400000000000405640000000000080664000000000004056C0'
 ANTI_MERIDIAN = 'LINESTRING (180 89.0, 180 -89)'.freeze

 has_many :cached_map_items, inverse_of: :geographic_item

 has_many :geographic_areas_geographic_items, dependent: :destroy, inverse_of: :geographic_item
 has_many :geographic_areas, through: :geographic_areas_geographic_items
 has_many :asserted_distributions, through: :geographic_areas
 has_many :geographic_area_types, through: :geographic_areas
 has_many :parent_geographic_areas, through: :geographic_areas, source: :parent

 has_many :georeferences, inverse_of: :geographic_item
 has_many :georeferences_through_error_geographic_item,
   class_name: 'Georeference', foreign_key: :error_geographic_item_id, inverse_of: :error_geographic_item
 has_many :collecting_events_through_georeferences, through: :georeferences, source: :collecting_event
 has_many :collecting_events_through_georeference_error_geographic_item,
   through: :georeferences_through_error_geographic_item, source: :collecting_event

   # TODO: THIS IS NOT GOOD
 before_validation :set_type_if_geography_present

 validate :some_data_is_provided
 validates :type, presence: true

 scope :include_collecting_event, -> { includes(:collecting_events_through_georeferences) }
 scope :geo_with_collecting_event, -> { joins(:collecting_events_through_georeferences) }
 scope :err_with_collecting_event, -> { joins(:georeferences_through_error_geographic_item) }

 after_save :set_cached, unless: Proc.new {|n| n.no_cached || errors.any? }

 class << self

   def aliased_geographic_sql(name = 'a')
     "CASE #{name}.type \
        WHEN 'GeographicItem::MultiPolygon' THEN #{name}.multi_polygon \
        WHEN 'GeographicItem::Point' THEN #{name}.point \
        WHEN 'GeographicItem::LineString' THEN #{name}.line_string \
        WHEN 'GeographicItem::Polygon' THEN #{name}.polygon \
        WHEN 'GeographicItem::MultiLineString' THEN #{name}.multi_line_string \
        WHEN 'GeographicItem::MultiPoint' THEN #{name}.multi_point \
        WHEN 'GeographicItem::GeometryCollection' THEN #{name}.geometry_collection \
        END"
   end

   def st_union(geographic_item_scope)
     GeographicItem.select("ST_Union(#{GeographicItem::GEOMETRY_SQL.to_sql}) as collection")
       .where(id: geographic_item_scope.pluck(:id))
   end

   def st_collect(geographic_item_scope)
     GeographicItem.select("ST_Collect(#{GeographicItem::GEOMETRY_SQL.to_sql}) as collection")
       .where(id: geographic_item_scope.pluck(:id))
   end

   # @return [GeographicItem::ActiveRecord_Relation]
   # @params [Array] array of geographic area ids
   def default_by_geographic_area_ids(geographic_area_ids = [])
     GeographicItem.
       joins(:geographic_areas_geographic_items).
       merge(::GeographicAreasGeographicItem.default_geographic_item_data).
       where(geographic_areas_geographic_items: {geographic_area_id: geographic_area_ids})
   end

   # @param [String] wkt
   # @return [Boolean]
   #   whether or not the wtk intersects with the anti-meridian
   #   !! StrongParams security considerations
   def crosses_anti_meridian?(wkt)
     GeographicItem.find_by_sql(
       ['SELECT ST_Intersects(ST_GeogFromText(?), ST_GeogFromText(?)) as r;', wkt, ANTI_MERIDIAN]
     ).first.r
   end

   # @param [Integer] ids
   # @return [Boolean]
   #   whether or not any GeographicItem passed intersects the anti-meridian
   #   !! StrongParams security considerations
   #   This is our first line of defense against queries that define multiple shapes, one or
   #   more of which crosses the anti-meridian.  In this case the current TW strategy within the
   #   UI is to abandon the search, and prompt the user to refactor the query.
   def crosses_anti_meridian_by_id?(*ids)
     q1 = ["SELECT ST_Intersects((SELECT single_geometry FROM (#{GeographicItem.single_geometry_sql(*ids)}) as " \
           'left_intersect), ST_GeogFromText(?)) as r;', ANTI_MERIDIAN]
     _q2 = ActiveRecord::Base.send(:sanitize_sql_array, ['SELECT ST_Intersects((SELECT single_geometry FROM (?) as ' \
                                                         'left_intersect), ST_GeogFromText(?)) as r;', GeographicItem.single_geometry_sql(*ids), ANTI_MERIDIAN])
     GeographicItem.find_by_sql(q1).first.r
   end

   #
   # SQL fragments
   #

   # @param [Integer, String]
   # @return [String]
   #   a SQL select statement that returns the *geometry* for the geographic_item with the specified id
   def select_geometry_sql(geographic_item_id)
     "SELECT #{GeographicItem::GEOMETRY_SQL.to_sql} from geographic_items where geographic_items.id = #{geographic_item_id}"
   end

   # @param [Integer, String]
   # @return [String]
   #   a SQL select statement that returns the geography for the geographic_item with the specified id
   def select_geography_sql(geographic_item_id)
     ActiveRecord::Base.send(:sanitize_sql_for_conditions, [
       "SELECT #{GeographicItem::GEOMETRY_SQL.to_sql} from geographic_items where geographic_items.id = ?",
       geographic_item_id])
   end

   # @param [Symbol] choice
   # @return [String]
   #   a fragment returning either latitude or longitude columns
   def lat_long_sql(choice)
     return nil unless [:latitude, :longitude].include?(choice)
     f = "'D.DDDDDD'" # TODO: probably a constant somewhere
     v = (choice == :latitude ? 1 : 2)
     "CASE type
     WHEN 'GeographicItem::GeometryCollection' THEN split_part(ST_AsLatLonText(ST_Centroid" \
   "(geometry_collection::geometry), #{f}), ' ', #{v})
     WHEN 'GeographicItem::LineString' THEN split_part(ST_AsLatLonText(ST_Centroid(line_string::geometry), " \
   "#{f}), ' ', #{v})
     WHEN 'GeographicItem::MultiPolygon' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(multi_polygon::geometry), #{f}), ' ', #{v})
     WHEN 'GeographicItem::Point' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(point::geometry), #{f}), ' ', #{v})
     WHEN 'GeographicItem::Polygon' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(polygon::geometry), #{f}), ' ', #{v})
     WHEN 'GeographicItem::MultiLineString' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(multi_line_string::geometry), #{f} ), ' ', #{v})
     WHEN 'GeographicItem::MultiPoint' THEN split_part(ST_AsLatLonText(" \
   "ST_Centroid(multi_point::geometry), #{f}), ' ', #{v})
   END as #{choice}"
   end

   # @param [Integer] geographic_item_id
   # @param [Integer] distance
   # @return [String]
   def within_radius_of_item_sql(geographic_item_id, distance)
     "ST_DWithin((#{GeographicItem::GEOGRAPHY_SQL}), (#{select_geography_sql(geographic_item_id)}), #{distance})"
   end

   # TODO: 3D is overkill here
   # @param [String] wkt
   # @param [Integer] distance (meters)
   # @return [String]
   # !! This is intersecting
   def intersecting_radius_of_wkt_sql(wkt, distance)
     "ST_DWithin((#{GeographicItem::GEOGRAPHY_SQL}), ST_Transform( ST_GeomFromText('#{wkt}', " \
       "4326), 4326), #{distance})"
   end

   # @param [String] wkt
   # @param [Integer] distance (meters)
   # @return [String]
   # !! This is fully covering
   def within_radius_of_wkt_sql(wkt, distance)
     "ST_Covers( ST_Buffer(ST_SetSRID( ST_GeomFromText('#{wkt}'), 4326)::geography, #{distance}), (#{GeographicItem::GEOGRAPHY_SQL}))"
   end

   # @param [String, Integer, String]
   # @return [String]
   #   a SQL fragment for ST_Contains() function, returns
   #   all geographic items which are contained in the item supplied
   def containing_sql(target_column_name = nil, geographic_item_id = nil, source_column_name = nil)
     return 'false' if geographic_item_id.nil? || source_column_name.nil? || target_column_name.nil?
     "ST_Contains(#{target_column_name}::geometry, (#{geometry_sql(geographic_item_id, source_column_name)}))"
   end

   # @param [String, Integer, String]
   # @return [String]
   #   a SQL fragment for ST_Contains(), returns
   #   all geographic_items which contain the supplied geographic_item
   def reverse_containing_sql(target_column_name = nil, geographic_item_id = nil, source_column_name = nil)
     return 'false' if geographic_item_id.nil? || source_column_name.nil? || target_column_name.nil?
     "ST_Contains((#{geometry_sql(geographic_item_id, source_column_name)}), #{target_column_name}::geometry)"
   end

   # @param [Integer, String]
   # @return [String]
   #   a SQL fragment that represents the geometry of the geographic item specified (which has data in the
   # source_column_name, i.e. geo_object_type)
   def geometry_sql(geographic_item_id = nil, source_column_name = nil)
     return 'false' if geographic_item_id.nil? || source_column_name.nil?
     "select geom_alias_tbl.#{source_column_name}::geometry from geographic_items geom_alias_tbl " \
       "where geom_alias_tbl.id = #{geographic_item_id}"
   end

   # rubocop:disable Metrics/MethodLength
   # @param [String] column_name
   # @param [GeographicItem] geographic_item
   # @return [String] of SQL
   def is_contained_by_sql(column_name, geographic_item)
     geo_id = geographic_item.id
     geo_type = geographic_item.geo_object_type
     template = '(ST_Contains((select geographic_items.%s::geometry from geographic_items where ' \
       'geographic_items.id = %d), %s::geometry))'
     retval = []
     column_name.downcase!
     case column_name
     when 'any'
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           retval.push(template % [geo_type, geo_id, column])
         end
       }
     when 'any_poly', 'any_line'
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           if column.to_s.index(column_name.gsub('any_', ''))
             retval.push(template % [geo_type, geo_id, column])
           end
         end
       }
     else
       retval = template % [geo_type, geo_id, column_name]
     end
     retval = retval.join(' OR ') if retval.instance_of?(Array)
     retval
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   #   a select query that returns a single geometry (column name 'single_geometry' for the collection of ids
   # provided via ST_Collect)
   def st_collect_sql(*geographic_item_ids)
     geographic_item_ids.flatten!
     ActiveRecord::Base.send(:sanitize_sql_for_conditions, [
       "SELECT ST_Collect(f.the_geom) AS single_geometry
    FROM (
       SELECT (ST_DUMP(#{GeographicItem::GEOMETRY_SQL.to_sql})).geom as the_geom
       FROM geographic_items
       WHERE id in (?))
     AS f", geographic_item_ids])
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   #    returns one or more geographic items combined as a single geometry in column 'single'
   def single_geometry_sql(*geographic_item_ids)
     a = GeographicItem.st_collect_sql(geographic_item_ids)
     '(SELECT single.single_geometry FROM (' + a + ' ) AS single)'
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   #   returns a single geometry "column" (paren wrapped) as "single" for multiple geographic item ids, or the
   # geometry as 'geometry' for a single id
   def geometry_sql2(*geographic_item_ids)
     geographic_item_ids.flatten! # *ALWAYS* reduce the pile to a single level of ids
     if geographic_item_ids.count == 1
       "(#{GeographicItem.geometry_for_sql(geographic_item_ids.first)})"
     else
       GeographicItem.single_geometry_sql(geographic_item_ids)
     end
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   def containing_where_sql(*geographic_item_ids)
     "ST_CoveredBy(
   #{GeographicItem.geometry_sql2(*geographic_item_ids)},
    CASE geographic_items.type
      WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon::geometry
      WHEN 'GeographicItem::Point' THEN point::geometry
      WHEN 'GeographicItem::LineString' THEN line_string::geometry
      WHEN 'GeographicItem::Polygon' THEN polygon::geometry
      WHEN 'GeographicItem::MultiLineString' THEN multi_line_string::geometry
      WHEN 'GeographicItem::MultiPoint' THEN multi_point::geometry
   END)"
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String]
   def containing_where_sql_geog(*geographic_item_ids)
     "ST_CoveredBy(
   #{GeographicItem.geometry_sql2(*geographic_item_ids)},
    CASE geographic_items.type
      WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon::geography
      WHEN 'GeographicItem::Point' THEN point::geography
      WHEN 'GeographicItem::LineString' THEN line_string::geography
      WHEN 'GeographicItem::Polygon' THEN polygon::geography
      WHEN 'GeographicItem::MultiLineString' THEN multi_line_string::geography
      WHEN 'GeographicItem::MultiPoint' THEN multi_point::geography
   END)"
   end

   # @param [Interger, Array of Integer] ids
   # @return [Array]
   #   If we detect that some query id has crossed the meridian, then loop through
   #   and "manually" build up a list of results.
   #   Should only be used if GeographicItem.crosses_anti_meridian_by_id? is true.
   #   Note that this does not return a Scope, so you can't chain it like contained_by?
   # TODO: test this
   def contained_by_with_antimeridian_check(*ids)
     ids.flatten! # make sure there is only one level of splat (*)
     results = []

     crossing_ids = []

     ids.each do |id|
       # push each which crosses
       crossing_ids.push(id) if GeographicItem.crosses_anti_meridian_by_id?(id)
     end

     non_crossing_ids = ids - crossing_ids
     results.push GeographicItem.contained_by(non_crossing_ids).to_a if non_crossing_ids.any?

     crossing_ids.each do |id|
       # [61666, 61661, 61659, 61654, 61639]
       q1 = ActiveRecord::Base.send(:sanitize_sql_array, ['SELECT ST_AsText((SELECT polygon FROM geographic_items ' \
                                                          'WHERE id = ?))', id])
       r = GeographicItem.where(
         # GeographicItem.contained_by_wkt_shifted_sql(GeographicItem.find(id).geo_object.to_s)
         GeographicItem.contained_by_wkt_shifted_sql(
           ApplicationRecord.connection.execute(q1).first['st_astext'])
       ).to_a
       results.push(r)
     end

     results.flatten.uniq
   end

   # @params [String] well known text
   # @return [String] the SQL fragment for the specific geometry type, shifted by longitude
   # Note: this routine is called when it is already known that the A argument crosses anti-meridian
   def contained_by_wkt_shifted_sql(wkt)
     "ST_Contains(ST_ShiftLongitude(ST_GeomFromText('#{wkt}', 4326)), (
       CASE geographic_items.type
          WHEN 'GeographicItem::MultiPolygon' THEN ST_ShiftLongitude(multi_polygon::geometry)
          WHEN 'GeographicItem::Point' THEN ST_ShiftLongitude(point::geometry)
          WHEN 'GeographicItem::LineString' THEN ST_ShiftLongitude(line_string::geometry)
          WHEN 'GeographicItem::Polygon' THEN ST_ShiftLongitude(polygon::geometry)
          WHEN 'GeographicItem::MultiLineString' THEN ST_ShiftLongitude(multi_line_string::geometry)
          WHEN 'GeographicItem::MultiPoint' THEN ST_ShiftLongitude(multi_point::geometry)
       END
       )
     )"
   end

   # TODO: Remove the hard coded 4326 reference
   # @params [String] wkt
   # @return [String] SQL fragment limiting geographics items to those in this WKT
   def contained_by_wkt_sql(wkt)
     if crosses_anti_meridian?(wkt)
       retval = contained_by_wkt_shifted_sql(wkt)
     else
       retval = "ST_Contains(ST_GeomFromText('#{wkt}', 4326), (
       CASE geographic_items.type
          WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon::geometry
          WHEN 'GeographicItem::Point' THEN point::geometry
          WHEN 'GeographicItem::LineString' THEN line_string::geometry
          WHEN 'GeographicItem::Polygon' THEN polygon::geometry
          WHEN 'GeographicItem::MultiLineString' THEN multi_line_string::geometry
          WHEN 'GeographicItem::MultiPoint' THEN multi_point::geometry
       END
       )
     )"
     end
     retval
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String] sql for contained_by via ST_ContainsProperly
   # Note: Can not use GEOMETRY_SQL because geometry_collection is not supported in ST_ContainsProperly
   # Note: !! If the target GeographicItem#id crosses the anti-meridian then you may/will get unexpected results.
   def contained_by_where_sql(*geographic_item_ids)
     "ST_Contains(
       #{GeographicItem.geometry_sql2(*geographic_item_ids)},
       CASE geographic_items.type
         WHEN 'GeographicItem::MultiPolygon' THEN multi_polygon::geometry
         WHEN 'GeographicItem::Point' THEN point::geometry
         WHEN 'GeographicItem::LineString' THEN line_string::geometry
         WHEN 'GeographicItem::Polygon' THEN polygon::geometry
         WHEN 'GeographicItem::MultiLineString' THEN multi_line_string::geometry
         WHEN 'GeographicItem::MultiPoint' THEN multi_point::geometry
       END)"
   end

   # @param [RGeo:Point] rgeo_point
   # @return [String] sql for containing via ST_CoveredBy
   # TODO: Remove the hard coded 4326 reference
   # TODO: should this be wkt_point instead of rgeo_point?
   def containing_where_for_point_sql(rgeo_point)
     "ST_CoveredBy(
     ST_GeomFromText('#{rgeo_point}', 4326),
     #{GeographicItem::GEOMETRY_SQL.to_sql}
    )"
   end

   # @param [Interger] geographic_item_id
   # @return [String] SQL for geometries
   # example, not used
   def geometry_for_sql(geographic_item_id)
     'SELECT ' + GeographicItem::GEOMETRY_SQL.to_sql + ' AS geometry FROM geographic_items WHERE id = ' \
       "#{geographic_item_id} LIMIT 1"
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [String] SQL for geometries
   # example, not used
   def geometry_for_collection_sql(*geographic_item_ids)
     'SELECT ' + GeographicItem::GEOMETRY_SQL.to_sql + ' AS geometry FROM geographic_items WHERE id IN ' \
       "( #{geographic_item_ids.join(',')} )"
   end

   #
   # Scopes
   #

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [Scope]
   #    the geographic items containing these collective geographic_item ids, not including self
   def containing(*geographic_item_ids)
     where(GeographicItem.containing_where_sql(geographic_item_ids)).not_ids(*geographic_item_ids)
   end

   # @param [Interger, Array of Integer] geographic_item_ids
   # @return [Scope]
   #    the geographic items contained by any of these geographic_item ids, not including self
   # (works via ST_ContainsProperly)
   def contained_by(*geographic_item_ids)
     where(GeographicItem.contained_by_where_sql(geographic_item_ids))
   end

   # @param [RGeo::Point] rgeo_point
   # @return [Scope]
   #    the geographic items containing this point
   # TODO: should be containing_wkt ?
   def containing_point(rgeo_point)
     where(GeographicItem.containing_where_for_point_sql(rgeo_point))
   end

   # @return [Scope]
   #   adds an area_in_meters field, with meters
   def with_area
     select("ST_Area(#{GeographicItem::GEOGRAPHY_SQL}, false) as area_in_meters")
   end

   # return [Scope]
   #   A scope that limits the result to those GeographicItems that have a collecting event
   #   through either the geographic_item or the error_geographic_item
   #
   # A raw SQL join approach for comparison
   #
   # GeographicItem.joins('LEFT JOIN georeferences g1 ON geographic_items.id = g1.geographic_item_id').
   #   joins('LEFT JOIN georeferences g2 ON geographic_items.id = g2.error_geographic_item_id').
   #   where("(g1.geographic_item_id IS NOT NULL OR g2.error_geographic_item_id IS NOT NULL)").uniq

   # @return [Scope] GeographicItem
   # This uses an Arel table approach, this is ultimately more decomposable if we need. Of use:
   #  http://danshultz.github.io/talks/mastering_activerecord_arel  <- best
   #  https://github.com/rails/arel
   #  http://stackoverflow.com/questions/4500629/use-arel-for-a-nested-set-join-query-and-convert-to-activerecordrelation
   #  http://rdoc.info/github/rails/arel/Arel/SelectManager
   #  http://stackoverflow.com/questions/7976358/activerecord-arel-or-condition
   #
   def with_collecting_event_through_georeferences
     geographic_items = GeographicItem.arel_table
     georeferences = Georeference.arel_table
     g1 = georeferences.alias('a')
     g2 = georeferences.alias('b')

     c = geographic_items.join(g1, Arel::Nodes::OuterJoin).on(geographic_items[:id].eq(g1[:geographic_item_id]))
       .join(g2, Arel::Nodes::OuterJoin).on(geographic_items[:id].eq(g2[:error_geographic_item_id]))

     GeographicItem.joins(# turn the Arel back into scope
                          c.join_sources # translate the Arel join to a join hash(?)
                         ).where(
                           g1[:id].not_eq(nil).or(g2[:id].not_eq(nil)) # returns a Arel::Nodes::Grouping
                         ).distinct
   end

   # @return [Scope] include a 'latitude' column
   def with_latitude
     select(lat_long_sql(:latitude))
   end

   # @return [Scope] include a 'longitude' column
   def with_longitude
     select(lat_long_sql(:longitude))
   end

   # @param [String, GeographicItems]
   # @return [Scope]
   def intersecting(column_name, *geographic_items)
     if column_name.downcase == 'any'
       pieces = []
       DATA_TYPES.each { |column|
         pieces.push(GeographicItem.intersecting(column.to_s, geographic_items).to_a)
       }

       # @TODO change 'id in (?)' to some other sql construct

       GeographicItem.where(id: pieces.flatten.map(&:id))
     else
       q = geographic_items.flatten.collect { |geographic_item|
         "ST_Intersects(#{column_name}, '#{geographic_item.geo_object}'    )" # seems like we want this: http://danshultz.github.io/talks/mastering_activerecord_arel/#/15/2
       }.join(' or ')

       where(q)
     end
   end

   # @param [GeographicItem#id] geographic_item_id
   # @param [Float] distance in meters ?!?!
   # @return [ActiveRecord::Relation]
   # !! should be distance, not radius?!
   def within_radius_of_item(geographic_item_id, distance)
     where(within_radius_of_item_sql(geographic_item_id, distance))
   end

   # @param [String, GeographicItem]
   # @return [Scope]
   #   a SQL fragment for ST_DISJOINT, specifies all geographic_items that have data in column_name
   #   that are disjoint from the passed geographic_items
   def disjoint_from(column_name, *geographic_items)
     q = geographic_items.flatten.collect { |geographic_item|
       "ST_DISJOINT(#{column_name}::geometry, (#{geometry_sql(geographic_item.to_param,
                                                            geographic_item.geo_object_type)}))"
     }.join(' and ')

     where(q)
   end

   # @return [Scope]
   #   see are_contained_in_item_by_id
   # @param [String] column_name
   # @param [GeographicItem, Array] geographic_items
   def are_contained_in_item(column_name, *geographic_items)
     are_contained_in_item_by_id(column_name, geographic_items.flatten.map(&:id))
   end

   # rubocop:disable Metrics/MethodLength
   # @param [String] column_name to search
   # @param [GeographicItem] geographic_item_ids or array of geographic_item_ids to be tested.
   # @return [Scope] of GeographicItems
   #
   # If this scope is given an Array of GeographicItems as a second parameter,
   # it will return the 'OR' of each of the objects against the table.
   # SELECT COUNT(*) FROM "geographic_items"
   #        WHERE (ST_Contains(polygon::geometry, GeomFromEWKT('srid=4326;POINT (0.0 0.0 0.0)'))
   #               OR ST_Contains(polygon::geometry, GeomFromEWKT('srid=4326;POINT (-9.8 5.0 0.0)')))
   #
   def are_contained_in_item_by_id(column_name, *geographic_item_ids) # = containing
     geographic_item_ids.flatten! # in case there is a array of arrays, or multiple objects
     column_name.downcase!
     case column_name
     when 'any'
       part = []
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           part.push(GeographicItem.are_contained_in_item_by_id(column.to_s, geographic_item_ids).to_a)
         end
       }
       # TODO: change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten.map(&:id))
     when 'any_poly', 'any_line'
       part = []
       DATA_TYPES.each { |column|
         if column.to_s.index(column_name.gsub('any_', ''))
           part.push(GeographicItem.are_contained_in_item_by_id("#{column}", geographic_item_ids).to_a)
         end
       }
       # TODO: change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten.map(&:id))
     else
       q = geographic_item_ids.flatten.collect { |geographic_item_id|
         # discover the item types, and convert type to database type for 'multi_'
         b = GeographicItem.where(id: geographic_item_id)
           .pluck(:type)[0].split(':')[2].downcase.gsub('lti', 'lti_')
         # a = GeographicItem.find(geographic_item_id).geo_object_type
         GeographicItem.containing_sql(column_name, geographic_item_id, b)
       }.join(' or ')
       q = 'FALSE' if q.blank? # this will prevent the invocation of *ALL* of the GeographicItems, if there are
       # no GeographicItems in the request (see CollectingEvent.name_hash(types)).
       where(q) # .not_including(geographic_items)
     end
   end

   # rubocop:enable Metrics/MethodLength

   # @param [String] column_name
   # @param [String] geometry of WKT
   # @return [Scope]
   # a single WKT geometry is compared against column or columns (except geometry_collection) to find geographic_items
   # which are contained in the WKT
   def are_contained_in_wkt(column_name, geometry)
     column_name.downcase!
     # column_name = 'point'
     case column_name
     when 'any'
       part = []
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           part.push(GeographicItem.are_contained_in_wkt(column.to_s, geometry).pluck(:id).to_a)
         end
       }
       # TODO: change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten)
     when 'any_poly', 'any_line'
       part = []
       DATA_TYPES.each { |column|
         if column.to_s.index(column_name.gsub('any_', ''))
           part.push(GeographicItem.are_contained_in_wkt("#{column}", geometry).pluck(:id).to_a)
         end
       }
       # TODO: change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten)
     else
       # column = points, geometry = square
       q = "ST_Contains(ST_GeomFromEWKT('srid=4326;#{geometry}'), #{column_name}::geometry)"
       where(q) # .not_including(geographic_items)
     end
   end

   # rubocop:disable Metrics/MethodLength
   #    containing the items the shape of which is contained in the geographic_item[s] supplied.
   # @param column_name [String] can be any of DATA_TYPES, or 'any' to check against all types, 'any_poly' to check
   # against 'polygon' or 'multi_polygon', or 'any_line' to check against 'line_string' or 'multi_line_string'.
   #  CANNOT be 'geometry_collection'.
   # @param geographic_items [GeographicItem] Can be a single GeographicItem, or an array of GeographicItem.
   # @return [Scope]
   def is_contained_by(column_name, *geographic_items)
     column_name.downcase!
     case column_name
     when 'any'
       part = []
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           part.push(GeographicItem.is_contained_by(column.to_s, geographic_items).to_a)
         end
       }
       # @TODO change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten.map(&:id))

     when 'any_poly', 'any_line'
       part = []
       DATA_TYPES.each { |column|
         unless column == :geometry_collection
           if column.to_s.index(column_name.gsub('any_', ''))
             part.push(GeographicItem.is_contained_by(column.to_s, geographic_items).to_a)
           end
         end
       }
       # @TODO change 'id in (?)' to some other sql construct
       GeographicItem.where(id: part.flatten.map(&:id))

     else
       q = geographic_items.flatten.collect { |geographic_item|
         GeographicItem.reverse_containing_sql(column_name, geographic_item.to_param,
                                               geographic_item.geo_object_type)
       }.join(' or ')
       where(q) # .not_including(geographic_items)
     end
   end

   # rubocop:enable Metrics/MethodLength

   # @param [String, GeographicItem]
   # @return [Scope]
   def ordered_by_shortest_distance_from(column_name, geographic_item)
     if true # check_geo_params(column_name, geographic_item)
       select_distance_with_geo_object(column_name, geographic_item)
         .where_distance_greater_than_zero(column_name, geographic_item).order('distance')
     else
       where('false')
     end
   end

   # @param [String, GeographicItem]
   # @return [Scope]
   def ordered_by_longest_distance_from(column_name, geographic_item)
     if true # check_geo_params(column_name, geographic_item)
       q = select_distance_with_geo_object(column_name, geographic_item)
         .where_distance_greater_than_zero(column_name, geographic_item)
         .order('distance desc')
       q
     else
       where('false')
     end
   end

   # @param [String] column_name
   # @param [GeographicItem] geographic_item
   # @return [String]
   def select_distance_with_geo_object(column_name, geographic_item)
     select("*, ST_Distance(#{column_name}, GeomFromEWKT('srid=4326;#{geographic_item.geo_object}')) as distance")
   end

   # @param [String, GeographicItem]
   # @return [Scope]
   def where_distance_greater_than_zero(column_name, geographic_item)
     where("#{column_name} is not null and ST_Distance(#{column_name}, " \
           "GeomFromEWKT('srid=4326;#{geographic_item.geo_object}')) > 0")
   end

   # @param [GeographicItem]
   # @return [Scope]
   def not_including(geographic_items)
     where.not(id: geographic_items)
   end

   # @return [Scope]
   #   includes an 'is_valid' attribute (True/False) for the passed geographic_item.  Uses St_IsValid.
   # @param [RGeo object] geographic_item
   def with_is_valid_geometry_column(geographic_item)
     where(id: geographic_item.id).select("ST_IsValid(ST_AsBinary(#{geographic_item.geo_object_type})) is_valid")
   end

   #
   # Other
   #

   # @param [Integer] geographic_item_id1
   # @param [Integer] geographic_item_id2
   # @return [Float]
   def distance_between(geographic_item_id1, geographic_item_id2)
     q1 = "ST_Distance(#{GeographicItem::GEOGRAPHY_SQL}, " \
       "(#{select_geography_sql(geographic_item_id2)})) as distance"
     _q2 = ActiveRecord::Base.send(
       :sanitize_sql_array, ['ST_Distance(?, (?)) as distance',
                             GeographicItem::GEOGRAPHY_SQL,
                             select_geography_sql(geographic_item_id2)])
     GeographicItem.where(id: geographic_item_id1).pluck(Arel.sql(q1)).first
   end

   # @param [RGeo::Point] point
   # @return [Hash]
   #   as per #inferred_geographic_name_hierarchy but for Rgeo point
   def point_inferred_geographic_name_hierarchy(point)
     GeographicItem.containing_point(point).order(cached_total_area: :ASC).limit(1).first&.inferred_geographic_name_hierarchy
   end

   # @param [String] type_name ('polygon', 'point', 'line' [, 'circle'])
   # @return [String] if type
   def eval_for_type(type_name)
     retval = 'GeographicItem'
     case type_name.upcase
     when 'POLYGON'
       retval += '::Polygon'
     when 'LINESTRING'
       retval += '::LineString'
     when 'POINT'
       retval += '::Point'
     when 'MULTIPOLYGON'
       retval += '::MultiPolygon'
     when 'MULTILINESTRING'
       retval += '::MultiLineString'
     when 'MULTIPOINT'
       retval += '::MultiPoint'
     else
       retval = nil
     end
     retval
   end

   # example, not used
   # @param [Integer] geographic_item_id
   # @return [RGeo::Geographic object]
   def geometry_for(geographic_item_id)
     GeographicItem.select(GeographicItem::GEOMETRY_SQL.to_sql + ' AS geometry').find(geographic_item_id)['geometry']
   end

   # example, not used
   # @param [Integer, Array] geographic_item_ids
   # @return [Scope]
   def st_multi(*geographic_item_ids)
     GeographicItem.find_by_sql(
       "SELECT ST_Multi(ST_Collect(g.the_geom)) AS singlegeom
    FROM (
       SELECT (ST_DUMP(#{GeographicItem::GEOMETRY_SQL.to_sql})).geom AS the_geom
       FROM geographic_items
       WHERE id IN (?))
     AS g;