Skip to content

Commit

Permalink
Addition of tests for the Mapping Object of GoogleMaps.
Browse files Browse the repository at this point in the history
Addition of a tiler, which gets data form WMS servers and tiles it accoriding to the Google Maps scheme.
Addition of some code to support the easy manipulation of pre-tiledlayers (tiled with the tiler above) as well as layers directly from WMS servers.
  • Loading branch information
gvellut committed Jun 8, 2006
1 parent 05933d7 commit a81b7fe
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 39 deletions.
98 changes: 98 additions & 0 deletions lib/ym4r/google_maps/layer.rb
@@ -0,0 +1,98 @@
module Ym4r
module GoogleMaps
#Map types of the map
class GMapType
include MappingObject

G_NORMAL_MAP = Variable.new("GMapType.G_NORMAL_MAP")
G_SATELLITE_MAP = Variable.new("GMapType.G_SATELLITE_MAP")
G_HYBRID_MAP = Variable.new("GMapType.G_HYBRID_MAP")

attr_accessor :layers, :name, :projection

def initialize(layers, name, projection = GMercatorProjection.new,options = {})
@layers = layers
end

def create
"new GMapType(#{javascriptify_variable(Array(layers))}, #{javascriptify_variable(projection)}, #{javascriptify_variable(name)}, #{javascriptify_variable(options)})"
end
end

class GMercatorProjection
include MappingObject

attr_accessor :n

def initialize(n = nil)
@n = n
end

def create
if n.nil?
return "G_NORMAL_MAP.getProjection()"
else
"new GMercatorProjection(#{@n})"
end
end
end

class GTileLayer
include MappingObject

attr_accessor :opacity, :zoom_inter, :copyright

def initialize(zoom_inter = 0..17, copyright= ['prefix' => '', 'copyright_texts' => [""]], opacity = 1.0)
@opacity = opacity
@zoom_inter = zoom_inter
@copyright = copyright
end

def create
"addPropertiesToLayer(new GTileLayer(new GCopyrightCollection(\"\"),#{zoom_inter.begin},#{zoom_inter.end}),#{get_tile_url}, function(a,b) {return #{MappingObject.javascriptify_variable(@copyright)};}, function() {return #{@opacity};})"
end

#for subclasses to implement
def get_tile_url
end
end

class PreTiledLayer < GTileLayer
include MappingObject

attr_accessor :base_url, :format

def initialize(base_url, format = "png", zoom_inter = 0..17, copyright = ['prefix' => '', 'copyright_texts' => [""]], opacity = 1.0)
super(zoom_inter, copyright, opacity)
@base_url = base_url
@format = format
end

#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}
def get_tile_url
"function(a,b,c) { return '#{@base_url}/tile_' + b + '_' + a.x + '_' + a.y + '.#{@format}';}"
end
end

#needs to modify the wms-gs.js script for this to work : check with the people who wrote it if it is ok
#needs to include the JavaScript file wms-gs.js for this to work
#see http://docs.codehaus.org/display/GEOSDOC/Google+Maps
class WMSLayer < GTileLayer
include MappingObject

attr_accessor :base_url, :layers, :styles, :format

def initialize(base_url, layers, styles = "", format= "png", zoom_inter = 0..17, copyright = ['prefix' => '', 'copyright_texts' => [""]], opacity = 1.0)
super(zoom_inter, copyright, opacity)
@base_url = base_url
@layers = layers
@styles = styles
@format = format
end

def get_tile_url
"CustomGetTileUrl"
end
end
end
end
17 changes: 9 additions & 8 deletions lib/ym4r/google_maps/map.rb
Expand Up @@ -24,6 +24,10 @@ def header(with_vml = true)
a << "<style type=\"text/css\">\n v\:* { behavior:url(#default#VML);}\n</style>" if with_vml
a
end

def div
"<div id=\"#{@container}\"></div>"
end

#Outputs a style declaration setting the dimensions of the DIV container of the map. This info can also be set manually in a CSS.
def header_width_height(width,height)
Expand Down Expand Up @@ -72,25 +76,27 @@ def icon_init(icon , variable)
#Outputs the initialization code for the map. By default, it outputs the script tags, performs the initialization inside a function called +load+ and makes the map globally available.
def to_html(options = {})
no_load = options[:no_load]
load_method = options[:load_method] || "load"
no_script_tag = options[:no_script_tag]
no_declare = options[:no_declare]
no_global = options[:no_global]

html = ""
html << "<script type=\"text/javascript\">\n" if !no_script_tag
#put the functions in a separate javascript file to be included in the page
html << "function addInfoWindowToMarker(marker,info){\nGEvent.addListener(marker, \"click\", function() {\nmarker.openInfoWindowHtml(info);\n});\nreturn marker;\n}\n"
html << "function addInfoWindowTabsToMarker(marker,info){\nGEvent.addListener(marker, \"click\", function() {\nmarker.openInfoWindowTabsHtml(info);\n});\nreturn marker;\n}\n"
html << "function addPropertiesToLayer(layer,getTile,copyright,opacity){\nlayer.getTileUrl = getTile;\nlayer.getCopyright = copyright;\nlayer.getOpacity = opacity;\nreturn layer;\n}\n"
html << @global_init * "\n"
html << "var #{@variable};\n" if !no_declare and !no_global
html << "function #{load_method}() {\nif (GBrowserIsCompatible()) {\n" if !no_load
html << "window.onload = function() {\nif (GBrowserIsCompatible()) {\n" if !no_load
if !no_declare and no_global
html << "#{declare(@variable)}\n"
else
html << "#{assign_to(@variable)}\n"
end
html << @init * "\n"
html << "\n}\n}\n" if !no_load
html << "window.onunload = GUnload;\n"
html << "</script>" if !no_script_tag
html
end
Expand All @@ -101,12 +107,7 @@ def create
end
end

#Map types of the map
module GMapType
G_NORMAL_MAP = Variable.new("GMapType.G_NORMAL_MAP")
G_SATELLITE_MAP = Variable.new("GMapType.G_SATELLITE_MAP")
G_HYBRID_MAP = Variable.new("GMapType.G_HYBRID_MAP")
end

end
end

22 changes: 16 additions & 6 deletions lib/ym4r/google_maps/mapping.rb
Expand Up @@ -8,29 +8,39 @@ module MappingObject
#Creates javascript code for missing methods
def method_missing(name,*args)
args.collect! do |arg|
javascriptify_variable(arg)
MappingObject.javascriptify_variable(arg)
end
Variable.new("#{to_javascript}.#{javascriptify_method(name.to_s)}(#{args.join(",")})")
Variable.new("#{to_javascript}.#{MappingObject.javascriptify_method(name.to_s)}(#{args.join(",")})")
end

def [](index) #index could be an integer or string
return Variable.new("#{to_javascript}[#{MappingObject.javascriptify_variable(index)}]")
end

#Transforms a Ruby object into a JavaScript string
def javascriptify_variable(arg)
#Transforms a Ruby object into a JavaScript string : MAppingObject, String, Array, Hash and general case (using to_s)
def self.javascriptify_variable(arg)
if arg.is_a?(MappingObject)
arg.to_javascript
elsif arg.is_a?(String)
"\"#{escape_javascript(arg)}\""
elsif arg.is_a?(Array)
"[" + arg.collect{ |a| javascriptify_variable(a)}.join(",") + "]"
elsif arg.is_a?(Hash)
"{" + arg.to_a.collect do |v|
"#{v[0].to_s} : #{MappingObject::javascriptify_variable(v[1])}"
end.join(",") + "}"
else
arg.to_s
end
end

#Escape string to be used in JavaScript. Lifted from rails.
def escape_javascript(javascript)
def self.escape_javascript(javascript)
javascript.gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }
end

#Transform a ruby-type method name (like add_overlay) to a JavaScript-style one (like addOverlay).
def javascriptify_method(method_name)
def self.javascriptify_method(method_name)
method_name.gsub(/_(\w)/){|s| $1.upcase}
end

Expand Down
27 changes: 11 additions & 16 deletions lib/ym4r/google_maps/overlay.rb
Expand Up @@ -20,19 +20,14 @@ def initialize(point, options = {})
#Creates a marker: If an info_window or info_window_tabs is present, the response to the click action from the user is setup here.
def create
if @options.empty?
creation = "new GMarker(#{@point.to_javascript})"
creation = "new GMarker(#{MappingObject::javascriptify_variable(@point)})"
else
options = "{"
options << @options.to_a.collect do |v|
"#{v[0].to_s} : #{javascriptify_variable(v[1])}"
end.join(",")
options << "}"
creation = "new GMarker(#{@point.to_javascript},#{options})"
creation = "new GMarker(#{MappingObject::javascriptify_variable(@point)},#{MappingObject::javascriptify_variable(@options)})"
end
if @info_window
"addInfoWindowToMarker(#{creation},#{javascriptify_variable(@info_window)})"
"addInfoWindowToMarker(#{creation},#{MappingObject::javascriptify_variable(@info_window)})"
elsif @tab_info_window
"addInfoWindowTabsToMarker(#{creation},[#{@tab_info_window.collect{ |tab| tab.to_javascript}.join(",")}])"
"addInfoWindowTabsToMarker(#{creation},#{MappingObject::javascriptify_variable(Array(@tab_info_window))})"
else
creation
end
Expand All @@ -43,7 +38,7 @@ def create
class GInfoWindowTab < Struct.new(:tab,:content)
include MappingObject
def create
"new GInfoWindowTab(#{javascriptify_variable(tab)},#{javascriptify_variable(content)})"
"new GInfoWindowTab(#{MappingObject::javascriptify_variable(tab)},#{MappingObject::javascriptify_variable(content)})"
end
end

Expand All @@ -61,7 +56,7 @@ def initialize(options = {})
#Creates a GIcon.
def create
if @copy_base
"new GIcon(#{@copy_base.to_javascript})"
"new GIcon(#{MappingObject::javascriptify_variable(@copy_base)})"
else
"new GIcon()"
end
Expand All @@ -70,7 +65,7 @@ def create
def declare(variable)
decl = super(variable) + "\n"
@options.each do |key,value|
decl << "#{to_javascript}.#{javascriptify_method(key.to_s)} = #{javascriptify_variable(value)};\n"
decl << "#{to_javascript}.#{MappingObject::javascriptify_method(key.to_s)} = #{MappingObject::javascriptify_variable(value)};\n"
end
decl
end
Expand All @@ -93,10 +88,10 @@ def initialize(points,color = nil,weight = nil,opacity = nil)
end
#Creates a new polyline.
def create
a = "new GPolyline([#{@points.collect{|pt| pt.to_javascript}.join(",")}]"
a << ",#{javascriptify_variable(@color)}" if @color
a << ",#{javascriptify_variable(@weight)}" if @weight
a << ",#{javascriptify_variable(@opacity)}" if @opacity
a = "new GPolyline([#{@points.collect{|pt| MappingObject::javascriptify_variable(pt)}.join(",")}]"
a << ",#{MappingObject::javascriptify_variable(@color)}" if @color
a << ",#{MappingObject::javascriptify_variable(@weight)}" if @weight
a << ",#{MappingObject::javascriptify_variable(@opacity)}" if @opacity
a << ")"
end

Expand Down
48 changes: 39 additions & 9 deletions test/test_google_maps.rb
Expand Up @@ -6,14 +6,44 @@
include Ym4r::GoogleMaps

class TestGoogleMaps< Test::Unit::TestCase
def test_js_export
map = GMap.new("map_div")
var = Variable.new("hello")
yuo = Variable.new("salam")
poi = Variable.new("poi")
map.record_init map.add_overlay(GMarker.new([123.5,123.56]))
map.record_init map.dummy_method(var.other_dummy_method(yuo.kaka_boudin),poi)
map.control_init(:small_map => true)
puts map.to_html
def test_javascriptify_method
assert_equal("addOverlayToHello",MappingObject::javascriptify_method("add_overlay_to_hello"))
end

def test_javascriptify_variable_mapping_object
map = GMap.new("div")
assert_equal(map.to_javascript,MappingObject::javascriptify_variable(map))
end

def test_javascriptify_variable_numeric
assert_equal("123.4",MappingObject::javascriptify_variable(123.4))
end

def test_javascriptify_variable_array
map = GMap.new("div")
assert_equal("[123.4,#{map.to_javascript},[123.4,#{map.to_javascript}]]",MappingObject::javascriptify_variable([123.4,map,[123.4,map]]))
end

def test_javascriptify_variable_hash
map = GMap.new("div")
test_str = MappingObject::javascriptify_variable("hello" => map, "chopotopoto" => [123.55,map])
assert("{hello : #{map.to_javascript},chopotopoto : [123.55,#{map.to_javascript}]}" == test_str || "{chopotopoto : [123.55,#{map.to_javascript}],hello : #{map.to_javascript}}" == test_str)
end

def test_method_call_on_mapping_object
map = GMap.new("div","map")
assert_equal("map.addHello(123.4);",map.add_hello(123.4).to_s)
end

def test_nested_calls_on_mapping_object
gmap = GMap.new("div","map")
assert_equal("map.addHello(map.hoYoYo(123.4),map);",gmap.add_hello(gmap.ho_yo_yo(123.4),gmap).to_s)
end

def test_declare_variable_marker
point = GLatLng.new([123.4,123.6])
assert_equal("var point = new GLatLng(123.4,123.6);",point.declare("point"))
assert_equal("point",point.variable)
end

end

0 comments on commit a81b7fe

Please sign in to comment.