Add Google Map of voters
[selectricity-live] / vendor / plugins / ym4r_gm / lib / gm_plugin / layer.rb
diff --git a/vendor/plugins/ym4r_gm/lib/gm_plugin/layer.rb b/vendor/plugins/ym4r_gm/lib/gm_plugin/layer.rb
new file mode 100644 (file)
index 0000000..bab6625
--- /dev/null
@@ -0,0 +1,125 @@
+module Ym4r\r
+  module GmPlugin\r
+    #Map types of the map\r
+    class GMapType\r
+      include MappingObject\r
+      \r
+      G_NORMAL_MAP = Variable.new("G_NORMAL_MAP")\r
+      G_SATELLITE_MAP = Variable.new("G_SATELLITE_MAP")\r
+      G_HYBRID_MAP = Variable.new("G_HYBRID_MAP")\r
+      \r
+      attr_accessor :layers, :name, :projection, :options\r
+      \r
+      #The options can be any of the GMapType options detailed in the documentation + a <tt>:projection</tt>.\r
+      def initialize(layers, name, options = {})\r
+        @layers = layers\r
+        @name = name\r
+        @projection = options.delete(:projection) || GMercatorProjection.new\r
+        @options = options\r
+      end\r
+\r
+      def create\r
+        "new GMapType(#{MappingObject.javascriptify_variable(Array(layers))}, #{MappingObject.javascriptify_variable(projection)}, #{MappingObject.javascriptify_variable(name)}, #{MappingObject.javascriptify_variable(options)})"\r
+      end\r
+    end\r
+\r
+    #Represents a mercator projection for zoom levels 0 to 17 (more than that by passing an argument to the constructor)\r
+    class GMercatorProjection\r
+      include MappingObject\r
+      \r
+      attr_accessor :n\r
+      \r
+      def initialize(n = nil)\r
+        @n = n\r
+      end\r
+\r
+      def create\r
+        if n.nil?\r
+          return "G_NORMAL_MAP.getProjection()"\r
+        else\r
+          "new GMercatorProjection(#{@n})"\r
+        end\r
+      end\r
+    end\r
+\r
+    #Abstract Tile layer. Subclasses must implement a get_tile_url method.\r
+    class GTileLayer\r
+      include MappingObject\r
+            \r
+      attr_accessor :opacity, :zoom_range, :copyright, :format\r
+\r
+      #Options are the following, with default values:\r
+      #:zoom_range (0..17), :copyright ({'prefix' => '', 'copyright_texts' => [""]}), :opacity (1.0), :format ("png")\r
+      def initialize(options = {})\r
+        @opacity = options[:opacity] || 1.0\r
+        @zoom_range = options[:zoom_range] || (0..17)\r
+        @copyright = options[:copyright] || {'prefix' => '', 'copyright_texts' => [""]}\r
+        @format = (options[:format] || "png").to_s\r
+      end\r
+\r
+      def create\r
+        "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
+      end\r
+      \r
+      #for subclasses to implement\r
+      def get_tile_url\r
+      end\r
+    end\r
+    \r
+    #Represents a pre tiled layer, taking images directly from a server, without using a server script.\r
+    class PreTiledLayer < GTileLayer\r
+      attr_accessor :base_url\r
+      \r
+      #Possible options are the same as for the GTileLayer constructor\r
+      def initialize(base_url,options = {})\r
+        super(options)\r
+        @base_url = base_url\r
+      end\r
+      \r
+      #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
+      def get_tile_url\r
+        "function(a,b) { return '#{@base_url}/tile_' + b + '_' + a.x + '_' + a.y + '.#{format}';}"\r
+      end \r
+    end\r
+\r
+    #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
+    class PreTiledLayerFromAction < PreTiledLayer\r
+      def get_tile_url\r
+        "function(a,b) { return '#{base_url}?x=' + a.x + '&y=' + a.y + '&z=' + b ;}"\r
+      end\r
+    end\r
+    \r
+    #Represents a TileLayer where the tiles are generated dynamically from a WMS server (MapServer, GeoServer,...)\r
+    #You need to include the JavaScript file wms-gs.js for this to work\r
+    #see http://docs.codehaus.org/display/GEOSDOC/Google+Maps\r
+    class WMSLayer < GTileLayer\r
+      attr_accessor :base_url, :layers, :styles, :merc_proj, :use_geographic\r
+\r
+      #Options are the same as with GTileLayer + :styles (""), :merc_proj (:mapserver), :use_geographic (false)\r
+      def initialize(base_url, layers, options = {})\r
+        super(options)\r
+        @base_url = base_url.gsub(/\?$/,"") #standardize the url\r
+        @layers = layers\r
+        @styles = options[:styles] || ""\r
+        merc_proj = options[:merc_proj] || :mapserver\r
+        @merc_proj = if merc_proj == :mapserver\r
+                       "54004"\r
+                     elsif merc_proj == :geoserver\r
+                       "41001"\r
+                     else\r
+                       merc_proj.to_s\r
+                     end\r
+        @use_geographic = options.has_key?(:use_geographic)? options[:use_geographic] : false\r
+        puts format\r
+      end\r
+      \r
+      def get_tile_url\r
+        "getTileUrlForWMS"\r
+      end\r
+\r
+      def create\r
+        "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
+      end\r
+    end\r
+  end\r
+end\r

Benjamin Mako Hill || Want to submit a patch?