2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
* ImgIndex Class definition
10
* @author Keith Hughitt <keith.hughitt@nasa.gov>
11
* @author Patrick Schmiedel <patrick.schmiedel@gmx.net>
12
* @license http://www.mozilla.org/MPL/MPL-1.1.html Mozilla Public License 1.1
13
* @link http://launchpad.net/helioviewer.org
16
* Provides methods for interacting with a JPEG 2000 archive.
19
* @package Helioviewer
20
* @author Keith Hughitt <keith.hughitt@nasa.gov>
21
* @author Patrick Schmiedel <patrick.schmiedel@gmx.net>
22
* @license http://www.mozilla.org/MPL/MPL-1.1.html Mozilla Public License 1.1
23
* @link http://launchpad.net/helioviewer.org
25
class Database_ImgIndex
27
private $_dbConnection;
30
* Creates an ImgIndex instance
34
public function __construct()
36
include_once 'DbConnection.php';
37
$this->_dbConnection = new Database_DbConnection();
41
* Finds the closest available image to the requested one, and returns information from
42
* database and XML box.
44
* @param string $date A UTC date string of the form "2003-10-05T00:00:00Z."
45
* @param int $sourceId An identifier specifying the image type or source requested.
47
* @return array Information about the image match including it's location, time, scale, and dimensions.
49
public function getClosestImage($date, $sourceId)
51
$img = $this->getImageFromDatabase($date, $sourceId);
52
$filename = HV_JP2_DIR . $img["filepath"] . "/" .$img["filename"];
53
$xmlBox = $this->extractJP2MetaInfo($filename);
55
return array_merge($img, $xmlBox);
59
* Queries database and finds the best matching image.
61
* @param string $date A UTC date string of the form "2003-10-05T00:00:00Z."
62
* @param int $sourceId An identifier specifying the image type or source requested.
64
* @return array Array including the image id, filepath, filename, date, and sourceId.
66
public function getImageFromDatabase($date, $sourceId)
68
include_once 'src/Helper/DateTimeConversions.php';
70
$datestr = isoDateToMySQL($date);
72
// Search left and right side of image database B-Tree separately
73
$lhs = sprintf("SELECT id, filepath, filename, date FROM image WHERE sourceId = %d AND date < '%s' ORDER BY date DESC LIMIT 1;", $sourceId, $datestr);
74
$rhs = sprintf("SELECT id, filepath, filename, date FROM image WHERE sourceId = %d AND date >= '%s' ORDER BY date ASC LIMIT 1;", $sourceId, $datestr);
76
//die("$lhs<br><br><span style='color: green;'>$rhs</span><br><br><hr>");
78
$left = mysqli_fetch_array($this->_dbConnection->query($lhs), MYSQL_ASSOC);
79
$right = mysqli_fetch_array($this->_dbConnection->query($rhs), MYSQL_ASSOC);
81
$dateTimestamp = toUnixTimestamp($date);
83
// Select closest match
84
if (abs($dateTimestamp - toUnixTimestamp($left["date"])) < abs($dateTimestamp - toUnixTimestamp($right["date"]))) {
90
// Check to make sure a match was found
92
$sql = "SELECT name FROM datasource WHERE id=$sourceId";
93
$result = mysqli_fetch_array($this->_dbConnection->query($sql), MYSQL_ASSOC);
94
$source = $result["name"];
95
throw new Exception("No images of the requested type ($source) were found in the database.");
99
$img["id"] = (int) $img["id"];
105
* Returns the number of images in the database for a given source and time range
107
* @param datetime $start Query start time
108
* @param datetime $end Query end time
109
* @param int $sourceId The sourceId to query
111
* @return int The number of images in the database within the specified constraints
113
public function getImageCount($start, $end, $sourceId) {
114
include_once 'src/Helper/DateTimeConversions.php';
115
$startDate = isoDateToMySQL($start);
116
$endDate = isoDateToMySQL($end);
118
$sql = "SELECT COUNT(*) FROM image WHERE sourceId=$sourceId AND date BETWEEN '$startDate' AND '$endDate'";
119
$result = mysqli_fetch_array($this->_dbConnection->query($sql));
120
return (int) $result[0];
124
* Returns an array containing all images for a given source and time range
126
* @param datetime $start Query start time
127
* @param datetime $end Query end time
128
* @param int $sourceId The sourceId to query
130
* @return int The number of images in the database within the specified constraints
132
public function getImageRange($start, $end, $sourceId) {
133
include_once 'src/Helper/DateTimeConversions.php';
134
$startDate = isoDateToMySQL($start);
135
$endDate = isoDateToMySQL($end);
138
$sql = "SELECT * FROM image WHERE sourceId=$sourceId AND date BETWEEN '$startDate' AND '$endDate'";
139
$result = $this->_dbConnection->query($sql);
141
while ($image = $result->fetch_array(MYSQL_ASSOC)) {
142
array_push($images, $image);
148
* Extract necessary meta-information from an image
150
* @param string $img Location of a JP2 image.
152
* @return array A subset of the information stored in the jp2 header
154
public function extractJP2MetaInfo ($img)
156
include_once "src/Image/JPEG2000/JP2ImageXMLBox.php";
159
$xmlBox = new Image_JPEG2000_JP2ImageXMLBox($img);
161
$dimensions = $xmlBox->getImageDimensions();
162
$center = $xmlBox->getSunCenter();
165
"scale" => (float) $xmlBox->getImagePlateScale(),
166
"width" => (int) $dimensions[0],
167
"height" => (int) $dimensions[1],
168
"rotated" => (bool) $xmlBox->getImageRotationStatus(),
169
"sunCenterX" => (float) $center[0],
170
"sunCenterY" => (float) $center[1],
172
} catch (Exception $e) {
173
logErrorMsg($img['filename'] . ": " . $e->getMessage(), true);
181
* Returns the sourceId for a given set of parameters.
183
* @param string $obs Observatory
184
* @param string $inst Instrument
185
* @param string $det Detector
186
* @param string $meas Measurement
188
* @return int The matched sourceId.
190
public function getSourceId ($obs, $inst, $det, $meas)
196
LEFT JOIN observatory ON datasource.observatoryId = observatory.id
197
LEFT JOIN instrument ON datasource.instrumentId = instrument.id
198
LEFT JOIN detector ON datasource.detectorId = detector.id
199
LEFT JOIN measurement ON datasource.measurementId = measurement.id
201
observatory.name='%s' AND
202
instrument.name='%s' AND
203
detector.name='%s' AND
204
measurement.name='%s';",
205
mysqli_real_escape_string($this->_dbConnection->link, $obs),
206
mysqli_real_escape_string($this->_dbConnection->link, $inst),
207
mysqli_real_escape_string($this->_dbConnection->link, $det),
208
mysqli_real_escape_string($this->_dbConnection->link, $meas)
210
$result = $this->_dbConnection->query($sql);
211
$result_array = mysqli_fetch_array($result, MYSQL_ASSOC);
213
return (int) ($result_array["id"]);
217
* Returns a list of the known data sources
219
* @return array A tree representation of the known data sources
221
public function getDataSources ()
226
datasource.name as name,
228
datasource.layeringOrder as layeringOrder,
229
observatory.name as observatory,
230
instrument.name as instrument,
231
detector.name as detector,
232
measurement.name as measurement
234
LEFT JOIN observatory ON datasource.observatoryId = observatory.id
235
LEFT JOIN instrument ON datasource.instrumentId = instrument.id
236
LEFT JOIN detector ON datasource.detectorId = detector.id
237
LEFT JOIN measurement ON datasource.measurementId = measurement.id;";
239
// Fetch available data-sources
240
$result = $this->_dbConnection->query($sql);
244
while ($row = $result->fetch_array(MYSQL_ASSOC)) {
245
array_push($sources, $row);
248
// Convert results into a more easily traversable tree structure
251
foreach ($sources as $source) {
254
$obs = $source["observatory"];
255
$inst = $source["instrument"];
256
$det = $source["detector"];
257
$meas = $source["measurement"];
258
$name = $source["name"];
259
$ord = (int) ($source["layeringOrder"]);
260
$id = (int) ($source["id"]);
263
if (!isset($tree[$obs])) {
264
$tree[$obs] = array();
266
if (!isset($tree[$obs][$inst])) {
267
$tree[$obs][$inst] = array();
269
if (!isset($tree[$obs][$inst][$det])) {
270
$tree[$obs][$inst][$det] = array();
272
$tree[$obs][$inst][$det][$meas] = array("sourceId"=>$id, "name"=>$name, "layeringOrder"=>$ord);
279
* Finds the closest match for a requested image and returns it's location
281
* @param string $date A UTC date string of the form "2003-10-05T00:00:00Z."
282
* @param int $sourceId An identifier specifying the image type or source requested.
284
* @return string Local filepath for the JP2 image.
287
public function getJP2FilePath($date, $sourceId)
289
$img = $this->getImageFromDatabase($date, $sourceId);
290
return $img["filepath"] . "/" . $img["filename"];