Sha256: c68f4dc7bdccbf86e101abb7677c8f1410eb32a4af179acc433259b89dcf8503

Contents?: true

Size: 1.09 KB

Versions: 21

Compression:

Stored size: 1.09 KB

Contents

require_dependency SpatialFeatures::Engine.root.join('app/models/abstract_feature')

class AggregateFeature < AbstractFeature
  has_many :features, lambda { |aggregate| where(:spatial_model_type => aggregate.spatial_model_type) }, :foreign_key => :spatial_model_id, :primary_key => :spatial_model_id

  # Aggregate the features for the spatial model into a single feature
  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
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
spatial_features-2.17.2 app/models/aggregate_feature.rb
spatial_features-2.17.1 app/models/aggregate_feature.rb
spatial_features-2.17.0 app/models/aggregate_feature.rb
spatial_features-2.16.0 app/models/aggregate_feature.rb
spatial_features-2.15.1 app/models/aggregate_feature.rb
spatial_features-2.15.0 app/models/aggregate_feature.rb
spatial_features-2.14.1 app/models/aggregate_feature.rb
spatial_features-2.14.0 app/models/aggregate_feature.rb
spatial_features-2.13.0 app/models/aggregate_feature.rb
spatial_features-2.12.3 app/models/aggregate_feature.rb
spatial_features-2.12.2 app/models/aggregate_feature.rb
spatial_features-2.12.1 app/models/aggregate_feature.rb
spatial_features-2.12.0 app/models/aggregate_feature.rb
spatial_features-2.11.1 app/models/aggregate_feature.rb
spatial_features-2.11.0 app/models/aggregate_feature.rb
spatial_features-2.10.9 app/models/aggregate_feature.rb
spatial_features-2.10.8 app/models/aggregate_feature.rb
spatial_features-2.10.6 app/models/aggregate_feature.rb
spatial_features-2.10.5 app/models/aggregate_feature.rb
spatial_features-2.10.4 app/models/aggregate_feature.rb