Module: SpatialFeatures::InstanceMethods
- Defined in:
- lib/spatial_features/has_spatial_features.rb
Instance Method Summary collapse
- #features? ⇒ Boolean
- #features_area_in_hectares ⇒ Object
- #features_area_in_square_meters ⇒ Object
- #features_cache_key ⇒ Object
- #has_spatial_features? ⇒ Boolean
-
#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.
- #intersects?(other) ⇒ Boolean
- #lines? ⇒ Boolean
- #points? ⇒ Boolean
- #polygons? ⇒ Boolean
- #spatial_cache_for(klass) ⇒ Object
- #spatial_cache_for?(klass, buffer_in_meters) ⇒ Boolean
- #total_intersection_area_in_hectares(klass) ⇒ Object
- #total_intersection_area_in_square_meters(klass, options = {}) ⇒ Object
- #total_intersection_area_percentage(klass) ⇒ Object
Instance Method Details
#features? ⇒ Boolean
104 105 106 |
# File 'lib/spatial_features/has_spatial_features.rb', line 104 def features? features.present? end |
#features_area_in_hectares ⇒ Object
143 144 145 |
# File 'lib/spatial_features/has_spatial_features.rb', line 143 def features_area_in_hectares Formatters::HECTARES.call(features_area_in_square_meters) end |
#features_area_in_square_meters ⇒ Object
139 140 141 |
# File 'lib/spatial_features/has_spatial_features.rb', line 139 def features_area_in_square_meters @features_area_in_square_meters ||= features.sum('ST_Area(features.geog_lowres)') end |
#features_cache_key ⇒ Object
88 89 90 |
# File 'lib/spatial_features/has_spatial_features.rb', line 88 def features_cache_key "#{self.class.name}/#{self.id}-#{features.maximum(:id)}-#{features.size}" end |
#has_spatial_features? ⇒ Boolean
84 85 86 |
# File 'lib/spatial_features/has_spatial_features.rb', line 84 def has_spatial_features? true 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
109 110 111 |
# File 'lib/spatial_features/has_spatial_features.rb', line 109 def has_spatial_features_hash? respond_to?(:features_hash) end |
#intersects?(other) ⇒ Boolean
113 114 115 |
# File 'lib/spatial_features/has_spatial_features.rb', line 113 def intersects?(other) self.class.intersecting(other).exists?(self) end |
#lines? ⇒ Boolean
96 97 98 |
# File 'lib/spatial_features/has_spatial_features.rb', line 96 def lines? !features.lines.empty? end |
#points? ⇒ Boolean
100 101 102 |
# File 'lib/spatial_features/has_spatial_features.rb', line 100 def points? !features.points.empty? end |
#polygons? ⇒ Boolean
92 93 94 |
# File 'lib/spatial_features/has_spatial_features.rb', line 92 def polygons? !features.polygons.empty? end |
#spatial_cache_for(klass) ⇒ Object
147 148 149 |
# File 'lib/spatial_features/has_spatial_features.rb', line 147 def spatial_cache_for(klass) spatial_cache.where(:intersection_model_type => klass).first end |
#spatial_cache_for?(klass, buffer_in_meters) ⇒ Boolean
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/spatial_features/has_spatial_features.rb', line 151 def spatial_cache_for?(klass, buffer_in_meters) if cache = spatial_cache_for(klass) return cache.cache_distance.nil? if buffer_in_meters.nil? # cache must be total if no buffer_in_meters return true if cache.cache_distance.nil? # always good if cache is total return buffer_in_meters <= cache.cache_distance else return false end end |
#total_intersection_area_in_hectares(klass) ⇒ Object
129 130 131 |
# File 'lib/spatial_features/has_spatial_features.rb', line 129 def total_intersection_area_in_hectares(klass) Formatters::HECTARES.call(total_intersection_area_in_square_meters(klass)) end |
#total_intersection_area_in_square_meters(klass, options = {}) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/spatial_features/has_spatial_features.rb', line 117 def total_intersection_area_in_square_meters(klass, = {}) self.class .select(%Q(ST_Area(ST_Intersection(ST_Union(features_for.geog_lowres::geometry), ST_Union(features_for_other.geog_lowres::geometry))::geography) AS intersection_area_in_square_meters)) .joins(%Q(INNER JOIN features "features_for" ON "features_for".spatial_model_type = '#{self.class}' AND "features_for".spatial_model_id = #{self.class.table_name}.id)) .joins(%Q(INNER JOIN features "features_for_other" ON "features_for_other".spatial_model_type = '#{klass}')) .where(:id => self.id) .where('ST_DWithin(features_for.geog_lowres, features_for_other.geog_lowres, 0)') .group("#{self.class.table_name}.id") .first .try(:intersection_area_in_square_meters) || 0 end |
#total_intersection_area_percentage(klass) ⇒ Object
133 134 135 136 137 |
# File 'lib/spatial_features/has_spatial_features.rb', line 133 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 |