Module: SpatialFeatures::InstanceMethods

Defined in:
lib/spatial_features/has_spatial_features.rb

Instance Method Summary collapse

Instance Method Details

#covers?(other) ⇒ Boolean



192
193
194
# File 'lib/spatial_features/has_spatial_features.rb', line 192

def covers?(other)
  self.class.covering(other).exists?(self)
end

#features?Boolean



188
189
190
# File 'lib/spatial_features/has_spatial_features.rb', line 188

def features?
  features.present?
end

#features_area_in_square_metersObject



206
207
208
# File 'lib/spatial_features/has_spatial_features.rb', line 206

def features_area_in_square_meters
  @features_area_in_square_meters ||= features.area
end

#features_cache_keyObject



171
172
173
174
# File 'lib/spatial_features/has_spatial_features.rb', line 171

def features_cache_key
  max_id, count = features.pluck("MAX(id), COUNT(*)").first
  "#{self.class.name}/#{self.id}-#{max_id}-#{count}"
end

#has_spatial_features?Boolean



167
168
169
# File 'lib/spatial_features/has_spatial_features.rb', line 167

def has_spatial_features?
  true
end

#intersects?(other) ⇒ Boolean



196
197
198
# File 'lib/spatial_features/has_spatial_features.rb', line 196

def intersects?(other)
  self.class.intersecting(other).exists?(self)
end

#lines?Boolean



180
181
182
# File 'lib/spatial_features/has_spatial_features.rb', line 180

def lines?
  !features.lines.empty?
end

#points?Boolean



184
185
186
# File 'lib/spatial_features/has_spatial_features.rb', line 184

def points?
  !features.points.empty?
end

#polygons?Boolean



176
177
178
# File 'lib/spatial_features/has_spatial_features.rb', line 176

def polygons?
  !features.polygons.empty?
end

#spatial_cache_for(klass) ⇒ Object



227
228
229
# File 'lib/spatial_features/has_spatial_features.rb', line 227

def spatial_cache_for(klass)
  spatial_cache.where(:intersection_model_type => klass).first
end

#spatial_cache_for?(klass, buffer_in_meters) ⇒ Boolean



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/spatial_features/has_spatial_features.rb', line 215

def spatial_cache_for?(klass, buffer_in_meters)
  if cache = spatial_cache_for(klass)
    return cache.intersection_cache_distance.nil? if buffer_in_meters.nil? # cache must be total if no buffer_in_meters
    return false if cache.stale? # cache must be for current features
    return true if cache.intersection_cache_distance.nil? # always good if cache is total

    return buffer_in_meters <= cache.intersection_cache_distance
  else
    return false
  end
end

#total_intersection_area_in_square_meters(other) ⇒ Object



210
211
212
213
# File 'lib/spatial_features/has_spatial_features.rb', line 210

def total_intersection_area_in_square_meters(other)
  other = other.intersecting(self) unless other.is_a?(ActiveRecord::Base)
  return features.total_intersection_area_in_square_meters(other.features)
end

#total_intersection_area_percentage(klass) ⇒ Object



200
201
202
203
204
# File 'lib/spatial_features/has_spatial_features.rb', line 200

def total_intersection_area_percentage(klass)
  return 0.0 unless features_area_in_square_meters > 0

  ((total_intersection_area_in_square_meters(klass) / features_area_in_square_meters) * 100).round(1)
end

#update_features_areaObject



231
232
233
# File 'lib/spatial_features/has_spatial_features.rb', line 231

def update_features_area
  update_column :features_area, features.area(:cache => false)
end