Module: SpatialStats::Weights::Contiguous
- Defined in:
- lib/spatial_stats/weights/contiguous.rb
Overview
Contiguous weights module includes methods that provide an interface to coniguous weights queries and formats the result properly to utilize a weights matrix.
Class Method Summary collapse
-
.queen(scope, field) ⇒ WeightsMatrix
Compute queen weights matrix for a scope.
-
.rook(scope, field) ⇒ WeightsMatrix
Compute rook weights matrix for a scope.
Class Method Details
.queen(scope, field) ⇒ WeightsMatrix
Compute queen weights matrix for a scope.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/spatial_stats/weights/contiguous.rb', line 47 def self.queen(scope, field) neighbors = SpatialStats::Queries::Weights .queen_contiguity_neighbors(scope, field) # get keys to make sure we have consistent dimensions when # some entries don't have neighbors. # define a new hash that has all the keys from scope keys = SpatialStats::Queries::Variables.query_field(scope, scope.klass.primary_key) neighbors = neighbors.group_by(&:i_id) missing_neighbors = Hash[(keys - neighbors.keys).map { |key| [key, []] }] neighbors = neighbors.merge(missing_neighbors) weights = neighbors.transform_values do |value| value.map do |neighbor| hash = { id: neighbor[:j_id] } hash[:weight] = 1 hash end end SpatialStats::Weights::WeightsMatrix.new(weights) end |
.rook(scope, field) ⇒ WeightsMatrix
Compute rook weights matrix for a scope.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/spatial_stats/weights/contiguous.rb', line 17 def self.rook(scope, field) neighbors = SpatialStats::Queries::Weights .rook_contiguity_neighbors(scope, field) # get keys to make sure we have consistent dimensions when # some entries don't have neighbors. # define a new hash that has all the keys from scope keys = SpatialStats::Queries::Variables.query_field(scope, scope.klass.primary_key) neighbors = neighbors.group_by(&:i_id) missing_neighbors = Hash[(keys - neighbors.keys).map { |key| [key, []] }] neighbors = neighbors.merge(missing_neighbors) weights = neighbors.transform_values do |value| value.map do |neighbor| hash = { id: neighbor[:j_id] } hash[:weight] = 1 hash end end SpatialStats::Weights::WeightsMatrix.new(weights) end |