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, within_distance, #wkt

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
# File 'app/models/aggregate_feature.rb', line 7

def refresh
  feature_array_sql = <<~SQL
    ARRAY[
      (#{features.select('ST_UNION(ST_CollectionExtract(geog::geometry, 1))').to_sql}),
      (#{features.select('ST_UNION(ST_CollectionExtract(geog::geometry, 2))').to_sql}),
      (#{features.select('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
  self.geog = ActiveRecord::Base.connection.select_value <<~SQL
    SELECT COALESCE(ST_Collect(unnest)::geography, ST_GeogFromText('MULTIPOLYGON EMPTY'))
    FROM (SELECT unnest(#{feature_array_sql})) AS features
    WHERE NOT ST_IsEmpty(unnest)
  SQL
  self.save!
end