fix security issue
[selectricity-live] / vendor / plugins / ym4r_gm / lib / gm_plugin / layer.rb
1 module Ym4r\r
2   module GmPlugin\r
3     #Map types of the map\r
4     class GMapType\r
5       include MappingObject\r
6       \r
7       G_NORMAL_MAP = Variable.new("G_NORMAL_MAP")\r
8       G_SATELLITE_MAP = Variable.new("G_SATELLITE_MAP")\r
9       G_HYBRID_MAP = Variable.new("G_HYBRID_MAP")\r
10       \r
11       attr_accessor :layers, :name, :projection, :options\r
12       \r
13       #The options can be any of the GMapType options detailed in the documentation + a <tt>:projection</tt>.\r
14       def initialize(layers, name, options = {})\r
15         @layers = layers\r
16         @name = name\r
17         @projection = options.delete(:projection) || GMercatorProjection.new\r
18         @options = options\r
19       end\r
20 \r
21       def create\r
22         "new GMapType(#{MappingObject.javascriptify_variable(Array(layers))}, #{MappingObject.javascriptify_variable(projection)}, #{MappingObject.javascriptify_variable(name)}, #{MappingObject.javascriptify_variable(options)})"\r
23       end\r
24     end\r
25 \r
26     #Represents a mercator projection for zoom levels 0 to 17 (more than that by passing an argument to the constructor)\r
27     class GMercatorProjection\r
28       include MappingObject\r
29       \r
30       attr_accessor :n\r
31       \r
32       def initialize(n = nil)\r
33         @n = n\r
34       end\r
35 \r
36       def create\r
37         if n.nil?\r
38           return "G_NORMAL_MAP.getProjection()"\r
39         else\r
40           "new GMercatorProjection(#{@n})"\r
41         end\r
42       end\r
43     end\r
44 \r
45     #Abstract Tile layer. Subclasses must implement a get_tile_url method.\r
46     class GTileLayer\r
47       include MappingObject\r
48             \r
49       attr_accessor :opacity, :zoom_range, :copyright, :format\r
50 \r
51       #Options are the following, with default values:\r
52       #:zoom_range (0..17), :copyright ({'prefix' => '', 'copyright_texts' => [""]}), :opacity (1.0), :format ("png")\r
53       def initialize(options = {})\r
54         @opacity = options[:opacity] || 1.0\r
55         @zoom_range = options[:zoom_range] || (0..17)\r
56         @copyright = options[:copyright] || {'prefix' => '', 'copyright_texts' => [""]}\r
57         @format = (options[:format] || "png").to_s\r
58       end\r
59 \r
60       def create\r
61         "addPropertiesToLayer(new GTileLayer(new GCopyrightCollection(\"\"),#{zoom_range.begin},#{zoom_range.end}),#{get_tile_url},function(a,b) {return #{MappingObject.javascriptify_variable(@copyright)};}\n,function() {return #{@opacity};},function(){return #{@format == "png"};})"\r
62       end\r
63       \r
64       #for subclasses to implement\r
65       def get_tile_url\r
66       end\r
67     end\r
68     \r
69     #Represents a pre tiled layer, taking images directly from a server, without using a server script.\r
70     class PreTiledLayer < GTileLayer\r
71       attr_accessor :base_url\r
72       \r
73       #Possible options are the same as for the GTileLayer constructor\r
74       def initialize(base_url,options = {})\r
75         super(options)\r
76         @base_url = base_url\r
77       end\r
78       \r
79       #Returns the code to determine the url to fetch the tile. Follows the convention adopted by the tiler: {base_url}/tile_{b}_{a.x}_{a.y}.{format}\r
80       def get_tile_url\r
81         "function(a,b) { return '#{@base_url}/tile_' + b + '_' + a.x + '_' + a.y + '.#{format}';}"\r
82       end \r
83     end\r
84 \r
85     #Represents a pretiled layer (it actually does not really matter where the tiles come from). Calls an action on the server to get back the tiles. It passes the action arguments x, y (coordinates of the tile) and z (zoom level). It can be used, for example, to return default tiles when the requested tile is not present.\r
86     class PreTiledLayerFromAction < PreTiledLayer\r
87       def get_tile_url\r
88         "function(a,b) { return '#{base_url}?x=' + a.x + '&y=' + a.y + '&z=' + b ;}"\r
89       end\r
90     end\r
91     \r
92     #Represents a TileLayer where the tiles are generated dynamically from a WMS server (MapServer, GeoServer,...)\r
93     #You need to include the JavaScript file wms-gs.js for this to work\r
94     #see http://docs.codehaus.org/display/GEOSDOC/Google+Maps\r
95     class WMSLayer < GTileLayer\r
96       attr_accessor :base_url, :layers, :styles, :merc_proj, :use_geographic\r
97 \r
98       #Options are the same as with GTileLayer + :styles (""), :merc_proj (:mapserver), :use_geographic (false)\r
99       def initialize(base_url, layers, options = {})\r
100         super(options)\r
101         @base_url = base_url.gsub(/\?$/,"") #standardize the url\r
102         @layers = layers\r
103         @styles = options[:styles] || ""\r
104         merc_proj = options[:merc_proj] || :mapserver\r
105         @merc_proj = if merc_proj == :mapserver\r
106                        "54004"\r
107                      elsif merc_proj == :geoserver\r
108                        "41001"\r
109                      else\r
110                        merc_proj.to_s\r
111                      end\r
112         @use_geographic = options.has_key?(:use_geographic)? options[:use_geographic] : false\r
113         puts format\r
114       end\r
115       \r
116       def get_tile_url\r
117         "getTileUrlForWMS"\r
118       end\r
119 \r
120       def create\r
121         "addWMSPropertiesToLayer(#{super},#{MappingObject.javascriptify_variable(@base_url)},#{MappingObject.javascriptify_variable(@layers)},#{MappingObject.javascriptify_variable(@styles)},#{MappingObject.javascriptify_variable(format)},#{MappingObject.javascriptify_variable(@merc_proj)},#{MappingObject.javascriptify_variable(@use_geographic)})"\r
122       end\r
123     end\r
124   end\r
125 end\r

Benjamin Mako Hill || Want to submit a patch?