Module: SpatialStats::Weights::Distant

Defined in:
lib/spatial_stats/weights/distant.rb

Overview

Distant weights module includes methods that provide an interface to distance-based weights queries and formats the result properly to utilize a weights matrix.

Class Method Summary collapse

Class Method Details

.distance_band(scope, field, bandwidth) ⇒ WeightsMatrix

Compute distance band weights matrix for a scope.

Parameters:

  • scope (ActiveRecord::Relation)

    to query

  • field (Symbol, String)

    with geometry in it

  • bandwidth (Numeric)

    of distance band

Returns:



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/distant.rb', line 17

def self.distance_band(scope, field, bandwidth)
  neighbors = SpatialStats::Queries::Weights
              .distance_band_neighbors(scope, field, bandwidth)

  # 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

.idw_band(scope, field, bandwidth, alpha = 1) ⇒ WeightsMatrix

Compute idw, distance band weights matrix for a scope.

Parameters:

  • scope (ActiveRecord::Relation)

    to query

  • field (Symbol, String)

    with geometry in it

  • bandwidth (Numeric)

    of distance band

  • alpha (Numeric) (defaults to: 1)

    used in weighting calculation (usually 1 or 2)

Returns:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/spatial_stats/weights/distant.rb', line 80

def self.idw_band(scope, field, bandwidth, alpha = 1)
  neighbors = SpatialStats::Queries::Weights
              .idw_band(scope, field, bandwidth, alpha)

  # 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 { |pair| pair[:i_id] }
  missing_neighbors = Hash[(keys - neighbors.keys).map { |key| [key, []] }]
  neighbors = neighbors.merge(missing_neighbors)

  # only keep j_id and weight
  weights = neighbors.transform_values do |value|
    value.map do |neighbor|
      { weight: neighbor[:weight], id: neighbor[:j_id] }
    end
  end
  SpatialStats::Weights::WeightsMatrix.new(weights)
end

.idw_knn(scope, field, k, alpha = 1) ⇒ WeightsMatrix

Compute idw, knn weights matrix for a scope.

Parameters:

  • scope (ActiveRecord::Relation)

    to query

  • field (Symbol, String)

    with geometry in it

  • k (Integer)

    neighbors to find

  • alpha (Numeric) (defaults to: 1)

    used in weighting calculation (usually 1 or 2)

Returns:



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/spatial_stats/weights/distant.rb', line 110

def self.idw_knn(scope, field, k, alpha = 1)
  neighbors = SpatialStats::Queries::Weights
              .idw_knn(scope, field, k, alpha)

  # 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 { |pair| pair[:i_id] }
  missing_neighbors = Hash[(keys - neighbors.keys).map { |key| [key, []] }]
  neighbors = neighbors.merge(missing_neighbors)

  # only keep j_id and weight
  weights = neighbors.transform_values do |value|
    value.map do |neighbor|
      { weight: neighbor[:weight], id: neighbor[:j_id] }
    end
  end
  SpatialStats::Weights::WeightsMatrix.new(weights)
end

.knn(scope, field, k) ⇒ WeightsMatrix

Compute distance band weights matrix for a scope.

Parameters:

  • scope (ActiveRecord::Relation)

    to query

  • field (Symbol, String)

    with geometry in it

  • k (Integer)

    neighbors to find

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/spatial_stats/weights/distant.rb', line 48

def self.knn(scope, field, k)
  neighbors = SpatialStats::Queries::Weights
              .knn(scope, field, k)

  # 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