spatial_features 2.2.3 → 2.2.4
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d84b44917dac8280a1de81c1a440d4127485846
|
4
|
+
data.tar.gz: fa5a503cce899bcb8d75e7c1fc3087c414b2fabd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7e260e34f722ab7b668bcd9b92830d4e8d720e8159f1ac488c61091a57f78842cef8579ae5cb47973f328f46276fdf29e59d131a1b66ffe74069e52f791ac92
|
7
|
+
data.tar.gz: bd354f3f27912b24c8aa2f4cd92ad1e3dd2d29815217e1aca1ab77b5977ea6be196470e36c2066abd67caf90322954cf7df1c232aa0ff02e567e42e8089ff30d
|
data/app/models/feature.rb
CHANGED
@@ -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(
|
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.
|
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
|
-
|
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.
|
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
|
-
|
77
|
-
proximity.
|
78
|
-
proximity.
|
79
|
-
proximity.
|
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)
|
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(
|
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
|
-
|
251
|
+
area_in_square_meters
|
247
252
|
else
|
248
253
|
proxy_association.owner.features_area.to_f
|
249
254
|
end
|