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 setBounds(map.getExtent());
30 if (map.baseLayer.name == "Mapnik") {
31 $("format_mapnik").checked = true;
32 } else if (map.baseLayer.name == "Osmarender") {
33 $("format_osmarender").checked = true;
38 $("viewanchor").className = "";
39 $("exportanchor").className = "active";
42 function stopExport() {
43 $("viewanchor").className = "active";
44 $("exportanchor").className = "";
47 map.events.unregister("moveend", map, mapMoved);
48 map.removeLayer(vectors);
51 function boundsChanged() {
52 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
53 var bounds = new OpenLayers.Bounds($("minlon").value,
58 bounds.transform(epsg4326, map.getProjectionObject());
60 map.events.unregister("moveend", map, mapMoved);
61 map.zoomToExtent(bounds);
70 $("maxlat").onchange = boundsChanged;
71 $("minlon").onchange = boundsChanged;
72 $("maxlon").onchange = boundsChanged;
73 $("minlat").onchange = boundsChanged;
75 function startDrag() {
76 $("drag_box").innerHTML='Drag a box on the map to select an area';
82 $("drag_box").onclick = startDrag;
84 function endDrag(bbox) {
85 var bounds = bbox.getBounds();
87 map.events.unregister("moveend", map, mapMoved);
93 $("drag_box").innerHTML = "Manually select a different area";
97 setBounds(map.getExtent());
101 function setBounds(bounds) {
102 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
103 var decimals = Math.pow(10, Math.floor(map.getZoom() / 3));
105 bounds = bounds.clone().transform(map.getProjectionObject(), epsg4326);
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;
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 formatChanged() {
164 if ($("format_osm").checked) {
165 $("export_osm").style.display = "inline";
167 $("export_osm").style.display = "none";
170 if ($("format_mapnik").checked) {
171 $("mapnik_scale").value = roundScale(map.getScale());
172 $("export_mapnik").style.display = "inline";
174 $("export_mapnik").style.display = "none";
177 if ($("format_osmarender").checked) {
178 var zoom = Math.min(map.getZoom(), maxOsmarenderZoom());
180 $("osmarender_zoom").options.selectedIndex = zoom - 4;
181 $("export_osmarender").style.display = "inline";
183 $("export_osmarender").style.display = "none";
189 $("format_osm").onclick = formatChanged;
190 $("format_mapnik").onclick = formatChanged;
191 $("format_osmarender").onclick = formatChanged;
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");
198 bounds.transform(epsg4326, epsg900913);
200 return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
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");
208 bounds.transform(epsg4326, epsg900913);
210 return new OpenLayers.Size(Math.round(bounds.getWidth() / scale / 0.00028),
211 Math.round(bounds.getHeight() / scale / 0.00028));
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)))))
221 return Math.floor(Math.min(xzoom, yzoom));
224 function roundScale(scale) {
225 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
227 return precision * Math.ceil(scale / precision);
230 function mapnikSizeChanged() {
231 var size = mapnikImageSize($("mapnik_scale").value);
233 $("mapnik_image_width").innerHTML = size.w;
234 $("mapnik_image_height").innerHTML = size.h;
237 function mapnikScaleChanged() {
242 $("mapnik_scale").onchange = mapnikScaleChanged;