Browse Source

Add a Django command to separate ground altitude from height

It uses the API to estimate the ground altitude for each point, and then
sets the remaining altitude to "height above ground".  The total altitude
(ground altitude + height above ground) remains unchanged.

To see what would happen with your data without saving the result, run:

    ./manage.py fix_ground_altitude --dry-run

If you're happy with the result, run it for real:

    ./manage.py fix_ground_altitude
Baptiste Jonglez 6 years ago
parent
commit
ee65e93b8f
1 changed files with 43 additions and 0 deletions
  1. 43 0
      panorama/management/commands/fix_ground_altitude.py

+ 43 - 0
panorama/management/commands/fix_ground_altitude.py

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