1 page.replace_html :sidebar_title, 'Export'
2 page.replace_html :sidebar_content, :partial => 'start'
7 function startExport() {
8 vectors = new OpenLayers.Layer.Vector("Vector Layer", {
9 displayInLayerSwitcher: false
11 map.addLayer(vectors);
13 box = new OpenLayers.Control.DrawFeature(vectors, OpenLayers.Handler.RegularPolygon, {
19 callbacks: { done: endDrag }
24 map.events.register("moveend", map, mapMoved);
26 openSidebar({ onclose: stopExport });
28 if (map.baseLayer.name == "Mapnik") {
29 $("format_mapnik").checked = true;
30 } else if (map.baseLayer.name == "Osmarender") {
31 $("format_osmarender").checked = true;
35 setBounds(map.getExtent());
37 $("viewanchor").className = "";
38 $("exportanchor").className = "active";
41 function stopExport() {
42 $("viewanchor").className = "active";
43 $("exportanchor").className = "";
46 map.events.unregister("moveend", map, mapMoved);
47 map.removeLayer(vectors);
50 function boundsChanged() {
51 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
52 var bounds = new OpenLayers.Bounds($("minlon").value,
57 bounds.transform(epsg4326, map.getProjectionObject());
59 map.events.unregister("moveend", map, mapMoved);
60 map.zoomToExtent(bounds);
69 $("maxlat").onchange = boundsChanged;
70 $("minlon").onchange = boundsChanged;
71 $("maxlon").onchange = boundsChanged;
72 $("minlat").onchange = boundsChanged;
74 function startDrag() {
75 $("drag_box").innerHTML='Drag a box on the map to select an area';
81 $("drag_box").onclick = startDrag;
83 function endDrag(bbox) {
84 var bounds = bbox.getBounds();
86 map.events.unregister("moveend", map, mapMoved);
92 $("drag_box").innerHTML = "Manually select a different area";
96 setBounds(map.getExtent());
100 function setBounds(bounds) {
101 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
102 var decimals = Math.pow(10, Math.floor(map.getZoom() / 3));
104 bounds = bounds.clone().transform(map.getProjectionObject(), epsg4326);
106 $("minlon").value = Math.round(bounds.left * decimals) / decimals;
107 $("minlat").value = Math.round(bounds.bottom * decimals) / decimals;
108 $("maxlon").value = Math.round(bounds.right * decimals) / decimals;
109 $("maxlat").value = Math.round(bounds.top * decimals) / decimals;
115 function clearBox() {
116 vectors.destroyFeatures();
119 function drawBox(bounds) {
120 var feature = new OpenLayers.Feature.Vector(bounds.toGeometry());
122 vectors.addFeatures(feature);
125 function validateControls() {
126 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
128 if (bounds.getWidth() * bounds.getHeight() > 0.25) {
129 $("format_osm").disabled = true;
130 $("format_osm").checked = false;
131 $("export_osm").style.display = "none";
133 $("format_osm").disabled = false;
136 var max_scale = maxMapnikScale();
138 if ($("format_mapnik").checked && $("mapnik_scale").value < max_scale) {
139 $("export_commit").disabled = true;
141 $("export_commit").disabled = false;
144 $("mapnik_max_scale").innerHTML = roundScale(max_scale);
146 var max_zoom = maxOsmarenderZoom();
148 for (var o = 0; o < $("osmarender_zoom").options.length; o++) {
149 var option = $("osmarender_zoom").options[o];
151 if (option.value > max_zoom) {
152 option.disabled = true;
154 option.disabled = false;
158 if ($("osmarender_zoom").options.selectedIndex + 4 > max_zoom) {
159 $("osmarender_zoom").options.selectedIndex = max_zoom - 4;
163 function htmlBoundsChanged() {
164 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
165 var projBounds = bounds.clone().transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));
166 var zoom = map.getZoomForExtent(projBounds);
167 var center = bounds.getCenterLonLat();
168 var url = "http://#{SERVER_URL}/export/embed.html?lat=" + center.lat.toFixed(5) + "&lon=" + center.lon.toFixed(5) + "&zoom=" + zoom;
170 if ($("html_marker").checked) {
171 url += "&marker=true";
174 var html = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+url+'"></iframe>';
175 $("export_html_text").value = html;
177 if ($("format_html").checked) {
178 $("export_html_text").select();
182 $("html_marker").onclick = htmlBoundsChanged;
184 function formatChanged() {
185 $("export_commit").style.display = "inline";
187 if ($("format_osm").checked) {
188 $("export_osm").style.display = "inline";
190 $("export_osm").style.display = "none";
193 if ($("format_mapnik").checked) {
194 $("mapnik_scale").value = roundScale(map.getScale());
195 $("export_mapnik").style.display = "inline";
197 $("export_mapnik").style.display = "none";
200 if ($("format_osmarender").checked) {
201 var zoom = Math.min(map.getZoom(), maxOsmarenderZoom());
203 $("osmarender_zoom").options.selectedIndex = zoom - 4;
204 $("export_osmarender").style.display = "inline";
206 $("export_osmarender").style.display = "none";
209 if ($("format_html").checked) {
210 $("export_html").style.display = "inline";
211 $("export_commit").style.display = "none";
212 $("export_html_text").select();
214 $("export_html").style.display = "none";
220 $("format_osm").onclick = formatChanged;
221 $("format_mapnik").onclick = formatChanged;
222 $("format_osmarender").onclick = formatChanged;
223 $("format_html").onclick = formatChanged;
225 function maxMapnikScale() {
226 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
227 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
228 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
230 bounds.transform(epsg4326, epsg900913);
232 return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
235 function mapnikImageSize(scale) {
236 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
237 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
238 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
240 bounds.transform(epsg4326, epsg900913);
242 return new OpenLayers.Size(Math.round(bounds.getWidth() / scale / 0.00028),
243 Math.round(bounds.getHeight() / scale / 0.00028));
246 function maxOsmarenderZoom() {
247 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
248 var xzoom = Math.LOG2E * Math.log(2000 * 1.40625 / bounds.getWidth());
249 var ymin = bounds.bottom * Math.PI / 180;
250 var ymax = bounds.top * Math.PI / 180;
251 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)))))
253 return Math.floor(Math.min(xzoom, yzoom));
256 function roundScale(scale) {
257 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
259 return precision * Math.ceil(scale / precision);
262 function mapnikSizeChanged() {
263 var size = mapnikImageSize($("mapnik_scale").value);
265 $("mapnik_image_width").innerHTML = size.w;
266 $("mapnik_image_height").innerHTML = size.h;
269 function mapnikScaleChanged() {
274 $("mapnik_scale").onchange = mapnikScaleChanged;