Class: AggregateFeature

Inherits:
AbstractFeature show all
Defined in:
app/models/aggregate_feature.rb

Constant Summary

Constants inherited from AbstractFeature

AbstractFeature::FEATURE_TYPES

Instance Method Summary collapse

Methods inherited from AbstractFeature

area_in_square_meters, cache_derivatives, #cache_derivatives, cache_key, #envelope, #feature_bounds, #geojson, geojson, intersecting, invalid, #kml, lines, #make_valid?, points, polygons, total_intersection_area_in_square_meters, valid, with_metadata

Instance Method Details

#refreshObject

Aggregate the features for the spatial model into a single feature



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/aggregate_feature.rb', line 7

def refresh
  feature_array_sql = <<~SQL
    ARRAY[
      (#{features.select('ST_Multi(ST_Union(ST_CollectionExtract(geog::geometry, 1)))').to_sql}),
      (#{features.select('ST_Multi(ST_Union(ST_CollectionExtract(geog::geometry, 2)))').to_sql}),
      (#{features.select('ST_Multi(ST_Union(ST_CollectionExtract(geog::geometry, 3)))').to_sql})
    ]
  SQL

  # Remove empty features so ST_COLLECT doesn't choke. This seems to be a difference between PostGIS 2.x and 3.x
  compacted_feature_array_sql = <<~SQL
    array_remove(
      array_remove(
        array_remove(#{feature_array_sql},
        ST_GeomFromText('MultiPoint EMPTY')),
      ST_GeomFromText('MultiLinestring EMPTY')),
    ST_GeomFromText('MultiPolygon EMPTY'))
  SQL

  self.geog = ActiveRecord::Base.connection.select_value <<~SQL
    SELECT ST_Collect(#{compacted_feature_array_sql})::geography
  SQL
  self.save!
end