create_pano.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. """
  3. Create a panorama from the command line. This is useful when manipulating large image files.
  4. """
  5. from __future__ import unicode_literals, division, print_function
  6. import sys
  7. import os
  8. from django.core.management.base import BaseCommand, CommandError
  9. from django.core.files import File
  10. from django.core.exceptions import ObjectDoesNotExist
  11. from django.conf import settings
  12. from altitude.providers import get_altitude
  13. from panorama.models import Panorama, ReferencePoint, Reference
  14. class Command(BaseCommand):
  15. help = __doc__
  16. def add_arguments(self, parser):
  17. parser.add_argument('--name', '-n', required=True,
  18. help='Name of the panorama')
  19. parser.add_argument('--image', '-i', required=True,
  20. help='Image of the panorama to create')
  21. parser.add_argument('--latitude', '-l', type=float, required=True)
  22. parser.add_argument('--longitude', '-L', type=float, required=True)
  23. parser.add_argument('--height', '-H', type=float, required=True,
  24. help='Height above ground, in meters')
  25. parser.add_argument('--loop', action='store_true', help='Is the image a 360° panorama?')
  26. def handle(self, *args, **options):
  27. self.stdout.write("Getting ground altitude for these coordinates...")
  28. alt = get_altitude([settings.ALTITUDE_PROVIDERS[0]],
  29. timeout=10.,
  30. latitude=options['latitude'],
  31. longitude=options['longitude'])
  32. self.stdout.write("Ground altitude is {}m".format(alt))
  33. p = Panorama(name=options["name"], latitude=options["latitude"],
  34. longitude=options["longitude"],
  35. ground_altitude=alt,
  36. height_above_ground=options["height"],
  37. loop=options["loop"])
  38. # http://www.revsys.com/blog/2014/dec/03/loading-django-files-from-code/
  39. with open(options['image'], "rb") as f:
  40. self.stdout.write("Reading image file...")
  41. p.image.save(os.path.basename(options['image']), File(f), save=False)
  42. self.stdout.write("Saving panorama to database...")
  43. p.save()
  44. self.stdout.write("Launching tile generation...")
  45. p.generate_tiles()
  46. self.stdout.write("Success!")