Class: Feature
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Feature
- Defined in:
- app/models/feature.rb
Constant Summary collapse
- FEATURE_TYPES =
%w(polygon point line)
Instance Attribute Summary collapse
-
#make_valid ⇒ Object
writeonly
Sets the attribute make_valid.
Class Method Summary collapse
- .area_in_square_meters(geom = 'geom_lowres') ⇒ Object
- .cache_derivatives(options = {}) ⇒ Object
- .cache_key ⇒ Object
- .detect_srid(column_name) ⇒ Object
- .intersecting(other) ⇒ Object
- .invalid(column = 'geog::geometry') ⇒ Object
- .join_other_features(other) ⇒ Object
- .lines ⇒ Object
- .points ⇒ Object
- .polygons ⇒ Object
- .total_intersection_area_in_square_meters(other_features, geom = 'geom_lowres') ⇒ Object
- .valid ⇒ Object
- .with_metadata(k, v) ⇒ Object
Instance Method Summary collapse
- #cache_derivatives(*args) ⇒ Object
- #envelope(buffer_in_meters = 0) ⇒ Object
- #feature_bounds ⇒ Object
- #kml(options = {}) ⇒ Object
- #make_valid? ⇒ Boolean
Instance Attribute Details
#make_valid=(value) ⇒ Object
Sets the attribute make_valid
4 5 6 |
# File 'app/models/feature.rb', line 4 def make_valid=(value) @make_valid = value end |
Class Method Details
.area_in_square_meters(geom = 'geom_lowres') ⇒ Object
39 40 41 42 |
# File 'app/models/feature.rb', line 39 def self.area_in_square_meters(geom = 'geom_lowres') current_scope = all.polygons unscoped { connection.select_value(select("ST_Area(ST_Union(#{geom}))").from(current_scope, :features)).to_f } end |
.cache_derivatives(options = {}) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/models/feature.rb', line 75 def self.cache_derivatives( = {}) .reverse_merge! :lowres_simplification => 2, :lowres_precision => 5 update_all " geom = ST_Transform(geog::geometry, \#{detect_srid('geom')}),\n north = ST_YMax(geog::geometry),\n east = ST_XMax(geog::geometry),\n south = ST_YMin(geog::geometry),\n west = ST_XMin(geog::geometry),\n area = ST_Area(geog),\n centroid = ST_PointOnSurface(geog::geometry)\n SQL\n\n invalid('geom').update_all <<-SQL.squish\n geom = ST_Buffer(geom, 0)\n SQL\n\n update_all <<-SQL.squish\n geom_lowres = ST_SimplifyPreserveTopology(geom, \#{options[:lowres_simplification]})\n SQL\n\n invalid('geom_lowres').update_all <<-SQL.squish\n geom_lowres = ST_Buffer(geom_lowres, 0)\n SQL\n\n update_all <<-SQL.squish\n kml = ST_AsKML(geog, 6),\n kml_lowres = ST_AsKML(geom_lowres, \#{options[:lowres_precision]}),\n kml_centroid = ST_AsKML(centroid)\n SQL\nend\n".squish |
.cache_key ⇒ Object
15 16 17 |
# File 'app/models/feature.rb', line 15 def self.cache_key "#{maximum(:id)}-#{count}" end |
.detect_srid(column_name) ⇒ Object
137 138 139 |
# File 'app/models/feature.rb', line 137 def self.detect_srid(column_name) SRID_CACHE[column_name] ||= connection.select_value("SELECT Find_SRID('public', '#{table_name}', '#{column_name}')") end |
.intersecting(other) ⇒ Object
54 55 56 |
# File 'app/models/feature.rb', line 54 def self.intersecting(other) join_other_features(other).where('ST_Intersects(features.geom_lowres, other_features.geom_lowres)').uniq end |
.invalid(column = 'geog::geometry') ⇒ Object
58 59 60 |
# File 'app/models/feature.rb', line 58 def self.invalid(column = 'geog::geometry') select("features.*, ST_IsValidReason(#{column}) AS invalid_geometry_message").where.not("ST_IsValid(#{column})") end |
.join_other_features(other) ⇒ Object
141 142 143 |
# File 'app/models/feature.rb', line 141 def self.join_other_features(other) joins('INNER JOIN features AS other_features ON true').where(:other_features => {:id => other}) end |
.lines ⇒ Object
31 32 33 |
# File 'app/models/feature.rb', line 31 def self.lines where(:feature_type => 'line') end |
.points ⇒ Object
35 36 37 |
# File 'app/models/feature.rb', line 35 def self.points where(:feature_type => 'point') end |
.polygons ⇒ Object
27 28 29 |
# File 'app/models/feature.rb', line 27 def self.polygons where(:feature_type => 'polygon') end |
.total_intersection_area_in_square_meters(other_features, geom = 'geom_lowres') ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'app/models/feature.rb', line 44 def self.total_intersection_area_in_square_meters(other_features, geom = 'geom_lowres') scope = unscope(:select).select("ST_Union(#{geom}) AS geom").polygons other_scope = other_features.polygons query = unscoped.select('ST_Area(ST_Intersection(ST_Union(features.geom), ST_Union(other_features.geom)))') .from(scope, "features") .joins("INNER JOIN (#{other_scope.to_sql}) AS other_features ON ST_Intersects(features.geom, other_features.geom)") return connection.select_value(query).to_f end |
.valid ⇒ Object
62 63 64 |
# File 'app/models/feature.rb', line 62 def self.valid where('ST_IsValid(geog::geometry)') end |
.with_metadata(k, v) ⇒ Object
19 20 21 22 23 24 25 |
# File 'app/models/feature.rb', line 19 def self.(k, v) if k.present? && v.present? where('metadata->? = ?', k, v) else all end end |
Instance Method Details
#cache_derivatives(*args) ⇒ Object
111 112 113 |
# File 'app/models/feature.rb', line 111 def cache_derivatives(*args) self.class.where(:id => self.id).cache_derivatives(*args) end |
#envelope(buffer_in_meters = 0) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'app/models/feature.rb', line 66 def envelope(buffer_in_meters = 0) envelope_json = JSON.parse(self.class.select("ST_AsGeoJSON(ST_Envelope(ST_Buffer(features.geog, #{buffer_in_meters})::geometry)) AS result").where(:id => id).first.result) envelope_json = envelope_json["coordinates"].first raise "Can't calculate envelope for Feature #{self.id}" if envelope_json.blank? return envelope_json.values_at(0,2) end |
#feature_bounds ⇒ Object
107 108 109 |
# File 'app/models/feature.rb', line 107 def feature_bounds {n: north, e: east, s: south, w: west} end |
#kml(options = {}) ⇒ Object
115 116 117 118 119 |
# File 'app/models/feature.rb', line 115 def kml( = {}) geometry = [:lowres] ? kml_lowres : super() geometry = "<MultiGeometry>#{geometry}#{kml_centroid}</MultiGeometry>" if [:centroid] return geometry end |
#make_valid? ⇒ Boolean
121 122 123 |
# File 'app/models/feature.rb', line 121 def make_valid? @make_valid end |