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)


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

def acts_like_spatial_features?
  true
end

#area_in_square_metersObject



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

def area_in_square_meters
  features.area_in_square_meters
end

#covering(other) ⇒ Object



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

def covering(other)
  scope = joins_features_for(other).select("#{table_name}.*").group("#{table_name}.#{primary_key}")
  scope = scope.where('ST_Covers(features.geom, features_for_other.geom)')

  return scope
end

#featuresObject



82
83
84
85
86
87
88
# File 'lib/spatial_features/has_spatial_features.rb', line 82

def features
  if all == unscoped
    Feature.where(:spatial_model_type => self)
  else
    Feature.where(:spatial_model_type => self, :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



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

def features_cache_key
  # Do two separate queries because it is much faster for some reason
  "#{name}/#{features.maximum(:id)}-#{features.count}"
end

#has_features_area?Boolean

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

Returns:

  • (Boolean)


108
109
110
# File 'lib/spatial_features/has_spatial_features.rb', line 108

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)


103
104
105
# File 'lib/spatial_features/has_spatial_features.rb', line 103

def has_spatial_features_hash?
  column_names.include? 'features_hash'
end

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



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

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

#joins_features_for(other, table_alias = 'features_for') ⇒ Object

Returns a scope that includes the features for this record as the table_alias and the features for other as #table_alias_other Can be used to perform spatial calculations on the relationship between the two sets of features



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

def joins_features_for(other, table_alias = 'features_for')
  joins(:features).joins(%Q(INNER JOIN (#{other_features_union(other).to_sql}) AS "#{table_alias}_other" ON true))
end

#linesObject



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

def lines
  features.lines
end

#other_features_union(other) ⇒ Object



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

def other_features_union(other)
  scope = Feature.select('ST_Union(geom) AS geom').where(:spatial_model_type => class_for(other))
  scope = scope.where(:spatial_model_id => other) unless class_for(other) == other
  return scope
end

#pointsObject



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

def points
  features.points
end

#polygonsObject



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

def polygons
  features.polygons
end

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



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

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