spatial_features 0.0.1
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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +19 -0
- data/lib/spatial_features/caching.rb +66 -0
- data/lib/spatial_features/controller_helpers/spatial_extensions.rb +35 -0
- data/lib/spatial_features/has_spatial_features.rb +158 -0
- data/lib/spatial_features/import/arcgis_kmz_features.rb +104 -0
- data/lib/spatial_features/models/feature.rb +59 -0
- data/lib/spatial_features/models/spatial_cache.rb +3 -0
- data/lib/spatial_features/models/spatial_proximity.rb +4 -0
- data/lib/spatial_features/venn_polygons.rb +56 -0
- data/lib/spatial_features/version.rb +3 -0
- data/lib/spatial_features.rb +21 -0
- data/lib/tasks/spatial_features_tasks.rake +4 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config/application.rb +23 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +82 -0
- data/test/dummy/config/environments/test.rb +39 -0
- data/test/dummy/config/initializers/assets.rb +8 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/spatial_features_test.rb +7 -0
- data/test/test_helper.rb +15 -0
- metadata +171 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c42265b78f3d41490bf8e89691d57f1cab48d9b2
|
4
|
+
data.tar.gz: bdc3542a36c9584b6f18bceddda2b7e175997a2e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a17091a2662a74ac313c47059ac13f63a220a45fbce3c11aedffc7ccc6c74c8425b07aec89ccc0a1b00772c5405bcaa5432ac39bd20aa8398d93ac405d7407eb
|
7
|
+
data.tar.gz: 7a15bb57318905523faa487ff24ac34b48a331e893d4088dfa77aff0c8dda933604599428f2030263d9b67bf287cfd4e316486b007c893978c51ab9cd263ea0d
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
Bundler::GemHelper.install_tasks
|
8
|
+
|
9
|
+
require 'rake/testtask'
|
10
|
+
|
11
|
+
Rake::TestTask.new(:test) do |t|
|
12
|
+
t.libs << 'lib'
|
13
|
+
t.libs << 'test'
|
14
|
+
t.pattern = 'test/**/*_test.rb'
|
15
|
+
t.verbose = false
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
task default: :test
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module SpatialFeatures
|
2
|
+
mattr_accessor :default_cache_buffer_in_meters
|
3
|
+
self.default_cache_buffer_in_meters = 100
|
4
|
+
|
5
|
+
# Create or update the spatial cache of a spatial class in relation to another
|
6
|
+
# NOTE: Arguments are order independent, so their names do not reflect the _a _b
|
7
|
+
# naming scheme used in other cache methods
|
8
|
+
def self.cache_proximity(klass, clazz)
|
9
|
+
clear_cache(klass, clazz)
|
10
|
+
|
11
|
+
klass.find_each do |record|
|
12
|
+
create_spatial_proximities(record, clazz)
|
13
|
+
create_spatial_cache(record, clazz)
|
14
|
+
end
|
15
|
+
|
16
|
+
clazz.find_each do |record|
|
17
|
+
create_spatial_cache(record, klass)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Create or update the spatial cache of a single record in relation to another spatial class
|
22
|
+
def self.cache_record_proximity(record, klass)
|
23
|
+
clear_record_cache(record, klass)
|
24
|
+
create_spatial_proximities(record, klass)
|
25
|
+
create_spatial_cache(record, klass)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Delete all cache entries relating klass to clazz
|
29
|
+
def self.clear_cache(klass = nil, clazz = nil)
|
30
|
+
if klass.blank? && clazz.blank?
|
31
|
+
SpatialCache.delete_all
|
32
|
+
SpatialProximity.delete_all
|
33
|
+
else
|
34
|
+
SpatialCache.where(:spatial_model_type => klass, :intersection_model_type => clazz).delete_all
|
35
|
+
SpatialCache.where(:spatial_model_type => clazz, :intersection_model_type => klass).delete_all
|
36
|
+
SpatialProximity.where(:model_a_type => klass, :model_b_type => clazz).delete_all
|
37
|
+
SpatialProximity.where(:model_a_type => clazz, :model_b_type => klass).delete_all
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.clear_record_cache(record, klass)
|
42
|
+
record.spatial_cache.where(:intersection_model_type => klass.name).delete_all
|
43
|
+
SpatialProximity.where(:model_a_type => record.class.name, :model_a_id => record.id, :model_b_type => klass.name).delete_all
|
44
|
+
SpatialProximity.where(:model_b_type => record.class.name, :model_b_id => record.id, :model_a_type => klass.name).delete_all
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.create_spatial_proximities(record, klass)
|
48
|
+
record_is_a = record.class.name < klass.name
|
49
|
+
|
50
|
+
scope = klass.within_buffer(record, default_cache_buffer_in_meters, :intersection_area => true, :distance => true, :cache => false)
|
51
|
+
scope.find_each do |klass_record|
|
52
|
+
SpatialProximity.create!(
|
53
|
+
:model_a => record_is_a ? record : klass_record,
|
54
|
+
:model_b => record_is_a ? klass_record : record,
|
55
|
+
:distance_in_meters => klass_record.distance_in_meters,
|
56
|
+
:intersection_area_in_square_meters => klass_record.intersection_area_in_square_meters)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.create_spatial_cache(model, klass)
|
61
|
+
SpatialCache.create!(
|
62
|
+
:spatial_model => model,
|
63
|
+
:intersection_model_type => klass.name,
|
64
|
+
:cache_distance => default_cache_buffer_in_meters)
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module SpatialExtensions
|
2
|
+
private
|
3
|
+
|
4
|
+
def abstract_proximity_action(scope, target, distance, &block)
|
5
|
+
@nearby_records = scope_for_search(scope).within_buffer(target, distance, :distance => true, :intersection_area => true).order('distance_in_meters ASC, intersection_area_in_square_meters DESC, id ASC')
|
6
|
+
@target = target
|
7
|
+
|
8
|
+
yield if block_given?
|
9
|
+
|
10
|
+
respond_to do |format|
|
11
|
+
format.html { render :template => 'shared/spatial/feature_proximity', :layout => false }
|
12
|
+
format.kml { render :template => 'shared/spatial/feature_proximity' }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def abstract_venn_polygons_action(scope, target, &block)
|
17
|
+
@venn_polygons = SpatialFeatures.venn_polygons(scope_for_search(scope).intersecting(target), target.class.where(:id => target), :simplified => false)
|
18
|
+
@klass = klass_for_search(scope)
|
19
|
+
@target = target
|
20
|
+
|
21
|
+
yield if block_given?
|
22
|
+
|
23
|
+
respond_to do |format|
|
24
|
+
format.kml { render :template => 'shared/spatial/feature_venn_polygons' }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def klass_for_search(scope_or_class)
|
29
|
+
scope_or_class.is_a?(ActiveRecord::Relation) ? scope_or_class.klass : scope_or_class
|
30
|
+
end
|
31
|
+
|
32
|
+
def scope_for_search(scope)
|
33
|
+
params.key?(:ids) ? scope.where(:id => params[:ids]) : scope
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,158 @@
|
|
1
|
+
module SpatialFeatures
|
2
|
+
module ActMethod
|
3
|
+
def has_spatial_features(options = {})
|
4
|
+
has_many :features, :as => :spatial_model, :dependent => :delete_all
|
5
|
+
scope :with_features, lambda { where(:id => Feature.select(:spatial_model_id).where(:spatial_model_type => name)) }
|
6
|
+
scope :without_features, lambda { where.not(:id => Feature.select(:spatial_model_id).where(:spatial_model_type => name)) }
|
7
|
+
|
8
|
+
has_many :spatial_cache, :as => :spatial_model
|
9
|
+
|
10
|
+
extend SpatialFeatures::ClassMethods
|
11
|
+
include SpatialFeatures::InstanceMethods
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
# Add methods to generate cache keys for a record or all records of this class
|
17
|
+
# NOTE: features are never updated, only deleted and created, therefore we can
|
18
|
+
# tell if they have changed by finding the maximum id and count instead of needing timestamps
|
19
|
+
def features_cache_key
|
20
|
+
"#{name}/#{Feature.where(:spatial_model_type => self).maximum(:id)}-#{Feature.where(:spatial_model_type => self).count}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def intersecting(other, options = {})
|
24
|
+
within_buffer(other, 0, options)
|
25
|
+
end
|
26
|
+
|
27
|
+
def within_buffer(other, buffer_in_meters = 0, options = {})
|
28
|
+
raise "Can't intersect with #{other} because it does not implement has_features" unless other.has_spatial_features?
|
29
|
+
|
30
|
+
if other.spatial_cache_for?(self, buffer_in_meters) && options[:cache] != false # CACHED
|
31
|
+
scope = cached_spatial_join(other)
|
32
|
+
.select("#{table_name}.*, spatial_proximities.distance_in_meters, spatial_proximities.intersection_area_in_square_meters")
|
33
|
+
|
34
|
+
scope = scope.where("spatial_proximities.distance_in_meters <= ?", buffer_in_meters) if buffer_in_meters
|
35
|
+
else # NON-CACHED
|
36
|
+
scope = joins_features_for(other)
|
37
|
+
.select("#{table_name}.*")
|
38
|
+
.group("#{table_name}.#{primary_key}")
|
39
|
+
|
40
|
+
scope = scope.where('ST_DWithin(features_for.geog_lowres, features_for_other.geog_lowres, ?)', buffer_in_meters) if buffer_in_meters
|
41
|
+
scope = scope.select("MIN(ST_Distance(features_for.geog_lowres, features_for_other.geog_lowres)) AS distance_in_meters") if options[:distance]
|
42
|
+
scope = scope.select("SUM(ST_Area(ST_Intersection(features_for.geog_lowres, features_for_other.geog_lowres))) AS intersection_area_in_square_meters") if options[:intersection_area]
|
43
|
+
end
|
44
|
+
|
45
|
+
return scope
|
46
|
+
end
|
47
|
+
|
48
|
+
def polygons
|
49
|
+
Feature.polygons.where(:spatial_model_type => self.class)
|
50
|
+
end
|
51
|
+
|
52
|
+
def lines
|
53
|
+
Feature.lines.where(:spatial_model_type => self.class)
|
54
|
+
end
|
55
|
+
|
56
|
+
def points
|
57
|
+
Feature.points.where(:spatial_model_type => self.class)
|
58
|
+
end
|
59
|
+
|
60
|
+
def cached_spatial_join(other)
|
61
|
+
raise "Cannot use cached spatial join for the same class" if other.class.name == self.name
|
62
|
+
|
63
|
+
other_column = other.class.name < self.name ? :model_a : :model_b
|
64
|
+
self_column = other_column == :model_a ? :model_b : :model_a
|
65
|
+
|
66
|
+
joins("INNER JOIN spatial_proximities ON spatial_proximities.#{self_column}_type = '#{self}' AND spatial_proximities.#{self_column}_id = #{table_name}.id AND spatial_proximities.#{other_column}_type = '#{other.class}' AND spatial_proximities.#{other_column}_id = '#{other.id}'")
|
67
|
+
end
|
68
|
+
|
69
|
+
def joins_features_for(other, table_alias = 'features_for')
|
70
|
+
joins_features(table_alias)
|
71
|
+
.joins(%Q(INNER JOIN features "#{table_alias}_other" ON "#{table_alias}_other".spatial_model_type = '#{other.class.name}' AND "#{table_alias}_other".spatial_model_id = #{other.id}))
|
72
|
+
end
|
73
|
+
|
74
|
+
def joins_features(table_alias = 'features_for')
|
75
|
+
joins(%Q(INNER JOIN features "#{table_alias}" ON "#{table_alias}".spatial_model_type = '#{name}' AND "#{table_alias}".spatial_model_id = #{table_name}.id))
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
module InstanceMethods
|
80
|
+
def has_spatial_features?
|
81
|
+
true
|
82
|
+
end
|
83
|
+
|
84
|
+
def features_cache_key
|
85
|
+
"#{self.class.name}/#{self.id}-#{features.maximum(:id)}-#{features.size}"
|
86
|
+
end
|
87
|
+
|
88
|
+
def polygons?
|
89
|
+
!features.polygons.empty?
|
90
|
+
end
|
91
|
+
|
92
|
+
def lines?
|
93
|
+
!features.lines.empty?
|
94
|
+
end
|
95
|
+
|
96
|
+
def points?
|
97
|
+
!features.points.empty?
|
98
|
+
end
|
99
|
+
|
100
|
+
def features?
|
101
|
+
features.present?
|
102
|
+
end
|
103
|
+
|
104
|
+
# 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
|
105
|
+
def has_spatial_features_hash?
|
106
|
+
respond_to?(:features_hash)
|
107
|
+
end
|
108
|
+
|
109
|
+
def intersects?(other)
|
110
|
+
self.class.intersecting(other).exists?(self)
|
111
|
+
end
|
112
|
+
|
113
|
+
def total_intersection_area_in_square_meters(klass, options = {})
|
114
|
+
self.class
|
115
|
+
.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))
|
116
|
+
.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))
|
117
|
+
.joins(%Q(INNER JOIN features "features_for_other" ON "features_for_other".spatial_model_type = '#{klass}'))
|
118
|
+
.where(:id => self.id)
|
119
|
+
.where('ST_DWithin(features_for.geog_lowres, features_for_other.geog_lowres, 0)')
|
120
|
+
.group("#{self.class.table_name}.id")
|
121
|
+
.first
|
122
|
+
.try(:intersection_area_in_square_meters) || 0
|
123
|
+
end
|
124
|
+
|
125
|
+
def total_intersection_area_in_hectares(klass)
|
126
|
+
Formatters::HECTARES.call(total_intersection_area_in_square_meters(klass))
|
127
|
+
end
|
128
|
+
|
129
|
+
def total_intersection_area_percentage(klass)
|
130
|
+
return 0.0 unless features_area_in_square_meters > 0
|
131
|
+
|
132
|
+
((total_intersection_area_in_square_meters(klass) / features_area_in_square_meters) * 100).round(1)
|
133
|
+
end
|
134
|
+
|
135
|
+
def features_area_in_square_meters
|
136
|
+
@features_area_in_square_meters ||= features.sum('ST_Area(features.geog_lowres)')
|
137
|
+
end
|
138
|
+
|
139
|
+
def features_area_in_hectares
|
140
|
+
Formatters::HECTARES.call(features_area_in_square_meters)
|
141
|
+
end
|
142
|
+
|
143
|
+
def spatial_cache_for(klass)
|
144
|
+
spatial_cache.where(:intersection_model_type => klass).first
|
145
|
+
end
|
146
|
+
|
147
|
+
def spatial_cache_for?(klass, buffer_in_meters)
|
148
|
+
if cache = spatial_cache_for(klass)
|
149
|
+
return cache.cache_distance.nil? if buffer_in_meters.nil? # cache must be total if no buffer_in_meters
|
150
|
+
return true if cache.cache_distance.nil? # always good if cache is total
|
151
|
+
|
152
|
+
return buffer_in_meters <= cache.cache_distance
|
153
|
+
else
|
154
|
+
return false
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
module ArcGISKmzFeatures
|
2
|
+
require 'open-uri'
|
3
|
+
require 'digest/md5'
|
4
|
+
|
5
|
+
def update_features!
|
6
|
+
@feature_error_messages = []
|
7
|
+
kml_array = []
|
8
|
+
cache_kml = ''
|
9
|
+
|
10
|
+
Array(arcgis_kmz_url).each do |url|
|
11
|
+
kml_array << open_kmz_url(url)
|
12
|
+
cache_kml << kml_array.last.to_s
|
13
|
+
end
|
14
|
+
|
15
|
+
if has_spatial_features_hash?
|
16
|
+
new_features_hash = Digest::MD5.hexdigest(cache_kml) if cache_kml.present?
|
17
|
+
|
18
|
+
if new_features_hash != self.features_hash
|
19
|
+
replace_features(kml_array)
|
20
|
+
update_attributes(:features_hash => new_features_hash)
|
21
|
+
else
|
22
|
+
return false
|
23
|
+
end
|
24
|
+
else
|
25
|
+
replace_features(kml_array)
|
26
|
+
end
|
27
|
+
|
28
|
+
return true
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def replace_features(kml_array)
|
34
|
+
new_features = []
|
35
|
+
kml_array.each {|kml| new_features.concat build_features(kml) }
|
36
|
+
|
37
|
+
ActiveRecord::Base.transaction do
|
38
|
+
self.features.destroy_all
|
39
|
+
new_features.each(&:save)
|
40
|
+
|
41
|
+
@feature_error_messages.concat new_features.collect {|feature| "Feature #{feature.name}: #{feature.errors.full_messages.to_sentence}" if feature.errors.present? }.compact.flatten
|
42
|
+
if @feature_error_messages.present?
|
43
|
+
raise UpdateError, "Error updating #{self.class} #{self.id}. #{@feature_error_messages.to_sentence}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def build_features(kml)
|
49
|
+
new_type_features = []
|
50
|
+
|
51
|
+
extract_kml_features(kml) do |feature_type, feature, name, metadata|
|
52
|
+
begin
|
53
|
+
new_type_features << build_feature(feature_type, name, metadata, build_geom(feature))
|
54
|
+
rescue => e
|
55
|
+
@feature_error_messages << e.message
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
return new_type_features
|
60
|
+
end
|
61
|
+
|
62
|
+
# Use ST_Force_2D to discard z-coordinates that cause failures later in the process
|
63
|
+
def build_geom(feature)
|
64
|
+
if make_valid?
|
65
|
+
geom = ActiveRecord::Base.connection.select_value("SELECT ST_CollectionExtract(ST_MakeValid(ST_Force_2D(ST_GeomFromKML('#{feature}'))),3 )")
|
66
|
+
else
|
67
|
+
geom = ActiveRecord::Base.connection.select_value("SELECT ST_Force_2D(ST_GeomFromKML('#{feature}'))")
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def extract_kml_features(kml, &block)
|
72
|
+
Nokogiri::XML(kml).css('Placemark').each do |placemark|
|
73
|
+
name = placemark.css('name').text
|
74
|
+
metadata = Hash[Nokogiri::XML(placemark.css('description').text).css('td').collect(&:text).each_slice(2).to_a]
|
75
|
+
|
76
|
+
{'Polygon' => 'POLYGON', 'LineString' => 'LINE', 'Point' => 'POINT'}.each do |kml_type, sql_type|
|
77
|
+
placemark.css(kml_type).each do |feature|
|
78
|
+
yield sql_type, feature, name, metadata
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def build_feature(feature_type, name, metadata, geom)
|
85
|
+
Feature.new(spatial_model: self, name: name, metadata: metadata, feature_type: feature_type, geog: geom)
|
86
|
+
end
|
87
|
+
|
88
|
+
def open_kmz_url(url)
|
89
|
+
Zip::InputStream.open(open(url)) do |io|
|
90
|
+
while (entry = io.get_next_entry)
|
91
|
+
return io.read if entry.name.downcase == 'doc.kml'
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
return nil
|
96
|
+
end
|
97
|
+
|
98
|
+
# Can be overridden to use PostGIS to force geometry to be valid
|
99
|
+
def make_valid?
|
100
|
+
false
|
101
|
+
end
|
102
|
+
|
103
|
+
class UpdateError < StandardError; end
|
104
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
class Feature < ActiveRecord::Base
|
2
|
+
belongs_to :spatial_model, :polymorphic => :true
|
3
|
+
|
4
|
+
self.rgeo_factory_generator = RGeo::Geos.factory_generator
|
5
|
+
|
6
|
+
before_validation :sanitize_feature_type
|
7
|
+
validates_presence_of :geog
|
8
|
+
validate :geometry_is_valid
|
9
|
+
validates_inclusion_of :feature_type, :in => ['polygon', 'point', 'line']
|
10
|
+
after_save :cache_derivatives
|
11
|
+
|
12
|
+
store :metadata
|
13
|
+
|
14
|
+
def self.polygons
|
15
|
+
where(:feature_type => 'polygon')
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.lines
|
19
|
+
where(:feature_type => 'line')
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.points
|
23
|
+
where(:feature_type => 'point')
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.for_kml(options = {})
|
27
|
+
if options[:simplified]
|
28
|
+
select("features.name, features.kml, features.metadata").where("features.name IS NULL OR features.name NOT IN ('s', 't')")
|
29
|
+
else
|
30
|
+
select("features.name, ST_AsKML(features.geog, 6) AS kml, features.metadata")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.invalid
|
35
|
+
select('features.*, ST_IsValidReason(geog::geometry) AS invalid_geometry_message').where.not('ST_IsValid(geog::geometry)')
|
36
|
+
end
|
37
|
+
|
38
|
+
def envelope(buffer_in_meters = 0)
|
39
|
+
self.class.select("ST_Envelope(ST_Buffer(features.geog, #{buffer_in_meters})::geometry) AS result").where(:id => id).first.result.exterior_ring.points.values_at(0,2)
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def cache_derivatives
|
45
|
+
self.class.connection.execute "UPDATE features SET geog_lowres = ST_SimplifyPreserveTopology(geog::geometry, 0.0001) WHERE id = #{self.id}"
|
46
|
+
self.class.connection.execute "UPDATE features SET kml = ST_AsKML(geog_lowres::geometry, 5) WHERE id = #{self.id}"
|
47
|
+
end
|
48
|
+
|
49
|
+
def geometry_is_valid
|
50
|
+
if geog?
|
51
|
+
instance = self.class.unscoped.invalid.from("(SELECT ST_GeometryFromText('#{self.geog}') AS geog) #{self.class.table_name}").to_a.first
|
52
|
+
errors.add :geog, instance.invalid_geometry_message if instance
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def sanitize_feature_type
|
57
|
+
self.feature_type = self.feature_type.to_s.strip.downcase
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module SpatialFeatures
|
2
|
+
# Splits overlapping features into separate polygons at their areas of overlap, and returns an array of objects
|
3
|
+
# with kml for the overlapping area and a list of the record ids whose kml overlapped within that area
|
4
|
+
def self.venn_polygons(*scopes)
|
5
|
+
options = scopes.extract_options!
|
6
|
+
column = options[:simplified] ? 'geog_lowres' : 'geog'
|
7
|
+
scope = scopes.collect do |scope|
|
8
|
+
scope.klass.from(scope, scope.klass.table_name).joins(:features).where('features.feature_type = ?', 'polygon').select("features.#{column}::geometry AS the_geom").to_sql
|
9
|
+
end.join(' UNION ')
|
10
|
+
|
11
|
+
sql = "
|
12
|
+
SELECT scope.id, scope.type, ST_AsKML(geom) AS kml FROM ST_Dump((
|
13
|
+
SELECT ST_Polygonize(the_geom) AS the_geom FROM (
|
14
|
+
|
15
|
+
SELECT ST_Union(the_geom) AS the_geom FROM (
|
16
|
+
|
17
|
+
-- Handle Multigeometry
|
18
|
+
SELECT ST_ExteriorRing((ST_DumpRings(the_geom)).geom) AS the_geom
|
19
|
+
FROM (#{scope}) AS scope
|
20
|
+
|
21
|
+
) AS exterior_lines
|
22
|
+
|
23
|
+
) AS noded_lines
|
24
|
+
WHERE NOT ST_IsEmpty(the_geom) -- Ignore empty geometry from ST_Union if there are no polygons because polygonize will explode
|
25
|
+
|
26
|
+
)) AS venn_polygons
|
27
|
+
"
|
28
|
+
|
29
|
+
# Join with the original polygons so we can determine which original polygons each venn polygon came from
|
30
|
+
scope = scopes.collect do |scope|
|
31
|
+
scope.klass.from(scope, scope.klass.table_name).joins(:features).where('features.feature_type = ?', 'polygon').select("#{scope.klass.table_name}.id, features.spatial_model_type AS type, features.#{column}").to_sql
|
32
|
+
end.join(' UNION ')
|
33
|
+
sql <<
|
34
|
+
"INNER JOIN (#{scope}) AS scope
|
35
|
+
ON ST_Covers(scope.#{column}, ST_PointOnSurface(venn_polygons.geom)) -- Shrink the venn polygons so they don't share edges with the original polygons which could cause varying results due to tiny inaccuracy"
|
36
|
+
|
37
|
+
# Eager load the records for each venn polygon
|
38
|
+
eager_load_hash = Hash.new {|hash, key| hash[key] = []}
|
39
|
+
polygons = ActiveRecord::Base.connection.select_all(sql)
|
40
|
+
polygons.group_by{|row| row['type']}.each do |record_type, rows|
|
41
|
+
rows.each do |row|
|
42
|
+
eager_load_hash[record_type] << row['id']
|
43
|
+
end
|
44
|
+
end
|
45
|
+
eager_load_hash.each do |record_type, ids|
|
46
|
+
eager_load_hash[record_type] = record_type.constantize.find(ids)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Instantiate objects to hold the kml and records for each venn polygon
|
50
|
+
polygons.group_by{|row| row['kml']}.collect do |kml, rows|
|
51
|
+
# Uniq on row id in case a single record had self intersecting multi geometry, which would cause it to appear duplicated on a single venn polygon
|
52
|
+
records = rows.uniq{|row| row.values_at('id', 'type') }.collect{|row| eager_load_hash.fetch(row['type']).detect{|record| record.id == row['id'].to_i } }
|
53
|
+
OpenStruct.new(:kml => kml, :records => records)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# GEMS
|
2
|
+
require "activerecord-postgis-adapter"
|
3
|
+
|
4
|
+
# LIB
|
5
|
+
require 'spatial_features/caching'
|
6
|
+
require 'spatial_features/venn_polygons'
|
7
|
+
require 'spatial_features/has_spatial_features'
|
8
|
+
|
9
|
+
require 'spatial_features/import/arcgis_kmz_features'
|
10
|
+
|
11
|
+
require 'spatial_features/controller_helpers/spatial_extensions'
|
12
|
+
|
13
|
+
require 'spatial_features/models/feature'
|
14
|
+
require 'spatial_features/models/spatial_cache'
|
15
|
+
require 'spatial_features/models/spatial_proximity'
|
16
|
+
|
17
|
+
module SpatialFeatures
|
18
|
+
end
|
19
|
+
|
20
|
+
# Load the act method
|
21
|
+
ActiveRecord::Base.send :extend, SpatialFeatures::ActMethod
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/test/dummy/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
|
6
|
+
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|