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;
114 function clearBox() {
115 vectors.destroyFeatures();
118 function drawBox(bounds) {
119 var feature = new OpenLayers.Feature.Vector(bounds.toGeometry());
121 vectors.addFeatures(feature);
124 function validateControls() {
125 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
127 if (bounds.getWidth() * bounds.getHeight() > 0.25) {
128 $("format_osm").disabled = true;
129 $("format_osm").checked = false;
130 $("export_osm").style.display = "none";
132 $("format_osm").disabled = false;
135 var max_scale = maxMapnikScale();
137 if ($("format_mapnik").checked && $("mapnik_scale").value < max_scale) {
138 $("export_commit").disabled = true;
140 $("export_commit").disabled = false;
143 $("mapnik_max_scale").innerHTML = roundScale(max_scale);
145 var max_zoom = maxOsmarenderZoom();
147 for (var o = 0; o < $("osmarender_zoom").options.length; o++) {
148 var option = $("osmarender_zoom").options[o];
150 if (option.value > max_zoom) {
151 option.disabled = true;
153 option.disabled = false;
157 if ($("osmarender_zoom").options.selectedIndex + 4 > max_zoom) {
158 $("osmarender_zoom").options.selectedIndex = max_zoom - 4;
162 function formatChanged() {
163 if ($("format_osm").checked) {
164 $("export_osm").style.display = "inline";
166 $("export_osm").style.display = "none";
169 if ($("format_mapnik").checked) {
170 $("mapnik_scale").value = roundScale(map.getScale());
171 $("export_mapnik").style.display = "inline";
173 $("export_mapnik").style.display = "none";
176 if ($("format_osmarender").checked) {
177 var zoom = Math.min(map.getZoom(), maxOsmarenderZoom());
179 $("osmarender_zoom").options.selectedIndex = zoom - 4;
180 $("export_osmarender").style.display = "inline";
182 $("export_osmarender").style.display = "none";
188 $("format_osm").onclick = formatChanged;
189 $("format_mapnik").onclick = formatChanged;
190 $("format_osmarender").onclick = formatChanged;
192 function maxMapnikScale() {
193 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
194 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
195 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
197 bounds.transform(epsg4326, epsg900913);
199 return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
202 function mapnikImageSize(scale) {
203 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
204 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
205 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
207 bounds.transform(epsg4326, epsg900913);
209 return new OpenLayers.Size(Math.round(bounds.getWidth() / scale / 0.00028),
210 Math.round(bounds.getHeight() / scale / 0.00028));
213 function maxOsmarenderZoom() {
214 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
215 var xzoom = Math.LOG2E * Math.log(2000 * 1.40625 / bounds.getWidth());
216 var ymin = bounds.bottom * Math.PI / 180;
217 var ymax = bounds.top * Math.PI / 180;
218 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 return Math.floor(Math.min(xzoom, yzoom));
223 function roundScale(scale) {
224 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
226 return precision * Math.ceil(scale / precision);
229 function mapnikSizeChanged() {
230 var size = mapnikImageSize($("mapnik_scale").value);
232 $("mapnik_image_width").innerHTML = size.w;
233 $("mapnik_image_height").innerHTML = size.h;
236 function mapnikScaleChanged() {
241 $("mapnik_scale").onchange = mapnikScaleChanged;