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) {
16 var locationFilter = new L.LocationFilter({
21 locationFilter.on("change", filterChanged);
23 map.on("moveend", mapMoved);
24 map.on("baselayerchange", htmlUrlChanged);
26 $("#sidebar_title").html(I18n.t('export.start_rjs.export'));
27 $("#sidebar_content").html(sidebarHtml);
29 $("#maxlat,#minlon,#maxlon,#minlat").change(boundsChanged);
31 $("#drag_box").click(enableFilter);
33 $("#add_marker").click(startMarker);
35 $("#format_osm,#format_mapnik,#format_html").click(formatChanged);
37 $("#mapnik_scale").change(mapnikSizeChanged);
41 if (map.hasLayer(layers[0].layer)) {
42 $("#format_mapnik").prop("checked", true);
45 setBounds(map.getBounds());
48 $("body").removeClass("site-index").addClass("site-export");
50 $("#sidebar").one("closed", function () {
51 $("body").removeClass("site-export").addClass("site-index");
53 map.removeLayer(locationFilter);
56 map.off("moveend", mapMoved);
57 map.off("baselayerchange", htmlUrlChanged);
58 locationFilter.off("change", filterChanged);
61 function getBounds() {
62 return L.latLngBounds(L.latLng($("#minlat").val(), $("#minlon").val()),
63 L.latLng($("#maxlat").val(), $("#maxlon").val()));
67 var bounds = map.getBounds(),
68 centerLat = bounds.getCenter().lat,
69 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
70 meters = halfWorldMeters * (bounds.getNorthEast().lng - bounds.getSouthWest().lng) / 180,
71 pixelsPerMeter = map.getSize().x / meters,
72 metersPerPixel = 1 / (92 * 39.3701);
73 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
76 function getMercatorBounds() {
77 var bounds = getBounds();
78 return L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
79 L.CRS.EPSG3857.project(bounds.getNorthEast()));
82 function boundsChanged() {
83 var bounds = getBounds();
85 map.fitBounds(bounds);
88 locationFilter.setBounds(bounds);
94 function enableFilter() {
95 $("#drag_box").hide();
96 locationFilter.enable();
99 function filterChanged() {
100 setBounds(locationFilter.getBounds());
104 function startMarker() {
105 $("#add_marker").html(I18n.t('export.start_rjs.click_add_marker'));
107 map.on("click", endMarker);
112 function endMarker(event) {
113 map.off("click", endMarker);
115 $("#add_marker").html(I18n.t('export.start_rjs.change_marker'));
116 $("#marker_inputs").show();
118 var latlng = event.latlng;
121 map.removeLayer(marker);
124 marker = L.marker(latlng).addTo(map);
126 $("#marker_lon").val(latlng.lng.toFixed(5));
127 $("#marker_lat").val(latlng.lat.toFixed(5));
132 function clearMarker() {
133 $("#marker_lon,#marker_lat").val("");
134 $("#marker_inputs").hide();
135 $("#add_marker").html(I18n.t('export.start_rjs.add_marker'));
138 map.removeLayer(marker);
142 function mapMoved() {
143 if (!locationFilter.isEnabled()) {
144 setBounds(map.getBounds());
149 function setBounds(bounds) {
150 var toPrecision = zoomPrecision(map.getZoom());
152 $("#minlon").val(toPrecision(bounds.getWestLng()));
153 $("#minlat").val(toPrecision(bounds.getSouthLat()));
154 $("#maxlon").val(toPrecision(bounds.getEastLng()));
155 $("#maxlat").val(toPrecision(bounds.getNorthLat()));
161 function validateControls() {
162 var bounds = getBounds();
164 var tooLarge = bounds.getSize() > OSM.MAX_REQUEST_AREA;
166 $("#export_osm_too_large").show();
168 $("#export_osm_too_large").hide();
171 var max_scale = maxMapnikScale();
174 if ($("#format_osm").prop("checked")) {
176 } else if ($("#format_mapnik").prop("checked")) {
177 disabled = $("#mapnik_scale").val() < max_scale;
180 $("#export_commit").prop("disabled", disabled);
181 $("#mapnik_max_scale").html(roundScale(max_scale));
184 function htmlUrlChanged() {
185 var bounds = getBounds();
186 var layerName = getMapBaseLayer().keyid;
188 var url = "http://" + OSM.SERVER_URL + "/export/embed.html?bbox=" + bounds.toBBOX() + "&layer=" + layerName;
191 if ($("#marker_lat").val() && $("#marker_lon").val()) {
192 markerUrl = "&mlat=" + $("#marker_lat").val() + "&mlon=" + $("#marker_lon").val();
193 url += "&marker=" + $("#marker_lat").val() + "," + $("#marker_lon").val();
196 var html = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+url+'" style="border: 1px solid black"></iframe>';
198 // Create "larger map" link
199 var center = bounds.getCenter();
201 var zoom = map.getBoundsZoom(bounds);
203 var layers = getMapLayers();
205 var text = I18n.t('export.start_rjs.view_larger_map');
208 for (var i = 0; i < text.length; ++i) {
209 var c = text.charCodeAt(i);
210 escaped.push(c < 127 ? text.charAt(i) : "&#" + c + ";");
213 html += '<br /><small><a href="http://' + OSM.SERVER_URL + '/?lat='+center.lat+'&lon='+center.lng+'&zoom='+zoom+'&layers='+layers+markerUrl+'">'+escaped.join("")+'</a></small>';
215 $("#export_html_text").val(html);
217 if ($("#format_html").prop("checked")) {
218 $("#export_html_text").prop("selected", true);
222 function formatChanged() {
223 $("#export_commit").show();
225 if ($("#format_osm").prop("checked")) {
226 $("#export_osm").show();
228 $("#export_osm").hide();
231 if ($("#format_mapnik").prop("checked")) {
232 $("#mapnik_scale").val(getScale());
233 $("#export_mapnik").show();
237 $("#export_mapnik").hide();
240 if ($("#format_html").prop("checked")) {
241 $("#export_html").show();
242 $("#export_commit").hide();
243 $("#export_html_text").prop("selected", true);
245 $("#export_html").hide();
253 function maxMapnikScale() {
254 var bounds = getMercatorBounds();
256 return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
259 function mapnikImageSize(scale) {
260 var bounds = getMercatorBounds();
262 return {w: Math.round(bounds.getWidth() / scale / 0.00028),
263 h: Math.round(bounds.getHeight() / scale / 0.00028)};
266 function roundScale(scale) {
267 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
269 return precision * Math.ceil(scale / precision);
272 function mapnikSizeChanged() {
273 var size = mapnikImageSize($("#mapnik_scale").val());
275 $("#mapnik_image_width").html(size.w);
276 $("#mapnik_image_height").html(size.h);