spatial_features 2.2.3 → 2.2.4

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: 417844aeba55e727bdb873b0492a6de02c9597af
4
- data.tar.gz: '096586ef1f32f9091c55c9e2142c2736cd3db0ad'
3
+ metadata.gz: 0d84b44917dac8280a1de81c1a440d4127485846
4
+ data.tar.gz: fa5a503cce899bcb8d75e7c1fc3087c414b2fabd
5
5
  SHA512:
6
- metadata.gz: 4fbaab3c02159dea567e0456fc5996f0d5979f26607d7c8e20ff264d27758af6417ae36837b60514ea71531babddb6d0d681772f4c9ffda9fd2589c188ceea2b
7
- data.tar.gz: 0d823427501023ceec92a8f6f6e581653d299dfa9534f7d96cd79606476f22be61c484b82c16656b6f8f0129433a7da183aed4993d2e42a9fa038d7cc2ec8566
6
+ metadata.gz: a7e260e34f722ab7b668bcd9b92830d4e8d720e8159f1ac488c61091a57f78842cef8579ae5cb47973f328f46276fdf29e59d131a1b66ffe74069e52f791ac92
7
+ data.tar.gz: bd354f3f27912b24c8aa2f4cd92ad1e3dd2d29815217e1aca1ab77b5977ea6be196470e36c2066abd67caf90322954cf7df1c232aa0ff02e567e42e8089ff30d
@@ -32,16 +32,18 @@ class Feature < ActiveRecord::Base
32
32
  where(:feature_type => 'point')
33
33
  end
34
34
 
35
- def self.area_in_square_meters
36
- current_scope = all
37
- unscoped { connection.select_value(select('ST_Area(ST_Union(geom))').from(current_scope, :features)).to_f }
35
+ def self.area_in_square_meters(geom = 'geom_lowres')
36
+ current_scope = all.polygons
37
+ unscoped { connection.select_value(select("ST_Area(ST_Union(#{geom}))").from(current_scope, :features)).to_f }
38
38
  end
39
39
 
40
40
  def self.total_intersection_area_in_square_meters(other_features, geom = 'geom_lowres')
41
- scope = unscope(:select).select("ST_Union(#{geom}) AS geom")
42
- other_scope = other_features.unscope(:select).select("ST_Union(#{geom}) AS geom")
43
- query = unscoped.select('ST_Area(ST_Intersection(features.geom, other_features.geom))').from("(#{scope.to_sql}) features, (#{other_scope.to_sql}) other_features")
41
+ scope = unscope(:select).select("ST_Union(#{geom}) AS geom").polygons
42
+ other_scope = other_features.polygons
44
43
 
44
+ query = unscoped.select('ST_Area(ST_Intersection(ST_Union(features.geom), ST_Union(other_features.geom)))')
45
+ .from(scope, "features")
46
+ .joins("INNER JOIN (#{other_scope.to_sql}) AS other_features ON ST_Intersects(features.geom, other_features.geom)")
45
47
  return connection.select_value(query).to_f
46
48
  end
47
49
 
@@ -68,15 +68,22 @@ module SpatialFeatures
68
68
 
69
69
  def self.create_spatial_proximities(record, klass)
70
70
  klass = klass.to_s.constantize
71
- record_is_a = record.class.to_s < klass.to_s
71
+ klass_record = klass.new
72
+ model_a, model_b = record.class.to_s < klass.to_s ? [record, klass_record] : [klass_record, record]
72
73
 
73
- scope = klass.within_buffer(record, default_cache_buffer_in_meters, :intersection_area => true, :distance => true, :cache => false)
74
- scope.find_each do |klass_record|
74
+ scope = klass.within_buffer(record, default_cache_buffer_in_meters, :columns => :id, :intersection_area => true, :distance => true, :cache => false)
75
+ results = klass.connection.select_rows(scope.to_sql)
76
+
77
+ results.each do |id, distance, area|
78
+ klass_record.id = id
75
79
  SpatialProximity.create! do |proximity|
76
- proximity.model_a = record_is_a ? record : klass_record
77
- proximity.model_b = record_is_a ? klass_record : record
78
- proximity.distance_in_meters = klass_record.distance_in_meters
79
- proximity.intersection_area_in_square_meters = klass_record.intersection_area_in_square_meters
80
+ # Set id and type instead of model to avoid autosaving the klass_record
81
+ proximity.model_a_id = model_a.id
82
+ proximity.model_a_type = model_a.class
83
+ proximity.model_b_id = model_b.id
84
+ proximity.model_b_type = model_b.class
85
+ proximity.distance_in_meters = distance
86
+ proximity.intersection_area_in_square_meters = area
80
87
  end
81
88
  end
82
89
  end
@@ -97,10 +97,13 @@ module SpatialFeatures
97
97
  private
98
98
 
99
99
  def cached_within_buffer_scope(other, buffer_in_meters, options)
100
+ options = options.reverse_merge(:columns => "#{table_name}.*")
101
+
100
102
  # Don't use the cache if it doesn't exist
101
103
  return all.extending(UncachedRelation) unless other.spatial_cache_for?(class_for(self), buffer_in_meters)
102
104
 
103
- scope = cached_spatial_join(other).select("#{table_name}.*")
105
+ scope = cached_spatial_join(other)
106
+ scope = scope.select(options[:columns])
104
107
  scope = scope.where("spatial_proximities.distance_in_meters <= ?", buffer_in_meters) if buffer_in_meters
105
108
  scope = scope.select("spatial_proximities.distance_in_meters") if options[:distance]
106
109
  scope = scope.select("spatial_proximities.intersection_area_in_square_meters") if options[:intersection_area]
@@ -119,8 +122,10 @@ module SpatialFeatures
119
122
  end
120
123
 
121
124
  def uncached_within_buffer_scope(other, buffer_in_meters, options)
125
+ options = options.reverse_merge(:columns => "#{table_name}.*")
126
+
122
127
  scope = spatial_join(other, buffer_in_meters)
123
- scope = scope.select("#{table_name}.*")
128
+ scope = scope.select(options[:columns])
124
129
 
125
130
  # Ensure records with multiple features don't appear multiple times
126
131
  if options[:distance] || options[:intersection_area]
@@ -130,7 +135,7 @@ module SpatialFeatures
130
135
  end
131
136
 
132
137
  scope = scope.select("MIN(ST_Distance(features.geom, other_features.geom)) AS distance_in_meters") if options[:distance]
133
- scope = scope.select("ST_Area(ST_UNION(ST_Intersection(features.geom, other_features.geom))) AS intersection_area_in_square_meters") if options[:intersection_area]
138
+ scope = scope.select("ST_Area(ST_UNION(ST_Intersection(ST_CollectionExtract(features.geom, 3), ST_CollectionExtract(other_features.geom, 3)))) AS intersection_area_in_square_meters") if options[:intersection_area]
134
139
  return scope
135
140
  end
136
141
 
@@ -243,7 +248,7 @@ module SpatialFeatures
243
248
  module FeaturesAssociationExtensions
244
249
  def area(options = {})
245
250
  if options[:cache] == false || !proxy_association.owner.class.has_features_area?
246
- pluck('ST_Area(ST_UNION(geom))').first.to_f
251
+ area_in_square_meters
247
252
  else
248
253
  proxy_association.owner.features_area.to_f
249
254
  end
@@ -1,3 +1,3 @@
1
1
  module SpatialFeatures
2
- VERSION = "2.2.3"
2
+ VERSION = "2.2.4"
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: 2.2.3
4
+ version: 2.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Wallace