spatial_features 1.4.4 → 1.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d233745523bccf1bc197738dea299c3e9176b3db
4
- data.tar.gz: cbf5d686fa6041965c9484f4b76025bd98ef5e8c
3
+ metadata.gz: 0cd63a859a5b2dc1ea05a30d24bd61d6454f7591
4
+ data.tar.gz: e74e1827facb883756275fbbe0e2a549a7c474a0
5
5
  SHA512:
6
- metadata.gz: d6cde3c67b95ba0fbc807c6cdd564d7e5f7c9ce280f0d2091e934016b28bf25902b28fc5e8730badc7ebe8ed63b71e8107ddef0c5658afbe08ced72cb8c2f856
7
- data.tar.gz: 0c03056c50fa4edc07a2539258c4729f35ab16539c2283c457e75c32cd34b26a16d88c03ee34e1c60ed9c01ad4cca2c154eef022aa40fe4fda49411b6b462c65
6
+ metadata.gz: d485766d5075a0e7dd13cffc468738c4d8f11404ebcf398e248872d0fffe3024b9dd4540cec931636f3c9fdaa54dfaf2fdc194a610803fe417f7e1fee0cccc4b
7
+ data.tar.gz: c6d356fd5432119e148a76ef9e02b685da9d5e0a51bf255adf3c7c1a557fccbc688abf9b3093a3e57fed07a80aae89268295a6884e562b28761866ae3eaf1c2b
@@ -1,7 +1,11 @@
1
1
  module SpatialFeatures
2
2
  module ActMethod
3
3
  def has_spatial_features(options = {})
4
- has_many :features, :as => :spatial_model, :dependent => :delete_all
4
+ extend ClassMethods
5
+ include InstanceMethods
6
+
7
+ has_many :features, lambda { extending FeaturesAssociationExtensions }, :as => :spatial_model, :dependent => :delete_all
8
+
5
9
  scope :with_features, lambda { where(:id => Feature.select(:spatial_model_id).where(:spatial_model_type => name)) }
6
10
  scope :without_features, lambda { where.not(:id => Feature.select(:spatial_model_id).where(:spatial_model_type => name)) }
7
11
 
@@ -9,8 +13,9 @@ module SpatialFeatures
9
13
  has_many :model_a_spatial_proximities, :as => :model_a, :class_name => 'SpatialProximity', :dependent => :delete_all
10
14
  has_many :model_b_spatial_proximities, :as => :model_b, :class_name => 'SpatialProximity', :dependent => :delete_all
11
15
 
12
- extend SpatialFeatures::ClassMethods
13
- include SpatialFeatures::InstanceMethods
16
+ after_save :update_features_area, :if => :features_hash_changed? if has_features_area? && has_spatial_features_hash?
17
+
18
+ delegate :has_spatial_features_hash?, :has_features_area?, :to => self
14
19
  end
15
20
  end
16
21
 
@@ -81,7 +86,6 @@ module SpatialFeatures
81
86
  end
82
87
  end
83
88
 
84
-
85
89
  def features
86
90
  Feature.where(:spatial_model_type => self, :spatial_model_id => all)
87
91
  end
@@ -101,6 +105,16 @@ module SpatialFeatures
101
105
  AND "#{table_alias}".spatial_model_id IN (#{spatial_model_id}))
102
106
  end
103
107
 
108
+ # 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
+ def has_spatial_features_hash?
110
+ column_names.include? 'features_hash'
111
+ end
112
+
113
+ # Returns true if the model stores a cache of the features area
114
+ def has_features_area?
115
+ column_names.include? 'features_area'
116
+ end
117
+
104
118
  private
105
119
 
106
120
  def cached_spatial_join(other)
@@ -165,11 +179,6 @@ module SpatialFeatures
165
179
  features.present?
166
180
  end
167
181
 
168
- # 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
169
- def has_spatial_features_hash?
170
- respond_to?(:features_hash)
171
- end
172
-
173
182
  def covers?(other)
174
183
  self.class.covering(other).exists?(self)
175
184
  end
@@ -178,17 +187,6 @@ module SpatialFeatures
178
187
  self.class.intersecting(other).exists?(self)
179
188
  end
180
189
 
181
- def total_intersection_area_in_square_meters(klass, options = {})
182
- self.class
183
- .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))
184
- .joins_features_for(klass)
185
- .where(:id => self.id)
186
- .where('ST_DWithin(features_for.geog_lowres, features_for_other.geog_lowres, 0)')
187
- .group("#{self.class.table_name}.id")
188
- .first
189
- .try(:intersection_area_in_square_meters) || 0
190
- end
191
-
192
190
  def total_intersection_area_in_hectares(klass)
193
191
  Formatters::HECTARES.call(total_intersection_area_in_square_meters(klass))
194
192
  end
@@ -199,12 +197,33 @@ module SpatialFeatures
199
197
  ((total_intersection_area_in_square_meters(klass) / features_area_in_square_meters) * 100).round(1)
200
198
  end
201
199
 
200
+ def features_area_in_hectares
201
+ Formatters::HECTARES.call(features_area_in_square_meters)
202
+ end
203
+
202
204
  def features_area_in_square_meters
203
- @features_area_in_square_meters ||= features.sum('features.area')
205
+ @features_area_in_square_meters ||= features.area
204
206
  end
205
207
 
206
- def features_area_in_hectares
207
- Formatters::HECTARES.call(features_area_in_square_meters)
208
+ def total_intersection_area_in_square_meters(klass, options = {})
209
+ self.class
210
+ .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))
211
+ .joins_features_for(klass)
212
+ .where(:id => self.id)
213
+ .where('ST_DWithin(features_for.geog_lowres, features_for_other.geog_lowres, 0)')
214
+ .group("#{self.class.table_name}.id")
215
+ .first
216
+ .try(:intersection_area_in_square_meters) || 0
217
+ end
218
+
219
+ def update_features_area
220
+ update_column :features_area, features.area
221
+ end
222
+ end
223
+
224
+ module FeaturesAssociationExtensions
225
+ def area(options = {})
226
+ options[:cache] == false ? connection.select_value(all.select('ST_Area(ST_UNION(geom))')).to_f : proxy_association.owner.features_area
208
227
  end
209
228
  end
210
229
  end
@@ -1,3 +1,3 @@
1
1
  module SpatialFeatures
2
- VERSION = "1.4.4"
2
+ VERSION = "1.4.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spatial_features
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.4
4
+ version: 1.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Wallace
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-01-06 00:00:00.000000000 Z
12
+ date: 2016-01-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -117,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
117
  version: '0'
118
118
  requirements: []
119
119
  rubyforge_project:
120
- rubygems_version: 2.4.5.1
120
+ rubygems_version: 2.4.6
121
121
  signing_key:
122
122
  specification_version: 4
123
123
  summary: Adds spatial methods to a model.