import_single_pano.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 panorama.models import Panorama, ReferencePoint, Reference
  19. class Command(BaseCommand):
  20. args = '<image>'
  21. help = __doc__
  22. def handle(self, *args, **options):
  23. if len(args) < 1:
  24. raise CommandError('Usage: php upgrade/export_single_pano.php site.params | ./manage.py import_single_pano panorama.tif')
  25. self.stdout.write("Loading parameters file...")
  26. data = json.load(sys.stdin)
  27. p = Panorama(name=data["titre"], latitude=float(data["latitude"]),
  28. longitude=float(data["longitude"]),
  29. altitude=float(data["altitude"]),
  30. loop=data["image_loop"])
  31. # http://www.revsys.com/blog/2014/dec/03/loading-django-files-from-code/
  32. with open(args[0], "rb") as f:
  33. self.stdout.write("Reading image file...")
  34. p.image.save(os.path.basename(args[0]), File(f), save=False)
  35. self.stdout.write("Saving panorama to database...")
  36. p.save()
  37. self.stdout.write("Saving references to database...")
  38. for refname, xy in data["reference"].items():
  39. xy = xy.split(",")
  40. x = float(xy[0])
  41. y = float(xy[1])
  42. refpoint = ReferencePoint.objects.get(name=refname)
  43. r = Reference(reference_point=refpoint, panorama=p,
  44. x=int(x * p.image_width),
  45. y=int((y + 0.5) * p.image_height))
  46. r.save()
  47. self.stdout.write("Success!")