]> git.openstreetmap.org Git - rails.git/blob - app/views/export/start.rjs
drop _view from action names
[rails.git] / app / views / export / start.rjs
1 page.replace_html :sidebar_title, 'Export'
2 page.replace_html :sidebar_content, :partial => 'start'
3 page << <<EOJ
4   var vectors;
5   var box;
6
7   function startExport() {
8     vectors = new OpenLayers.Layer.Vector("Vector Layer", {
9       displayInLayerSwitcher: false
10     });
11     map.addLayer(vectors);
12
13     box = new OpenLayers.Control.DrawFeature(vectors, OpenLayers.Handler.RegularPolygon, { 
14       handlerOptions: {
15         sides: 4,
16         snapAngle: 90,
17         irregular: true,
18         persist: true,
19         callbacks: { done: endDrag }
20       }
21     });
22     map.addControl(box);
23
24     map.events.register("moveend", map, mapMoved);
25
26     openSidebar({ onclose: stopExport });
27
28     setBounds(map.getExtent());
29
30     if (map.baseLayer.name == "Mapnik") {
31       $("format_mapnik").checked = true;
32     } else if (map.baseLayer.name == "Osmarender") {
33       $("format_osmarender").checked = true;
34     }
35
36     formatChanged();
37
38     $("viewanchor").className = "";
39     $("exportanchor").className = "active";
40   }
41
42   function stopExport() {
43     $("viewanchor").className = "active";
44     $("exportanchor").className = "";
45
46     clearBox();
47     map.events.unregister("moveend", map, mapMoved);
48     map.removeLayer(vectors);
49   }
50
51   function boundsChanged() {
52     var epsg4326 = new OpenLayers.Projection("EPSG:4326");
53     var bounds = new OpenLayers.Bounds($("minlon").value,
54                                        $("minlat").value,
55                                        $("maxlon").value,
56                                        $("maxlat").value);
57  
58     bounds.transform(epsg4326, map.getProjectionObject());
59
60     map.events.unregister("moveend", map, mapMoved);
61     map.zoomToExtent(bounds);
62
63     clearBox();
64     drawBox(bounds);
65
66     validateControls();
67     mapnikSizeChanged();
68   }
69
70   $("maxlat").onchange = boundsChanged;
71   $("minlon").onchange = boundsChanged;
72   $("maxlon").onchange = boundsChanged;
73   $("minlat").onchange = boundsChanged;
74
75   function startDrag() {
76     $("drag_box").innerHTML='Drag a box on the map to select an area';
77
78     clearBox();
79     box.activate();
80   };
81
82   $("drag_box").onclick = startDrag;
83
84   function endDrag(bbox) {
85     var bounds = bbox.getBounds();
86
87     map.events.unregister("moveend", map, mapMoved);
88     setBounds(bounds);
89     drawBox(bounds);
90     box.deactivate();
91     validateControls();
92
93     $("drag_box").innerHTML = "Manually select a different area";
94   }
95
96   function mapMoved() {
97     setBounds(map.getExtent());
98     validateControls();
99   }
100
101   function setBounds(bounds) {
102     var epsg4326 = new OpenLayers.Projection("EPSG:4326");
103     var decimals = Math.pow(10, Math.floor(map.getZoom() / 3));
104
105     bounds = bounds.clone().transform(map.getProjectionObject(), epsg4326);
106
107     $("minlon").value = Math.round(bounds.left * decimals) / decimals;
108     $("minlat").value = Math.round(bounds.bottom * decimals) / decimals;
109     $("maxlon").value = Math.round(bounds.right * decimals) / decimals;
110     $("maxlat").value = Math.round(bounds.top * decimals) / decimals;
111
112     mapnikSizeChanged();
113   }
114
115   function clearBox() {
116     vectors.destroyFeatures();
117   }
118
119   function drawBox(bounds) {
120     var feature = new OpenLayers.Feature.Vector(bounds.toGeometry());
121
122     vectors.addFeatures(feature);
123   }
124
125   function validateControls() {
126     var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
127
128     if (bounds.getWidth() * bounds.getHeight() > 0.25) {
129       $("format_osm").disabled = true;
130       $("format_osm").checked = false;
131       $("export_osm").style.display = "none";
132     } else {
133       $("format_osm").disabled = false;
134     }
135
136     var max_scale = maxMapnikScale();
137
138     if ($("format_mapnik").checked && $("mapnik_scale").value < max_scale) {
139       $("export_commit").disabled = true;
140     } else {
141       $("export_commit").disabled = false;
142     }
143
144     $("mapnik_max_scale").innerHTML = roundScale(max_scale);
145   
146     var max_zoom = maxOsmarenderZoom();
147
148     for (var o = 0; o < $("osmarender_zoom").options.length; o++) {
149       var option = $("osmarender_zoom").options[o];
150
151       if (option.value > max_zoom) {
152         option.disabled = true;
153       } else {
154         option.disabled = false;
155       }
156     }
157
158     if ($("osmarender_zoom").options.selectedIndex + 4 > max_zoom) {
159       $("osmarender_zoom").options.selectedIndex = max_zoom - 4;
160     }
161   }
162
163   function formatChanged() {
164     if ($("format_osm").checked) {
165       $("export_osm").style.display = "inline";
166     } else {
167       $("export_osm").style.display = "none";
168     }
169
170     if ($("format_mapnik").checked) {
171       $("mapnik_scale").value = roundScale(map.getScale());
172       $("export_mapnik").style.display = "inline";
173     } else {
174       $("export_mapnik").style.display = "none";
175     }
176
177     if ($("format_osmarender").checked) {
178       var zoom = Math.min(map.getZoom(), maxOsmarenderZoom());
179
180       $("osmarender_zoom").options.selectedIndex = zoom - 4;
181       $("export_osmarender").style.display = "inline";
182     } else {
183       $("export_osmarender").style.display = "none";
184     }
185
186     validateControls();
187   }
188
189   $("format_osm").onclick = formatChanged;
190   $("format_mapnik").onclick = formatChanged;
191   $("format_osmarender").onclick = formatChanged;
192
193   function maxMapnikScale() {
194     var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
195     var epsg4326 = new OpenLayers.Projection("EPSG:4326");
196     var epsg900913 = new OpenLayers.Projection("EPSG:900913");
197
198     bounds.transform(epsg4326, epsg900913);
199
200     return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
201   }
202
203   function mapnikImageSize(scale) {
204     var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
205     var epsg4326 = new OpenLayers.Projection("EPSG:4326");
206     var epsg900913 = new OpenLayers.Projection("EPSG:900913");
207
208     bounds.transform(epsg4326, epsg900913);
209
210     return new OpenLayers.Size(Math.round(bounds.getWidth() / scale / 0.00028),
211                                Math.round(bounds.getHeight() / scale / 0.00028));
212   }
213
214   function maxOsmarenderZoom() {
215     var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
216     var xzoom = Math.LOG2E * Math.log(2000 * 1.40625 / bounds.getWidth());
217     var ymin = bounds.bottom * Math.PI / 180;
218     var ymax = bounds.top * Math.PI / 180;
219     var yzoom = Math.LOG2E * (Math.log(2000 * 2 * Math.PI) - Math.log(Math.log((Math.tan(ymax) + 1 / Math.cos(ymax)) / (Math.tan(ymin) + 1 / Math.cos(ymin)))))
220
221     return Math.floor(Math.min(xzoom, yzoom));
222   }
223
224   function roundScale(scale) {
225     var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
226
227     return precision * Math.ceil(scale / precision);
228   }
229
230   function mapnikSizeChanged() {
231     var size = mapnikImageSize($("mapnik_scale").value);
232
233     $("mapnik_image_width").innerHTML = size.w;
234     $("mapnik_image_height").innerHTML = size.h;
235   }
236
237   function mapnikScaleChanged() {
238     mapnikSizeChanged();
239     validateControls();
240   }
241
242   $("mapnik_scale").onchange = mapnikScaleChanged;
243
244   startExport();
245 EOJ