Browse Source

Hack the current js code to make it kinda work with the Django backend

Baptiste Jonglez 10 years ago
parent
commit
34856287d7
2 changed files with 62 additions and 74 deletions
  1. 38 1
      panorama/models.py
  2. 24 73
      panorama/templates/panorama/view.html

+ 38 - 1
panorama/models.py

@@ -3,7 +3,7 @@ from __future__ import unicode_literals, division, print_function
 
 import subprocess
 import os
-from math import radians, degrees, sin, cos, asin, atan2, sqrt
+from math import radians, degrees, sin, cos, asin, atan2, sqrt, ceil
 
 from django.db import models
 from django.conf import settings
@@ -149,6 +149,43 @@ class Panorama(ReferencePoint):
     def get_absolute_url(self):
         return reverse('panorama:view_pano', args=[str(self.pk)])
 
+    def tiles_data(self):
+        """Hack to feed the current js code with tiles data (we should use the
+        JSON API instead, and get rid of this function)"""
+        data = dict()
+        for zoomlevel in range(9):
+            width = self.image_width >> zoomlevel
+            height = self.image_height >> zoomlevel
+            d = dict()
+            d["tile_width"] = d["tile_height"] = 256
+            # Python3-style division
+            d["ntiles_x"] = int(ceil(width / 256))
+            d["ntiles_y"] = int(ceil(height / 256))
+            d["last_tile_width"] = width % 256
+            d["last_tile_height"] = height % 256
+            data[zoomlevel] = d
+        return data
+
+    def refpoints_data(self):
+        """Similar hack, returns all reference points around the panorama."""
+        refpoints = [refpoint for refpoint in ReferencePoint.objects.all()
+                     if self.great_circle_distance(refpoint) <= settings.PANORAMA_MAX_DISTANCE and refpoint.pk != self.pk]
+        return enumerate([{"name": r.name,
+                           "cap": self.bearing(r),
+                           "elevation": self.elevation(r),
+                           "distance": self.line_distance(r) / 1000}
+                          for r in refpoints])
+
+    def references_data(self):
+        """Similar hack, returns all references currently associated to the
+        panorama."""
+        return [{"name": r.reference_point.name,
+                 "x": r.x,
+                 "y": r.y,
+                 "cap": self.bearing(r.reference_point),
+                 "elevation": self.elevation(r.reference_point)}
+                for r in self.panorama_references.all()]
+
     def __str__(self):
         return "Panorama : " + self.name
 

+ 24 - 73
panorama/templates/panorama/view.html

@@ -7,79 +7,29 @@
     <script>
       var title = "{{ panorama.name|escapejs }}";
       var img_prefix = '{{ panorama.tiles_url }}';
-      var image_loop = true;
+      var image_loop = {{ panorama.loop }};
     </script>
     <script src="{% static "panorama/js/pano.js" %}"></script>
     <script>window.onload = load_pano</script>
     <script>
-      zooms[0] = new tzoom(0);
-      zooms[0].ntiles.x = 94;
-      zooms[0].ntiles.y = 7;
-      zooms[0].tile.width = 256;
-      zooms[0].tile.height = 256;
-      zooms[0].last_tile.width = 109;
-      zooms[0].last_tile.height = 5;
-      zooms[1] = new tzoom(1);
-      zooms[1].ntiles.x = 47;
-      zooms[1].ntiles.y = 4;
-      zooms[1].tile.width = 256;
-      zooms[1].tile.height = 256;
-      zooms[1].last_tile.width = 183;
-      zooms[1].last_tile.height = 3;
-      zooms[2] = new tzoom(2);
-      zooms[2].ntiles.x = 24;
-      zooms[2].ntiles.y = 2;
-      zooms[2].tile.width = 256;
-      zooms[2].tile.height = 256;
-      zooms[2].last_tile.width = 91;
-      zooms[2].last_tile.height = 129;
-      zooms[3] = new tzoom(3);
-      zooms[3].ntiles.x = 12;
-      zooms[3].ntiles.y = 1;
-      zooms[3].tile.width = 256;
-      zooms[3].tile.height = 193;
-      zooms[3].last_tile.width = 174;
-      zooms[3].last_tile.height = 193;
-      zooms[4] = new tzoom(4);
-      zooms[4].ntiles.x = 6;
-      zooms[4].ntiles.y = 1;
-      zooms[4].tile.width = 256;
-      zooms[4].tile.height = 96;
-      zooms[4].last_tile.width = 215;
-      zooms[4].last_tile.height = 96;
-      zooms[5] = new tzoom(5);
-      zooms[5].ntiles.x = 3;
-      zooms[5].ntiles.y = 1;
-      zooms[5].tile.width = 256;
-      zooms[5].tile.height = 48;
-      zooms[5].last_tile.width = 234;
-      zooms[5].last_tile.height = 48;
-      zooms[6] = new tzoom(6);
-      zooms[6].ntiles.x = 2;
-      zooms[6].ntiles.y = 1;
-      zooms[6].tile.width = 256;
-      zooms[6].tile.height = 24;
-      zooms[6].last_tile.width = 117;
-      zooms[6].last_tile.height = 24;
-      zooms[7] = new tzoom(7);
-      zooms[7].ntiles.x = 1;
-      zooms[7].ntiles.y = 1;
-      zooms[7].tile.width = 187;
-      zooms[7].tile.height = 12;
-      zooms[7].last_tile.width = 187;
-      zooms[7].last_tile.height = 12;
-      zooms[8] = new tzoom(8);
-      zooms[8].ntiles.x = 1;
-      zooms[8].ntiles.y = 1;
-      zooms[8].tile.width = 93;
-      zooms[8].tile.height = 6;
-      zooms[8].last_tile.width = 93;
-      zooms[8].last_tile.height = 6;
-      point_list[0] = new Array("Tour Part-Dieu (crayon)", 0.714758, 41.956659, 11.233478, "");
-      point_list[1] = new Array("Tour métallique de Fourvière", 2.133944, -66.563873, 4.625330, "");
+      {% for zoom_level, data in panorama.tiles_data.items %}
+      zooms[{{ zoom_level }}] = new tzoom({{ zoom_level }});
+      zooms[{{ zoom_level }}].ntiles.x = {{ data.ntiles_x }};
+      zooms[{{ zoom_level }}].ntiles.y = {{ data.ntiles_y }};
+      zooms[{{ zoom_level }}].tile.width = {{ data.tile_width }};
+      zooms[{{ zoom_level }}].tile.height = {{ data.tile_height }};
+      zooms[{{ zoom_level }}].last_tile.width = {{ data.last_tile_width }};
+      zooms[{{ zoom_level }}].last_tile.height = {{ data.last_tile_height }};
+      {% endfor %}
+
+      {% for id, refpoint in panorama.refpoints_data %}
+      point_list[{{ id }}] = new Array("{{ refpoint.name }}", {{ refpoint.distance }}, {{ refpoint.cap }}, {{ refpoint.elevation }}, "");
+      {% endfor %}
+
       ref_points = new Array();
-      ref_points["Tour Part-Dieu (crayon)"] = {x:0.82724, cap:41.95666, y:0.43590, ele:11.23348};
-      ref_points["Tour métallique de Fourvière"] = {x:0.52327, cap:-66.56387, y:0.13405, ele:4.62533};
+      {% for ref in panorama.references_data %}
+      ref_points["{{ ref.name }}"] = {x: {{ ref.x }}, y: {{ ref.y }}, cap: {{ ref.cap }}, ele: {{ ref.elevation }}};
+      {% endfor %}
     </script>
     <link type="image/x-icon" rel="shortcut icon" href="{% static "panorama/img/tsf.png" %}"/>
     <link rel="stylesheet" media="screen" href="{% static "panorama/css/map.css" %}" />
@@ -97,9 +47,9 @@
     </fieldset>
 
     <div id="params">
-      <p>latitude :   <em><span id="pos_lat">45.75628</span>°</em></p>
-      <p>longitude : <em><span id="pos_lon">4.84759</span>°</em></p>
-      <p>altitude : <em><span id="pos_alt">189</span> m</em></p>
+      <p>latitude :   <em><span id="pos_lat">{{ panorama.latitude }}</span>°</em></p>
+      <p>longitude : <em><span id="pos_lon">{{ panorama.longitude }}</span>°</em></p>
+      <p>altitude : <em><span id="pos_alt">{{ panorama.altitude }}</span> m</em></p>
     </div>
     <img src="{% static "panorama/img/locapoint.svg" %}" id="loca_show" alt="localiser un point" title="pour localiser un point..."/>
     <fieldset id="locadraw"><legend id="loca_hide">Localiser un point</legend>
@@ -115,8 +65,9 @@
       </div>
     </fieldset><p id="info"></p>
     <p id="insert"><select id="sel_point" name="known_points">
-        <option>Tour Part-Dieu (crayon)</option>
-        <option>Tour métallique de Fourvière</option>
+        {% for id, refpoint in panorama.refpoints_data %}
+        <option>{{ refpoint.name }}</option>
+        {% endfor %}
       </select>
       <input type="button" id="do-insert" value="insérer"/>
       <input type="button" id="do-delete" value="suppimer"/>