map_layers 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,66 @@
1
+ rere# GeoNames REST services
2
+ #
3
+ # http://www.geonames.org/export/web-services.html
4
+ #
5
+ module Geonames
6
+
7
+ module JsonFormat # :nodoc:
8
+ extend self
9
+
10
+ def extension
11
+ "json"
12
+ end
13
+
14
+ def mime_type
15
+ "application/json"
16
+ end
17
+
18
+ def encode(hash)
19
+ hash.to_json
20
+ end
21
+
22
+ def decode(json)
23
+ h = ActiveSupport::JSON.decode(json)
24
+ h.values.flatten # Return type must be an array of hashes
25
+ end
26
+ end
27
+
28
+ # GeoNames REST services base class
29
+ class GeonamesResource < ActiveResource::Base
30
+ self.site = "http://ws.geonames.org/"
31
+ self.format = JsonFormat
32
+ end
33
+
34
+ # GeoNames Postalode REST services
35
+ class Postalcode < GeonamesResource
36
+ # Postal code search
37
+ #
38
+ # http://www.geonames.org/export/web-services.html#postalCodeSearch
39
+ #
40
+ def self.search(placename, options = {:maxRows => 50})
41
+ self.find(:all, :from => "/postalCodeSearchJSON", :params => { :placename => placename }.merge(options))
42
+ end
43
+ end
44
+
45
+ # GeoNames Weather REST services
46
+ class Weather < GeonamesResource
47
+ # Weather stations with the most recent weather observation
48
+ #
49
+ # Example: Geonames::Weather.weather(:north => 44.1, :south => -9.9, :east => -22.4, :west => 55.2)
50
+ #
51
+ # http://www.geonames.org/export/JSON-webservices.html#weatherJSON
52
+ #
53
+ def self.weather(options)
54
+ self.find(:all, :from => "/weatherJSON", :params => options)
55
+ end
56
+
57
+ # Weather station and the most recent weather observation for the ICAO code
58
+ #
59
+ # http://www.geonames.org/export/JSON-webservices.html#weatherIcaoJSON
60
+ #
61
+ def self.weatherIcao(icao, options = {})
62
+ self.find(:all, :from => "/weatherIcaoJSON", :params => { :ICAO => icao }.merge(options))
63
+ end
64
+ end
65
+
66
+ end
@@ -0,0 +1,66 @@
1
+ # GeoNames REST services
2
+ #
3
+ # http://www.geonames.org/export/web-services.html
4
+ #
5
+ module Geonames
6
+
7
+ module JsonFormat # :nodoc:
8
+ extend self
9
+
10
+ def extension
11
+ "json"
12
+ end
13
+
14
+ def mime_type
15
+ "application/json"
16
+ end
17
+
18
+ def encode(hash)
19
+ hash.to_json
20
+ end
21
+
22
+ def decode(json)
23
+ h = ActiveSupport::JSON.decode(json)
24
+ h.values.flatten # Return type must be an array of hashes
25
+ end
26
+ end
27
+
28
+ # GeoNames REST services base class
29
+ class GeonamesResource < ActiveResource::Base
30
+ self.site = "http://ws.geonames.org/"
31
+ self.format = JsonFormat
32
+ end
33
+
34
+ # GeoNames Postalode REST services
35
+ class Postalcode < GeonamesResource
36
+ # Postal code search
37
+ #
38
+ # http://www.geonames.org/export/web-services.html#postalCodeSearch
39
+ #
40
+ def self.search(placename, options = {:maxRows => 50})
41
+ self.find(:all, :from => "/postalCodeSearchJSON", :params => { :placename => placename }.merge(options))
42
+ end
43
+ end
44
+
45
+ # GeoNames Weather REST services
46
+ class Weather < GeonamesResource
47
+ # Weather stations with the most recent weather observation
48
+ #
49
+ # Example: Geonames::Weather.weather(:north => 44.1, :south => -9.9, :east => -22.4, :west => 55.2)
50
+ #
51
+ # http://www.geonames.org/export/JSON-webservices.html#weatherJSON
52
+ #
53
+ def self.weather(options)
54
+ self.find(:all, :from => "/weatherJSON", :params => options)
55
+ end
56
+
57
+ # Weather station and the most recent weather observation for the ICAO code
58
+ #
59
+ # http://www.geonames.org/export/JSON-webservices.html#weatherIcaoJSON
60
+ #
61
+ def self.weatherIcao(icao, options = {})
62
+ self.find(:all, :from => "/weatherIcaoJSON", :params => { :ICAO => icao }.merge(options))
63
+ end
64
+ end
65
+
66
+ end
@@ -0,0 +1,178 @@
1
+ module MapLayers
2
+
3
+ #The module where all the Ruby-to-JavaScript conversion takes place.
4
+ #Based on Ym4r::GmPlugin::MappingObject from Guilhem Vellut
5
+ module JsWrapper
6
+ #The name of the variable in JavaScript space.
7
+ attr_reader :variable
8
+
9
+ #Creates javascript code for missing methods + takes care of listeners
10
+ def method_missing(name,*args)
11
+ str_name = name.to_s
12
+ args.collect! do |arg|
13
+ JsWrapper.javascriptify_variable(arg)
14
+ end
15
+ JsExpr.new("#{to_javascript}.#{JsWrapper.javascriptify_method(str_name)}(#{args.join(",")})")
16
+ end
17
+
18
+ # Creates javascript code for method calls
19
+ def javascriptify_method_call(name,*args)
20
+ args.collect! do |arg|
21
+ JsWrapper.javascriptify_variable(arg)
22
+ end
23
+ JsExpr.new("#{to_javascript}.#{JsWrapper.javascriptify_method(name.to_s)}(#{args.join(",")})")
24
+ end
25
+
26
+ #Creates javascript code for array or hash indexing
27
+ def [](index) #index could be an integer or string
28
+ return JsExpr.new("#{to_javascript}[#{JsWrapper.javascriptify_variable(index)}]")
29
+ end
30
+
31
+ #Transforms a Ruby object into a JavaScript string : JsWrapper, String, Array, Hash and general case (using to_s)
32
+ def self.javascriptify_variable(arg)
33
+ if arg.is_a?(JsWrapper)
34
+ arg.to_javascript
35
+ elsif arg.is_a?(String)
36
+ "\"#{JsWrapper.escape_javascript(arg)}\""
37
+ elsif arg.is_a?(Array)
38
+ "[" + arg.collect{ |a| JsWrapper.javascriptify_variable(a)}.join(",") + "]"
39
+ elsif arg.is_a?(Hash)
40
+ "{" + arg.to_a.collect do |v|
41
+ "#{JsWrapper.javascriptify_method(v[0].to_s)} : #{JsWrapper.javascriptify_variable(v[1])}"
42
+ end.join(",") + "}"
43
+ elsif arg.nil?
44
+ "undefined"
45
+ else
46
+ arg.to_s
47
+ end
48
+ end
49
+
50
+ #Escape string to be used in JavaScript. Lifted from rails.
51
+ def self.escape_javascript(javascript)
52
+ javascript.gsub(/\r\n|\n|\r/, "\\n").gsub("\"") { |m| "\\#{m}" }
53
+ end
54
+
55
+ #Transform a ruby-type method name (like add_overlay) to a JavaScript-style one (like addOverlay).
56
+ def self.javascriptify_method(method_name)
57
+ method_name.gsub(/_(\w)/){|s| $1.upcase}
58
+ end
59
+
60
+ #Declares a Mapping Object bound to a JavaScript variable of name +variable+.
61
+ def declare(variable)
62
+ @variable = variable
63
+ "var #{@variable} = #{create};"
64
+ end
65
+
66
+ #declare with a random variable name
67
+ def declare_random(init,size = 8)
68
+ s = init.clone
69
+ 6.times { s << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }
70
+ declare(s)
71
+ end
72
+
73
+ #Checks if the MappinObject has been declared
74
+ def declared?
75
+ !@variable.nil?
76
+ end
77
+
78
+ #Binds a Mapping object to a previously declared JavaScript variable of name +variable+.
79
+ def assign_to(variable)
80
+ @variable = variable
81
+ "#{@variable} = #{create};"
82
+ end
83
+
84
+ #Assign the +value+ to the +property+ of the JsWrapper
85
+ def set_property(property, value)
86
+ "#{to_javascript}.#{JsWrapper.javascriptify_method(property.to_s)} = #{JsWrapper.javascriptify_variable(value)}"
87
+ end
88
+
89
+ #Returns the code to get a +property+ from the JsWrapper
90
+ def get_property(property)
91
+ JsExpr.new("#{to_javascript}.#{JsWrapper.javascriptify_method(property.to_s)}")
92
+ end
93
+
94
+ #Returns a Javascript code representing the object
95
+ def to_javascript
96
+ unless @variable.nil?
97
+ @variable
98
+ else
99
+ create
100
+ end
101
+ end
102
+
103
+ #To cheat JavaScriptGenerator::GeneratorMethods::javascript_object_for
104
+ def to_json(options = {})
105
+ to_javascript
106
+ end
107
+
108
+ #Creates a Mapping Object in JavaScript.
109
+ #To be implemented by subclasses if needed
110
+ def create
111
+ end
112
+ end
113
+
114
+ #A valid JavaScript expression that has a value.
115
+ class JsExpr
116
+ include JsWrapper
117
+
118
+ def initialize(expr)
119
+ @variable = expr
120
+ end
121
+ #Returns the javascript expression contained in the object.
122
+ def create
123
+ @variable
124
+ end
125
+ def to_s
126
+ @variable
127
+ end
128
+
129
+ UNDEFINED = JsExpr.new("undefined")
130
+ end
131
+
132
+
133
+ #Used to bind a ruby variable to an already existing JavaScript one.
134
+ class JsVar < JsExpr
135
+ end
136
+
137
+
138
+ #Minimal Wrapper around a Javascript class
139
+ class JsClass
140
+ include JsWrapper
141
+
142
+ def self.const_missing(sym)
143
+ if self.const_defined?(sym)
144
+ k = self.const_get(sym)
145
+ else
146
+ k = self.const_set(sym, Class.new(JsClass))
147
+ end
148
+ end
149
+
150
+ def initialize(*args)
151
+ @args = args
152
+ end
153
+
154
+ def create
155
+ jsclass = self.class.to_s.split(/::/)[1..-1]
156
+ jsclass.insert(0, 'OpenLayers') unless jsclass[0] == 'OpenLayers'
157
+ args = @args.collect{ |arg| JsWrapper.javascriptify_variable(arg) }
158
+ JsExpr.new("new #{jsclass.join('.')}(#{args.join(',')})")
159
+ end
160
+
161
+ end
162
+
163
+ class JsGenerator # :nodoc:
164
+ def initialize()
165
+ @lines = ''
166
+ end
167
+ def <<(javascript)
168
+ @lines << (javascript.is_a?(JsWrapper) ? javascript.to_javascript : javascript) << ";\n"
169
+ end
170
+ def assign(variable, value)
171
+ @lines << "#{variable} = #{JsWrapper::javascriptify_variable(value)};\n"
172
+ end
173
+ def to_s
174
+ @lines
175
+ end
176
+ end
177
+
178
+ end
@@ -0,0 +1,89 @@
1
+ module MapLayers
2
+
3
+ GOOGLE = OpenLayers::Layer::Google.new("Google Street")
4
+ GOOGLE_SATELLITE = OpenLayers::Layer::Google.new("Google Satelite", {:type => :G_SATELLITE_MAP})
5
+ GOOGLE_HYBRID = OpenLayers::Layer::Google.new("Google Hybrid", {:type => :G_HYBRID_MAP})
6
+ GOOGLE_PHYSICAL = OpenLayers::Layer::Google.new("Google Physical", {:type => :G_PHYSICAL_MAP})
7
+ VE_ROAD = OpenLayers::Layer::VirtualEarth.new("Virtual Earth Raods", {:type => JsExpr.new('VEMapStyle.Road')})
8
+ VE_AERIAL = OpenLayers::Layer::VirtualEarth.new("Virtual Earth Aerial", {:type => JsExpr.new('VEMapStyle.Aerial')})
9
+ VE_HYBRID = OpenLayers::Layer::VirtualEarth.new("Virtual Earth Hybrid", {:type => JsExpr.new('VEMapStyle.Hybrid')})
10
+ YAHOO = OpenLayers::Layer::Yahoo.new("Yahoo Street")
11
+ YAHOO_SATELLITE = OpenLayers::Layer::Yahoo.new("Yahoo Satelite", {:type => :YAHOO_MAP_SAT})
12
+ YAHOO_HYBRID = OpenLayers::Layer::Yahoo.new("Yahoo Hybrid", {:type => :YAHOO_MAP_HYB})
13
+ MULTIMAP = OpenLayers::Layer::MultiMap.new("MultiMap")
14
+ OSM_MAPNIK = OpenLayers::Layer::OSM.new("OpenStreetMap")
15
+ OSM_TELASCIENCE = OpenLayers::Layer::WMS.new("OpenStreetMap",
16
+ [
17
+ "http://t1.hypercube.telascience.org/tiles?",
18
+ "http://t2.hypercube.telascience.org/tiles?",
19
+ "http://t3.hypercube.telascience.org/tiles?",
20
+ "http://t4.hypercube.telascience.org/tiles?"
21
+ ],
22
+ {:layers => 'osm-4326', :format => 'image/png' } )
23
+ GEOPOLE_OSM = OpenLayers::Layer::TMS.new("Geopole Street Map",
24
+ "http://tms.geopole.org/",
25
+ {:layername => 'geopole_street', :type => 'png', :maxResolution => 0.703125,
26
+ :attribution => 'Map data <a href="http://creativecommons.org/licenses/by-sa/2.0/">CCBYSA</a> 2009 <a href="http://openstreetmap.org/">OpenStreetMap.org</a>'})
27
+ NASA_GLOBAL_MOSAIC = OpenLayers::Layer::WMS.new("NASA Global Mosaic",
28
+ [
29
+ "http://t1.hypercube.telascience.org/cgi-bin/landsat7",
30
+ "http://t2.hypercube.telascience.org/cgi-bin/landsat7",
31
+ "http://t3.hypercube.telascience.org/cgi-bin/landsat7",
32
+ "http://t4.hypercube.telascience.org/cgi-bin/landsat7"
33
+ ],
34
+ {:layers => 'landsat7'} )
35
+ BLUE_MARBLE_NG = OpenLayers::Layer::WMS.new("Blue Marble NG",
36
+ "http://wms.telascience.org/cgi-bin/ngBM_wms",
37
+ {:layers => 'world_topo_bathy'} )
38
+ METACARTA_VMAP0 = OpenLayers::Layer::WMS.new("OpenLayers WMS",
39
+ "http://labs.metacarta.com/wms/vmap0",
40
+ {:layers => 'basic'} )
41
+ WORLDWIND = OpenLayers::Layer::WorldWind.new("World Wind LANDSAT",
42
+ "http://worldwind25.arc.nasa.gov/tile/tile.aspx", 2.25, 4, {:T => "105"}, {:tileSize => OpenLayers::Size.new(512,512)})
43
+ WORLDWIND_URBAN = OpenLayers::Layer::WorldWind.new("World Wind Urban",
44
+ "http://worldwind25.arc.nasa.gov/tile/tile.aspx", 0.8, 9, {:T => "104"}, {:tileSize => OpenLayers::Size.new(512,512)})
45
+ WORLDWIND_BATHY = OpenLayers::Layer::WorldWind.new("World Wind Bathymetry",
46
+ "http://worldwind25.arc.nasa.gov/tile/tile.aspx", 36, 4, {:T => "bmng.topo.bathy.200406"}, {:tileSize => OpenLayers::Size.new(512,512)})
47
+
48
+
49
+ #Map viewer main class
50
+ class Map
51
+ include JsWrapper
52
+
53
+ def initialize(map, options = {}, &block)
54
+ @container = map
55
+ @variable = map
56
+ @options = {:theme => false}.merge(options)
57
+ @js = JsGenerator.new
58
+ yield(self, @js) if block_given?
59
+ end
60
+
61
+ #Outputs in JavaScript the creation of a OpenLayers.Map object
62
+ def create
63
+ "new OpenLayers.Map('#{@container}', #{JsWrapper::javascriptify_variable(@options)})"
64
+ end
65
+
66
+ #Outputs the initialization code for the map
67
+ def to_html(options = {})
68
+ no_script_tag = options[:no_script_tag]
69
+ no_declare = options[:no_declare]
70
+ no_global = options[:no_global]
71
+
72
+ html = ""
73
+ html << "<script defer=\"defer\" type=\"text/javascript\">\n" if !no_script_tag
74
+ #put the functions in a separate javascript file to be included in the page
75
+ html << "var #{@variable};\n" if !no_declare and !no_global
76
+
77
+ if !no_declare and no_global
78
+ html << "#{declare(@variable)}\n"
79
+ else
80
+ html << "#{assign_to(@variable)}\n"
81
+ end
82
+ html << @js.to_s
83
+ html << "</script>\n" if !no_script_tag
84
+
85
+ html
86
+ end
87
+ end
88
+
89
+ end
@@ -0,0 +1,11 @@
1
+ module OpenLayers
2
+
3
+ def self.const_missing(sym)
4
+ if OpenLayers.const_defined?(sym)
5
+ OpenLayers.const_get(sym)
6
+ else
7
+ OpenLayers.const_set(sym, Class.new(MapLayers::JsClass))
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,3 @@
1
+ module MapLayers
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,48 @@
1
+ module MapLayers
2
+ # Provides methods to generate HTML tags and JavaScript code
3
+ module ViewHelpers
4
+ # Insert javascript include tags
5
+ #
6
+ # * options[:google] with GMAPS Key: Include Google Maps
7
+ # * options[:multimap] with MultiMap Key: Include MultiMap
8
+ # * options[:virtualearth]: Include VirtualEarth
9
+ # * options[:yahoo] with Yahoo appid: Include Yahoo! Maps
10
+ # * options[:proxy] with name of controller with proxy action. Defaults to current controller.
11
+ def map_layers_includes(options = {})
12
+ options.assert_valid_keys(:google, :multimap, :openstreetmap, :virtualearth, :yahoo, :proxy,:img_path)
13
+ html = ''
14
+ if options.has_key?(:google)
15
+ html << "<script type=\"text/javascript\" src=\"http://maps.google.com/maps?file=api&amp;v=2&amp;key=#{options[:google]}\"></script>"
16
+ end
17
+ if options.has_key?(:multimap)
18
+ html << "<script type=\"text/javascript\" src=\"http://clients.multimap.com/API/maps/1.1/#{options[:multimap]}\"></script>"
19
+ end
20
+ if options.has_key?(:virtualearth)
21
+ html << "<script type=\"text/javascript\" src=\"http://dev.virtualearth.net/mapcontrol/v3/mapcontrol.js\"></script>"
22
+ end
23
+ if options.has_key?(:yahoo)
24
+ html << "<script type=\"text/javascript\" src=\"http://api.maps.yahoo.com/ajaxymap?v=3.0&appid=#{options[:yahoo]}\"></script>"
25
+ end
26
+ img_path = '/images/OpenLayers/'
27
+ if options.has_key?(:img_path)
28
+ img_path = options[:img_path]
29
+ end
30
+ if RAILS_ENV == "development" && File.exist?(File.join(RAILS_ROOT, 'public/javascripts/lib/OpenLayers.js'))
31
+ html << '<script src="/javascripts/lib/Firebug/firebug.js"></script>'
32
+ html << '<script src="/javascripts/lib/OpenLayers.js"></script>'
33
+ else
34
+ html << javascript_include_tag('OpenLayers')
35
+ end
36
+
37
+
38
+ html << stylesheet_link_tag("map")
39
+ img_path=(Pathname(ActionController::Base.relative_url_root||"") +img_path).cleanpath.to_s
40
+ html << javascript_tag("OpenLayers.ImgPath='"+ img_path + "/';")
41
+ proxy = options.has_key?(:proxy) ? options[:proxy] : controller.controller_name
42
+ html << javascript_tag("OpenLayers.ProxyHost='/#{proxy}/proxy?url=';")
43
+
44
+ html
45
+ end
46
+ end
47
+
48
+ end