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) {
14 var marker, rectangle;
16 var drawHandler = new L.Rectangle.Draw(map, {title: I18n.t('export.start_rjs.drag_a_box')});
17 map.on('draw:rectangle-created', endDrag);
19 map.on("moveend", mapMoved);
20 map.on("baselayerchange", htmlUrlChanged);
22 $("#sidebar_title").html(I18n.t('export.start_rjs.export'));
23 $("#sidebar_content").html(sidebarHtml);
25 $("#maxlat,#minlon,#maxlon,#minlat").change(boundsChanged);
27 $("#drag_box").click(startDrag);
29 $("#add_marker").click(startMarker);
31 $("#format_osm,#format_mapnik,#format_html").click(formatChanged);
33 $("#mapnik_scale").change(mapnikSizeChanged);
37 if (map.hasLayer(layers[0].layer)) {
38 $("#format_mapnik").prop("checked", true);
41 setBounds(map.getBounds());
44 $("body").removeClass("site-index").addClass("site-export");
46 $("#sidebar").one("closed", function () {
47 $("body").removeClass("site-export").addClass("site-index");
52 map.off("moveend", mapMoved);
53 map.off("baselayerchange", htmlUrlChanged);
54 map.off('draw:rectangle-created', endDrag);
56 drawHandler.disable();
59 function getBounds() {
60 return L.latLngBounds(L.latLng($("#minlat").val(), $("#minlon").val()),
61 L.latLng($("#maxlat").val(), $("#maxlon").val()));
65 var bounds = map.getBounds(),
66 centerLat = bounds.getCenter().lat,
67 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
68 meters = halfWorldMeters * (bounds.getNorthEast().lng - bounds.getSouthWest().lng) / 180,
69 pixelsPerMeter = map.getSize().x / meters,
70 metersPerPixel = 1 / (92 * 39.3701);
71 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
74 function getMercatorBounds() {
75 var bounds = getBounds();
76 return L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
77 L.CRS.EPSG3857.project(bounds.getNorthEast()));
80 function boundsChanged() {
81 var bounds = getBounds();
83 map.off("moveend", mapMoved);
84 map.fitBounds(bounds);
93 function startDrag() {
94 $("#drag_box").html(I18n.t('export.start_rjs.drag_a_box'));
100 function endDrag(e) {
101 var bounds = e.rect.getBounds();
103 map.off("moveend", mapMoved);
108 $("#drag_box").html(I18n.t('export.start_rjs.manually_select'));
111 function transformComplete(event) {
112 setBounds(event.feature.geometry.bounds);
116 function startMarker() {
117 $("#add_marker").html(I18n.t('export.start_rjs.click_add_marker'));
119 map.on("click", endMarker);
124 function endMarker(event) {
125 map.off("click", endMarker);
127 $("#add_marker").html(I18n.t('export.start_rjs.change_marker'));
128 $("#marker_inputs").show();
130 var latlng = event.latlng;
133 map.removeLayer(marker);
136 marker = L.marker(latlng).addTo(map);
138 $("#marker_lon").val(latlng.lng.toFixed(5));
139 $("#marker_lat").val(latlng.lat.toFixed(5));
144 function clearMarker() {
145 $("#marker_lon,#marker_lat").val("");
146 $("#marker_inputs").hide();
147 $("#add_marker").html(I18n.t('export.start_rjs.add_marker'));
150 map.removeLayer(marker);
154 function mapMoved() {
155 setBounds(map.getBounds());
159 function setBounds(bounds) {
160 var toPrecision = zoomPrecision(map.getZoom());
162 $("#minlon").val(toPrecision(bounds.getWestLng()));
163 $("#minlat").val(toPrecision(bounds.getSouthLat()));
164 $("#maxlon").val(toPrecision(bounds.getEastLng()));
165 $("#maxlat").val(toPrecision(bounds.getNorthLat()));
171 function clearBox() {
173 map.removeLayer(rectangle);
178 function drawBox(bounds) {
179 rectangle = L.rectangle(bounds);
180 rectangle.addTo(map);
183 function validateControls() {
184 var bounds = getBounds();
186 var tooLarge = bounds.getSize() > OSM.MAX_REQUEST_AREA;
188 $("#export_osm_too_large").show();
190 $("#export_osm_too_large").hide();
193 var max_scale = maxMapnikScale();
196 if ($("#format_osm").prop("checked")) {
198 } else if ($("#format_mapnik").prop("checked")) {
199 disabled = $("#mapnik_scale").val() < max_scale;
202 $("#export_commit").prop("disabled", disabled);
203 $("#mapnik_max_scale").html(roundScale(max_scale));
206 function htmlUrlChanged() {
207 var bounds = getBounds();
208 var layerName = getMapBaseLayer().keyid;
210 var url = "http://" + OSM.SERVER_URL + "/export/embed.html?bbox=" + bounds.toBBOX() + "&layer=" + layerName;
213 if ($("#marker_lat").val() && $("#marker_lon").val()) {
214 markerUrl = "&mlat=" + $("#marker_lat").val() + "&mlon=" + $("#marker_lon").val();
215 url += "&marker=" + $("#marker_lat").val() + "," + $("#marker_lon").val();
218 var html = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+url+'" style="border: 1px solid black"></iframe>';
220 // Create "larger map" link
221 var center = bounds.getCenter();
223 var zoom = map.getBoundsZoom(bounds);
225 var layers = getMapLayers();
227 var text = I18n.t('export.start_rjs.view_larger_map');
230 for (var i = 0; i < text.length; ++i) {
231 var c = text.charCodeAt(i);
232 escaped.push(c < 127 ? text.charAt(i) : "&#" + c + ";");
235 html += '<br /><small><a href="http://' + OSM.SERVER_URL + '/?lat='+center.lat+'&lon='+center.lng+'&zoom='+zoom+'&layers='+layers+markerUrl+'">'+escaped.join("")+'</a></small>';
237 $("#export_html_text").val(html);
239 if ($("#format_html").prop("checked")) {
240 $("#export_html_text").prop("selected", true);
244 function formatChanged() {
245 $("#export_commit").show();
247 if ($("#format_osm").prop("checked")) {
248 $("#export_osm").show();
250 $("#export_osm").hide();
253 if ($("#format_mapnik").prop("checked")) {
254 $("#mapnik_scale").val(getScale());
255 $("#export_mapnik").show();
259 $("#export_mapnik").hide();
262 if ($("#format_html").prop("checked")) {
263 $("#export_html").show();
264 $("#export_commit").hide();
265 $("#export_html_text").prop("selected", true);
267 $("#export_html").hide();
275 function maxMapnikScale() {
276 var bounds = getMercatorBounds();
278 return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
281 function mapnikImageSize(scale) {
282 var bounds = getMercatorBounds();
284 return {w: Math.round(bounds.getWidth() / scale / 0.00028),
285 h: Math.round(bounds.getHeight() / scale / 0.00028)};
288 function roundScale(scale) {
289 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
291 return precision * Math.ceil(scale / precision);
294 function mapnikSizeChanged() {
295 var size = mapnikImageSize($("#mapnik_scale").val());
297 $("#mapnik_image_width").html(size.w);
298 $("#mapnik_image_height").html(size.h);