1 page.replace_html :sidebar_title, 'Export'
2 page.replace_html :sidebar_content, :partial => 'start'
9 function startExport() {
10 vectors = new OpenLayers.Layer.Vector("Vector Layer", {
11 displayInLayerSwitcher: false
13 map.addLayer(vectors);
15 box = new OpenLayers.Control.DrawFeature(vectors, OpenLayers.Handler.RegularPolygon, {
21 callbacks: { done: endDrag }
26 map.events.register("moveend", map, mapMoved);
28 openSidebar({ onclose: stopExport });
30 if (map.baseLayer.name == "Mapnik") {
31 $("format_mapnik").checked = true;
32 } else if (map.baseLayer.name == "Osmarender") {
33 $("format_osmarender").checked = true;
37 setBounds(map.getExtent());
39 $("viewanchor").className = "";
40 $("exportanchor").className = "active";
43 function stopExport() {
44 $("viewanchor").className = "active";
45 $("exportanchor").className = "";
49 map.events.unregister("moveend", map, mapMoved);
50 map.removeLayer(vectors);
53 function boundsChanged() {
54 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
55 var bounds = new OpenLayers.Bounds($("minlon").value,
60 bounds.transform(epsg4326, map.getProjectionObject());
62 map.events.unregister("moveend", map, mapMoved);
63 map.zoomToExtent(bounds);
72 $("maxlat").onchange = boundsChanged;
73 $("minlon").onchange = boundsChanged;
74 $("maxlon").onchange = boundsChanged;
75 $("minlat").onchange = boundsChanged;
77 function startDrag() {
78 $("drag_box").innerHTML='Drag a box on the map to select an area';
84 $("drag_box").onclick = startDrag;
86 function endDrag(bbox) {
87 var bounds = bbox.getBounds();
89 map.events.unregister("moveend", map, mapMoved);
95 $("drag_box").innerHTML = "Manually select a different area";
98 function startMarker() {
99 $("add_marker").innerHTML='Click on the map to add a marker';
101 markerLayer = new OpenLayers.Layer.Vector("",{displayInLayerSwitcher: false});
102 markerControl = new OpenLayers.Control.DrawFeature(markerLayer, OpenLayers.Handler.Point);
103 map.addLayer(markerLayer);
104 map.addControl(markerControl);
105 markerLayer.events.on({"featureadded": endMarker});
107 markerLayer.destroyFeatures();
108 markerControl.activate();
112 $("add_marker").onclick = startMarker;
114 function endMarker(event) {
116 markerControl.deactivate();
117 $("add_marker").innerHTML='Change marker position';
118 $("marker_inputs").style.display="block";
120 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
121 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
122 var geom = event.feature.geometry.clone().transform( epsg900913, epsg4326 );
123 $("marker_lon").value = geom.x.toFixed(5);
124 $("marker_lat").value = geom.y.toFixed(5);
128 function clearMarker() {
129 $("marker_lon").value = "";
130 $("marker_lat").value = "";
131 $("marker_inputs").style.display = "none";
132 $("add_marker").innerHTML='Add a marker to the map';
134 markerControl.destroy();
135 markerLayer.destroy();
137 markerControl = null;
141 function mapMoved() {
142 setBounds(map.getExtent());
146 function setBounds(bounds) {
147 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
148 var decimals = Math.pow(10, Math.floor(map.getZoom() / 3));
150 bounds = bounds.clone().transform(map.getProjectionObject(), epsg4326);
152 $("minlon").value = Math.round(bounds.left * decimals) / decimals;
153 $("minlat").value = Math.round(bounds.bottom * decimals) / decimals;
154 $("maxlon").value = Math.round(bounds.right * decimals) / decimals;
155 $("maxlat").value = Math.round(bounds.top * decimals) / decimals;
161 function clearBox() {
162 vectors.destroyFeatures();
165 function drawBox(bounds) {
166 var feature = new OpenLayers.Feature.Vector(bounds.toGeometry());
168 vectors.addFeatures(feature);
171 function validateControls() {
172 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
174 if (bounds.getWidth() * bounds.getHeight() > 0.25) {
175 $("format_osm").disabled = true;
176 $("format_osm").checked = false;
177 $("export_osm").style.display = "none";
179 $("format_osm").disabled = false;
182 var max_scale = maxMapnikScale();
184 if ($("format_mapnik").checked && $("mapnik_scale").value < max_scale) {
185 $("export_commit").disabled = true;
187 $("export_commit").disabled = false;
190 $("mapnik_max_scale").innerHTML = roundScale(max_scale);
192 var max_zoom = maxOsmarenderZoom();
194 for (var o = 0; o < $("osmarender_zoom").options.length; o++) {
195 var option = $("osmarender_zoom").options[o];
197 if (option.value > max_zoom) {
198 option.disabled = true;
200 option.disabled = false;
204 if ($("osmarender_zoom").options.selectedIndex + 4 > max_zoom) {
205 $("osmarender_zoom").options.selectedIndex = max_zoom - 4;
209 function htmlUrlChanged() {
210 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
211 var url = "http://#{SERVER_URL}/export/embed.html?bbox=" + bounds.toBBOX();
212 if ($("marker_lat").value && $("marker_lon").value) {
213 url += "&marker=" + $("marker_lat").value + "," + $("marker_lon").value;
215 var html = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+url+'" style="border: 1px solid black"></iframe>';
216 $("export_html_text").value = html;
218 if ($("format_html").checked) {
219 $("export_html_text").select();
223 function formatChanged() {
224 $("export_commit").style.display = "inline";
226 if ($("format_osm").checked) {
227 $("export_osm").style.display = "inline";
229 $("export_osm").style.display = "none";
232 if ($("format_mapnik").checked) {
233 $("mapnik_scale").value = roundScale(map.getScale());
234 $("export_mapnik").style.display = "inline";
236 $("export_mapnik").style.display = "none";
239 if ($("format_osmarender").checked) {
240 var zoom = Math.min(map.getZoom(), maxOsmarenderZoom());
242 $("osmarender_zoom").options.selectedIndex = zoom - 4;
243 $("export_osmarender").style.display = "inline";
245 $("export_osmarender").style.display = "none";
248 if ($("format_html").checked) {
249 $("export_html").style.display = "inline";
250 $("export_commit").style.display = "none";
251 $("export_html_text").select();
253 $("export_html").style.display = "none";
260 $("format_osm").onclick = formatChanged;
261 $("format_mapnik").onclick = formatChanged;
262 $("format_osmarender").onclick = formatChanged;
263 $("format_html").onclick = formatChanged;
265 function maxMapnikScale() {
266 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
267 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
268 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
270 bounds.transform(epsg4326, epsg900913);
272 return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
275 function mapnikImageSize(scale) {
276 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
277 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
278 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
280 bounds.transform(epsg4326, epsg900913);
282 return new OpenLayers.Size(Math.round(bounds.getWidth() / scale / 0.00028),
283 Math.round(bounds.getHeight() / scale / 0.00028));
286 function maxOsmarenderZoom() {
287 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
288 var xzoom = Math.LOG2E * Math.log(2000 * 1.40625 / bounds.getWidth());
289 var ymin = bounds.bottom * Math.PI / 180;
290 var ymax = bounds.top * Math.PI / 180;
291 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)))))
293 return Math.floor(Math.min(xzoom, yzoom));
296 function roundScale(scale) {
297 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
299 return precision * Math.ceil(scale / precision);
302 function mapnikSizeChanged() {
303 var size = mapnikImageSize($("mapnik_scale").value);
305 $("mapnik_image_width").innerHTML = size.w;
306 $("mapnik_image_height").innerHTML = size.h;
309 function mapnikScaleChanged() {
314 $("mapnik_scale").onchange = mapnikScaleChanged;