Class: Feature
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Feature
- Defined in:
- lib/spatial_features/models/feature.rb
Class Method Summary collapse
- .area_in_square_meters ⇒ Object
- .cache_derivatives(options = {}) ⇒ Object
- .invalid ⇒ Object
- .lines ⇒ Object
- .points ⇒ Object
- .polygons ⇒ Object
- .total_intersection_area_in_square_meters(other) ⇒ Object
- .valid ⇒ Object
- .with_metadata(k, v) ⇒ Object
Instance Method Summary collapse
- #cache_derivatives(*args) ⇒ Object
- #envelope(buffer_in_meters = 0) ⇒ Object
- #kml(options = {}) ⇒ Object
Class Method Details
.area_in_square_meters ⇒ Object
30 31 32 33 |
# File 'lib/spatial_features/models/feature.rb', line 30 def self.area_in_square_meters current_scope = all unscoped { connection.select_value(select('ST_Area(ST_Union(geom))').from(current_scope, :features)).to_f } end |
.cache_derivatives(options = {}) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/spatial_features/models/feature.rb', line 60 def self.cache_derivatives( = {}) .reverse_merge! :lowres_simplification => 0.00001, :lowres_precision => 5 update_all("area = ST_Area(geog), geom = ST_Transform(geog::geometry, 26910), geog_lowres = ST_SimplifyPreserveTopology(geog::geometry, #{options[:lowres_simplification]})" .squish) update_all("kml = ST_AsKML(features.geog, 6), kml_lowres = ST_AsKML(geog_lowres::geometry, #{options[:lowres_precision]})" .squish) end |
.invalid ⇒ Object
43 44 45 |
# File 'lib/spatial_features/models/feature.rb', line 43 def self.invalid select('features.*, ST_IsValidReason(geog::geometry) AS invalid_geometry_message').where.not('ST_IsValid(geog::geometry)') end |
.lines ⇒ Object
22 23 24 |
# File 'lib/spatial_features/models/feature.rb', line 22 def self.lines where(:feature_type => 'line') end |
.points ⇒ Object
26 27 28 |
# File 'lib/spatial_features/models/feature.rb', line 26 def self.points where(:feature_type => 'point') end |
.polygons ⇒ Object
18 19 20 |
# File 'lib/spatial_features/models/feature.rb', line 18 def self.polygons where(:feature_type => 'polygon') end |
.total_intersection_area_in_square_meters(other) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/spatial_features/models/feature.rb', line 35 def self.total_intersection_area_in_square_meters(other) scope = join_other_features(other) .where('ST_Intersects(features.geog_lowres, other_features.geog_lowres)') .select('ST_Area(ST_Intersection(ST_Union(features.geog_lowres::geometry), ST_Union(other_features.geog_lowres::geometry))::geography) AS intersection_area_in_square_meters') connection.select_value(scope).to_f end |
.valid ⇒ Object
47 48 49 |
# File 'lib/spatial_features/models/feature.rb', line 47 def self.valid where('ST_IsValid(geog::geometry)') end |
.with_metadata(k, v) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/spatial_features/models/feature.rb', line 10 def self.(k, v) if k.present? && v.present? where('metadata->? = ?', k, v) else all end end |
Instance Method Details
#cache_derivatives(*args) ⇒ Object
72 73 74 |
# File 'lib/spatial_features/models/feature.rb', line 72 def cache_derivatives(*args) self.class.where(:id => self.id).cache_derivatives(*args) end |
#envelope(buffer_in_meters = 0) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/spatial_features/models/feature.rb', line 51 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 |
#kml(options = {}) ⇒ Object
76 77 78 |
# File 'lib/spatial_features/models/feature.rb', line 76 def kml( = {}) [:lowres] ? kml_lowres : super() end |