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({
19 onChange: filterChanged
22 map.on("moveend", mapMoved);
23 map.on("baselayerchange", htmlUrlChanged);
25 $("#sidebar_title").html(I18n.t('export.start_rjs.export'));
26 $("#sidebar_content").html(sidebarHtml);
28 $("#maxlat,#minlon,#maxlon,#minlat").change(boundsChanged);
30 $("#drag_box").click(enableFilter);
32 $("#add_marker").click(startMarker);
34 $("#format_osm,#format_mapnik,#format_html").click(formatChanged);
36 $("#mapnik_scale").change(mapnikSizeChanged);
40 if (map.hasLayer(layers[0].layer)) {
41 $("#format_mapnik").prop("checked", true);
44 setBounds(map.getBounds());
47 $("body").removeClass("site-index").addClass("site-export");
49 $("#sidebar").one("closed", function () {
50 $("body").removeClass("site-export").addClass("site-index");
52 map.removeLayer(locationFilter);
55 map.off("moveend", mapMoved);
56 map.off("baselayerchange", htmlUrlChanged);
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.fitBounds(bounds);
86 locationFilter.setBounds(bounds);
92 function enableFilter() {
93 $("#drag_box").hide();
94 locationFilter.enable();
97 function filterChanged() {
98 setBounds(locationFilter.getBounds());
102 function startMarker() {
103 $("#add_marker").html(I18n.t('export.start_rjs.click_add_marker'));
105 map.on("click", endMarker);
110 function endMarker(event) {
111 map.off("click", endMarker);
113 $("#add_marker").html(I18n.t('export.start_rjs.change_marker'));
114 $("#marker_inputs").show();
116 var latlng = event.latlng;
119 map.removeLayer(marker);
122 marker = L.marker(latlng).addTo(map);
124 $("#marker_lon").val(latlng.lng.toFixed(5));
125 $("#marker_lat").val(latlng.lat.toFixed(5));
130 function clearMarker() {
131 $("#marker_lon,#marker_lat").val("");
132 $("#marker_inputs").hide();
133 $("#add_marker").html(I18n.t('export.start_rjs.add_marker'));
136 map.removeLayer(marker);
140 function mapMoved() {
141 if (!locationFilter.isEnabled()) {
142 setBounds(map.getBounds());
147 function setBounds(bounds) {
148 var toPrecision = zoomPrecision(map.getZoom());
150 $("#minlon").val(toPrecision(bounds.getWestLng()));
151 $("#minlat").val(toPrecision(bounds.getSouthLat()));
152 $("#maxlon").val(toPrecision(bounds.getEastLng()));
153 $("#maxlat").val(toPrecision(bounds.getNorthLat()));
159 function validateControls() {
160 var bounds = getBounds();
162 var tooLarge = bounds.getSize() > OSM.MAX_REQUEST_AREA;
164 $("#export_osm_too_large").show();
166 $("#export_osm_too_large").hide();
169 var max_scale = maxMapnikScale();
172 if ($("#format_osm").prop("checked")) {
174 } else if ($("#format_mapnik").prop("checked")) {
175 disabled = $("#mapnik_scale").val() < max_scale;
178 $("#export_commit").prop("disabled", disabled);
179 $("#mapnik_max_scale").html(roundScale(max_scale));
182 function htmlUrlChanged() {
183 var bounds = getBounds();
184 var layerName = getMapBaseLayer().keyid;
186 var url = "http://" + OSM.SERVER_URL + "/export/embed.html?bbox=" + bounds.toBBOX() + "&layer=" + layerName;
189 if ($("#marker_lat").val() && $("#marker_lon").val()) {
190 markerUrl = "&mlat=" + $("#marker_lat").val() + "&mlon=" + $("#marker_lon").val();
191 url += "&marker=" + $("#marker_lat").val() + "," + $("#marker_lon").val();
194 var html = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+url+'" style="border: 1px solid black"></iframe>';
196 // Create "larger map" link
197 var center = bounds.getCenter();
199 var zoom = map.getBoundsZoom(bounds);
201 var layers = getMapLayers();
203 var text = I18n.t('export.start_rjs.view_larger_map');
206 for (var i = 0; i < text.length; ++i) {
207 var c = text.charCodeAt(i);
208 escaped.push(c < 127 ? text.charAt(i) : "&#" + c + ";");
211 html += '<br /><small><a href="http://' + OSM.SERVER_URL + '/?lat='+center.lat+'&lon='+center.lng+'&zoom='+zoom+'&layers='+layers+markerUrl+'">'+escaped.join("")+'</a></small>';
213 $("#export_html_text").val(html);
215 if ($("#format_html").prop("checked")) {
216 $("#export_html_text").prop("selected", true);
220 function formatChanged() {
221 $("#export_commit").show();
223 if ($("#format_osm").prop("checked")) {
224 $("#export_osm").show();
226 $("#export_osm").hide();
229 if ($("#format_mapnik").prop("checked")) {
230 $("#mapnik_scale").val(getScale());
231 $("#export_mapnik").show();
235 $("#export_mapnik").hide();
238 if ($("#format_html").prop("checked")) {
239 $("#export_html").show();
240 $("#export_commit").hide();
241 $("#export_html_text").prop("selected", true);
243 $("#export_html").hide();
251 function maxMapnikScale() {
252 var bounds = getMercatorBounds();
254 return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
257 function mapnikImageSize(scale) {
258 var bounds = getMercatorBounds();
260 return {w: Math.round(bounds.getWidth() / scale / 0.00028),
261 h: Math.round(bounds.getHeight() / scale / 0.00028)};
264 function roundScale(scale) {
265 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
267 return precision * Math.ceil(scale / precision);
270 function mapnikSizeChanged() {
271 var size = mapnikImageSize($("#mapnik_scale").val());
273 $("#mapnik_image_width").html(size.w);
274 $("#mapnik_image_height").html(size.h);