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);
86 locationFilter.setBounds(bounds);
93 function enableFilter() {
94 if (!locationFilter.getBounds().isValid()) {
95 locationFilter.setBounds(map.getBounds().pad(-0.2));
98 $("#drag_box").hide();
99 locationFilter.enable();
102 function filterChanged() {
103 setBounds(locationFilter.getBounds());
107 function startMarker() {
108 $("#add_marker").html(I18n.t('export.start_rjs.click_add_marker'));
110 map.on("click", endMarker);
115 function endMarker(event) {
116 map.off("click", endMarker);
118 $("#add_marker").html(I18n.t('export.start_rjs.change_marker'));
119 $("#marker_inputs").show();
121 var latlng = event.latlng;
124 map.removeLayer(marker);
127 marker = L.marker(latlng).addTo(map);
129 $("#marker_lon").val(latlng.lng.toFixed(5));
130 $("#marker_lat").val(latlng.lat.toFixed(5));
135 function clearMarker() {
136 $("#marker_lon,#marker_lat").val("");
137 $("#marker_inputs").hide();
138 $("#add_marker").html(I18n.t('export.start_rjs.add_marker'));
141 map.removeLayer(marker);
145 function mapMoved() {
146 if (!locationFilter.isEnabled()) {
147 setBounds(map.getBounds());
152 function setBounds(bounds) {
153 var toPrecision = zoomPrecision(map.getZoom());
155 $("#minlon").val(toPrecision(bounds.getWestLng()));
156 $("#minlat").val(toPrecision(bounds.getSouthLat()));
157 $("#maxlon").val(toPrecision(bounds.getEastLng()));
158 $("#maxlat").val(toPrecision(bounds.getNorthLat()));
164 function validateControls() {
165 var bounds = getBounds();
167 var tooLarge = bounds.getSize() > OSM.MAX_REQUEST_AREA;
169 $("#export_osm_too_large").show();
171 $("#export_osm_too_large").hide();
174 var max_scale = maxMapnikScale();
177 if ($("#format_osm").prop("checked")) {
179 } else if ($("#format_mapnik").prop("checked")) {
180 disabled = $("#mapnik_scale").val() < max_scale;
183 $("#export_commit").prop("disabled", disabled);
184 $("#mapnik_max_scale").html(roundScale(max_scale));
187 function htmlUrlChanged() {
188 var bounds = getBounds();
189 var layerName = getMapBaseLayer().keyid;
191 var url = "http://" + OSM.SERVER_URL + "/export/embed.html?bbox=" + bounds.toBBOX() + "&layer=" + layerName;
194 if ($("#marker_lat").val() && $("#marker_lon").val()) {
195 markerUrl = "&mlat=" + $("#marker_lat").val() + "&mlon=" + $("#marker_lon").val();
196 url += "&marker=" + $("#marker_lat").val() + "," + $("#marker_lon").val();
199 var html = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+url+'" style="border: 1px solid black"></iframe>';
201 // Create "larger map" link
202 var center = bounds.getCenter();
204 var zoom = map.getBoundsZoom(bounds);
206 var layers = getMapLayers();
208 var text = I18n.t('export.start_rjs.view_larger_map');
211 for (var i = 0; i < text.length; ++i) {
212 var c = text.charCodeAt(i);
213 escaped.push(c < 127 ? text.charAt(i) : "&#" + c + ";");
216 html += '<br /><small><a href="http://' + OSM.SERVER_URL + '/?lat='+center.lat+'&lon='+center.lng+'&zoom='+zoom+'&layers='+layers+markerUrl+'">'+escaped.join("")+'</a></small>';
218 $("#export_html_text").val(html);
220 if ($("#format_html").prop("checked")) {
221 $("#export_html_text").prop("selected", true);
225 function formatChanged() {
226 $("#export_commit").show();
228 if ($("#format_osm").prop("checked")) {
229 $("#export_osm").show();
231 $("#export_osm").hide();
234 if ($("#format_mapnik").prop("checked")) {
235 $("#mapnik_scale").val(getScale());
236 $("#export_mapnik").show();
240 $("#export_mapnik").hide();
243 if ($("#format_html").prop("checked")) {
244 $("#export_html").show();
245 $("#export_commit").hide();
246 $("#export_html_text").prop("selected", true);
248 $("#export_html").hide();
256 function maxMapnikScale() {
257 var bounds = getMercatorBounds();
259 return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
262 function mapnikImageSize(scale) {
263 var bounds = getMercatorBounds();
265 return {w: Math.round(bounds.getWidth() / scale / 0.00028),
266 h: Math.round(bounds.getHeight() / scale / 0.00028)};
269 function roundScale(scale) {
270 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
272 return precision * Math.ceil(scale / precision);
275 function mapnikSizeChanged() {
276 var size = mapnikImageSize($("#mapnik_scale").val());
278 $("#mapnik_image_width").html(size.w);
279 $("#mapnik_image_height").html(size.h);