Browse Source

Add scripts to manually export and import a panorama

Baptiste Jonglez 9 years ago
parent
commit
b7efd72346
3 changed files with 106 additions and 0 deletions
  1. 21 0
      UPGRADE.md
  2. 60 0
      panorama/management/commands/import_single_pano.py
  3. 25 0
      upgrade/export_single_pano.php

+ 21 - 0
UPGRADE.md

@@ -78,3 +78,24 @@ in the dropdown list of actions.
 
 This will take a few minutes to complete, you can follow the progress by
 reloading the admin page (look at the "Has tiles" column).
+
+Manually importing a panorama
+-----------------------------
+
+Sometimes, things are not that simple, and you need to "manually" import a
+panorama.  For instance, if the panorama image is at an unusual place or
+has an unusual name.
+
+In this case, you can use the two scripts `upgrade/export_single_pano.php`
+and `./manage.py import_single_pano` to manually export and import a
+panorama.  Usage:
+
+    php upgrade/export_single_pano.php /path/to/site.params | ./manage.py import_single_pano /path/to/panorama.tif
+
+The PHP script will convert the parameters file to a JSON representation,
+which is then passed to a Django command (along with the actual panorama
+image) for importing into the databse.  A copy of the image file will be
+put into the `media/pano` directory, meaning you can remove the original
+image file once you ensured that the import process went smoothly.
+
+Then, you need to regenerate tiles for the imported panorama (see above).

+ 60 - 0
panorama/management/commands/import_single_pano.py

@@ -0,0 +1,60 @@
+# -*- coding: utf-8 -*-
+
+"""
+Parse a single "site.params" file from the old PHP version of celutz
+(as JSON), and import it as a panorama in the Django version.
+
+Usage:
+
+    php upgrade/export_single_pano.php /path/to/site.params | ./manage.py import_single_pano /path/to/panorama.tif
+
+For an easier "bulk import" method, see `UPGRADE.md`.  You should only use
+this manual importing method if you have a highly unusual installation
+(for instance if the name of the image and the name of the tiles directory
+do not match).
+
+"""
+
+from __future__ import unicode_literals, division, print_function
+
+import json
+import sys
+import os
+
+from django.core.management.base import BaseCommand, CommandError
+from django.core.files import File
+
+from panorama.models import Panorama, ReferencePoint, Reference
+
+
+class Command(BaseCommand):
+    args = '<image>'
+    help = __doc__
+    
+    def handle(self, *args, **options):
+        if len(args) < 1:
+            raise CommandError('Usage: php upgrade/export_single_pano.php site.params | ./manage.py import_single_pano panorama.tif')
+
+        self.stdout.write("Loading parameters file...")
+        data = json.load(sys.stdin)
+        p = Panorama(name=data["titre"], latitude=float(data["latitude"]),
+                     longitude=float(data["longitude"]),
+                     altitude=float(data["altitude"]),
+                     loop=data["image_loop"])
+        # http://www.revsys.com/blog/2014/dec/03/loading-django-files-from-code/
+        with open(args[0], "rb") as f:
+            self.stdout.write("Reading image file...")
+            p.image.save(os.path.basename(args[0]), File(f), save=False)
+            self.stdout.write("Saving panorama to database...")
+            p.save()
+        self.stdout.write("Saving references to database...")
+        for refname, xy in data["reference"].items():
+            xy = xy.split(",")
+            x = float(xy[0])
+            y = float(xy[1])
+            refpoint = ReferencePoint.objects.get(name=refname)
+            r = Reference(reference_point=refpoint, panorama=p,
+                          x=int(x * p.image_width),
+                          y=int((y + 0.5) * p.image_height))
+            r.save()
+        self.stdout.write("Success!")

+ 25 - 0
upgrade/export_single_pano.php

@@ -0,0 +1,25 @@
+<?php
+
+/* Loads a single "site.params" file and outputs it to json. */
+
+function load_site_params($filename) {
+    $params = parse_ini_file($filename);
+    if (! $params)
+        return false;
+    // Normalise for convenience (otherwise, it's empty string for false
+    // and "1" for true...)
+    $params["image_loop"] = (boolean) $params["image_loop"];
+    return $params;
+}
+
+if (isset($argv[1])) {
+    $result = load_site_params($argv[1]);
+    if (! $result)
+        print("Error");
+    else
+        print(json_encode($result, JSON_PRETTY_PRINT));
+} else {
+    printf("Usage: %s /path/to/site.params\n", $argv[0]);
+}
+
+?>