import_single_pano.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -*- coding: utf-8 -*-
  2. """
  3. Parse a single "site.params" file from the old PHP version of celutz
  4. (as JSON), and import it as a panorama in the Django version.
  5. Usage:
  6. php upgrade/export_single_pano.php /path/to/site.params | ./manage.py import_single_pano /path/to/panorama.tif
  7. For an easier "bulk import" method, see `UPGRADE.md`. You should only use
  8. this manual importing method if you have a highly unusual installation
  9. (for instance if the name of the image and the name of the tiles directory
  10. do not match).
  11. """
  12. from __future__ import unicode_literals, division, print_function
  13. import json
  14. import sys
  15. import os
  16. from django.core.management.base import BaseCommand, CommandError
  17. from django.core.files import File
  18. from django.core.exceptions import ObjectDoesNotExist
  19. from panorama.models import Panorama, ReferencePoint, Reference
  20. class Command(BaseCommand):
  21. args = '<image>'
  22. help = __doc__
  23. def handle(self, *args, **options):
  24. if len(args) < 1:
  25. raise CommandError('Usage: php upgrade/export_single_pano.php site.params | ./manage.py import_single_pano panorama.tif')
  26. self.stdout.write("Loading parameters file...")
  27. data = json.load(sys.stdin)
  28. p = Panorama(name=data["titre"], latitude=float(data["latitude"]),
  29. longitude=float(data["longitude"]),
  30. altitude=float(data["altitude"]),
  31. loop=data["image_loop"])
  32. # http://www.revsys.com/blog/2014/dec/03/loading-django-files-from-code/
  33. with open(args[0], "rb") as f:
  34. self.stdout.write("Reading image file...")
  35. p.image.save(os.path.basename(args[0]), File(f), save=False)
  36. self.stdout.write("Saving panorama to database...")
  37. p.save()
  38. self.stdout.write("Launching tile generation...")
  39. p.generate_tiles()
  40. self.stdout.write("Saving references to database...")
  41. for refname, xy in data["reference"].items():
  42. xy = xy.split(",")
  43. x = float(xy[0])
  44. y = float(xy[1])
  45. try:
  46. refpoint = ReferencePoint.objects.get(name=refname)
  47. except ObjectDoesNotExist:
  48. self.stderr.write('WARNING: reference point "%s" not found. Continuing anyway, please check the result!' % refname)
  49. continue
  50. r = Reference(reference_point=refpoint, panorama=p,
  51. x=int(x * p.image_width),
  52. y=int((y + 0.5) * p.image_height))
  53. r.save()
  54. self.stdout.write("Success!")