spatial_features 1.5.12 → 1.6.0

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: 73e929f19dbe88a8a660db9905520f65298ae749
4
- data.tar.gz: ea15f17e06a0c28e3737acde7d102a8111e9624f
3
+ metadata.gz: 22436d934425f3bf26fa67267809556fee323e29
4
+ data.tar.gz: 61ffeaba1a567cde42cd084c3ef058389dc10595
5
5
  SHA512:
6
- metadata.gz: 0bfdb3c23c67fd37602798cd56702d87ffb74bb56f1f56066620cec18d40be2e350f761e9442b4007faa3c79df4f09836857f4fa446ef27ed0ce9c0232a9de17
7
- data.tar.gz: 8531be35e8009f784944d55d979c7450304141736d6f0805dbb21c844452da039b883177a9bd539aac47ff9ca814b6eb2f7ccc32fd263270798e286f448768f1
6
+ metadata.gz: 7f4569629dbefd9a0d2f338135229ab285471d16a2ecbe52a1db52b2f0eacb3e23d29f24547bcd9066617d8071f05a2606254faa371ebe0878b8f37e56203a50
7
+ data.tar.gz: d46d59474311d6171ebcea73a6576ae4419cb6600a6a34cdbf294210880d8812fc30afe8d77021d5645ef8df0a9455f6d4af5fceee1c0779e59ccf1792d4b83f
data/README.md CHANGED
@@ -12,12 +12,16 @@ execute("
12
12
  name character varying(255),
13
13
  feature_type character varying(255),
14
14
  geog geography,
15
+ geom geometry(Geometry,3005),
16
+ geom_lowres geometry(Geometry,3005),
15
17
  kml text,
16
- geog_lowres geography(Geometry,4326),
17
- geom geometry(Geometry,26910),
18
- metadata hstore,
19
18
  kml_lowres text,
20
- area double precision
19
+ metadata hstore,
20
+ area double precision,
21
+ north numeric(9,6),
22
+ east numeric(9,6),
23
+ south numeric(9,6),
24
+ west numeric(9,6)
21
25
  );
22
26
 
23
27
  CREATE TABLE spatial_caches (
@@ -32,17 +32,15 @@ class Feature < ActiveRecord::Base
32
32
  unscoped { connection.select_value(select('ST_Area(ST_Union(geom))').from(current_scope, :features)).to_f }
33
33
  end
34
34
 
35
- def self.total_intersection_area_in_square_meters(other)
36
- other_features = unscoped.flattened_features.where(:id => intersecting(other).select('other_features.id'))
37
-
35
+ def self.total_intersection_area_in_square_meters(other_features)
38
36
  unscoped
39
- .from("(#{flattened_features.to_sql}) features, (#{other_features.to_sql}) other_features")
40
- .pluck('ST_Area(ST_Intersection(features.geom, other_features.geom))')
37
+ .from("(#{select("ST_Union(geom) AS geom").to_sql}) features, (#{other_features.to_sql}) other_features")
38
+ .pluck('ST_Area(ST_UNION(ST_Intersection(features.geom, other_features.geom)))')
41
39
  .first.to_f
42
40
  end
43
41
 
44
42
  def self.intersecting(other)
45
- join_other_features(other).where('ST_Intersects(features.geog_lowres, other_features.geog_lowres)').uniq
43
+ join_other_features(other).where('ST_Intersects(features.geom_lowres, other_features.geom_lowres)').uniq
46
44
  end
47
45
 
48
46
  def self.invalid
@@ -63,19 +61,25 @@ class Feature < ActiveRecord::Base
63
61
  end
64
62
 
65
63
  def self.cache_derivatives(options = {})
66
- options.reverse_merge! :lowres_simplification => 0.00001, :lowres_precision => 5
64
+ options.reverse_merge! :lowres_simplification => 2, :lowres_precision => 5
65
+
66
+ update_all <<-SQL.squish
67
+ geom = ST_Transform(geog::geometry, #{detect_srid('geom')}),
68
+ north = ST_YMax(geog::geometry),
69
+ east = ST_XMax(geog::geometry),
70
+ south = ST_YMin(geog::geometry),
71
+ west = ST_XMin(geog::geometry),
72
+ area = ST_Area(geog)
73
+ SQL
74
+
75
+ update_all <<-SQL.squish
76
+ geom_lowres = ST_SimplifyPreserveTopology(geom, #{options[:lowres_simplification]})
77
+ SQL
67
78
 
68
- update_all("area = ST_Area(geog),
69
- geom = ST_Transform(geog::geometry, #{detect_srid('geom')}),
70
- geog_lowres = ST_SimplifyPreserveTopology(geog::geometry, #{options[:lowres_simplification]})"
71
- .squish)
72
- update_all("kml = ST_AsKML(features.geog, 6),
73
- kml_lowres = ST_AsKML(geog_lowres::geometry, #{options[:lowres_precision]}),
74
- north = ST_YMax(geog::geometry),
75
- east = ST_XMax(geog::geometry),
76
- south = ST_YMin(geog::geometry),
77
- west = ST_XMin(geog::geometry)"
78
- .squish)
79
+ update_all <<-SQL.squish
80
+ kml = ST_AsKML(geog, 6),
81
+ kml_lowres = ST_AsKML(geom_lowres, #{options[:lowres_precision]})
82
+ SQL
79
83
  end
80
84
 
81
85
  def feature_bounds
@@ -92,10 +96,6 @@ class Feature < ActiveRecord::Base
92
96
 
93
97
  private
94
98
 
95
- def self.flattened_features
96
- select('ST_Union(features.geom) AS geom')
97
- end
98
-
99
99
  def self.detect_srid(column_name)
100
100
  connection.select_value("SELECT Find_SRID('public', '#{table_name}', '#{column_name}')")
101
101
  end
@@ -2,6 +2,14 @@ module SpatialFeatures
2
2
  mattr_accessor :default_cache_buffer_in_meters
3
3
  self.default_cache_buffer_in_meters = 100
4
4
 
5
+ def self.update_spatial_cache(scope)
6
+ scope.with_stale_spatial_cache.includes(:spatial_cache).find_each do |record|
7
+ record.spatial_cache.each do |spatial_cache|
8
+ cache_record_proximity(record, spatial_cache.intersection_model_type) if spatial_cache.stale?
9
+ end
10
+ end
11
+ end
12
+
5
13
  # Create or update the spatial cache of a spatial class in relation to another
6
14
  # NOTE: Arguments are order independent, so their names do not reflect the _a _b
7
15
  # naming scheme used in other cache methods
@@ -41,13 +49,14 @@ module SpatialFeatures
41
49
  end
42
50
 
43
51
  def self.clear_record_cache(record, klass)
44
- record.spatial_cache.where(:intersection_model_type => klass.name).delete_all
45
- SpatialProximity.where(:model_a_type => record.class.name, :model_a_id => record.id, :model_b_type => klass.name).delete_all
46
- SpatialProximity.where(:model_b_type => record.class.name, :model_b_id => record.id, :model_a_type => klass.name).delete_all
52
+ record.spatial_cache.where(:intersection_model_type => klass).delete_all
53
+ SpatialProximity.where(:model_a_type => record.class, :model_a_id => record.id, :model_b_type => klass).delete_all
54
+ SpatialProximity.where(:model_b_type => record.class, :model_b_id => record.id, :model_a_type => klass).delete_all
47
55
  end
48
56
 
49
57
  def self.create_spatial_proximities(record, klass)
50
- record_is_a = record.class.name < klass.name
58
+ klass = klass.to_s.constantize
59
+ record_is_a = record.class.to_s < klass.to_s
51
60
 
52
61
  scope = klass.within_buffer(record, default_cache_buffer_in_meters, :intersection_area => true, :distance => true, :cache => false)
53
62
  scope.find_each do |klass_record|
@@ -63,7 +72,7 @@ module SpatialFeatures
63
72
  def self.create_spatial_cache(model, klass)
64
73
  SpatialCache.create! do |cache|
65
74
  cache.spatial_model = model
66
- cache.intersection_model_type = klass.name
75
+ cache.intersection_model_type = klass
67
76
  cache.intersection_cache_distance = default_cache_buffer_in_meters
68
77
  cache.features_hash = model.features_hash if model.has_spatial_features_hash?
69
78
  end
@@ -234,7 +234,7 @@ module SpatialFeatures
234
234
  module FeaturesAssociationExtensions
235
235
  def area(options = {})
236
236
  if options[:cache] == false || !proxy_association.owner.class.has_features_area?
237
- connection.select_value(all.select('ST_Area(ST_UNION(geom))')).try(:to_f)
237
+ pluck('ST_Area(ST_UNION(geom))').first.to_f
238
238
  else
239
239
  proxy_association.owner.features_area
240
240
  end
@@ -1,3 +1,3 @@
1
1
  module SpatialFeatures
2
- VERSION = "1.5.12"
2
+ VERSION = "1.6.0"
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.5.12
4
+ version: 1.6.0
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-07-29 00:00:00.000000000 Z
12
+ date: 2016-07-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails