~jstys-z/helioviewer.org/client5

« back to all changes in this revision

Viewing changes to src/php/Helper/HelioviewerLayers.php

  • Committer: Keith Hughitt
  • Date: 2012-04-23 16:02:25 UTC
  • mto: This revision was merged to the branch mainline in revision 732.
  • Revision ID: keith.hughitt@nasa.gov-20120423160225-xzoh82ejf37c8yr7
Incorporated HVPull code

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
 
/**
4
 
 * Helper_HelioviewerLayers Class Definition
5
 
 *
6
 
 * PHP version 5
7
 
 *
8
 
 * @category Helper
9
 
 * @package  Helioviewer
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
14
 
 */
15
 
/**
16
 
 * A simple class to represent one or more Helioviewer layers in a request.
17
 
 *
18
 
 * @category Helper
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
24
 
 *
25
 
 * TODO 11/23/2010: Check to make sure number of valid layers is > 0
26
 
 * and stop execution otherwise
27
 
 */
28
 
require_once HV_ROOT_DIR.'/src/php/Database/ImgIndex.php';
29
 
 
30
 
class Helper_HelioviewerLayers {
31
 
 
32
 
    private $_layers = array();
33
 
    private $_layerString;
34
 
    private $_db;
35
 
 
36
 
    /**
37
 
     * Creates a new HelioviewerLayers instance
38
 
     *
39
 
     * @param string $layerString Layer string in one of two recognized
40
 
     *                            formats:
41
 
     *                            [obs,inst,det,meas,visible,opacity] or
42
 
     *                            [sourceId,visible,opacity].
43
 
     *
44
 
     * @return void
45
 
     */
46
 
    public function __construct($layerString) {
47
 
 
48
 
        $this->_layerString = $layerString;
49
 
 
50
 
        $this->_db = new Database_ImgIndex();
51
 
 
52
 
        $layerStringArray = explode('],[', substr($layerString, 1, -1));
53
 
 
54
 
        // Process individual layers in string
55
 
        foreach ($layerStringArray as $singleLayerString) {
56
 
            $layer = $this->_decodeSingleLayerString($singleLayerString);
57
 
 
58
 
            // Only include layer if it is visible
59
 
            if ($layer['visible'] && ($layer['opacity'] > 0)) {
60
 
                array_push($this->_layers, $layer);
61
 
            }
62
 
        }
63
 
 
64
 
        // Check to make sure at least one valid layer was specified
65
 
        if (sizeOf($this->_layers) === 0) {
66
 
            throw new Exception(
67
 
                'No valid and visible layers specified for request.', 20);
68
 
        }
69
 
    }
70
 
 
71
 
    /**
72
 
     * Returns the number of layers in the collection
73
 
     *
74
 
     * @return int Number of layers in request
75
 
     */
76
 
    public function length() {
77
 
        return sizeOf($this->_layers);
78
 
    }
79
 
 
80
 
    /**
81
 
     * Returns the layers as an array of associative arrays
82
 
     *
83
 
     * @return array An array of hashes repre$layersenting the requested layers
84
 
     */
85
 
    public function toArray() {
86
 
        return $this->_layers;
87
 
    }
88
 
 
89
 
    /**
90
 
     * Returns a bitmask (binary string) representation of the datasources
91
 
     * included in the HelioviewerLayers object
92
 
     *
93
 
     * @return {string} A bitmask string, e.g. "10010000000000"
94
 
     */
95
 
    public function getBitMask() {
96
 
        $ids = array();
97
 
 
98
 
        foreach($this->_layers as $layer) {
99
 
            array_push($ids, $layer['sourceId']);
100
 
        }
101
 
        rsort($ids);
102
 
 
103
 
        $bitArray = array_pad(array(), $ids[0] + 1, 0);
104
 
 
105
 
        foreach ($ids as $id) {
106
 
            $bitArray[$ids[0] - $id] = 1;
107
 
        }
108
 
 
109
 
        return implode($bitArray);
110
 
    }
111
 
 
112
 
    /**
113
 
     * Returns a string representation of the request layers suitable for use
114
 
     * in queries
115
 
     *
116
 
     * @return string String representation of the request layers for use in
117
 
     *                API queries
118
 
     */
119
 
    public function serialize() {
120
 
        return $this->_layerString;
121
 
    }
122
 
 
123
 
    /**
124
 
     * Returns a human-readable representation of the request layers
125
 
     *
126
 
     * @return string Human-readable string
127
 
     */
128
 
    public function toHumanReadableString() {
129
 
        $layerString = '';
130
 
        foreach ($this->_layers as $i=>$layer) {
131
 
            foreach ($layer['uiLabels'] as $i=>$obj) {
132
 
                $layerString .= $obj['name'] . ' ';
133
 
            }
134
 
            $layerString = substr($layerString,0,-1) . ', ';
135
 
        }
136
 
        $layerString = substr($layerString,0,-2);
137
 
 
138
 
        return $layerString;
139
 
    }
140
 
 
141
 
    /**
142
 
     * Returns a string reprentation of the request layers suitable for use
143
 
     * in filenames
144
 
     *
145
 
     * @return string String representation of the request layers for use in
146
 
     *                filenames, etc.
147
 
     */
148
 
    public function toString() {
149
 
        $layerString = '';
150
 
 
151
 
        foreach ($this->_layers as $layer) {
152
 
            $layerString .= str_replace(' ', '_', $layer['name']) . '__';
153
 
        }
154
 
 
155
 
        // remove trailing __
156
 
        return substr($layerString, 0, -2);
157
 
    }
158
 
 
159
 
    /**
160
 
     * Takes a single layer string and converts it to a more convenient associative array. filling in any
161
 
     * missing details as neccessary
162
 
     *
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]
165
 
     *
166
 
     * @return array Associative array representation of the layer
167
 
     */
168
 
    private function _decodeSingleLayerString($layerString) {
169
 
        // Break up string into individual components
170
 
        $layerArray = explode(',', $layerString);
171
 
 
172
 
        if (sizeOf($layerArray) == 3) {
173
 
            // [sourceId,visible,opacity]
174
 
            list($sourceId, $layeringOrder, $opacity) = $layerArray;
175
 
 
176
 
            $source = $this->_db->getDatasourceInformationFromSourceId($sourceId);
177
 
 
178
 
            $layeringOrder = $layeringOrder;
179
 
            $name          = $source['name'];
180
 
            $uiLabels      = $source['uiLabels'];
181
 
        }
182
 
        else  {
183
 
            $opacity       = array_pop($layerArray);
184
 
            $layeringOrder = array_pop($layerArray);
185
 
 
186
 
            $info = $this->_db->getDatasourceInformationFromNames(
187
 
                        $layerArray);
188
 
            $sourceId      = $info["id"];
189
 
            $name          = $info["name"];
190
 
            $layeringOrder = $layeringOrder;
191
 
            $uiLabels      = $info['uiLabels'];
192
 
 
193
 
        }
194
 
 
195
 
        // Associative array form
196
 
        return array (
197
 
            'name'          => $name,
198
 
            'sourceId'      => (int)$sourceId,
199
 
            'layeringOrder' => (int)$layeringOrder,
200
 
            'uiLabels'      => $uiLabels,
201
 
            'visible'       => ($layeringOrder > 0) ? true : false,
202
 
            'opacity'       => (int)$opacity
203
 
        );
204
 
    }
205
 
}
206
 
?>