Module: SpatialFeatures::InstanceMethods

Defined in:
lib/spatial_features/has_spatial_features.rb

Instance Method Summary collapse

Instance Method Details

#features?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/spatial_features/has_spatial_features.rb', line 106

def features?
  features.present?
end

#features_area_in_hectaresObject



145
146
147
# File 'lib/spatial_features/has_spatial_features.rb', line 145

def features_area_in_hectares
  Formatters::HECTARES.call(features_area_in_square_meters)
end

#features_area_in_square_metersObject



141
142
143
# File 'lib/spatial_features/has_spatial_features.rb', line 141

def features_area_in_square_meters
  @features_area_in_square_meters ||= features.sum('ST_Area(features.geog_lowres)')
end

#features_cache_keyObject



89
90
91
92
# File 'lib/spatial_features/has_spatial_features.rb', line 89

def features_cache_key
    max_id, count = features.pluck("MAX(id), COUNT(*)").first
  "#{self.class.name}/#{self.id}-#{max_id}-#{count}"
end

#has_spatial_features?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/spatial_features/has_spatial_features.rb', line 85

def has_spatial_features?
  true
end

#has_spatial_features_hash?Boolean

Returns true if the model stores a hash of the features so we don’t need to process the features if they haven’t changed

Returns:

  • (Boolean)


111
112
113
# File 'lib/spatial_features/has_spatial_features.rb', line 111

def has_spatial_features_hash?
  respond_to?(:features_hash)
end

#intersects?(other) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/spatial_features/has_spatial_features.rb', line 115

def intersects?(other)
  self.class.intersecting(other).exists?(self)
end

#lines?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/spatial_features/has_spatial_features.rb', line 98

def lines?
  !features.lines.empty?
end

#points?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/spatial_features/has_spatial_features.rb', line 102

def points?
  !features.points.empty?
end

#polygons?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/spatial_features/has_spatial_features.rb', line 94

def polygons?
  !features.polygons.empty?
end

#spatial_cache_for(klass) ⇒ Object



149
150
151
# File 'lib/spatial_features/has_spatial_features.rb', line 149

def spatial_cache_for(klass)
  spatial_cache.where(:intersection_model_type => klass).first
end

#spatial_cache_for?(klass, buffer_in_meters) ⇒ Boolean

Returns:

  • (Boolean)


153
154
155
156
157
158
159
160
161
162
# File 'lib/spatial_features/has_spatial_features.rb', line 153

def spatial_cache_for?(klass, buffer_in_meters)
  if cache = spatial_cache_for(klass)
    return cache.cache_distance.nil? if buffer_in_meters.nil? # cache must be total if no buffer_in_meters
    return true if cache.cache_distance.nil? # always good if cache is total

    return buffer_in_meters <= cache.cache_distance
  else
    return false
  end
end

#total_intersection_area_in_hectares(klass) ⇒ Object



131
132
133
# File 'lib/spatial_features/has_spatial_features.rb', line 131

def total_intersection_area_in_hectares(klass)
  Formatters::HECTARES.call(total_intersection_area_in_square_meters(klass))
end

#total_intersection_area_in_square_meters(klass, options = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/spatial_features/has_spatial_features.rb', line 119

def total_intersection_area_in_square_meters(klass, options = {})
  self.class
    .select(%Q(ST_Area(ST_Intersection(ST_Union(features_for.geog_lowres::geometry), ST_Union(features_for_other.geog_lowres::geometry))::geography) AS intersection_area_in_square_meters))
    .joins(%Q(INNER JOIN features "features_for" ON "features_for".spatial_model_type = '#{self.class}' AND "features_for".spatial_model_id = #{self.class.table_name}.id))
    .joins(%Q(INNER JOIN features "features_for_other" ON "features_for_other".spatial_model_type = '#{klass}'))
    .where(:id => self.id)
    .where('ST_DWithin(features_for.geog_lowres, features_for_other.geog_lowres, 0)')
    .group("#{self.class.table_name}.id")
    .first
    .try(:intersection_area_in_square_meters) || 0
end

#total_intersection_area_percentage(klass) ⇒ Object



135
136
137
138
139
# File 'lib/spatial_features/has_spatial_features.rb', line 135

def total_intersection_area_percentage(klass)
  return 0.0 unless features_area_in_square_meters > 0

  ((total_intersection_area_in_square_meters(klass) / features_area_in_square_meters) * 100).round(1)
end