You are here

function openlayers_plus_behavior_scalepoints::render in OpenLayers Plus 7

Same name and namespace in other branches
  1. 7.3 behaviors/openlayers_plus_behavior_scalepoints.inc \openlayers_plus_behavior_scalepoints::render()
  2. 7.2 behaviors/openlayers_plus_behavior_scalepoints.inc \openlayers_plus_behavior_scalepoints::render()

Render.

Overrides openlayers_behavior::render

File

behaviors/openlayers_plus_behavior_scalepoints.inc, line 205

Class

openlayers_plus_behavior_scalepoints

Code

function render(&$map) {

  // Get the layers we are going to use.
  $layers = $this
    ->get_layers($map['layers']);
  $get_min = $this->options['min']['value'] === '' || !isset($this->options['min']['value']);
  $get_max = $this->options['max']['value'] === '' || !isset($this->options['max']['value']);
  $weights = $this
    ->get_weights();
  foreach ($layers as $k => $layer) {

    // Get the field we are going to use.
    if ($field = $this
      ->get_field($layer['features'])) {

      // Get min/max per layer.
      $min = isset($this->options['min']['value']) && is_numeric($this->options['min']['value']) ? $this->options['min']['value'] : 1000000;
      $max = isset($this->options['max']['value']) && is_numeric($this->options['max']['value']) ? $this->options['max']['value'] : 0;
      $count_min = 1000000;
      $count_max = 0;
      if ($get_min || $get_max) {
        foreach ($layer['features'] as $j => $feature) {
          if ($field && isset($feature['attributes'][$field]) && ($count = $feature['attributes'][$field])) {

            // Replace the count attribute with the selected one.
            if ($field !== 'count') {
              $map['layers'][$k]['features'][$j]['attributes']['count'] = $count;
            }
            $max = $count > $max && $get_max ? $count : $max;
            $min = $count < $min && $get_min ? $count : $min;
            $count_max = $count > $count_max ? $count : $count_max;
            $count_min = $count < $count_min ? $count : $count_min;
          }
        }
      }

      // Sensible defaults code right here: If count_max & count_min are both under 1,
      // assume data source is doing a ratio calc for us. Set max to 1.
      if ($count_max < 1 && $count_min < 1) {
        $max = 1;
      }
      foreach ($layer['features'] as $j => $feature) {
        if ($field && isset($feature['attributes'][$field]) && ($count = $feature['attributes'][$field])) {

          // For layers with only a single value always use the smallest weight.
          if ($count_max === $count_min) {
            $map['layers'][$k]['features'][$j]['attributes']['weight'] = 1;
          }
          else {
            $calculated = $this
              ->process_weight(($count - $min) / ($max - $min), $this->options['method']);
            foreach ($weights as $percentile => $weight) {
              if ($calculated <= $percentile) {
                $map['layers'][$k]['features'][$j]['attributes']['weight'] = $weight;
                break;
              }
            }
          }
        }
      }
    }
  }
  drupal_add_js(drupal_get_path('module', 'openlayers_plus') . '/behaviors/openlayers_plus_behavior_scalepoints.js');
  $options = array();
  $options['styles'] = $this
    ->get_styles();
  return $options;
}