]> git.openstreetmap.org Git - rails.git/blob - app/views/export/_start.rhtml
7359c39beedf5c227d31b84c188d1cc7e07c57ff
[rails.git] / app / views / export / _start.rhtml
1 <% form_tag :action => "finish" do %>
2
3   <p class="export_heading">Area to Export</p>
4
5   <div class="export_bounds">
6     <%= text_field_tag('maxlat', nil, :size => 10, :class => "export_bound") %>
7     <br/>
8     <%= text_field_tag('minlon', nil, :size => 10, :class => "export_bound") %>
9     <%= text_field_tag('maxlon', nil, :size => 10, :class => "export_bound") %>
10     <br/>
11     <%= text_field_tag('minlat', nil, :size => 10, :class => "export_bound") %>
12     <p class="export_hint">
13       Drag a box with control held down to select an area to export
14     </p>
15   </div>
16
17   <p class="export_heading">Format to Export</p>
18
19   <div class="export_details">
20     <p>
21       <%= radio_button_tag("format", "osm") %>OpenStreetMap XML Data
22       <br/>
23       <%= radio_button_tag("format", "mapnik") %>Mapnik Image
24       <br/>
25       <%= radio_button_tag("format", "osmarender") %>Osmarender Image
26     </p>
27   </div>
28
29   <div id="export_osm">
30     <p class="export_heading">Licence</p>
31
32     <div class="export_details">
33       <p>OpenStreetMap data is licensed under the <a href="http://creativecommons.org/licenses/by-sa/2.0/">Creative Commons Attribution-ShareAlike 2.0 license</a>.</p>
34     </div>
35   </div>
36
37   <div id="export_mapnik">
38     <p class="export_heading">Options</p>
39
40     <div class="export_details">
41       <p>Format <%= select_tag("mapnik_format", options_for_select([["PNG", "png"], ["JPEG", "jpeg"], ["SVG", "svg"], ["PDF", "pdf"], ["Postscript", "ps"]], "png")) %></p>
42       <p>Scale 1 : <%= text_field_tag("mapnik_scale", nil, :size => 8) %> <span class="export_hint">(max 1 : <span id="max_scale"></span>)</span></p>
43     </div>
44   </div>
45
46   <div id="export_osmarender">
47     <p class="export_heading">Options</p>
48
49     <div class="export_details">
50       <p>Format <%= select_tag("osmarender_format", options_for_select([["PNG", "png"], ["JPEG", "jpeg"]], "png")) %></p>
51       <p>Zoom <%= select_tag("osmarender_zoom", options_for_select([4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])) %></p>
52     </div>
53   </div>
54
55   <div class="export_buttons">
56     <p><%= submit_tag "Export", :id => "export_commit" %></p>
57   </div>
58
59 <% end %>
60
61 <script type="text/javascript">
62   <!--
63   var vectors;
64   var box;
65
66   function startExport() {
67     vectors = new OpenLayers.Layer.Vector("Vector Layer", {
68       displayInLayerSwitcher: false,
69     });
70     map.addLayer(vectors);
71
72     box = new OpenLayers.Control.DrawFeature(vectors, OpenLayers.Handler.RegularPolygon, { 
73       handlerOptions: {
74         keyMask: OpenLayers.Handler.MOD_CTRL,
75         sides: 4,
76         snapAngle: 90,
77         irregular: true,
78         persist: true,
79         callbacks: { done: boxComplete }
80       }
81     });
82     map.addControl(box);
83
84     box.activate();
85
86     map.events.register("moveend", map, mapMoved);
87
88     openSidebar({ onclose: stopExport });
89
90     updateRegion(map.getExtent());
91
92     if (map.baseLayer.name == "Mapnik") {
93       $("format_mapnik").checked = true;
94     } else if (map.baseLayer.name == "Osmarender") {
95       $("format_osmarender").checked = true;
96     }
97
98     formatChanged();
99
100     $("viewanchor").className = "";
101     $("exportanchor").className = "active";
102   }
103
104   function stopExport() {
105     $("viewanchor").className = "active";
106     $("exportanchor").className = "";
107
108     map.events.unregister("moveend", map, mapMoved);
109     box.handler.clear();
110     map.removeLayer(vectors);
111   }
112
113   function formatChanged() {
114     if ($("format_osm").checked) {
115       $("export_osm").style.display = "inline";
116     } else {
117       $("export_osm").style.display = "none";
118     }
119
120     if ($("format_mapnik").checked) {
121       $("mapnik_scale").value = roundScale(map.getScale());
122       $("export_mapnik").style.display = "inline";
123     } else {
124       $("export_mapnik").style.display = "none";
125     }
126
127     if ($("format_osmarender").checked) {
128       var zoom = Math.min(map.getZoom(), maxOsmarenderZoom());
129
130       $("osmarender_zoom").options.selectedIndex = zoom - 4;
131       $("export_osmarender").style.display = "inline";
132     } else {
133       $("export_osmarender").style.display = "none";
134     }
135   }
136
137   $("format_osm").onclick = function() { formatChanged() };
138   $("format_mapnik").onclick = function() { formatChanged() };
139   $("format_osmarender").onclick = function() { formatChanged() };
140
141   function boundsChanged() {
142     var epsg4326 = new OpenLayers.Projection("EPSG:4326");
143     var bounds = new OpenLayers.Bounds($("minlon").value,
144                                        $("minlat").value,
145                                        $("maxlon").value,
146                                        $("maxlat").value);
147  
148     bounds.transform(epsg4326, map.getProjectionObject());
149
150     map.events.unregister("moveend", map, mapMoved);
151     map.zoomToExtent(bounds);
152
153     box.handler.clear();
154     box.handler.feature = new OpenLayers.Feature.Vector(bounds.toGeometry());
155     box.handler.layer.addFeatures([box.handler.feature], [box.handler.style]);
156     box.handler.layer.drawFeature(box.handler.feature, box.handler.style);
157   }
158
159   $("maxlat").onchange = function() { boundsChanged() };
160   $("minlon").onchange = function() { boundsChanged() };
161   $("maxlon").onchange = function() { boundsChanged() };
162   $("minlat").onchange = function() { boundsChanged() };
163
164   function mapMoved() {
165     updateRegion(map.getExtent());
166   }
167
168   function boxComplete(box) {
169     map.events.unregister("moveend", map, mapMoved);
170     updateRegion(box.getBounds());
171   }
172
173   function updateRegion(bounds) {
174     var epsg4326 = new OpenLayers.Projection("EPSG:4326");
175     var decimals = Math.pow(10, Math.floor(map.getZoom() / 3));
176
177     bounds.transform(map.getProjectionObject(), epsg4326);
178
179     $("minlon").value = Math.round(bounds.left * decimals) / decimals;
180     $("minlat").value = Math.round(bounds.bottom * decimals) / decimals;
181     $("maxlon").value = Math.round(bounds.right * decimals) / decimals;
182     $("maxlat").value = Math.round(bounds.top * decimals) / decimals;
183
184     if (bounds.getWidth() * bounds.getHeight() > 0.25) {
185       $("format_osm").disabled = true;
186       $("format_osm").checked = false;
187
188       formatChanged();
189     } else {
190       $("format_osm").disabled = false;
191     }
192     
193     var max_zoom = maxOsmarenderZoom();
194     var max_scale = maxMapnikScale();
195
196     $("max_scale").innerHTML = roundScale(max_scale);
197
198     for (var o = 0; o < $("osmarender_zoom").options.length; o++) {
199       var option = $("osmarender_zoom").options[o];
200
201       if (option.value > max_zoom) {
202         option.disabled = true;
203       } else {
204         option.disabled = false;
205       }
206     }
207
208     if ($("osmarender_zoom").options.selectedIndex + 4 > max_zoom) {
209       $("osmarender_zoom").options.selectedIndex = max_zoom - 4;
210     }
211   }
212
213   function maxMapnikScale() {
214     var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
215     var epsg4326 = new OpenLayers.Projection("EPSG:4326");
216     var epsg900913 = new OpenLayers.Projection("EPSG:900913");
217
218     bounds.transform(epsg4326, epsg900913);
219
220     return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
221   }
222
223   function maxOsmarenderZoom() {
224     var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
225     var xzoom = Math.LOG2E * Math.log(2000 * 1.40625 / bounds.getWidth());
226     var ymin = bounds.bottom * Math.PI / 180;
227     var ymax = bounds.top * Math.PI / 180;
228     var yzoom = Math.LOG2E * (Math.log(2000 * 2 * Math.PI) - Math.log(Math.log((Math.tan(ymax) + 1 / Math.cos(ymax)) / (Math.tan(ymin) + 1 / Math.cos(ymin)))))
229
230     return Math.floor(Math.min(xzoom, yzoom));
231   }
232
233   function roundScale(scale) {
234     var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
235
236     return precision * Math.ceil(scale / precision);
237   }
238
239   function validateScale() {
240     if ($("mapnik_scale").value < maxMapnikScale()) {
241       $("export_commit").disabled = true;
242     } else {
243       $("export_commit").disabled = false;
244     }
245   }
246
247   $("mapnik_scale").onchange = function() { validateScale() };
248
249   startExport();
250   // -->
251 </script>