]> git.openstreetmap.org Git - rails.git/commitdiff
Merged 7296:7388 from rails_port trunk.
authorTom Hughes <tom@compton.nu>
Wed, 16 Apr 2008 23:52:32 +0000 (23:52 +0000)
committerTom Hughes <tom@compton.nu>
Wed, 16 Apr 2008 23:52:32 +0000 (23:52 +0000)
app/controllers/api_controller.rb
app/controllers/export_controller.rb [new file with mode: 0644]
app/views/export/_start.rhtml [new file with mode: 0644]
app/views/layouts/site.rhtml
app/views/site/_key.rhtml
app/views/site/_sidebar.rhtml
config/routes.rb
lib/bounding_box.rb [new file with mode: 0644]
public/javascripts/map.js
public/javascripts/site.js
public/stylesheets/site.css

index 3350e733cbb41a7cece2e62ba074c42e42efc42f..3ef5e753aef135fc6f7fbe0ddfc162bcbc6803d3 100644 (file)
@@ -79,8 +79,9 @@ class ApiController < ApplicationController
       exit!
     end
 
-    render :text => doc.to_s, :content_type => "text/xml"
+    response.headers("Content-Disposition") = "attachment; filename=\"map.osm\""
 
+    render :text => doc.to_s, :content_type => "text/xml"
   end
 
   def map
diff --git a/app/controllers/export_controller.rb b/app/controllers/export_controller.rb
new file mode 100644 (file)
index 0000000..b1d992e
--- /dev/null
@@ -0,0 +1,29 @@
+class ExportController < ApplicationController
+  def start
+    render :update do |page|
+      page.replace_html :sidebar_title, 'Export'
+      page.replace_html :sidebar_content, :partial => 'start'
+    end
+  end
+
+  def finish
+    bbox = BoundingBox.new(params[:minlon], params[:minlat], params[:maxlon], params[:maxlat])
+    format = params[:format]
+
+    if format == "osm"
+      redirect_to "http://api.openstreetmap.org/api/#{API_VERSION}/map?bbox=#{bbox}"
+    elsif format == "mapnik"
+      format = params[:mapnik_format]
+      scale = params[:mapnik_scale]
+
+      redirect_to "http://tile.openstreetmap.org/cgi-bin/export?bbox=#{bbox}&scale=#{scale}&format=#{format}"
+    elsif format == "osmarender"
+      format = params[:osmarender_format]
+      zoom = params[:osmarender_zoom].to_i
+      width = bbox.slippy_width(zoom).to_i
+      height = bbox.slippy_height(zoom).to_i
+
+      redirect_to "http://tah.openstreetmap.org/MapOf/index.php?long=#{bbox.centre_lon}&lat=#{bbox.centre_lat}&z=#{zoom}&w=#{width}&h=#{height}&format=#{format}"
+    end
+  end
+end
diff --git a/app/views/export/_start.rhtml b/app/views/export/_start.rhtml
new file mode 100644 (file)
index 0000000..11fc618
--- /dev/null
@@ -0,0 +1,243 @@
+<% form_tag :action => "finish" do %>
+
+  <p class="export_heading">Area to Export</p>
+
+  <div class="export_bounds">
+    <%= text_field_tag('maxlat', nil, :size => 10, :class => "export_bound") %>
+    <br/>
+    <%= text_field_tag('minlon', nil, :size => 10, :class => "export_bound") %>
+    <%= text_field_tag('maxlon', nil, :size => 10, :class => "export_bound") %>
+    <br/>
+    <%= text_field_tag('minlat', nil, :size => 10, :class => "export_bound") %>
+    <p class="export_hint">
+      Drag a box with control held down to select an area to export
+    </p>
+  </div>
+
+  <p class="export_heading">Format to Export</p>
+
+  <div class="export_details">
+    <p>
+      <%= radio_button_tag("format", "osm") %>OpenStreetMap XML Data
+      <br/>
+      <%= radio_button_tag("format", "mapnik") %>Mapnik Image
+      <br/>
+      <%= radio_button_tag("format", "osmarender") %>Osmarender Image
+    </p>
+  </div>
+
+  <div id="export_osm">
+    <p class="export_heading">Licence</p>
+
+    <div class="export_details">
+      <p>OSM license agreement blah blah blah...</p>
+    </div
+  </div>
+
+  <div id="export_mapnik">
+    <p class="export_heading">Options</p>
+
+    <div class="export_details">
+      <p>Format <%= select_tag("mapnik_format", options_for_select([["PNG", "png"], ["JPEG", "jpeg"], ["SVG", "svg"], ["PDF", "pdf"], ["Postscript", "ps"]], "png")) %></p>
+      <p>Scale 1 : <%= text_field_tag("mapnik_scale", nil, :size => 8) %> <span class="export_hint">(max 1 : <span id="max_scale"></span>)</span></p>
+    </div>
+  </div>
+
+  <div id="export_osmarender">
+    <p class="export_heading">Options</p>
+
+    <div class="export_details">
+      <p>Format <%= select_tag("osmarender_format", options_for_select([["PNG", "png"], ["JPEG", "jpeg"]], "png")) %></p>
+      <p>Zoom <%= select_tag("osmarender_zoom", options_for_select([4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])) %></p>
+    </div>
+  </div>
+
+  <div class="export_buttons">
+    <p><%= submit_tag "Export", :id => "export_commit" %></p>
+  </div>
+
+<% end %>
+
+<script type="text/javascript">
+  <!--
+  var vectors;
+  var box;
+
+  function startExport() {
+    vectors = new OpenLayers.Layer.Vector("Vector Layer", {
+      displayInLayerSwitcher: false,
+    });
+    map.addLayer(vectors);
+
+    box = new OpenLayers.Control.DrawFeature(vectors, OpenLayers.Handler.RegularPolygon, { 
+      handlerOptions: {
+        keyMask: OpenLayers.Handler.MOD_CTRL,
+        sides: 4,
+        snapAngle: 90,
+        irregular: true,
+        persist: true,
+        callbacks: { done: boxComplete }
+      }
+    });
+    map.addControl(box);
+
+    box.activate();
+
+    map.events.register("moveend", map, mapMoved);
+
+    $("viewanchor").className = "";
+    $("exportanchor").className = "active";
+
+    openSidebar({ onclose: stopExport });
+
+    updateRegion(map.getExtent());
+  }
+
+  function stopExport() {
+    $("viewanchor").className = "active";
+    $("exportanchor").className = "";
+
+    map.events.unregister("moveend", map, mapMoved);
+    box.handler.clear();
+    map.removeLayer(vectors);
+  }
+
+  function formatChanged() {
+    if ($("format_osm").checked) {
+      $("export_osm").style.display = "inline";
+    } else {
+      $("export_osm").style.display = "none";
+    }
+
+    if ($("format_mapnik").checked) {
+      $("mapnik_scale").value = roundScale(map.getScale());
+      $("export_mapnik").style.display = "inline";
+    } else {
+      $("export_mapnik").style.display = "none";
+    }
+
+    if ($("format_osmarender").checked) {
+      var zoom = Math.min(map.getZoom(), maxOsmarenderZoom());
+
+      $("osmarender_zoom").options.selectedIndex = zoom - 4;
+      $("export_osmarender").style.display = "inline";
+    } else {
+      $("export_osmarender").style.display = "none";
+    }
+  }
+
+  $("format_osm").onclick = function() { formatChanged() };
+  $("format_mapnik").onclick = function() { formatChanged() };
+  $("format_osmarender").onclick = function() { formatChanged() };
+
+  function boundsChanged() {
+    var epsg4326 = new OpenLayers.Projection("EPSG:4326");
+    var bounds = new OpenLayers.Bounds($("minlon").value,
+                                       $("minlat").value,
+                                       $("maxlon").value,
+                                       $("maxlat").value);
+    bounds.transform(epsg4326, map.getProjectionObject());
+
+    map.events.unregister("moveend", map, mapMoved);
+    map.zoomToExtent(bounds);
+
+    box.handler.clear();
+    box.handler.feature = new OpenLayers.Feature.Vector(bounds.toGeometry());
+    box.handler.layer.addFeatures([box.handler.feature], [box.handler.style]);
+    box.handler.layer.drawFeature(box.handler.feature, box.handler.style);
+  }
+
+  $("maxlat").onchange = function() { boundsChanged() };
+  $("minlon").onchange = function() { boundsChanged() };
+  $("maxlon").onchange = function() { boundsChanged() };
+  $("minlat").onchange = function() { boundsChanged() };
+
+  function mapMoved() {
+    updateRegion(map.getExtent());
+  }
+
+  function boxComplete(box) {
+    map.events.unregister("moveend", map, mapMoved);
+    updateRegion(box.getBounds());
+  }
+
+  function updateRegion(bounds) {
+    var epsg4326 = new OpenLayers.Projection("EPSG:4326");
+    var decimals = Math.pow(10, Math.floor(map.getZoom() / 3));
+
+    bounds.transform(map.getProjectionObject(), epsg4326);
+
+    $("minlon").value = Math.round(bounds.left * decimals) / decimals;
+    $("minlat").value = Math.round(bounds.bottom * decimals) / decimals;
+    $("maxlon").value = Math.round(bounds.right * decimals) / decimals;
+    $("maxlat").value = Math.round(bounds.top * decimals) / decimals;
+
+    if (bounds.getWidth() * bounds.getHeight() > 0.25) {
+      $("format_osm").disabled = true;
+      $("format_osm").checked = false;
+
+      formatChanged();
+    } else {
+      $("format_osm").disabled = false;
+    }
+    
+    var max_zoom = maxOsmarenderZoom();
+    var max_scale = maxMapnikScale();
+
+    $("max_scale").innerHTML = roundScale(max_scale);
+
+    for (var o = 0; o < $("osmarender_zoom").options.length; o++) {
+      var option = $("osmarender_zoom").options[o];
+
+      if (option.value > max_zoom) {
+        option.disabled = true;
+      } else {
+        option.disabled = false;
+      }
+    }
+
+    if ($("osmarender_zoom").options.selectedIndex + 4 > max_zoom) {
+      $("osmarender_zoom").options.selectedIndex = max_zoom - 4;
+    }
+  }
+
+  function maxMapnikScale() {
+    var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
+    var epsg4326 = new OpenLayers.Projection("EPSG:4326");
+    var epsg900913 = new OpenLayers.Projection("EPSG:900913");
+
+    bounds.transform(epsg4326, epsg900913);
+
+    return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
+  }
+
+  function maxOsmarenderZoom() {
+    var bounds = new OpenLayers.Bounds($("minlon").value, $("minlat").value, $("maxlon").value, $("maxlat").value);
+    var xzoom = Math.LOG2E * Math.log(2000 * 1.40625 / bounds.getWidth());
+    var ymin = bounds.bottom * Math.PI / 180;
+    var ymax = bounds.top * Math.PI / 180;
+    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)))))
+
+    return Math.floor(Math.min(xzoom, yzoom));
+  }
+
+  function roundScale(scale) {
+    var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
+
+    return precision * Math.ceil(scale / precision);
+  }
+
+  function validateScale() {
+    if ($("mapnik_scale").value < maxMapnikScale()) {
+      $("export_commit").disabled = true;
+    } else {
+      $("export_commit").disabled = false;
+    }
+  }
+
+  $("mapnik_scale").onchange = function() { validateScale() };
+
+  startExport();
+  // -->
+</script>
index bcb479ccacf7fdd600557e6f1317c1b478ea9389..c5ecfbd7c21398f0185a7ec2a4d92065a5f648e8 100644 (file)
         <%
         viewclass = ''
         editclass = ''
+        exportclass = ''
         traceclass = ''
         viewclass = 'active' if params['controller'] == 'site' and params['action'] == 'index' 
         editclass = 'active' if params['controller'] == 'site' and params['action'] == 'edit' 
-        editclass = 'active' if params['controller'] == 'campaign'
+        exportclass = 'active' if params['controller'] == 'site' and params['action'] == 'export'
         traceclass = 'active' if params['controller'] == 'trace'
         %>
-        <li><%= link_to 'View', {:controller => 'site', :action => 'index'}, {:id => 'viewanchor', :title => 'view maps', :class => viewclass  } %></li>
-        <li><%= link_to 'Edit', {:controller => 'site', :action => 'edit'}, {:id => 'editanchor', :title => 'edit maps', :class => editclass } %></li>
-        <li><%= link_to 'GPS traces', {:controller => 'trace', :action => 'list'}, {:id => 'traceanchor', :title => 'manage traces', :class => traceclass } %></li>
+        <li><%= link_to 'View', {:controller => 'site', :action => 'index'}, {:id => 'viewanchor', :title => 'view maps', :class => viewclass} %></li>
+        <li><%= link_to 'Edit', {:controller => 'site', :action => 'edit'}, {:id => 'editanchor', :title => 'edit maps', :class => editclass} %></li>
+        <li><%= link_to_remote 'Export', {:url => {:controller => 'export', :action => 'start'}}, {:id => 'exportanchor', :title => 'export map data', :class => exportclass, :href => url_for(:controller => 'site', :action => 'export')} %></li>
+        <li><%= link_to 'GPS traces', {:controller => 'trace', :action => 'list'}, {:id => 'traceanchor', :title => 'manage traces', :class => traceclass} %></li>
       </ul>
     </div>
 
index c6b6fe29a5912448f020c3a07dcb63cf5de60a7f..64309949815eadc0f80922876e3f8d304d800bfb 100644 (file)
@@ -12,7 +12,7 @@
     else                   { var imgname = 'keymapnik13.png'; }
 
     updateSidebar("Map key", "<p><img src='images/"+imgname+"' /></p>");
-    openSidebar("210px");
+    openSidebar({ width: "210px" });
   }
 
   function updateKey() {
index 7addbe177a19fe40d100ce7f259480a187fe484f..d813635905971a9c86a69b8c2b69aed017bca886 100644 (file)
 
 <script type="text/javascript">
 <!--
-  function openSidebar() {
-    if (arguments.length) { $("sidebar").style.width = arguments[0]; }
+  var onclose;
+
+  function openSidebar(options) {
+    if (onclose) {
+       onclose();
+       onclose = null;
+    }
+
+    if (options.width) { $("sidebar").style.width = options.width; }
     else { $("sidebar").style.width = "30%"; }
 
     $("sidebar").style.display = "block";
 
     <%= onopen %>
+
+    onclose = options.onclose;
   }
 
   function closeSidebar() {
     $("sidebar").style.display = "none";
 
     <%= onclose %>
+
+    if (onclose) {
+       onclose();
+       onclose = null;
+    }
   }
 
   function updateSidebar(title, content) {
index 1d3a135655c052e0274dbd077fabc1653d79c9b7..51914241193deb696fd2e979a65a5e5b5fe3f28d 100644 (file)
@@ -64,6 +64,7 @@ ActionController::Routing::Routes.draw do |map|
   map.connect '/user/upload_image', :controller => 'user', :action => 'upload_image'
   map.connect '/index.html', :controller => 'site', :action => 'index'
   map.connect '/edit.html', :controller => 'site', :action => 'edit'
+  map.connect '/export.html', :controller => 'site', :action => 'export'
   map.connect '/search.html', :controller => 'way_tag', :action => 'search'
   map.connect '/login.html', :controller => 'user', :action => 'login'
   map.connect '/logout.html', :controller => 'user', :action => 'logout'
@@ -121,6 +122,10 @@ ActionController::Routing::Routes.draw do |map|
   map.connect '/geocoder/search', :controller => 'geocoder', :action => 'search'
   map.connect '/geocoder/description', :controller => 'geocoder', :action => 'description'
 
+  # export
+  map.connect '/export/start', :controller => 'export', :action => 'start'
+  map.connect '/export/finish', :controller => 'export', :action => 'finish'
+
   # messages
 
   map.connect '/user/:display_name/inbox', :controller => 'message', :action => 'inbox'
@@ -131,6 +136,6 @@ ActionController::Routing::Routes.draw do |map|
   map.connect '/message/reply/:message_id', :controller => 'message', :action => 'reply'
 
   # fall through
-     map.connect ':controller/:id/:action'
+  map.connect ':controller/:id/:action'
   map.connect ':controller/:action'
 end
diff --git a/lib/bounding_box.rb b/lib/bounding_box.rb
new file mode 100644 (file)
index 0000000..d943768
--- /dev/null
@@ -0,0 +1,72 @@
+class BoundingBox
+  def initialize(min_lon, min_lat, max_lon, max_lat)
+    @bbox = [min_lon.to_f, min_lat.to_f, max_lon.to_f, max_lat.to_f]
+  end
+
+  def self.from_s(s)
+    BoundingBox.new(s.split(/,/))
+  end
+
+  def min_lon
+    @bbox[0]
+  end
+
+  def min_lon=(min_lon)
+    @bbox[0] = min_lon
+  end
+
+  def min_lat
+    @bbox[1]
+  end
+
+  def min_lat=(min_lat)
+    @bbox[1] = min_lat
+  end
+
+  def max_lon
+    @bbox[2]
+  end
+
+  def max_lon=(max_lon)
+    @bbox[2] = max_lon
+  end
+
+  def max_lat
+    @bbox[3]
+  end
+
+  def max_lat=(max_lat)
+    @bbox[3] = max_lat
+  end
+
+  def centre_lon
+    (@bbox[0] + @bbox[2]) / 2.0
+  end
+
+  def centre_lat
+    (@bbox[1] + @bbox[3]) / 2.0
+  end
+
+  def width
+    @bbox[2] - @bbox[0]
+  end
+
+  def height
+    @bbox[3] - @bbox[1]
+  end
+
+  def slippy_width(zoom)
+    width * 256.0 * 2.0 ** zoom / 360.0
+  end
+
+  def slippy_height(zoom)
+    min = min_lat * Math::PI / 180.0
+    max = max_lat * Math::PI / 180.0
+
+    Math.log((Math.tan(max) + 1.0 / Math.cos(max)) / (Math.tan(min) + 1.0 / Math.cos(min))) * 128.0 * 2.0 ** zoom / Math::PI
+  end
+
+  def to_s
+    return @bbox.join(",")
+  end
+end
index d8765fe1a93fcb6507427427468c429c116b133a..51cd9f6a5abfbc62abcf4c382825e59dad1e4e9b 100644 (file)
@@ -17,6 +17,7 @@ function createMap(divName) {
          new OpenLayers.Control.PanZoomBar(),
          new OpenLayers.Control.ScaleLine()
       ],
+      maxResolution: 156543,
       units: "m"
    });
 
@@ -130,12 +131,15 @@ function getMapLayers() {
 function setMapLayers(layers) {
    for (var i=0; i < layers.length; i++) {
       var layer = map.layers[i];
-      var c = layers.charAt(i);
 
-      if (c == "B") {
-         map.setBaseLayer(layer);
-      } else if ( (c == "T") || (c == "F") ) {
-         layer.setVisibility(c == "T");
+      if (layer) {
+         var c = layers.charAt(i);
+
+         if (c == "B") {
+            map.setBaseLayer(layer);
+         } else if ( (c == "T") || (c == "F") ) {
+            layer.setVisibility(c == "T");
+         }
       }
    }
 }
index 6fb68e65a1630c0bd208f4efda0e25edbc0ff4cc..a7651fa4568acbda9d341cfc25e38f1f9ba26dc4 100644 (file)
@@ -29,6 +29,18 @@ function updatelinks(lon,lat,zoom,layers) {
     node.href = setArgs(node.href, args);
   }
 
+  node = document.getElementById("exportanchor");
+  if (node) {
+    var args = getArgs(node.href);
+    args["lat"] = lat;
+    args["lon"] = lon;
+    args["zoom"] = zoom;
+    if (layers) {
+      args["layers"] = layers;
+    }
+    node.href = setArgs(node.href, args);
+  }
+
   node = document.getElementById("editanchor");
   if (node) {
     if (zoom >= 11) {
index f2306e7b4a37fbfa9032d4189a3661f85122792a..dee3e85049ce6e592f4bf95c89188ef4f55136af 100644 (file)
@@ -580,3 +580,45 @@ input {
 #attribution {
   display: none;
 }
+
+.export_heading {
+  margin: 0px;
+  padding: 3px 6px 3px 6px;
+  border: 1px solid #ccc;
+  background: #ddd;
+}
+
+.export_bounds {
+  width: 100%;
+  text-align: center;
+}
+
+.export_bound {
+  margin: 5px;
+}
+
+.export_details {
+  padding: 2px 6px 2px 6px;
+}
+
+#export_osm {
+  display: none;
+}
+
+#export_mapnik {
+  display: none;
+}
+
+#export_osmarender {
+  display: none;
+}
+
+.export_hint {
+  padding: 0px 12px 0px 12px;
+  font-style: italic;
+}
+
+.export_buttons {
+  width: 100%;
+  text-align: center;
+}