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);
27 map.events.register("changebaselayer", map, htmlUrlChanged);
29 openSidebar({ onclose: stopExport });
31 if (map.baseLayer.name == "Mapnik") {
32 $("format_mapnik").checked = true;
33 } else if (map.baseLayer.name == "Osmarender") {
34 $("format_osmarender").checked = true;
38 setBounds(map.getExtent());
40 $("viewanchor").className = "";
41 $("exportanchor").className = "active";
44 function stopExport() {
45 $("viewanchor").className = "active";
46 $("exportanchor").className = "";
50 map.events.unregister("moveend", map, mapMoved);
51 map.events.unregister("changebaselayer", map, htmlUrlChanged);
52 map.removeLayer(vectors);
55 function boundsChanged() {
56 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
57 var bounds = new OpenLayers.Bounds($("minlon").value,
62 bounds.transform(epsg4326, map.getProjectionObject());
64 map.events.unregister("moveend", map, mapMoved);
65 map.zoomToExtent(bounds);
74 $("maxlat").onchange = boundsChanged;
75 $("minlon").onchange = boundsChanged;
76 $("maxlon").onchange = boundsChanged;
77 $("minlat").onchange = boundsChanged;
79 function startDrag() {
80 $("drag_box").innerHTML='Drag a box on the map to select an area';
86 $("drag_box").onclick = startDrag;
88 function endDrag(bbox) {
89 var bounds = bbox.getBounds();
91 map.events.unregister("moveend", map, mapMoved);
97 $("drag_box").innerHTML = "Manually select a different area";
100 function startMarker() {
101 $("add_marker").innerHTML='Click on the map to add a marker';
104 markerLayer = new OpenLayers.Layer.Vector("",{
105 displayInLayerSwitcher: false,
107 externalGraphic: OpenLayers.Util.getImagesLocation() + "marker.png",
108 graphicXOffset: -10.5,
114 map.addLayer(markerLayer);
116 markerControl = new OpenLayers.Control.DrawFeature(markerLayer, OpenLayers.Handler.Point);
117 map.addControl(markerControl);
119 markerLayer.events.on({ "featureadded": endMarker });
122 markerLayer.destroyFeatures();
123 markerControl.activate();
128 $("add_marker").onclick = startMarker;
130 function endMarker(event) {
131 markerControl.deactivate();
133 $("add_marker").innerHTML = "Change marker position";
134 $("marker_inputs").style.display = "block";
136 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
137 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
138 var geom = event.feature.geometry.clone().transform(epsg900913, epsg4326);
140 $("marker_lon").value = geom.x.toFixed(5);
141 $("marker_lat").value = geom.y.toFixed(5);
146 function clearMarker() {
147 $("marker_lon").value = "";
148 $("marker_lat").value = "";
149 $("marker_inputs").style.display = "none";
150 $("add_marker").innerHTML = "Add a marker to the map";
153 markerControl.destroy();
154 markerLayer.destroy();
156 markerControl = null;
160 function mapMoved() {
161 setBounds(map.getExtent());
165 function setBounds(bounds) {
166 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
167 var decimals = Math.pow(10, Math.floor(map.getZoom() / 3));
169 bounds = bounds.clone().transform(map.getProjectionObject(), epsg4326);
171 $("minlon").value = Math.round(bounds.left * decimals) / decimals;
172 $("minlat").value = Math.round(bounds.bottom * decimals) / decimals;
173 $("maxlon").value = Math.round(bounds.right * decimals) / decimals;
174 $("maxlat").value = Math.round(bounds.top * decimals) / decimals;
180 function clearBox() {
181 vectors.destroyFeatures();
184 function drawBox(bounds) {
185 var feature = new OpenLayers.Feature.Vector(bounds.toGeometry());
187 vectors.addFeatures(feature);
190 function validateControls() {
191 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
193 if (bounds.getWidth() * bounds.getHeight() > 0.25) {
194 $("format_osm").disabled = true;
195 $("format_osm").checked = false;
196 $("export_osm").style.display = "none";
198 $("format_osm").disabled = false;
201 var max_scale = maxMapnikScale();
203 if ($("format_mapnik").checked && $("mapnik_scale").value < max_scale) {
204 $("export_commit").disabled = true;
206 $("export_commit").disabled = false;
209 $("mapnik_max_scale").innerHTML = roundScale(max_scale);
211 var max_zoom = maxOsmarenderZoom();
213 for (var o = 0; o < $("osmarender_zoom").options.length; o++) {
214 var option = $("osmarender_zoom").options[o];
216 if (option.value > max_zoom) {
217 option.disabled = true;
219 option.disabled = false;
223 if ($("osmarender_zoom").options.selectedIndex + 4 > max_zoom) {
224 $("osmarender_zoom").options.selectedIndex = max_zoom - 4;
228 function htmlUrlChanged() {
229 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
230 var layerName = map.baseLayer.name.toLowerCase();
231 var url = "http://#{SERVER_URL}/export/embed.html?bbox=" + bounds.toBBOX() + "&layer=" + layerName;
234 if ($("marker_lat").value && $("marker_lon").value) {
235 markerUrl = "&mlat=" + $("marker_lat").value + "&mlon=" + $("marker_lon").value;
236 url += "&marker=" + $("marker_lat").value + "," + $("marker_lon").value;
239 var html = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+url+'" style="border: 1px solid black"></iframe>';
241 // Create "larger map" link
242 var center = bounds.getCenterLonLat();
243 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
244 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
246 bounds.transform(epsg4326, epsg900913);
247 var zoom = map.getZoomForExtent(bounds);
249 var layers = getMapLayers();
251 html += '<br /><small><a href="http://#{SERVER_URL}/?lat='+center.lat+'&lon='+center.lon+'&zoom='+zoom+'&layers='+layers+markerUrl+'">View Larger Map</a></small>';
253 $("export_html_text").value = html;
255 if ($("format_html").checked) {
256 $("export_html_text").select();
260 function formatChanged() {
261 $("export_commit").style.display = "inline";
263 if ($("format_osm").checked) {
264 $("export_osm").style.display = "inline";
266 $("export_osm").style.display = "none";
269 if ($("format_mapnik").checked) {
270 $("mapnik_scale").value = roundScale(map.getScale());
271 $("export_mapnik").style.display = "inline";
273 $("export_mapnik").style.display = "none";
276 if ($("format_osmarender").checked) {
277 var zoom = Math.min(map.getZoom(), maxOsmarenderZoom());
279 $("osmarender_zoom").options.selectedIndex = zoom - 4;
280 $("export_osmarender").style.display = "inline";
282 $("export_osmarender").style.display = "none";
285 if ($("format_html").checked) {
286 $("export_html").style.display = "inline";
287 $("export_commit").style.display = "none";
288 $("export_html_text").select();
290 $("export_html").style.display = "none";
297 $("format_osm").onclick = formatChanged;
298 $("format_mapnik").onclick = formatChanged;
299 $("format_osmarender").onclick = formatChanged;
300 $("format_html").onclick = formatChanged;
302 function maxMapnikScale() {
303 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
304 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
305 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
307 bounds.transform(epsg4326, epsg900913);
309 return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
312 function mapnikImageSize(scale) {
313 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
314 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
315 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
317 bounds.transform(epsg4326, epsg900913);
319 return new OpenLayers.Size(Math.round(bounds.getWidth() / scale / 0.00028),
320 Math.round(bounds.getHeight() / scale / 0.00028));
323 function maxOsmarenderZoom() {
324 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
325 var xzoom = Math.LOG2E * Math.log(2000 * 1.40625 / bounds.getWidth());
326 var ymin = bounds.bottom * Math.PI / 180;
327 var ymax = bounds.top * Math.PI / 180;
328 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)))))
330 return Math.floor(Math.min(xzoom, yzoom));
333 function roundScale(scale) {
334 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
336 return precision * Math.ceil(scale / precision);
339 function mapnikSizeChanged() {
340 var size = mapnikImageSize($("mapnik_scale").value);
342 $("mapnik_image_width").innerHTML = size.w;
343 $("mapnik_image_height").innerHTML = size.h;
346 function mapnikScaleChanged() {
351 $("mapnik_scale").onchange = mapnikScaleChanged;