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, {
21 box.handler.callbacks.done = endDrag;
24 transform = new OpenLayers.Control.TransformFeature(vectors, {
28 transform.events.register("transformcomplete", transform, transformComplete);
29 map.addControl(transform);
31 map.events.register("moveend", map, mapMoved);
32 map.events.register("changebaselayer", map, htmlUrlChanged);
34 $("#sidebar_title").html("<%=j t 'export.start_rjs.export' %>");
35 $("#sidebar_content").html("<%=j render :partial => "sidebar" %>");
37 $("#maxlat").change(boundsChanged);
38 $("#minlon").change(boundsChanged);
39 $("#maxlon").change(boundsChanged);
40 $("#minlat").change(boundsChanged);
42 $("#drag_box").click(startDrag);
44 $("#add_marker").click(startMarker);
46 $("#format_osm").click(formatChanged);
47 $("#format_mapnik").click(formatChanged);
48 $("#format_html").click(formatChanged);
50 $("#mapnik_scale").change(mapnikSizeChanged);
52 openSidebar({ onclose: stopExport });
54 if (map.baseLayer.name == "Mapnik") {
55 $("#format_mapnik").prop("checked", true);
59 setBounds(map.getExtent());
61 $("#viewanchor").removeClass("active");
62 $("#exportanchor").addClass("active");
65 function stopExport() {
66 $("#viewanchor").addClass("active");
67 $("#exportanchor").removeClass("active");
71 map.events.unregister("moveend", map, mapMoved);
72 map.events.unregister("changebaselayer", map, htmlUrlChanged);
73 map.removeLayer(vectors);
76 function boundsChanged() {
77 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
78 var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(),
79 $("#maxlon").val(), $("#maxlat").val());
81 bounds.transform(epsg4326, map.getProjectionObject());
83 map.events.unregister("moveend", map, mapMoved);
84 map.zoomToExtent(bounds);
93 function startDrag() {
94 $("#drag_box").html("<%=j t 'export.start_rjs.drag_a_box' %>");
100 function endDrag(bbox) {
101 var bounds = bbox.getBounds();
103 map.events.unregister("moveend", map, mapMoved);
109 $("#drag_box").html("<%=j t 'export.start_rjs.manually_select' %>");
112 function transformComplete(event) {
113 setBounds(event.feature.geometry.bounds);
117 function startMarker() {
118 $("#add_marker").html("<%=j t 'export.start_rjs.click_add_marker' %>");
121 markerLayer = new OpenLayers.Layer.Vector("",{
122 displayInLayerSwitcher: false,
124 externalGraphic: OpenLayers.Util.getImageLocation("marker.png"),
125 graphicXOffset: -10.5,
131 map.addLayer(markerLayer);
133 markerControl = new OpenLayers.Control.DrawFeature(markerLayer, OpenLayers.Handler.Point);
134 map.addControl(markerControl);
136 markerLayer.events.on({ "featureadded": endMarker });
139 markerLayer.destroyFeatures();
140 markerControl.activate();
145 function endMarker(event) {
146 markerControl.deactivate();
148 $("#add_marker").html("<%=j t 'export.start_rjs.change_marker' %>");
149 $("#marker_inputs").show();
151 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
152 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
153 var geom = event.feature.geometry.clone().transform(epsg900913, epsg4326);
155 $("#marker_lon").val(geom.x.toFixed(5));
156 $("#marker_lat").val(geom.y.toFixed(5));
161 function clearMarker() {
162 $("#marker_lon").val("");
163 $("#marker_lat").val("");
164 $("#marker_inputs").hide();
165 $("#add_marker").html("<%=j t 'export.start_rjs.add_marker' %>");
168 markerControl.destroy();
169 markerLayer.destroy();
171 markerControl = null;
175 function mapMoved() {
176 setBounds(map.getExtent());
180 function setBounds(bounds) {
181 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
182 var decimals = Math.pow(10, Math.floor(map.getZoom() / 3));
184 bounds = bounds.clone().transform(map.getProjectionObject(), epsg4326);
186 $("#minlon").val(Math.round(bounds.left * decimals) / decimals);
187 $("#minlat").val(Math.round(bounds.bottom * decimals) / decimals);
188 $("#maxlon").val(Math.round(bounds.right * decimals) / decimals);
189 $("#maxlat").val(Math.round(bounds.top * decimals) / decimals);
195 function clearBox() {
196 transform.deactivate();
197 vectors.destroyFeatures();
200 function drawBox(bounds) {
201 var feature = new OpenLayers.Feature.Vector(bounds.toGeometry());
203 vectors.addFeatures(feature);
204 transform.setFeature(feature);
207 function validateControls() {
208 var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(), $("#maxlon").val(), $("#maxlat").val());
210 if (bounds.getWidth() * bounds.getHeight() > <%= MAX_REQUEST_AREA %>) {
211 $("#export_osm_too_large").show();
213 $("#export_osm_too_large").hide();
216 var max_scale = maxMapnikScale();
218 if ($("#format_osm").prop("checked") && bounds.getWidth() * bounds.getHeight() > <%= MAX_REQUEST_AREA %>) {
219 $("#export_commit").prop("disabled", true);
220 } else if ($("#format_mapnik").prop("checked") && $("#mapnik_scale").val() < max_scale) {
221 $("#export_commit").prop("disabled", true);
223 $("#export_commit").prop("disabled", false);
226 $("#mapnik_max_scale").html(roundScale(max_scale));
229 function htmlUrlChanged() {
230 var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(), $("#maxlon").val(), $("#maxlat").val());
231 var layerName = map.baseLayer.keyid;
232 var url = "http://<%= SERVER_URL %>/export/embed.html?bbox=" + bounds.toBBOX() + "&layer=" + layerName;
235 if ($("#marker_lat").val() && $("#marker_lon").val()) {
236 markerUrl = "&mlat=" + $("#marker_lat").val() + "&mlon=" + $("#marker_lon").val();
237 url += "&marker=" + $("#marker_lat").val() + "," + $("#marker_lon").val();
240 var html = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+url+'" style="border: 1px solid black"></iframe>';
242 // Create "larger map" link
243 var center = bounds.getCenterLonLat();
244 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
245 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
247 bounds.transform(epsg4326, epsg900913);
248 var zoom = map.getZoomForExtent(bounds);
250 var layers = getMapLayers();
252 html += '<br /><small><a href="http://<%= SERVER_URL %>/?lat='+center.lat+'&lon='+center.lon+'&zoom='+zoom+'&layers='+layers+markerUrl+'">'+"<%= html_escape_unicode(I18n.t('export.start_rjs.view_larger_map')) %>"+'</a></small>';
254 $("#export_html_text").val(html);
256 if ($("#format_html").prop("checked")) {
257 $("#export_html_text").prop("selected", true);
261 function formatChanged() {
262 $("#export_commit").show();
264 if ($("#format_osm").prop("checked")) {
265 $("#export_osm").show();
267 $("#export_osm").hide();
270 if ($("#format_mapnik").prop("checked")) {
271 $("#mapnik_scale").val(roundScale(map.getScale()));
272 $("#export_mapnik").show();
274 $("#export_mapnik").hide();
277 if ($("#format_html").prop("checked")) {
278 $("#export_html").show();
279 $("#export_commit").hide();
280 $("#export_html_text").prop("selected", true);
282 $("#export_html").hide();
290 function maxMapnikScale() {
291 var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(), $("#maxlon").val(), $("#maxlat").val());
292 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
293 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
295 bounds.transform(epsg4326, epsg900913);
297 return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
300 function mapnikImageSize(scale) {
301 var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(), $("#maxlon").val(), $("#maxlat").val());
302 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
303 var epsg900913 = new OpenLayers.Projection("EPSG:900913");
305 bounds.transform(epsg4326, epsg900913);
307 return new OpenLayers.Size(Math.round(bounds.getWidth() / scale / 0.00028),
308 Math.round(bounds.getHeight() / scale / 0.00028));
311 function roundScale(scale) {
312 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
314 return precision * Math.ceil(scale / precision);
317 function mapnikSizeChanged() {
318 var size = mapnikImageSize($("#mapnik_scale").val());
320 $("#mapnik_image_width").html(size.w);
321 $("#mapnik_image_height").html(size.h);