From c305ac2ad9cb1c71cbade45877b78e47d699f3a3 Mon Sep 17 00:00:00 2001 From: rWatcher Date: Sun, 28 Feb 2010 14:22:59 -0500 Subject: [PATCH] Scan existing photos for GPS coordinates. --- modules/exif_gps/helpers/exif_gps_task.php | 86 ++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 modules/exif_gps/helpers/exif_gps_task.php diff --git a/modules/exif_gps/helpers/exif_gps_task.php b/modules/exif_gps/helpers/exif_gps_task.php new file mode 100644 index 00000000..6158bc69 --- /dev/null +++ b/modules/exif_gps/helpers/exif_gps_task.php @@ -0,0 +1,86 @@ +delete("exif_coordinates") + ->where("item_id", "NOT IN", + db::build()->select("id")->from("items")) + ->execute(); + + // Display an option on the maintance screen for scanning existing photos + // for GPS data (in case photos were uploaded before the module was active). + return array(Task_Definition::factory() + ->callback("exif_gps_task::update_gps_index") + ->name(t("Extract Exif GPS data")) + ->description(t("Scan all photos for missing GPS data")) + ->severity(log::SUCCESS)); + } + + static function update_gps_index($task) { + $start = microtime(true); + + $total = $task->get("total"); + if (empty($total)) { + $task->set("total", $total = count(ORM::factory("item")->where("type", "=", "photo")->find_all())); + $task->set("last_id", 0); + $task->set("completed", 0); + } + + $last_id = $task->get("last_id"); + $completed = $task->get("completed"); + + $all_photos = ORM::factory("item") + ->where("id", ">", $last_id) + ->where("type", "=", "photo") + ->find_all(100); + + foreach (ORM::factory("item") + ->where("id", ">", $last_id) + ->where("type", "=", "photo") + ->find_all(100) as $item) { + + $record = ORM::factory("exif_coordinate")->where("item_id", "=", $item->id)->find(); + if (!$record->loaded()) { + exif_gps::extract($item); + } + $last_id = $item->id; + $completed++; + + if ($completed == $total || microtime(true) - $start > 1.5) { + break; + } + } + + $task->set("completed", $completed); + $task->set("last_id", $last_id); + + if ($total == $completed) { + $task->done = true; + $task->state = "success"; + $task->percent_complete = 100; + } else { + $task->percent_complete = round(100 * $completed / $total); + } + $task->status = t2("One photo scanned", "%count / %total photos scanned", $completed, + array("total" => $total)); + } +}