2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
* Helper_HelioviewerLayers Class Definition
10
* @author Jeff Stys <jeff.stys@nasa.gov>
11
* @author Keith Hughitt <keith.hughitt@nasa.gov>
12
* @license http://www.mozilla.org/MPL/MPL-1.1.html Mozilla Public License 1.1
13
* @link http://launchpad.net/helioviewer.org
16
* A simple class to represent one or more Helioviewer layers in a request.
19
* @package Helioviewer
20
* @author Jeff Stys <jeff.stys@nasa.gov>
21
* @author Keith Hughitt <keith.hughitt@nasa.gov>
22
* @license http://www.mozilla.org/MPL/MPL-1.1.html Mozilla Public License 1.1
23
* @link http://launchpad.net/helioviewer.org
25
* TODO 11/23/2010: Check to make sure number of valid layers is > 0
26
* and stop execution otherwise
28
require_once HV_ROOT_DIR.'/src/php/Database/ImgIndex.php';
30
class Helper_HelioviewerLayers {
32
private $_layers = array();
33
private $_layerString;
37
* Creates a new HelioviewerLayers instance
39
* @param string $layerString Layer string in one of two recognized
41
* [obs,inst,det,meas,visible,opacity] or
42
* [sourceId,visible,opacity].
46
public function __construct($layerString) {
48
$this->_layerString = $layerString;
50
$this->_db = new Database_ImgIndex();
52
$layerStringArray = explode('],[', substr($layerString, 1, -1));
54
// Process individual layers in string
55
foreach ($layerStringArray as $singleLayerString) {
56
$layer = $this->_decodeSingleLayerString($singleLayerString);
58
// Only include layer if it is visible
59
if ($layer['visible'] && ($layer['opacity'] > 0)) {
60
array_push($this->_layers, $layer);
64
// Check to make sure at least one valid layer was specified
65
if (sizeOf($this->_layers) === 0) {
67
'No valid and visible layers specified for request.', 20);
72
* Returns the number of layers in the collection
74
* @return int Number of layers in request
76
public function length() {
77
return sizeOf($this->_layers);
81
* Returns the layers as an array of associative arrays
83
* @return array An array of hashes repre$layersenting the requested layers
85
public function toArray() {
86
return $this->_layers;
90
* Returns a bitmask (binary string) representation of the datasources
91
* included in the HelioviewerLayers object
93
* @return {string} A bitmask string, e.g. "10010000000000"
95
public function getBitMask() {
98
foreach($this->_layers as $layer) {
99
array_push($ids, $layer['sourceId']);
103
$bitArray = array_pad(array(), $ids[0] + 1, 0);
105
foreach ($ids as $id) {
106
$bitArray[$ids[0] - $id] = 1;
109
return implode($bitArray);
113
* Returns a string representation of the request layers suitable for use
116
* @return string String representation of the request layers for use in
119
public function serialize() {
120
return $this->_layerString;
124
* Returns a human-readable representation of the request layers
126
* @return string Human-readable string
128
public function toHumanReadableString() {
130
foreach ($this->_layers as $i=>$layer) {
131
foreach ($layer['uiLabels'] as $i=>$obj) {
132
$layerString .= $obj['name'] . ' ';
134
$layerString = substr($layerString,0,-1) . ', ';
136
$layerString = substr($layerString,0,-2);
142
* Returns a string reprentation of the request layers suitable for use
145
* @return string String representation of the request layers for use in
148
public function toString() {
151
foreach ($this->_layers as $layer) {
152
$layerString .= str_replace(' ', '_', $layer['name']) . '__';
155
// remove trailing __
156
return substr($layerString, 0, -2);
160
* Takes a single layer string and converts it to a more convenient associative array. filling in any
161
* missing details as neccessary
163
* @param string $layerString A single layer represented as a string in one of the two following forms:
164
* [obs,inst,det,meas,visible,opacity] or [sourceId,visible,opacity]
166
* @return array Associative array representation of the layer
168
private function _decodeSingleLayerString($layerString) {
169
// Break up string into individual components
170
$layerArray = explode(',', $layerString);
172
if (sizeOf($layerArray) == 3) {
173
// [sourceId,visible,opacity]
174
list($sourceId, $layeringOrder, $opacity) = $layerArray;
176
$source = $this->_db->getDatasourceInformationFromSourceId($sourceId);
178
$layeringOrder = $layeringOrder;
179
$name = $source['name'];
180
$uiLabels = $source['uiLabels'];
183
$opacity = array_pop($layerArray);
184
$layeringOrder = array_pop($layerArray);
186
$info = $this->_db->getDatasourceInformationFromNames(
188
$sourceId = $info["id"];
189
$name = $info["name"];
190
$layeringOrder = $layeringOrder;
191
$uiLabels = $info['uiLabels'];
195
// Associative array form
198
'sourceId' => (int)$sourceId,
199
'layeringOrder' => (int)$layeringOrder,
200
'uiLabels' => $uiLabels,
201
'visible' => ($layeringOrder > 0) ? true : false,
202
'opacity' => (int)$opacity