Class: Travlrmap::SinatraApp

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/travlrmap/sinatra_app.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ SinatraApp

Returns a new instance of SinatraApp.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/travlrmap/sinatra_app.rb', line 3

def initialize(config)
  @config = config
  @config[:sets] ||= {}
  @map = @config[:map]
  @types = @config[:types]
  @sets = @config[:sets] || {}
  @js_map_update = false

  raise "The constant APPROOT should be set in config.ru to the directory your webserver is serving from" unless defined?(APPROOT)
  raise "The directory %s set in APPROOT does not exist" % APPROOT unless File.directory?(APPROOT)

  @map[:map_types] = ["hybrid", "roadmap", "satellite", "terrain", "osm"] unless @map[:map_types]
  @map[:default_map_type] = "roadmap" unless @map[:default_map_type]

  super()
end

Instance Method Details

#kml_style_url(type) ⇒ Object



59
60
61
# File 'lib/travlrmap/sinatra_app.rb', line 59

def kml_style_url(type)
  "travlrmap-%s-style" % type
end

#load_map(set = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/travlrmap/sinatra_app.rb', line 63

def load_map(set=nil)
  @points = []

  if set
    files = Array(@config[:sets][set][:data])
  else
    files = Array(@config[:map][:data])
  end

  files.each do |map|
    if map.is_a?(Symbol)
      @points.concat(load_map(map))
    else
      point_file = File.join(File.join(APPROOT, "config", map))

      if File.directory?(point_file)
        Dir.entries(point_file).grep(/\.yaml$/).each do |points|
          file = File.join(point_file, points)
          data = YAML.load_file(file)
          @points.concat(data[:points])
        end
      else
        data = YAML.load_file(point_file)
        @points.concat(data[:points])
      end
    end
  end
end

#set_map_vars(view, set = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/travlrmap/sinatra_app.rb', line 92

def set_map_vars(view, set=nil)
  @map_view = @config[:views][view]
  @zoom_control = @map[:zoom_control].nil? ? true : @map[:zoom_control]
  @map_type_control = @map[:map_type_control].nil? ? true : @map[:map_type_control]
  @street_view_control = @map[:street_view_control].nil? ? false : @map[:street_view_control]
  @overview_control = @map[:overview_control].nil? ? false : @map[:overview_control]
  @pan_control = @map[:pan_control].nil? ? true : @map[:pan_control]
  @js_map_update = true
  @active_set = set
end

#to_jsonObject



103
104
105
106
107
108
109
# File 'lib/travlrmap/sinatra_app.rb', line 103

def to_json
  @points.sort_by{|p| p[:country]}.map do |point|
    point[:popup_html] = erb(:"_point_comment", :layout => false, :locals => {:point => point})
    point[:icon] = @types[ point[:type] ][:icon]
    point
  end.to_json
end

#to_kmlObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/travlrmap/sinatra_app.rb', line 111

def to_kml
  require 'ruby_kml'

  kml = KMLFile.new
  document = KML::Document.new(:name => "Travlrmap Data")
  folder = KML::Folder.new(:name => "Countries")
  folders = {}

  @types.each do |k, t|
    document.styles << KML::Style.new(
      :id         => kml_style_url(k),
      :icon_style => KML::IconStyle.new(:icon => KML::Icon.new(:href => t[:icon]))
    )
  end

  @points.sort_by{|p| p[:country]}.each do |point|
    unless folders[point[:country]]
      folder.features << folders[point[:country]] = KML::Folder.new(:name => point[:country])
    end

    f = folders[point[:country]]

    f.features << KML::Placemark.new(
      :name        => point[:title],
      :description => erb(:"_point_comment", :layout => false, :locals => {:point => point}),
      :geometry    => KML::Point.new(:coordinates => {:lat => point[:lat], :lng => point[:lon]}),
      :style_url   => "#%s" % kml_style_url(point[:type])
    )
  end

  document.features << folder
  kml.objects << document

  kml.render
end