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 html += '<br /><small><a href="http://#{SERVER_URL}/?lat='+center.lat+'&lon='+center.lon+'&zoom='+zoom+markerUrl+'">View Larger Map</a></small>';
251 $("export_html_text").value = html;
253 if ($("format_html").checked) {
254 $("export_html_text").select();
258 function formatChanged() {
259 $("export_commit").style.display = "inline";
261 if ($("format_osm").checked) {
262 $("export_osm").style.display = "inline";
264 $("export_osm").style.display = "none";
267 if ($("format_mapnik").checked) {
268 $("mapnik_scale").value = roundScale(map.getScale());
269 $("export_mapnik").style.display = "inline";
271 $("export_mapnik").style.display = "none";
274 if ($("format_osmarender").checked) {
275 var zoom = Math.min(map.getZoom(), maxOsmarenderZoom());
277 $("osmarender_zoom").options.selectedIndex = zoom - 4;
278 $("export_osmarender").style.display = "inline";
280 $("export_osmarender").style.display = "none";
283 if ($("format_html").checked) {
284 $("export_html").style.display = "inline";
285 $("export_commit").style.display = "none";
286 $("export_html_text").select();
288 $("export_html").style.display = "none";
295 $("format_osm").onclick = formatChanged;
296 $("format_mapnik").onclick = formatChanged;
297 $("format_osmarender").onclick = formatChanged;
298 $("format_html").onclick = formatChanged;
300 function maxMapnikScale() {
301 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
302 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
303 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
305 bounds.transform(epsg4326, epsg900913);
307 return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
310 function mapnikImageSize(scale) {
311 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
312 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
313 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
315 bounds.transform(epsg4326, epsg900913);
317 return new OpenLayers.Size(Math.round(bounds.getWidth() / scale / 0.00028),
318 Math.round(bounds.getHeight() / scale / 0.00028));
321 function maxOsmarenderZoom() {
322 var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
323 var xzoom = Math.LOG2E * Math.log(2000 * 1.40625 / bounds.getWidth());
324 var ymin = bounds.bottom * Math.PI / 180;
325 var ymax = bounds.top * Math.PI / 180;
326 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)))))
328 return Math.floor(Math.min(xzoom, yzoom));
331 function roundScale(scale) {
332 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
334 return precision * Math.ceil(scale / precision);
337 function mapnikSizeChanged() {
338 var size = mapnikImageSize($("mapnik_scale").value);
340 $("mapnik_image_width").innerHTML = size.w;
341 $("mapnik_image_height").innerHTML = size.h;
344 function mapnikScaleChanged() {
349 $("mapnik_scale").onchange = mapnikScaleChanged;