fix_ground_altitude.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # -*- coding: utf-8 -*-
  2. """Fix the ground altitude of all reference points, by splitting it
  3. between the actual ground altitude and the height above ground.
  4. This makes call to an external API to get accurate ground altitude, and
  5. simply substract the remaining altitude as "height above aground".
  6. """
  7. from __future__ import unicode_literals, division, print_function
  8. from django.core.management.base import BaseCommand, CommandError
  9. from django.conf import settings
  10. from altitude.providers import get_altitude
  11. from panorama.models import ReferencePoint
  12. class Command(BaseCommand):
  13. help = __doc__
  14. def add_arguments(self, parser):
  15. parser.add_argument('--dry-run', action='store_true',
  16. help="Don't actually save the new altitudes")
  17. def handle(self, *args, **options):
  18. if options['dry_run']:
  19. self.stdout.write(self.style.SUCCESS("Running in dry run mode, not saving anything"))
  20. for p in ReferencePoint.objects.all():
  21. alt = get_altitude([settings.ALTITUDE_PROVIDERS[0]],
  22. timeout=5.,
  23. latitude=p.latitude,
  24. longitude=p.longitude)
  25. if alt == None:
  26. self.stderr.write("Error while fetching ground altitude for {}".format(p.name))
  27. continue
  28. self.stdout.write("{} before: {:.2f} m + {:.2f} m".format(p.name, p.ground_altitude, p.height_above_ground))
  29. p.height_above_ground = p.altitude - alt
  30. p.ground_altitude = alt
  31. self.stdout.write("{} after : {:.2f} m + {:.2f} m".format(p.name, p.ground_altitude, p.height_above_ground))
  32. if not options['dry_run']:
  33. p.save()