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);
49 openSidebar({ onclose: stopExport });
51 if (map.baseLayer.name == "Mapnik") {
52 $("#format_mapnik").prop("checked", true);
56 setBounds(map.getExtent());
58 $("#viewanchor").removeClass("active");
59 $("#exportanchor").addClass("active");
61 function stopExport() {
62 $("#viewanchor").addClass("active");
63 $("#exportanchor").removeClass("active");
67 map.events.unregister("moveend", map, mapMoved);
68 map.events.unregister("changebaselayer", map, htmlUrlChanged);
69 map.removeLayer(vectors);
72 function getMercatorBounds() {
73 var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(),
74 $("#maxlon").val(), $("#maxlat").val());
76 return bounds.transform(epsg4326, epsg900913);
79 function boundsChanged() {
80 var bounds = getMercatorBounds();
82 map.events.unregister("moveend", map, mapMoved);
83 map.zoomToExtent(bounds);
92 function startDrag() {
93 $("#drag_box").html(I18n.t('export.start_rjs.drag_a_box'));
99 function endDrag(bbox) {
100 var bounds = bbox.getBounds();
102 map.events.unregister("moveend", map, 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'));
120 markerLayer = new OpenLayers.Layer.Vector("",{
121 displayInLayerSwitcher: false,
123 externalGraphic: OpenLayers.Util.getImageLocation("marker.png"),
124 graphicXOffset: -10.5,
130 map.addLayer(markerLayer);
132 markerControl = new OpenLayers.Control.DrawFeature(markerLayer, OpenLayers.Handler.Point);
133 map.addControl(markerControl);
135 markerLayer.events.on({ "featureadded": endMarker });
138 markerLayer.destroyFeatures();
139 markerControl.activate();
144 function endMarker(event) {
145 markerControl.deactivate();
147 $("#add_marker").html(I18n.t('export.start_rjs.change_marker'));
148 $("#marker_inputs").show();
150 var geom = event.feature.geometry.clone().transform(epsg900913, epsg4326);
152 $("#marker_lon").val(geom.x.toFixed(5));
153 $("#marker_lat").val(geom.y.toFixed(5));
158 function clearMarker() {
159 $("#marker_lon,#marker_lat").val("");
160 $("#marker_inputs").hide();
161 $("#add_marker").html(I18n.t('export.start_rjs.add_marker'));
164 markerControl.destroy();
165 markerLayer.destroy();
167 markerControl = null;
171 function mapMoved() {
172 setBounds(map.getExtent());
176 function setBounds(bounds) {
177 var toPrecision = zoomPrecision(map.getZoom());
179 bounds = bounds.clone().transform(map.getProjectionObject(), epsg4326);
181 $("#minlon").val(toPrecision(bounds.left));
182 $("#minlat").val(toPrecision(bounds.bottom));
183 $("#maxlon").val(toPrecision(bounds.right));
184 $("#maxlat").val(toPrecision(bounds.top));
190 function clearBox() {
191 transform.deactivate();
192 vectors.destroyFeatures();
195 function drawBox(bounds) {
196 var feature = new OpenLayers.Feature.Vector(bounds.toGeometry());
198 vectors.addFeatures(feature);
199 transform.setFeature(feature);
202 function validateControls() {
203 var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(), $("#maxlon").val(), $("#maxlat").val());
205 if (bounds.getWidth() * bounds.getHeight() > OSM.MAX_REQUEST_AREA) {
206 $("#export_osm_too_large").show();
208 $("#export_osm_too_large").hide();
211 var max_scale = maxMapnikScale();
214 if ($("#format_osm").prop("checked")) {
215 disabled = bounds.getWidth() * bounds.getHeight() > OSM.MAX_REQUEST_AREA;
216 } else if ($("#format_mapnik").prop("checked")) {
217 disabled = $("#mapnik_scale").val() < max_scale;
220 $("#export_commit").prop("disabled", disabled);
221 $("#mapnik_max_scale").html(roundScale(max_scale));
224 function htmlUrlChanged() {
225 var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(), $("#maxlon").val(), $("#maxlat").val());
226 var layerName = map.baseLayer.keyid;
227 var url = "http://" + OSM.SERVER_URL + "/export/embed.html?bbox=" + bounds.toBBOX() + "&layer=" + layerName;
230 if ($("#marker_lat").val() && $("#marker_lon").val()) {
231 markerUrl = "&mlat=" + $("#marker_lat").val() + "&mlon=" + $("#marker_lon").val();
232 url += "&marker=" + $("#marker_lat").val() + "," + $("#marker_lon").val();
235 var html = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+url+'" style="border: 1px solid black"></iframe>';
237 // Create "larger map" link
238 var center = bounds.getCenterLonLat();
240 bounds.transform(epsg4326, epsg900913);
241 var zoom = map.getZoomForExtent(bounds);
243 var layers = getMapLayers();
245 var text = I18n.t('export.start_rjs.view_larger_map');
248 for (var i = 0; i < text.length; ++i) {
249 var c = text.charCodeAt(i);
250 escaped.push(c < 127 ? text.charAt(i) : "&#" + c + ";");
253 html += '<br /><small><a href="http://' + OSM.SERVER_URL + '/?lat='+center.lat+'&lon='+center.lon+'&zoom='+zoom+'&layers='+layers+markerUrl+'">'+escaped.join("")+'</a></small>';
255 $("#export_html_text").val(html);
257 if ($("#format_html").prop("checked")) {
258 $("#export_html_text").prop("selected", true);
262 function formatChanged() {
263 $("#export_commit").show();
265 if ($("#format_osm").prop("checked")) {
266 $("#export_osm").show();
268 $("#export_osm").hide();
271 if ($("#format_mapnik").prop("checked")) {
272 $("#mapnik_scale").val(roundScale(map.getScale()));
273 $("#export_mapnik").show();
277 $("#export_mapnik").hide();
280 if ($("#format_html").prop("checked")) {
281 $("#export_html").show();
282 $("#export_commit").hide();
283 $("#export_html_text").prop("selected", true);
285 $("#export_html").hide();
293 function maxMapnikScale() {
294 var bounds = getMercatorBounds();
296 return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
299 function mapnikImageSize(scale) {
300 var bounds = getMercatorBounds();
302 return new OpenLayers.Size(Math.round(bounds.getWidth() / scale / 0.00028),
303 Math.round(bounds.getHeight() / scale / 0.00028));
306 function roundScale(scale) {
307 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
309 return precision * Math.ceil(scale / precision);
312 function mapnikSizeChanged() {
313 var size = mapnikImageSize($("#mapnik_scale").val());
315 $("#mapnik_image_width").html(size.w);
316 $("#mapnik_image_height").html(size.h);