Module: SpatialFeatures::ClassMethods

Defined in:
lib/spatial_features/has_spatial_features.rb

Instance Method Summary collapse

Instance Method Details

#acts_like_spatial_features?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/spatial_features/has_spatial_features.rb', line 47

def acts_like_spatial_features?
  true
end

#aggregate_featuresObject



96
97
98
99
100
101
102
103
# File 'lib/spatial_features/has_spatial_features.rb', line 96

def aggregate_features
  type = base_class.to_s # Rails stores polymorphic foreign keys as the base class
  if all == unscoped
    AggregateFeature.where(:spatial_model_type => type)
  else
    AggregateFeature.where(:spatial_model_type => type, :spatial_model_id => all.unscope(:select))
  end
end

#area_in_square_metersObject



115
116
117
# File 'lib/spatial_features/has_spatial_features.rb', line 115

def area_in_square_meters
  abstract_features.area
end

#boundsObject



83
84
85
# File 'lib/spatial_features/has_spatial_features.rb', line 83

def bounds
  aggregate_features.bounds
end

#featuresObject



87
88
89
90
91
92
93
94
# File 'lib/spatial_features/has_spatial_features.rb', line 87

def features
  type = base_class.to_s # Rails stores polymorphic foreign keys as the base class
  if all == unscoped
    Feature.where(:spatial_model_type => type)
  else
    Feature.where(:spatial_model_type => type, :spatial_model_id => all.unscope(:select))
  end
end

#features_cache_keyObject



51
52
53
# File 'lib/spatial_features/has_spatial_features.rb', line 51

def features_cache_key
  "#{name}/#{aggregate_features.cache_key}"
end

#has_features_area?Boolean

Returns true if the model stores a cache of the features area

Returns:

  • (Boolean)


111
112
113
# File 'lib/spatial_features/has_spatial_features.rb', line 111

def has_features_area?
  owner_class_has_loaded_column?('features_area')
end

#has_spatial_features_hash?Boolean

Returns true if the model stores a hash of the features so we don’t need to process the features if they haven’t changed

Returns:

  • (Boolean)


106
107
108
# File 'lib/spatial_features/has_spatial_features.rb', line 106

def has_spatial_features_hash?
  owner_class_has_loaded_column?('features_hash')
end

#intersecting(other, options = {}) ⇒ Object



55
56
57
# File 'lib/spatial_features/has_spatial_features.rb', line 55

def intersecting(other, options = {})
  within_buffer(other, 0, options)
end

#linesObject



75
76
77
# File 'lib/spatial_features/has_spatial_features.rb', line 75

def lines
  features.lines
end

#pointsObject



79
80
81
# File 'lib/spatial_features/has_spatial_features.rb', line 79

def points
  features.points
end

#polygonsObject



71
72
73
# File 'lib/spatial_features/has_spatial_features.rb', line 71

def polygons
  features.polygons
end

#within_buffer(other, buffer_in_meters = 0, options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/spatial_features/has_spatial_features.rb', line 59

def within_buffer(other, buffer_in_meters = 0, options = {})
  return none if other.is_a?(ActiveRecord::Base) && other.new_record?

  # Cache only works on single records, not scopes.
  # This is because the cached intersection_area doesn't account for overlaps between the features in the scope.
  if options[:cache] != false && other.is_a?(ActiveRecord::Base)
    cached_within_buffer_scope(other, buffer_in_meters, options)
  else
    uncached_within_buffer_scope(other, buffer_in_meters, options)
  end
end