Class: Feature

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.area_in_square_metersObject



30
31
32
33
# File 'app/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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/feature.rb', line 63

def self.cache_derivatives(options = {})
  options.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  update_all <<-SQL.squish\n    geom_lowres  = ST_SimplifyPreserveTopology(geom, \#{options[:lowres_simplification]})\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

.intersecting(other) ⇒ Object



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

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

.invalidObject



46
47
48
# File 'app/models/feature.rb', line 46

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

.linesObject



22
23
24
# File 'app/models/feature.rb', line 22

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

.pointsObject



26
27
28
# File 'app/models/feature.rb', line 26

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

.polygonsObject



18
19
20
# File 'app/models/feature.rb', line 18

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

.total_intersection_area_in_square_meters(other_features) ⇒ Object



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

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



50
51
52
# File 'app/models/feature.rb', line 50

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

.with_metadata(k, v) ⇒ Object



10
11
12
13
14
15
16
# File 'app/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



91
92
93
# File 'app/models/feature.rb', line 91

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

#envelope(buffer_in_meters = 0) ⇒ Object



54
55
56
57
58
59
60
61
# File 'app/models/feature.rb', line 54

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



87
88
89
# File 'app/models/feature.rb', line 87

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

#kml(options = {}) ⇒ Object



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

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