1 function startExport(sidebarHtml) {
7 epsg4326 = new OpenLayers.Projection("EPSG:4326"),
8 epsg900913 = new OpenLayers.Projection("EPSG:900913");
10 vectors = new OpenLayers.Layer.Vector("Vector Layer", {
11 displayInLayerSwitcher: false
13 map.addLayer(vectors);
15 box = new OpenLayers.Control.DrawFeature(vectors, OpenLayers.Handler.RegularPolygon, {
23 box.handler.callbacks.done = endDrag;
26 transform = new OpenLayers.Control.TransformFeature(vectors, {
30 transform.events.register("transformcomplete", transform, transformComplete);
31 map.addControl(transform);
33 map.events.register("moveend", map, mapMoved);
34 map.events.register("changebaselayer", map, htmlUrlChanged);
36 $("#sidebar_title").html(I18n.t('export.start_rjs.export'));
37 $("#sidebar_content").html(sidebarHtml);
39 $("#maxlat,#minlon,#maxlon,#minlat").change(boundsChanged);
41 $("#drag_box").click(startDrag);
43 $("#add_marker").click(startMarker);
45 $("#format_osm,#format_mapnik,#format_html").click(formatChanged);
47 $("#mapnik_scale").change(mapnikSizeChanged);
51 if (map.baseLayer.name == "Mapnik") {
52 $("#format_mapnik").prop("checked", true);
56 setBounds(map.getExtent());
58 $("body").removeClass("site-index").addClass("site-export");
60 $("#sidebar").one("closed", function () {
61 $("body").removeClass("site-export").addClass("site-index");
65 map.events.unregister("moveend", map, mapMoved);
66 map.events.unregister("changebaselayer", map, htmlUrlChanged);
67 map.removeLayer(vectors);
70 function getMercatorBounds() {
71 var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(),
72 $("#maxlon").val(), $("#maxlat").val());
74 return bounds.transform(epsg4326, epsg900913);
77 function boundsChanged() {
78 var bounds = getMercatorBounds();
80 map.events.unregister("moveend", map, mapMoved);
81 map.zoomToExtent(bounds);
90 function startDrag() {
91 $("#drag_box").html(I18n.t('export.start_rjs.drag_a_box'));
97 function endDrag(bbox) {
98 var bounds = bbox.getBounds();
100 map.events.unregister("moveend", map, mapMoved);
106 $("#drag_box").html(I18n.t('export.start_rjs.manually_select'));
109 function transformComplete(event) {
110 setBounds(event.feature.geometry.bounds);
114 function startMarker() {
115 $("#add_marker").html(I18n.t('export.start_rjs.click_add_marker'));
118 markerLayer = new OpenLayers.Layer.Vector("",{
119 displayInLayerSwitcher: false,
121 externalGraphic: OpenLayers.Util.getImageLocation("marker.png"),
122 graphicXOffset: -10.5,
128 map.addLayer(markerLayer);
130 markerControl = new OpenLayers.Control.DrawFeature(markerLayer, OpenLayers.Handler.Point);
131 map.addControl(markerControl);
133 markerLayer.events.on({ "featureadded": endMarker });
136 markerLayer.destroyFeatures();
137 markerControl.activate();
142 function endMarker(event) {
143 markerControl.deactivate();
145 $("#add_marker").html(I18n.t('export.start_rjs.change_marker'));
146 $("#marker_inputs").show();
148 var geom = event.feature.geometry.clone().transform(epsg900913, epsg4326);
150 $("#marker_lon").val(geom.x.toFixed(5));
151 $("#marker_lat").val(geom.y.toFixed(5));
156 function clearMarker() {
157 $("#marker_lon,#marker_lat").val("");
158 $("#marker_inputs").hide();
159 $("#add_marker").html(I18n.t('export.start_rjs.add_marker'));
162 markerControl.destroy();
163 markerLayer.destroy();
165 markerControl = null;
169 function mapMoved() {
170 setBounds(map.getExtent());
174 function setBounds(bounds) {
175 var toPrecision = zoomPrecision(map.getZoom());
177 bounds = bounds.clone().transform(map.getProjectionObject(), epsg4326);
179 $("#minlon").val(toPrecision(bounds.left));
180 $("#minlat").val(toPrecision(bounds.bottom));
181 $("#maxlon").val(toPrecision(bounds.right));
182 $("#maxlat").val(toPrecision(bounds.top));
188 function clearBox() {
189 transform.deactivate();
190 vectors.destroyFeatures();
193 function drawBox(bounds) {
194 var feature = new OpenLayers.Feature.Vector(bounds.toGeometry());
196 vectors.addFeatures(feature);
197 transform.setFeature(feature);
200 function validateControls() {
201 var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(), $("#maxlon").val(), $("#maxlat").val());
203 if (bounds.getWidth() * bounds.getHeight() > OSM.MAX_REQUEST_AREA) {
204 $("#export_osm_too_large").show();
206 $("#export_osm_too_large").hide();
209 var max_scale = maxMapnikScale();
212 if ($("#format_osm").prop("checked")) {
213 disabled = bounds.getWidth() * bounds.getHeight() > OSM.MAX_REQUEST_AREA;
214 } else if ($("#format_mapnik").prop("checked")) {
215 disabled = $("#mapnik_scale").val() < max_scale;
218 $("#export_commit").prop("disabled", disabled);
219 $("#mapnik_max_scale").html(roundScale(max_scale));
222 function htmlUrlChanged() {
223 var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(), $("#maxlon").val(), $("#maxlat").val());
224 var layerName = map.baseLayer.keyid;
225 var url = "http://" + OSM.SERVER_URL + "/export/embed.html?bbox=" + bounds.toBBOX() + "&layer=" + layerName;
228 if ($("#marker_lat").val() && $("#marker_lon").val()) {
229 markerUrl = "&mlat=" + $("#marker_lat").val() + "&mlon=" + $("#marker_lon").val();
230 url += "&marker=" + $("#marker_lat").val() + "," + $("#marker_lon").val();
233 var html = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+url+'" style="border: 1px solid black"></iframe>';
235 // Create "larger map" link
236 var center = bounds.getCenterLonLat();
238 bounds.transform(epsg4326, epsg900913);
239 var zoom = map.getZoomForExtent(bounds);
241 var layers = getMapLayers();
243 var text = I18n.t('export.start_rjs.view_larger_map');
246 for (var i = 0; i < text.length; ++i) {
247 var c = text.charCodeAt(i);
248 escaped.push(c < 127 ? text.charAt(i) : "&#" + c + ";");
251 html += '<br /><small><a href="http://' + OSM.SERVER_URL + '/?lat='+center.lat+'&lon='+center.lon+'&zoom='+zoom+'&layers='+layers+markerUrl+'">'+escaped.join("")+'</a></small>';
253 $("#export_html_text").val(html);
255 if ($("#format_html").prop("checked")) {
256 $("#export_html_text").prop("selected", true);
260 function formatChanged() {
261 $("#export_commit").show();
263 if ($("#format_osm").prop("checked")) {
264 $("#export_osm").show();
266 $("#export_osm").hide();
269 if ($("#format_mapnik").prop("checked")) {
270 $("#mapnik_scale").val(roundScale(map.getScale()));
271 $("#export_mapnik").show();
275 $("#export_mapnik").hide();
278 if ($("#format_html").prop("checked")) {
279 $("#export_html").show();
280 $("#export_commit").hide();
281 $("#export_html_text").prop("selected", true);
283 $("#export_html").hide();
291 function maxMapnikScale() {
292 var bounds = getMercatorBounds();
294 return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
297 function mapnikImageSize(scale) {
298 var bounds = getMercatorBounds();
300 return new OpenLayers.Size(Math.round(bounds.getWidth() / scale / 0.00028),
301 Math.round(bounds.getHeight() / scale / 0.00028));
304 function roundScale(scale) {
305 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
307 return precision * Math.ceil(scale / precision);
310 function mapnikSizeChanged() {
311 var size = mapnikImageSize($("#mapnik_scale").val());
313 $("#mapnik_image_width").html(size.w);
314 $("#mapnik_image_height").html(size.h);