1 $(document).ready(function () {
2 $("#exportanchor").click(function (e) {
3 $.ajax({ url: $(this).data('url'), success: function (sidebarHtml) {
4 startExport(sidebarHtml);
9 if (window.location.pathname == "/export") {
10 $("#exportanchor").click();
13 function startExport(sidebarHtml) {
19 epsg4326 = new OpenLayers.Projection("EPSG:4326"),
20 epsg900913 = new OpenLayers.Projection("EPSG:900913");
22 vectors = new OpenLayers.Layer.Vector("Vector Layer", {
23 displayInLayerSwitcher: false
25 map.addLayer(vectors);
27 box = new OpenLayers.Control.DrawFeature(vectors, OpenLayers.Handler.RegularPolygon, {
35 box.handler.callbacks.done = endDrag;
38 transform = new OpenLayers.Control.TransformFeature(vectors, {
42 transform.events.register("transformcomplete", transform, transformComplete);
43 map.addControl(transform);
45 map.events.register("moveend", map, mapMoved);
46 map.events.register("changebaselayer", map, htmlUrlChanged);
48 $("#sidebar_title").html(I18n.t('export.start_rjs.export'));
49 $("#sidebar_content").html(sidebarHtml);
51 $("#maxlat,#minlon,#maxlon,#minlat").change(boundsChanged);
53 $("#drag_box").click(startDrag);
55 $("#add_marker").click(startMarker);
57 $("#format_osm,#format_mapnik,#format_html").click(formatChanged);
59 $("#mapnik_scale").change(mapnikSizeChanged);
63 if (map.baseLayer.name == "Mapnik") {
64 $("#format_mapnik").prop("checked", true);
68 setBounds(map.getExtent());
70 $("body").removeClass("site-index").addClass("site-export");
72 $("#sidebar").one("closed", function () {
73 $("body").removeClass("site-export").addClass("site-index");
77 map.events.unregister("moveend", map, mapMoved);
78 map.events.unregister("changebaselayer", map, htmlUrlChanged);
79 map.removeLayer(vectors);
82 function getMercatorBounds() {
83 var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(),
84 $("#maxlon").val(), $("#maxlat").val());
86 return bounds.transform(epsg4326, epsg900913);
89 function boundsChanged() {
90 var bounds = getMercatorBounds();
92 map.events.unregister("moveend", map, mapMoved);
93 map.zoomToExtent(bounds);
102 function startDrag() {
103 $("#drag_box").html(I18n.t('export.start_rjs.drag_a_box'));
109 function endDrag(bbox) {
110 var bounds = bbox.getBounds();
112 map.events.unregister("moveend", map, mapMoved);
118 $("#drag_box").html(I18n.t('export.start_rjs.manually_select'));
121 function transformComplete(event) {
122 setBounds(event.feature.geometry.bounds);
126 function startMarker() {
127 $("#add_marker").html(I18n.t('export.start_rjs.click_add_marker'));
130 markerLayer = new OpenLayers.Layer.Vector("",{
131 displayInLayerSwitcher: false,
133 externalGraphic: OpenLayers.Util.getImageLocation("marker.png"),
134 graphicXOffset: -10.5,
140 map.addLayer(markerLayer);
142 markerControl = new OpenLayers.Control.DrawFeature(markerLayer, OpenLayers.Handler.Point);
143 map.addControl(markerControl);
145 markerLayer.events.on({ "featureadded": endMarker });
148 markerLayer.destroyFeatures();
149 markerControl.activate();
154 function endMarker(event) {
155 markerControl.deactivate();
157 $("#add_marker").html(I18n.t('export.start_rjs.change_marker'));
158 $("#marker_inputs").show();
160 var geom = event.feature.geometry.clone().transform(epsg900913, epsg4326);
162 $("#marker_lon").val(geom.x.toFixed(5));
163 $("#marker_lat").val(geom.y.toFixed(5));
168 function clearMarker() {
169 $("#marker_lon,#marker_lat").val("");
170 $("#marker_inputs").hide();
171 $("#add_marker").html(I18n.t('export.start_rjs.add_marker'));
174 markerControl.destroy();
175 markerLayer.destroy();
177 markerControl = null;
181 function mapMoved() {
182 setBounds(map.getExtent());
186 function setBounds(bounds) {
187 var toPrecision = zoomPrecision(map.getZoom());
189 bounds = bounds.clone().transform(map.getProjectionObject(), epsg4326);
191 $("#minlon").val(toPrecision(bounds.left));
192 $("#minlat").val(toPrecision(bounds.bottom));
193 $("#maxlon").val(toPrecision(bounds.right));
194 $("#maxlat").val(toPrecision(bounds.top));
200 function clearBox() {
201 transform.deactivate();
202 vectors.destroyFeatures();
205 function drawBox(bounds) {
206 var feature = new OpenLayers.Feature.Vector(bounds.toGeometry());
208 vectors.addFeatures(feature);
209 transform.setFeature(feature);
212 function validateControls() {
213 var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(), $("#maxlon").val(), $("#maxlat").val());
215 if (bounds.getWidth() * bounds.getHeight() > OSM.MAX_REQUEST_AREA) {
216 $("#export_osm_too_large").show();
218 $("#export_osm_too_large").hide();
221 var max_scale = maxMapnikScale();
224 if ($("#format_osm").prop("checked")) {
225 disabled = bounds.getWidth() * bounds.getHeight() > OSM.MAX_REQUEST_AREA;
226 } else if ($("#format_mapnik").prop("checked")) {
227 disabled = $("#mapnik_scale").val() < max_scale;
230 $("#export_commit").prop("disabled", disabled);
231 $("#mapnik_max_scale").html(roundScale(max_scale));
234 function htmlUrlChanged() {
235 var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(), $("#maxlon").val(), $("#maxlat").val());
236 var layerName = map.baseLayer.keyid;
237 var url = "http://" + OSM.SERVER_URL + "/export/embed.html?bbox=" + bounds.toBBOX() + "&layer=" + layerName;
240 if ($("#marker_lat").val() && $("#marker_lon").val()) {
241 markerUrl = "&mlat=" + $("#marker_lat").val() + "&mlon=" + $("#marker_lon").val();
242 url += "&marker=" + $("#marker_lat").val() + "," + $("#marker_lon").val();
245 var html = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+url+'" style="border: 1px solid black"></iframe>';
247 // Create "larger map" link
248 var center = bounds.getCenterLonLat();
250 bounds.transform(epsg4326, epsg900913);
251 var zoom = map.getZoomForExtent(bounds);
253 var layers = getMapLayers();
255 var text = I18n.t('export.start_rjs.view_larger_map');
258 for (var i = 0; i < text.length; ++i) {
259 var c = text.charCodeAt(i);
260 escaped.push(c < 127 ? text.charAt(i) : "&#" + c + ";");
263 html += '<br /><small><a href="http://' + OSM.SERVER_URL + '/?lat='+center.lat+'&lon='+center.lon+'&zoom='+zoom+'&layers='+layers+markerUrl+'">'+escaped.join("")+'</a></small>';
265 $("#export_html_text").val(html);
267 if ($("#format_html").prop("checked")) {
268 $("#export_html_text").prop("selected", true);
272 function formatChanged() {
273 $("#export_commit").show();
275 if ($("#format_osm").prop("checked")) {
276 $("#export_osm").show();
278 $("#export_osm").hide();
281 if ($("#format_mapnik").prop("checked")) {
282 $("#mapnik_scale").val(roundScale(map.getScale()));
283 $("#export_mapnik").show();
287 $("#export_mapnik").hide();
290 if ($("#format_html").prop("checked")) {
291 $("#export_html").show();
292 $("#export_commit").hide();
293 $("#export_html_text").prop("selected", true);
295 $("#export_html").hide();
303 function maxMapnikScale() {
304 var bounds = getMercatorBounds();
306 return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
309 function mapnikImageSize(scale) {
310 var bounds = getMercatorBounds();
312 return new OpenLayers.Size(Math.round(bounds.getWidth() / scale / 0.00028),
313 Math.round(bounds.getHeight() / scale / 0.00028));
316 function roundScale(scale) {
317 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
319 return precision * Math.ceil(scale / precision);
322 function mapnikSizeChanged() {
323 var size = mapnikImageSize($("#mapnik_scale").val());
325 $("#mapnik_image_width").html(size.w);
326 $("#mapnik_image_height").html(size.h);