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;
233 if ($("marker_lat").value && $("marker_lon").value) {
234 url += "&marker=" + $("marker_lat").value + "," + $("marker_lon").value;
237 var html = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+url+'" style="border: 1px solid black"></iframe>';
238 $("export_html_text").value = html;
240 if ($("format_html").checked) {
241 $("export_html_text").select();
245 function formatChanged() {
246 $("export_commit").style.display = "inline";
248 if ($("format_osm").checked) {
249 $("export_osm").style.display = "inline";
251 $("export_osm").style.display = "none";
254 if ($("format_mapnik").checked) {
255 $("mapnik_scale").value = roundScale(map.getScale());
256 $("export_mapnik").style.display = "inline";
258 $("export_mapnik").style.display = "none";
261 if ($("format_osmarender").checked) {
262 var zoom = Math.min(map.getZoom(), maxOsmarenderZoom());
264 $("osmarender_zoom").options.selectedIndex = zoom - 4;
265 $("export_osmarender").style.display = "inline";
267 $("export_osmarender").style.display = "none";
270 if ($("format_html").checked) {
271 $("export_html").style.display = "inline";
272 $("export_commit").style.display = "none";
273 $("export_html_text").select();
275 $("export_html").style.display = "none";
282 $("format_osm").onclick = formatChanged;
283 $("format_mapnik").onclick = formatChanged;
284 $("format_osmarender").onclick = formatChanged;
285 $("format_html").onclick = formatChanged;
287 function maxMapnikScale() {
288 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
289 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
290 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
292 bounds.transform(epsg4326, epsg900913);
294 return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
297 function mapnikImageSize(scale) {
298 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
299 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
300 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
302 bounds.transform(epsg4326, epsg900913);
304 return new OpenLayers.Size(Math.round(bounds.getWidth() / scale / 0.00028),
305 Math.round(bounds.getHeight() / scale / 0.00028));
308 function maxOsmarenderZoom() {
309 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
310 var xzoom = Math.LOG2E * Math.log(2000 * 1.40625 / bounds.getWidth());
311 var ymin = bounds.bottom * Math.PI / 180;
312 var ymax = bounds.top * Math.PI / 180;
313 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)))))
315 return Math.floor(Math.min(xzoom, yzoom));
318 function roundScale(scale) {
319 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
321 return precision * Math.ceil(scale / precision);
324 function mapnikSizeChanged() {
325 var size = mapnikImageSize($("mapnik_scale").value);
327 $("mapnik_image_width").innerHTML = size.w;
328 $("mapnik_image_height").innerHTML = size.h;
331 function mapnikScaleChanged() {
336 $("mapnik_scale").onchange = mapnikScaleChanged;