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
102 103 104 |
# File 'lib/spatial_features/has_spatial_features.rb', line 102 def features? features.present? end |
#features_area_in_hectares ⇒ Object
141 142 143 |
# File 'lib/spatial_features/has_spatial_features.rb', line 141 def features_area_in_hectares Formatters::HECTARES.call(features_area_in_square_meters) end |
#features_area_in_square_meters ⇒ Object
137 138 139 |
# File 'lib/spatial_features/has_spatial_features.rb', line 137 def features_area_in_square_meters @features_area_in_square_meters ||= features.sum('ST_Area(features.geog_lowres)') end |
#features_cache_key ⇒ Object
86 87 88 |
# File 'lib/spatial_features/has_spatial_features.rb', line 86 def features_cache_key "#{self.class.name}/#{self.id}-#{features.maximum(:id)}-#{features.size}" end |
#has_spatial_features? ⇒ Boolean
82 83 84 |
# File 'lib/spatial_features/has_spatial_features.rb', line 82 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
107 108 109 |
# File 'lib/spatial_features/has_spatial_features.rb', line 107 def has_spatial_features_hash? respond_to?(:features_hash) end |
#intersects?(other) ⇒ Boolean
111 112 113 |
# File 'lib/spatial_features/has_spatial_features.rb', line 111 def intersects?(other) self.class.intersecting(other).exists?(self) end |
#lines? ⇒ Boolean
94 95 96 |
# File 'lib/spatial_features/has_spatial_features.rb', line 94 def lines? !features.lines.empty? end |
#points? ⇒ Boolean
98 99 100 |
# File 'lib/spatial_features/has_spatial_features.rb', line 98 def points? !features.points.empty? end |
#polygons? ⇒ Boolean
90 91 92 |
# File 'lib/spatial_features/has_spatial_features.rb', line 90 def polygons? !features.polygons.empty? end |
#spatial_cache_for(klass) ⇒ Object
145 146 147 |
# File 'lib/spatial_features/has_spatial_features.rb', line 145 def spatial_cache_for(klass) spatial_cache.where(:intersection_model_type => klass).first end |
#spatial_cache_for?(klass, buffer_in_meters) ⇒ Boolean
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/spatial_features/has_spatial_features.rb', line 149 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
127 128 129 |
# File 'lib/spatial_features/has_spatial_features.rb', line 127 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
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/spatial_features/has_spatial_features.rb', line 115 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
131 132 133 134 135 |
# File 'lib/spatial_features/has_spatial_features.rb', line 131 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 |