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 = "";
46 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);
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;
112 function clearBox() {
113 vectors.destroyFeatures();
116 function drawBox(bounds) {
117 var feature = new OpenLayers.Feature.Vector(bounds.toGeometry());
119 vectors.addFeatures(feature);
122 function validateControls() {
123 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
125 if (bounds.getWidth() * bounds.getHeight() > 0.25) {
126 $("format_osm").disabled = true;
127 $("format_osm").checked = false;
128 $("export_osm").style.display = "none";
130 $("format_osm").disabled = false;
133 var max_zoom = maxOsmarenderZoom();
134 var max_scale = maxMapnikScale();
136 $("mapnik_max_scale").innerHTML = roundScale(max_scale);
138 mapnikScaleChanged();
140 for (var o = 0; o < $("osmarender_zoom").options.length; o++) {
141 var option = $("osmarender_zoom").options[o];
143 if (option.value > max_zoom) {
144 option.disabled = true;
146 option.disabled = false;
150 if ($("osmarender_zoom").options.selectedIndex + 4 > max_zoom) {
151 $("osmarender_zoom").options.selectedIndex = max_zoom - 4;
155 function formatChanged() {
156 if ($("format_osm").checked) {
157 $("export_osm").style.display = "inline";
159 $("export_osm").style.display = "none";
162 if ($("format_mapnik").checked) {
163 $("mapnik_scale").value = roundScale(map.getScale());
164 $("export_mapnik").style.display = "inline";
166 mapnikScaleChanged();
168 $("export_mapnik").style.display = "none";
171 if ($("format_osmarender").checked) {
172 var zoom = Math.min(map.getZoom(), maxOsmarenderZoom());
174 $("osmarender_zoom").options.selectedIndex = zoom - 4;
175 $("export_osmarender").style.display = "inline";
177 $("export_osmarender").style.display = "none";
181 $("format_osm").onclick = formatChanged;
182 $("format_mapnik").onclick = formatChanged;
183 $("format_osmarender").onclick = formatChanged;
185 function maxMapnikScale() {
186 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
187 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
188 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
190 bounds.transform(epsg4326, epsg900913);
192 return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
195 function mapnikImageSize(scale) {
196 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
197 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
198 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
200 bounds.transform(epsg4326, epsg900913);
202 return new OpenLayers.Size(Math.round(bounds.getWidth() / scale / 0.00028),
203 Math.round(bounds.getHeight() / scale / 0.00028));
206 function maxOsmarenderZoom() {
207 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
208 var xzoom = Math.LOG2E * Math.log(2000 * 1.40625 / bounds.getWidth());
209 var ymin = bounds.bottom * Math.PI / 180;
210 var ymax = bounds.top * Math.PI / 180;
211 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)))))
213 return Math.floor(Math.min(xzoom, yzoom));
216 function roundScale(scale) {
217 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
219 return precision * Math.ceil(scale / precision);
222 function mapnikSizeChanged() {
223 var size = mapnikImageSize($("mapnik_scale").value);
225 $("mapnik_image_width").innerHTML = size.w;
226 $("mapnik_image_height").innerHTML = size.h;
229 function mapnikScaleChanged() {
232 if ($("mapnik_scale").value < maxMapnikScale()) {
233 $("export_commit").disabled = true;
235 $("export_commit").disabled = false;
239 $("mapnik_scale").onchange = mapnikScaleChanged;