Class: Feature

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/feature.rb

Constant Summary collapse

FEATURE_TYPES =
%w(polygon point line)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_metersObject



36
37
38
39
# File 'app/models/feature.rb', line 36

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/feature.rb', line 69

def self.cache_derivatives(options = {})
  options.reverse_merge! :lowres_simplification => 2, :lowres_precision => 5

  update_all <<-SQL.squish
    geom         = ST_Transform(geog::geometry, #{detect_srid('geom')}),
    north        = ST_YMax(geog::geometry),
    east         = ST_XMax(geog::geometry),
    south        = ST_YMin(geog::geometry),
    west         = ST_XMin(geog::geometry),
    area         = ST_Area(geog),
    centroid     = ST_PointOnSurface(geog::geometry)
  SQL

  update_all <<-SQL.squish
    geom_lowres  = ST_SimplifyPreserveTopology(geom, #{options[:lowres_simplification]})
  SQL

  update_all <<-SQL.squish
    kml          = ST_AsKML(geog, 6),
    kml_lowres   = ST_AsKML(geom_lowres, #{options[:lowres_precision]}),
    kml_centroid = ST_AsKML(centroid)
  SQL
end

.intersecting(other) ⇒ Object



48
49
50
# File 'app/models/feature.rb', line 48

def self.intersecting(other)
  join_other_features(other).where('ST_Intersects(features.geom_lowres, other_features.geom_lowres)').uniq
end

.invalidObject



52
53
54
# File 'app/models/feature.rb', line 52

def self.invalid
  select('features.*, ST_IsValidReason(geog::geometry) AS invalid_geometry_message').where.not('ST_IsValid(geog::geometry)')
end

.linesObject



28
29
30
# File 'app/models/feature.rb', line 28

def self.lines
  where(:feature_type => 'line')
end

.pointsObject



32
33
34
# File 'app/models/feature.rb', line 32

def self.points
  where(:feature_type => 'point')
end

.polygonsObject



24
25
26
# File 'app/models/feature.rb', line 24

def self.polygons
  where(:feature_type => 'polygon')
end

.total_intersection_area_in_square_meters(other_features) ⇒ Object



41
42
43
44
45
46
# File 'app/models/feature.rb', line 41

def self.total_intersection_area_in_square_meters(other_features)
  unscoped
    .from("(#{select("ST_Union(geom) AS geom").to_sql}) features, (#{other_features.to_sql}) other_features")
    .pluck('ST_Area(ST_UNION(ST_Intersection(features.geom, other_features.geom)))')
    .first.to_f
end

.validObject



56
57
58
# File 'app/models/feature.rb', line 56

def self.valid
  where('ST_IsValid(geog::geometry)')
end

.with_metadata(k, v) ⇒ Object



16
17
18
19
20
21
22
# File 'app/models/feature.rb', line 16

def self.(k, v)
  if k.present? && v.present?
    where('metadata->? = ?', k, v)
  else
    all
  end
end

Instance Method Details

#cache_derivatives(*args) ⇒ Object



97
98
99
# File 'app/models/feature.rb', line 97

def cache_derivatives(*args)
  self.class.where(:id => self.id).cache_derivatives(*args)
end

#envelope(buffer_in_meters = 0) ⇒ Object



60
61
62
63
64
65
66
67
# File 'app/models/feature.rb', line 60

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_boundsObject



93
94
95
# File 'app/models/feature.rb', line 93

def feature_bounds
  {n: north, e: east, s: south, w: west}
end

#kml(options = {}) ⇒ Object



101
102
103
104
105
# File 'app/models/feature.rb', line 101

def kml(options = {})
  geometry = options[:lowres] ? kml_lowres : super()
  geometry = "<MultiGeometry>#{geometry}#{kml_centroid}</MultiGeometry>" if options[:centroid]
  return geometry
end

#make_valid?Boolean



107
108
109
# File 'app/models/feature.rb', line 107

def make_valid?
  @make_valid
end