aboutsummaryrefslogtreecommitdiff
path: root/plugin.rb
blob: a80a5c1719fa527916a079f992f8398a122af02c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# name: discourse-umap
# description: Onebox UMAP from OpenStreeMap.fr
# url: http://code.cepheide.org/discourse-umap.git
# version: 0.2.2
# authors: Marcin Rataj, hellekin

class Onebox::Engine::OpenStreetMapOnebox
  include Onebox::Engine

  # current embed defaults from openstreetmap.org
  @@width     = 425
  @@height    = 350
  @@tile_size = 256
  @@REGEX     = /^https?:\/\/umap\.openstreetmap\.fr\/fr\/map\/(.*)\#([\d\.]+)\/([-\d\.]+)\/([-\d\.]+)/

  # enable oneboxing permalinks (http://wiki.openstreetmap.org/wiki/Permalink) into iframes
  matches_regexp(@@REGEX)

  def to_html
    map_name, zoom, lat, lon = @url.match(@@REGEX).captures
#    iframe_url = "//umap.openstreetmap.fr/fr/map/#{map_name}\##{zoom.to_i}/#{lat.to_f}/#{lon.to_f}"
    iframe_url = "//umap.openstreetmap.fr/fr/map/#{map_name}?scaleControl=false&miniMap=false&scrollWheelZoom=false&zoomControl=true&editMode=disabled&moreControl=true&searchControl=null&tilelayersControl=null&embedControl=null&datalayersControl=true&onLoadPanel=databrowser&captionBar=false&captionMenus=true"

    "<iframe src='#{iframe_url}' style='border: 0' width='#{@@width}' height='#{@@height}' frameborder='0' scrolling='no'></iframe>"
  end

  private

  # http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Lon..2Flat._to_tile_numbers_3
  # note: it is slightly modified as fractional part is not discarded (http://goo.gl/oNtQ5g)
  def get_tile_number(lat_deg, lng_deg, zoom)
    lat_rad = lat_deg/180 * Math::PI
    n = 2.0 ** zoom
    x = (lng_deg + 180.0) / 360.0 * n
    y = (1.0 - Math::log(Math::tan(lat_rad) + (1 / Math::cos(lat_rad))) / Math::PI) / 2.0 * n
    if !y.infinite?.nil?
      y = x
    end

    [x, y]
  end

  # http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Tile_numbers_to_lon..2Flat._3
  def get_lat_lng_for_number(xtile, ytile, zoom)
    n = 2.0 ** zoom
    lon_deg = xtile / n * 360.0 - 180.0
    lat_rad = Math::atan(Math::sinh(Math::PI * (1 - 2 * ytile / n)))
    lat_deg = 180.0 * (lat_rad / Math::PI)

    [lat_deg, lon_deg]
  end

  # http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Lon..2Flat._to_bbox
  def get_bbox(lat, lon, zoom)

    xtile, ytile = get_tile_number(lat, lon, zoom)

    xtile_s = (xtile * @@tile_size - @@width/2.0)  / @@tile_size
    ytile_s = (ytile * @@tile_size - @@height/2.0) / @@tile_size
    xtile_e = (xtile * @@tile_size + @@width/2.0)  / @@tile_size
    ytile_e = (ytile * @@tile_size + @@height/2.0) / @@tile_size

    lat_s, lon_s = get_lat_lng_for_number(xtile_s, ytile_s, zoom)
    lat_e, lon_e = get_lat_lng_for_number(xtile_e, ytile_e, zoom)

    "#{lon_s}%2C#{lat_s}%2C#{lon_e}%2C#{lat_e}"
  end

end

# vim:ts=2:sw=2:et: