Module: SpatialFeatures::ClassMethods
- Defined in:
- lib/spatial_features/has_spatial_features.rb
Instance Method Summary collapse
- #area_in_square_meters ⇒ Object
- #covering(other) ⇒ Object
- #features ⇒ Object
-
#features_cache_key ⇒ Object
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.
-
#has_features_area? ⇒ Boolean
Returns true if the model stores a cache of the features area.
-
#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.
- #intersecting(other, options = {}) ⇒ Object
-
#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.
- #lines ⇒ Object
- #other_features_union(other) ⇒ Object
- #points ⇒ Object
- #polygons ⇒ Object
- #within_buffer(other, buffer_in_meters = 0, options = {}) ⇒ Object
Instance Method Details
#area_in_square_meters ⇒ Object
98 99 100 |
# File 'lib/spatial_features/has_spatial_features.rb', line 98 def area_in_square_meters features.area_in_square_meters end |
#covering(other) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/spatial_features/has_spatial_features.rb', line 49 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 |
#features ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/spatial_features/has_spatial_features.rb', line 68 def features if all == unscoped.all Feature.where(:spatial_model_type => self) else Feature.where(:spatial_model_type => self, :spatial_model_id => all) end end |
#features_cache_key ⇒ Object
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
28 29 30 31 |
# File 'lib/spatial_features/has_spatial_features.rb', line 28 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
94 95 96 |
# File 'lib/spatial_features/has_spatial_features.rb', line 94 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
89 90 91 |
# File 'lib/spatial_features/has_spatial_features.rb', line 89 def has_spatial_features_hash? column_names.include? 'features_hash' end |
#intersecting(other, options = {}) ⇒ Object
33 34 35 |
# File 'lib/spatial_features/has_spatial_features.rb', line 33 def intersecting(other, = {}) within_buffer(other, 0, ) 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
78 79 80 |
# File 'lib/spatial_features/has_spatial_features.rb', line 78 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 |
#lines ⇒ Object
60 61 62 |
# File 'lib/spatial_features/has_spatial_features.rb', line 60 def lines features.lines end |
#other_features_union(other) ⇒ Object
82 83 84 85 86 |
# File 'lib/spatial_features/has_spatial_features.rb', line 82 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 |
#points ⇒ Object
64 65 66 |
# File 'lib/spatial_features/has_spatial_features.rb', line 64 def points features.points end |
#polygons ⇒ Object
56 57 58 |
# File 'lib/spatial_features/has_spatial_features.rb', line 56 def polygons features.polygons end |
#within_buffer(other, buffer_in_meters = 0, options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/spatial_features/has_spatial_features.rb', line 37 def within_buffer(other, buffer_in_meters = 0, = {}) 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 [:cache] != false && other.is_a?(ActiveRecord::Base) cached_within_buffer_scope(other, buffer_in_meters, ) else uncached_within_buffer_scope(other, buffer_in_meters, ) end end |