Class: AbstractFeature
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- AbstractFeature
- Defined in:
- app/models/abstract_feature.rb
Direct Known Subclasses
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
-
.geojson(lowres: false, precision: 6, properties: {}, srid: 4326) ⇒ Object
default srid is 4326 so output is Google Maps compatible.
- .intersecting(other) ⇒ Object
- .invalid(column = 'geog::geometry') ⇒ 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
- .within_distance(lat, lng, distance_in_meters) ⇒ Object
Instance Method Summary collapse
- #cache_derivatives(*args) ⇒ Object
- #envelope(buffer_in_meters = 0) ⇒ Object
- #feature_bounds ⇒ Object
- #geojson(*args) ⇒ Object
- #kml(options = {}) ⇒ Object
- #make_valid? ⇒ Boolean
Instance Attribute Details
#make_valid=(value) ⇒ Object
Sets the attribute make_valid
9 10 11 |
# File 'app/models/abstract_feature.rb', line 9 def make_valid=(value) @make_valid = value end |
Class Method Details
.area_in_square_meters(geom = 'geom_lowres') ⇒ Object
49 50 51 52 |
# File 'app/models/abstract_feature.rb', line 49 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
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/models/abstract_feature.rb', line 85 def self.cache_derivatives( = {}) 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 invalid('geom').update_all <<-SQL.squish geom = ST_Buffer(geom, 0) SQL update_all <<-SQL.squish geom_lowres = ST_SimplifyPreserveTopology(geom, #{.fetch(:lowres_simplification, lowres_simplification)}) SQL invalid('geom_lowres').update_all <<-SQL.squish geom_lowres = ST_Buffer(geom_lowres, 0) SQL end |
.cache_key ⇒ Object
19 20 21 22 |
# File 'app/models/abstract_feature.rb', line 19 def self.cache_key result = connection.select_one(all.select('max(id) AS max, count(*) AS count').to_sql) "#{result['max']}-#{result['count']}" end |
.geojson(lowres: false, precision: 6, properties: {}, srid: 4326) ⇒ Object
default srid is 4326 so output is Google Maps compatible
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'app/models/abstract_feature.rb', line 109 def self.geojson(lowres: false, precision: 6, properties: {}, srid: 4326) # default srid is 4326 so output is Google Maps compatible column = lowres ? "ST_Transform(geom_lowres, #{srid})" : 'geog' properties_sql = <<~SQL if properties.present? , 'properties', json_build_object(#{properties.map {|k,v| "'#{k}',#{v}" }.join(',') }) SQL sql = <<~SQL json_build_object( 'type', 'FeatureCollection', 'features', json_agg( json_build_object( 'type', 'Feature', 'geometry', ST_AsGeoJSON(#{column}, #{precision})::json #{properties_sql} ) ) ) SQL connection.select_value(all.select(sql)) end |
.intersecting(other) ⇒ Object
64 65 66 |
# File 'app/models/abstract_feature.rb', line 64 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
68 69 70 |
# File 'app/models/abstract_feature.rb', line 68 def self.invalid(column = 'geog::geometry') select("features.*, ST_IsValidReason(#{column}) AS invalid_geometry_message").where.not("ST_IsValid(#{column})") end |
.lines ⇒ Object
36 37 38 |
# File 'app/models/abstract_feature.rb', line 36 def self.lines where(:feature_type => 'line') end |
.points ⇒ Object
40 41 42 |
# File 'app/models/abstract_feature.rb', line 40 def self.points where(:feature_type => 'point') end |
.polygons ⇒ Object
32 33 34 |
# File 'app/models/abstract_feature.rb', line 32 def self.polygons where(:feature_type => 'polygon') end |
.total_intersection_area_in_square_meters(other_features, geom = 'geom_lowres') ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'app/models/abstract_feature.rb', line 54 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 = base_class.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
72 73 74 |
# File 'app/models/abstract_feature.rb', line 72 def self.valid where('ST_IsValid(geog::geometry)') end |
.with_metadata(k, v) ⇒ Object
24 25 26 27 28 29 30 |
# File 'app/models/abstract_feature.rb', line 24 def self.(k, v) if k.present? && v.present? where('metadata->? = ?', k, v) else all end end |
.within_distance(lat, lng, distance_in_meters) ⇒ Object
44 45 46 47 |
# File 'app/models/abstract_feature.rb', line 44 def self.within_distance(lat, lng, distance_in_meters) # where("ST_DWithin(features.geog, ST_SetSRID( ST_Point( -71.104, 42.315), 4326)::geography, :distance)", :lat => lat, :lng => lng, :distance => distance_in_meters) where("ST_DWithin(features.geog, ST_Point(:lng, :lat), :distance)", :lat => lat, :lng => lng, :distance => distance_in_meters) end |
Instance Method Details
#cache_derivatives(*args) ⇒ Object
134 135 136 |
# File 'app/models/abstract_feature.rb', line 134 def cache_derivatives(*args) self.class.where(:id => self.id).cache_derivatives(*args) end |
#envelope(buffer_in_meters = 0) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'app/models/abstract_feature.rb', line 76 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
130 131 132 |
# File 'app/models/abstract_feature.rb', line 130 def feature_bounds {n: north, e: east, s: south, w: west} end |
#geojson(*args) ⇒ Object
144 145 146 |
# File 'app/models/abstract_feature.rb', line 144 def geojson(*args) self.class.where(id: id).geojson(*args) end |
#kml(options = {}) ⇒ Object
138 139 140 141 142 |
# File 'app/models/abstract_feature.rb', line 138 def kml( = {}) geometry = [:lowres] ? kml_lowres : super() geometry = "<MultiGeometry>#{geometry}#{kml_centroid}</MultiGeometry>" if [:centroid] return geometry end |
#make_valid? ⇒ Boolean
148 149 150 |
# File 'app/models/abstract_feature.rb', line 148 def make_valid? @make_valid end |