Class: Feature

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

Constant Summary

Constants inherited from AbstractFeature

AbstractFeature::FEATURE_TYPES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractFeature

area_in_square_meters, #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

Class Method Details

.cache_derivatives(options = {}) ⇒ Object

Features are used for display so we also cache their KML representation



54
55
56
57
58
59
60
61
# File 'app/models/feature.rb', line 54

def self.cache_derivatives(options = {})
  super
  update_all <<-SQL.squish
    kml          = ST_AsKML(geog, 6),
    kml_lowres   = ST_AsKML(geom_lowres, #{options.fetch(:lowres_precision, lowres_precision)}),
    kml_centroid = ST_AsKML(centroid)
  SQL
end

.defer_aggregate_refresh(&block) ⇒ Object



14
15
16
17
18
19
20
21
# File 'app/models/feature.rb', line 14

def self.defer_aggregate_refresh(&block)
  start_at = Feature.maximum(:id).to_i + 1
  output = without_aggregate_refresh(&block)

  where(:id => start_at..Float::INFINITY).refresh_aggregates

  return output
end

.refresh_aggregatesObject



31
32
33
34
35
36
37
38
39
# File 'app/models/feature.rb', line 31

def self.refresh_aggregates
  # Find one feature from each spatial model and trigger the aggregate feature refresh
  ids = select('MAX(id)')
          .where.not(:spatial_model_type => nil, :spatial_model_id => nil)
          .group('spatial_model_type, spatial_model_id')

  # Unscope so that newly built AggregateFeatures get their type column set correctly
  AbstractFeature.unscoped { where(:id => ids).find_each(&:refresh_aggregate) }
end

.without_aggregate_refreshObject



23
24
25
26
27
28
29
# File 'app/models/feature.rb', line 23

def self.without_aggregate_refresh
  old = Feature.automatically_refresh_aggregate
  Feature.automatically_refresh_aggregate = false
  yield
ensure
  Feature.automatically_refresh_aggregate = old
end

Instance Method Details

#automatically_refresh_aggregate?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'app/models/feature.rb', line 46

def automatically_refresh_aggregate?
  # Check if there is a spatial model id because nothing prevents is from creating a Feature without one. Depending on
  # how you assign a feature to a record, you may end up saving it before assigning it to a record, thereby leaving
  # this field blank.
  spatial_model_id? && automatically_refresh_aggregate && saved_change_to_geog?
end

#refresh_aggregateObject



41
42
43
44
# File 'app/models/feature.rb', line 41

def refresh_aggregate
  build_aggregate_feature unless aggregate_feature&.persisted?
  aggregate_feature.refresh
end