Skip to content

Commit

Permalink
Final touches of black magic
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom MacWright committed Jun 9, 2010
1 parent 15479a5 commit f0b482b
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 24 deletions.
33 changes: 23 additions & 10 deletions stylewriter.module
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ function stylewriter_views_api() {
* @return array
*/
function stylewriter_color_ramp_grad($a, $b, $steps) {
$range = $b - $a; // range can be negative
$step = $range / $steps;
return range($a, $b, $step);
if ($a == $b) {
return array_fill(0, $steps + 1, $a);
}
else {
$range = $b - $a; // range can be negative
$step = $range / $steps;
return range($a, $b, $step);
}
}

/**
Expand All @@ -43,7 +48,7 @@ function stylewriter_color_ramp_merge($h_grad, $s_grad, $l_grad) {
if (count($h_grad) == count($s_grad) && count($s_grad) == count($l_grad)) {
$colors = array();
for ($i = 0; $i < count($h_grad); $i++) {
$colors[] = _color_pack(_color_hsl2rgb($h_grad, $s_grad, $l_grad));
$colors[] = _color_pack(_color_hsl2rgb(array($h_grad[$i], $s_grad[$i], $l_grad[$i])));
}
return $colors;
}
Expand All @@ -62,6 +67,8 @@ function stylewriter_color_ramp_merge($h_grad, $s_grad, $l_grad) {
* @return array
*/
function stylewriter_color_ramp($a, $b, $steps) {
module_load_include('module', 'color', 'color');

$a_pts = _color_rgb2hsl(_color_unpack($a));
$b_pts = _color_rgb2hsl(_color_unpack($b));

Expand All @@ -76,20 +83,26 @@ function stylewriter_color_ramp($a, $b, $steps) {
/**
* Implementation of hook_theme().
*/
function wfs_theme() {
function stylewriter_theme() {
$path = drupal_get_path('module', 'stylewriter');
return array(
'stylewriter_rule' => array(
'arguments' => array(
'value' => '',
'join_field' => '',
'value_field' => '',
'high' => '',
'join_field_name' => '',
'join_field_value' => '',
'color' => '',
'low' => ''),
),
'file' => 'stylewriter.theme.inc',
'template' => 'stylewriter-rule',
'path' => $path . "/views",
),
'stylewriter_document' => array(
'arguments' => array(
'rules' => '',
),
'file' => 'stylewriter.theme.inc',
'template' => 'stylewriter-document',
'path' => $path . "/views",
),
);
}
4 changes: 1 addition & 3 deletions views/stylewriter-document.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
<!DOCTYPE Map>
<Map bgcolor="#000FFF" srs="+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null">
<Style name="WorldBorders">
<?php foreach ($rules as $rule): ?>
<?php echo $rule; ?>
<?php endforeach; ?>
<?php echo $rules; ?>
</Style>
<!-- Any layers named with the prefix (NIKTILE*) will get data from the JSON applied -->
<Layer name="NIKTILE" srs="+proj=latlong +datum=WGS84">
Expand Down
10 changes: 3 additions & 7 deletions views/stylewriter-rule.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
/**
* Style a Mapnik Rule
*
* @param $join_field
* @param $value_field
* @param $value
* @param $join_field_name
* @param $join_field_value
*
* @param $low
* @param $high
* @param $color
*/
?>
<Rule>
<Filter>[<?php echo $join_field; ?>] &eq; <?php echo $value; ?></Filter>
<Filter>[<?php echo $value_field; ?>] &gt; <?php echo $low; ?> and [<?php echo $value_field; ?>] &lt; <?php echo $high; ?></Filter>
<Filter>[<?php echo $join_field_name; ?>] &eq; "<?php echo $join_field_value; ?>"</Filter>
<PolygonSymbolizer>
<CssParameter name="fill"><?php echo $color; ?></CssParameter>
</PolygonSymbolizer>
Expand Down
8 changes: 6 additions & 2 deletions views/stylewriter.theme.inc
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ function template_preprocess_stylewriter_mapfile(&$vars) {

$rows = '';
foreach ($points as $point) {
$rows .= theme('stylewriter_rule', $point['value'], $point['join_field']);
$rows .= theme('stylewriter_rule',
$point['join_field_name'],
$point['join_field_value'],
$point['color']);
}

exit($rows);

$vars['rows'] = $rows;
$vars['viewtitle'] = $view->name;

exit(theme('stylewriter_document', $rows));

// drupal_set_header('Content-Type: text/xml');
}
66 changes: 64 additions & 2 deletions views/views_plugin_style_mapfile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,37 @@ class views_plugin_style_mapfile extends views_plugin_style {
'#weight' => -10,
);

$form['colors'] = array(
'#type' => 'fieldset',
'#title' => 'Colors',
'#description' => t(''),
'#weight' => -20,
);

$form['colors']['color_min'] = array(
'#type' => 'textfield',
'#title' => t('Minimum color'),
'#default_value' => $this->options['colors']['color_min'],
);

$form['colors']['color_max'] = array(
'#type' => 'textfield',
'#title' => t('Maximum color'),
'#default_value' => $this->options['colors']['color_max'],
);

$form['colors']['steps'] = array(
'#type' => 'textfield',
'#title' => t('Gradations'),
'#default_value' => $this->options['colors']['steps'],
);

$form['fields']['join_field_name'] = array(
'#type' => 'textfield',
'#title' => t('Join Field Name'),
'#default_value' => $this->options['fields']['join_field_name'],
);

$form['fields']['join_field'] = array(
'#type' => 'select',
'#title' => t('Join Field'),
Expand Down Expand Up @@ -93,21 +124,52 @@ class views_plugin_style_mapfile extends views_plugin_style {
}

$points = array();
$values = array();

foreach ($renders as $id => $row) {
$point = array();

foreach ($this->view->field as $key => $field) {
$point['join_field_name'] = $this->options['fields']['join_field_name'];
if ($key == $this->options['fields']['join_field']) {
$point['join_field'] = $row[$key];
$point['join_field_value'] = $row[$key];
}
// echo "[{$key}]";
// TODO temporary hack
// TODO getting bad invalid output here
if ($key == 'wbapi_data_value_2007 wbapi_data_value') {
$point['value'] = $row[$key];
$values[] = intval($row[$key]);
}
/*
if ($key == $this->options['fields']['value']) {
$point['value'] = $row[$key];
}
*/
}
$points[] = $point;
}
print_r($points);

$value_min = min($values);
$value_max = max($values);

if ($value_max == 0) {
// TODO: allow for 0
throw new Exception('Maximum value cannot be 0');
}

$color_ramp = stylewriter_color_ramp(
$this->options['colors']['color_min'],
$this->options['colors']['color_max'],
$this->options['colors']['steps']);

foreach ($points as $i => $point) {
$rank = ($point['value'] - $value_min) / $value_max; // 0 -> 1
// TODO: floor or ceil?
$ramp_idx = floor($rank * $this->options['colors']['steps']);
$points[$i]['color'] = $color_ramp[$ramp_idx];
}

return $points;
}
}

0 comments on commit f0b482b

Please sign in to comment.