Debian Codegraph @

gavo/resources/web/js/footprintedit.js

raw
1 // A javascript region editor, inspired by
2 // https://github.com/lmichel/sky-region-editor
3 //
4 // Distributed by the GAVO project under Creative Commons CC0,
5 // see http://creativecommons.org/publicdomain/zero/1.0/
6 //
7 // Usage:
8 //
9 // Make a div styled according to your needs, pull in aladin lite
10 // and then arrange for this to be called:
11 //
12 // addEventListener("load", function() {
13 // embed_region_editor(document.getElementById("regionedit"),
14 // init_poly,
15 // function(poly) { do-whatever-updating-you-need });
16 // });
17 //
18 // The initial polygon needs to be an array of ICRS [ra, dec] arrays,
19 // and the updated arrays are passed in in the same way.
20
21
22 // furnish coo with a method to re-normalise after futzing around
23 // with its unit sphere coordinates.
24 Coo.prototype.normalize = function() {
25 var current_length = Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z);
26 this.x /= current_length;
27 this.y /= current_length;
28 this.z /= current_length;
29 }
30
31
32 var _GEO = {
33 // returns the minimal distance between a 2-array point
34 // and a line between two points.
35 euclid_dist_line: function(p, l1, l2) {
36 return Math.abs(
37 (l2[0]-l1[0])*(l1[1]-p[1])-(l1[0]-p[0])*(l2[1]-l1[1]))
38 /Math.sqrt((l2[0]-l1[0])**2+(l2[1]-l1[1])**2)
39 },
40
41 // return the sum for item[index] for all items in arr
42 _sum_over: function(arr, index) {
43 var sum = 0;
44 for (var i=0; i<arr.length; i++) {
45 sum += arr[i][index];
46 }
47 return sum;
48 },
49
50 // return {ra: .., dec: .., size: ..} for a view enclosing a spherical
51 // polygon enclosing an array of [ra, dec] points (this is mostly due
52 // to Laurent)
53 get_enclosing_view: function(points) {
54 // we do our computations in unit sphere coordinates
55 var vertices = points.map((p) => new Coo(p[0], p[1], 8));
56
57 // the we compute the center as the mean of the of the vertices
58 // (which can break in so many ways; let's say: we only deal
59 // with smallish, well-behaved polygons here).
60 var center = new Coo();
61 center.x = _GEO._sum_over(vertices, "x")*1./vertices.length;
62 center.y = _GEO._sum_over(vertices, "y")*1./vertices.length;
63 center.z = _GEO._sum_over(vertices, "z")*1./vertices.length;
64 center.normalize();
65 center.computeLonLat()
66
67 // center.ra, center.dec is now the center of the polygon (in
68 // some sense). The view size is twice the largest distance
69 // of our polygon points from this.
70 var view_radius = 0.001; // I claim that's a sane minimum
71 vertices.forEach((p) => {
72 view_radius = Math.max(view_radius, center.distance(p));});
73
74 return {
75 ra: center.lon,
76 dec: center.lat,
77 size: view_radius*2};
78 }
79 }
80
81 function AladinWithFootprint(container, footprint, change_callback) {
82 // The actual region editor, with is aladin lite with the footprint
83 // and an editable overlay.
84 //
85 // footprint is a sequence of 2-arrays containing ICRS world
86 // coordinates; it will be marked on the aladin display as
87 // an indication where the polygon can possibly be drawn.
88 // It doubles as an initial polygon.
89
90 this.init(container, footprint, change_callback);
91 }
92
93 AladinWithFootprint.prototype = {
94 // the constructor
95 init: function(container, footprint, change_callback) {
96 // the basic footprint
97 this.footprint = footprint;
98 // the currently selected polygon, initially the footprint
99 this.polygon = footprint.slice();
100 // the element embedding Aladin lite, a plain DOM element.
101 this.container = container;
102 // pixel radius of our handles
103 this.handle_radius = 15;
104
105 this.aladin = A.aladin($(this.container), {
106 showControl: true,
107 cooFrame: "ICRS",
108 survey: "P/DSS2/color",
109 showFullscreenControl: false,
110 showFrame: false,
111 showGotoControl: false});
112
113 this.eventSource = this.aladin.view.reticleCanvas;
114
115 var init_view = _GEO.get_enclosing_view(footprint);
116 this.aladin.gotoRaDec(init_view.ra, init_view.dec);
117 // For when Aladin isn't square, fudge view size with the aspect
118 // ratio
119 var aspect = container.offsetWidth/container.offsetHeight;
120 if (aspect<1) {
121 aspect = 1./aspect;
122 }
123 this.aladin.setFov(init_view.size*aspect);
124 this.add_footprint_overlay();
125 this.add_polygon_overlay();
126
127 // on click and hold, see if it's on one of our handles and
128 // start dragging if so. Pass on to aladin otherwise
129 this.eventSource.addEventListener("mousedown",
130 (ev) => {
131 var bounds = this.container.getBoundingClientRect();
132 var index_clicked = this.get_poly_index(
133 ev.clientX-bounds.x, ev.clientY-bounds.y);
134 if (index_clicked!=null) {
135 ev.preventDefault();
136 ev.stopPropagation();
137 // turn off AL's dragging
138 this.aladin.view.dragging = false;
139
140 this.start_dragging(index_clicked, change_callback);
141 }
142 });
143 },
144
145 // close a polygon defined by its vertices
146 close_poly: function(vert_list) {
147 var closed = vert_list.slice();
148 closed.push(vert_list[0]);
149 return closed;
150 },
151
152 // return a handle close to the canvas coordinates x, y, null if there
153 // is none
154 get_poly_index: function(x, y) {
155 for (index=0; index<this.polygon.length; index++) {
156 var pt = this.polygon[index];
157 var pc = this.aladin.world2pix(pt[0], pt[1]);
158 var dist = AstroMath.hypot(x-pc[0], y-pc[1]);
159 if (dist<=this.handle_radius*1.1) {
160 return index;
161 }
162 }
163 return null;
164 },
165
166 // set things up so the polygon vertex poly_index is being dragged
167 // as the mouse moves.
168 start_dragging: function(poly_index, change_callback) {
169 if (this.dragging_now) {
170 return;
171 }
172 this.dragging_now = true;
173
174 var bounds = this.container.getBoundingClientRect();
175 var root_x = bounds.x;
176 var root_y = bounds.y;
177 var self = this;
178
179 var move_listener = (ev) => {
180 ev.preventDefault();
181 ev.stopPropagation();
182
183 var new_pos = self.aladin.pix2world(
184 ev.clientX-root_x, ev.clientY-root_y);
185 self.polygon[poly_index] = new_pos;
186 self.update_polygon_overlay();
187 };
188
189 var finisher = (ev) => {
190 ev.preventDefault();
191 ev.stopPropagation();
192
193 self.eventSource.removeEventListener("mousemove", move_listener);
194 self.eventSource.removeEventListener("mouseup", finisher);
195 self.dragging_now = false;
196
197 if (change_callback) {
198 change_callback(this.polygon);
199 }
200 };
201
202 this.eventSource.addEventListener("mousemove", move_listener);
203 this.eventSource.addEventListener("mouseup", finisher);
204 },
205
206 // add the basic footprint as an overlay
207 add_footprint_overlay: function() {
208 var footprint_overlay = A.graphicOverlay({color: 'blue'});
209 this.aladin.addOverlay(footprint_overlay);
210 footprint_overlay.add(A.polyline(
211 this.close_poly(this.footprint)));
212 },
213
214 // add the overlay for the editable polygon (and another one for
215 // the draggable handles
216 add_polygon_overlay: function() {
217 this.polygon_overlay = A.graphicOverlay({color: 'red'});
218 this.aladin.addOverlay(this.polygon_overlay);
219
220 this.handle_overlay = A.catalog({
221 name: "Polygon handles",
222 shape: "circle",
223 sourceSize: self.handle_radius,
224 color: "red"});
225 this.aladin.addCatalog(this.handle_overlay);
226
227 this.update_polygon_overlay();
228 },
229
230 // update the polygon and the handles after the polygon has been
231 // changed.
232 update_polygon_overlay: function() {
233 this.polygon_overlay.removeAll();
234 this.handle_overlay.removeAll();
235
236 this.polygon_overlay.add(
237 A.polyline(
238 this.close_poly(this.polygon)));
239
240 for (var i=0; i<this.polygon.length; i++) {
241 var marker = A.source(
242 this.polygon[i][0], this.polygon[i][1],
243 {"index": i});
244 this.handle_overlay.addSources([marker]);
245 }
246 },
247
248 // return a world coordinate sequence as pixel coordinates using
249 // the current aladin view
250 to_pixel_coordinates: function(world_points) {
251 var a = this.aladin;
252 return world_points.map(function(p) {
253 return a.world2pix(p[0], p[1]);
254 });
255 }
256 };
257
258 // our API function: Embed the region editor in the DOM element container,
259 // starting with footprint, calling change_callback whenever
260 // the polygon is edited.
261 function embed_region_editor(container, footprint, change_callback) {
262 var awf = new AladinWithFootprint(container, footprint, change_callback);
263 }