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)


34
35
36
# File 'lib/spatial_features/has_spatial_features.rb', line 34

def acts_like_spatial_features?
  true
end

#area_in_square_metersObject



93
94
95
# File 'lib/spatial_features/has_spatial_features.rb', line 93

def area_in_square_meters
  features.area_in_square_meters
end

#featuresObject



74
75
76
77
78
79
80
81
# File 'lib/spatial_features/has_spatial_features.rb', line 74

def features
  type = base_class # 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

Add methods to generate cache keys for a record or all records of this class NOTE: features are never updated, only deleted and created, therefore we can tell if they have changed by finding the maximum id and count instead of needing timestamps



41
42
43
44
# File 'lib/spatial_features/has_spatial_features.rb', line 41

def features_cache_key
  # Do two separate queries because it is much faster for some reason
  "#{name}/#{features.cache_key}"
end

#has_features_area?Boolean

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

Returns:

  • (Boolean)


89
90
91
# File 'lib/spatial_features/has_spatial_features.rb', line 89

def has_features_area?
  column_names.include? '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)


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

def has_spatial_features_hash?
  column_names.include? 'features_hash'
end

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



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

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

#linesObject



66
67
68
# File 'lib/spatial_features/has_spatial_features.rb', line 66

def lines
  features.lines
end

#pointsObject



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

def points
  features.points
end

#polygonsObject



62
63
64
# File 'lib/spatial_features/has_spatial_features.rb', line 62

def polygons
  features.polygons
end

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



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/spatial_features/has_spatial_features.rb', line 50

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