1 class AmfController < ApplicationController
5 before_filter :check_write_availability
8 # RAILS_DEFAULT_LOGGER.error("Args: #{args[0]}, #{args[1]}, #{args[2]}, #{args[3]}")
10 # ====================================================================
13 # ---- talk process AMF request
16 req=StringIO.new(request.raw_post+0.chr) # Get POST data as request
17 # (cf http://www.ruby-forum.com/topic/122163)
18 req.read(2) # Skip version indicator and client ID
19 results={} # Results of each body
24 headers=getint(req) # Read number of headers
26 headers.times do # Read each header
27 name=getstring(req) # |
28 req.getc # | skip boolean
29 value=getvalue(req) # |
30 header["name"]=value # |
33 bodies=getint(req) # Read number of bodies
34 bodies.times do # Read each body
35 message=getstring(req) # | get message name
36 index=getstring(req) # | get index in response sequence
37 bytes=getlong(req) # | get total size in bytes
38 args=getvalue(req) # | get response (probably an array)
41 when 'getpresets'; results[index]=putdata(index,getpresets)
42 when 'whichways'; results[index]=putdata(index,whichways(args))
43 when 'getway'; results[index]=putdata(index,getway(args))
44 when 'putway'; results[index]=putdata(index,putway(args))
45 when 'deleteway'; results[index]=putdata(index,deleteway(args))
46 when 'putpoi'; results[index]=putdata(index,putpoi(args))
47 when 'getpoi'; results[index]=putdata(index,getpoi(args))
54 RAILS_DEFAULT_LOGGER.info(" Response: start")
55 a,b=results.length.divmod(256)
56 render :content_type => "application/x-amf", :text => proc { |response, output|
57 output.write 0.chr+0.chr+0.chr+0.chr+a.chr+b.chr
62 RAILS_DEFAULT_LOGGER.info(" Response: end")
68 # ====================================================================
72 # return presets,presetmenus and presetnames arrays
76 presetmenus={}; presetmenus['point']=[]; presetmenus['way']=[]; presetmenus['POI']=[]
77 presetnames={}; presetnames['point']={}; presetnames['way']={}; presetnames['POI']={}
81 RAILS_DEFAULT_LOGGER.info(" Message: getpresets")
83 # File.open("config/potlatch/presets.txt") do |file|
85 # Temporary patch to get around filepath problem
86 # To remove this patch and make the code nice again:
87 # 1. uncomment above line
88 # 2. fix the path in the above line
89 # 3. delete this here document, and the following line (StringIO....)
93 motorway: highway=motorway,ref=(type road number)
94 trunk road: highway=trunk,ref=(type road number),name=(type road name)
95 primary road: highway=primary,ref=(type road number),name=(type road name)
96 secondary road: highway=secondary,ref=(type road number),name=(type road name)
97 tertiary road: highway=tertiary,ref=,name=(type road name)
98 residential road: highway=residential,ref=,name=(type road name)
99 unclassified road: highway=unclassified,ref=,name=(type road name)
102 footpath: highway=footway,foot=yes
103 bridleway: highway=bridleway,foot=yes
104 byway: highway=unsurfaced,foot=yes
105 permissive path: highway=footway,foot=permissive
108 cycle lane: highway=cycleway,cycleway=lane,ncn_ref=
109 cycle track: highway=cycleway,cycleway=track,ncn_ref=
110 cycle lane (NCN): highway=cycleway,cycleway=lane,name=(type name here),ncn_ref=(type route number)
111 cycle track (NCN): highway=cycleway,cycleway=track,name=(type name here),ncn_ref=(type route number)
114 canal: waterway=canal,name=(type name here)
115 navigable river: waterway=river,boat=yes,name=(type name here)
116 navigable drain: waterway=drain,boat=yes,name=(type name here)
117 derelict canal: waterway=derelict_canal,name=(type name here)
118 unnavigable river: waterway=river,boat=no,name=(type name here)
119 unnavigable drain: waterway=drain,boat=no,name=(type name here)
122 railway: railway=rail
123 tramway: railway=tram
124 light railway: railway=light_rail
125 preserved railway: railway=preserved
126 disused railway tracks: railway=disused
127 course of old railway: railway=abandoned
130 lake: natural=water,landuse=
131 forest: landuse=forest,natural=
134 mini roundabout: highway=mini_roundabout
135 traffic lights: highway=traffic_signals
138 bridge: highway=bridge
141 cattle grid: highway=cattle_grid
147 lock gate: waterway=lock_gate
149 aqueduct: waterway=aqueduct
150 winding hole: waterway=turning_point
151 mooring: waterway=mooring
154 station: railway=station
155 viaduct: railway=viaduct
156 level crossing: railway=crossing
162 car park: amenity=parking
163 petrol station: amenity=fuel
166 bike park: amenity=bicycle_parking
169 city: place=city,name=(type name here),is_in=(type region or county)
170 town: place=town,name=(type name here),is_in=(type region or county)
171 suburb: place=suburb,name=(type name here),is_in=(type region or county)
172 village: place=village,name=(type name here),is_in=(type region or county)
173 hamlet: place=hamlet,name=(type name here),is_in=(type region or county)
176 attraction: tourism=attraction,amenity=,religion=,denomination=
177 church: tourism=,amenity=place_of_worship,name=(type name here),religion=christian,denomination=(type denomination here)
178 hotel: tourism=hotel,amenity=,religion=,denomination=
179 other religious: tourism=,amenity=place_of_worship,name=(type name here),religion=(type religion),denomination=
180 post box: amenity=post_box,tourism=,name=,religion=,denomination=
181 post office: amenity=post_office,tourism=,name=,religion=,denomination=
182 pub: tourism=,amenity=pub,name=(type name here),religion=,denomination=
188 StringIO.open(txt) do |file|
189 file.each_line {|line|
191 if (t=~/(\w+)\/(\w+)/) then
194 presetmenus[presettype].push(presetcategory)
195 presetnames[presettype][presetcategory]=["(no preset)"]
196 elsif (t=~/^(.+):\s?(.+)$/) then
198 presetnames[presettype][presetcategory].push(pre)
200 kv.split(',').each {|a|
201 if (a=~/^(.+)=(.*)$/) then presets[pre][$1]=$2 end
206 return [presets,presetmenus,presetnames]
209 # ----- whichways(left,bottom,right,top)
210 # return array of ways in current bounding box
211 # at present, instead of using correct (=more complex) SQL to find
212 # corner-crossing ways, it simply enlarges the bounding box by +/- 0.01
215 xmin = args[0].to_f-0.01
216 ymin = args[1].to_f-0.01
217 xmax = args[2].to_f+0.01
218 ymax = args[3].to_f+0.01
221 masterscale = args[6]
223 RAILS_DEFAULT_LOGGER.info(" Message: whichways, bbox=#{xmin},#{ymin},#{xmax},#{ymax}")
225 waylist = ActiveRecord::Base.connection.select_all("SELECT DISTINCT current_way_nodes.id AS wayid"+
226 " FROM current_way_nodes,current_nodes,current_ways "+
227 " WHERE current_nodes.id=current_way_nodes.node_id "+
228 " AND current_nodes.visible=1 "+
229 " AND current_ways.id=current_way_nodes.id "+
230 " AND current_ways.visible=1 "+
231 " AND "+OSM.sql_for_area(ymin, xmin, ymax, xmax, "current_nodes."))
233 ways = waylist.collect {|a| a['wayid'].to_i } # get an array of way id's
235 pointlist = ActiveRecord::Base.connection.select_all("SELECT current_nodes.id,current_nodes.latitude*0.0000001 AS lat,current_nodes.longitude*0.0000001 AS lng,current_nodes.tags "+
236 " FROM current_nodes "+
237 " LEFT OUTER JOIN current_way_nodes cwn ON cwn.node_id=current_nodes.id "+
238 " WHERE "+OSM.sql_for_area(ymin, xmin, ymax, xmax, "current_nodes.")+
239 " AND cwn.id IS NULL "+
240 " AND current_nodes.visible=1")
242 points = pointlist.collect {|a| [a['id'],long2coord(a['lng'].to_f,baselong,masterscale),lat2coord(a['lat'].to_f,basey,masterscale),tag2array(a['tags'])] } # get a list of node ids and their tags
247 # ----- getway (objectname, way, baselong, basey, masterscale)
248 # returns objectname, array of co-ordinates, attributes,
249 # xmin,xmax,ymin,ymax
252 objname,wayid,baselong,basey,masterscale=args
257 xmax = ymax = -999999
259 RAILS_DEFAULT_LOGGER.info(" Message: getway, id=#{wayid}")
261 readwayquery(wayid).each {|row|
262 points<<[long2coord(row['longitude'].to_f,baselong,masterscale),lat2coord(row['latitude'].to_f,basey,masterscale),row['id'].to_i,nil,tag2array(row['tags'])]
263 xmin = [xmin,row['longitude'].to_f].min
264 xmax = [xmax,row['longitude'].to_f].max
265 ymin = [ymin,row['latitude'].to_f].min
266 ymax = [ymax,row['latitude'].to_f].max
270 attrlist=ActiveRecord::Base.connection.select_all "SELECT k,v FROM current_way_tags WHERE id=#{wayid}"
271 attrlist.each {|a| attributes[a['k'].gsub(':','|')]=a['v'] }
273 [objname,points,attributes,xmin,xmax,ymin,ymax]
276 # ----- putway (user token, way, array of co-ordinates, array of attributes,
277 # baselong, basey, masterscale)
278 # returns current way ID, new way ID, hash of renumbered nodes,
279 # xmin,xmax,ymin,ymax
282 RAILS_DEFAULT_LOGGER.info(" putway started")
283 usertoken,originalway,points,attributes,baselong,basey,masterscale=args
284 uid=getuserid(usertoken)
286 RAILS_DEFAULT_LOGGER.info(" putway authenticated happily")
287 db_uqn='unin'+uid.to_s+originalway.to_i.abs.to_s+Time.new.to_i.to_s # temp uniquenodes table name, typically 51 chars
288 db_now='@now'+uid.to_s+originalway.to_i.abs.to_s+Time.new.to_i.to_s # 'now' variable name, typically 51 chars
289 ActiveRecord::Base.connection.execute("SET #{db_now}=NOW()")
290 originalway=originalway.to_i
292 RAILS_DEFAULT_LOGGER.info(" Message: putway, id=#{originalway}")
294 # -- 3. read original way into memory
296 xc={}; yc={}; tagc={}
299 readwayquery(way).each { |row|
301 xc[id]=row['longitude'].to_f
302 yc[id]=row['latitude' ].to_f
305 ActiveRecord::Base.connection.update("UPDATE current_ways SET timestamp=#{db_now},user_id=#{uid},visible=1 WHERE id=#{way}")
307 way=ActiveRecord::Base.connection.insert("INSERT INTO current_ways (user_id,timestamp,visible) VALUES (#{uid},#{db_now},1)")
310 # -- 4. get version by inserting new row into ways
312 version=ActiveRecord::Base.connection.insert("INSERT INTO ways (id,user_id,timestamp,visible) VALUES (#{way},#{uid},#{db_now},1)")
314 # -- 5. compare nodes and update xmin,xmax,ymin,ymax
322 points.each_index do |i|
323 xs=coord2long(points[i][0],masterscale,baselong)
324 ys=coord2lat(points[i][1],masterscale,basey)
325 xmin=[xs,xmin].min; xmax=[xs,xmax].max
326 ymin=[ys,ymin].min; ymax=[ys,ymax].max
327 node=points[i][2].to_i
328 tagstr=array2tag(points[i][4])
329 tagsql="'"+sqlescape(tagstr)+"'"
330 lat=(ys * 10000000).round
331 long=(xs * 10000000).round
332 tile=QuadTile.tile_for_point(ys, xs)
337 if renumberednodes[node.to_s].nil?
338 newnode=ActiveRecord::Base.connection.insert("INSERT INTO current_nodes ( latitude,longitude,timestamp,user_id,visible,tags,tile) VALUES ( #{lat},#{long},#{db_now},#{uid},1,#{tagsql},#{tile})")
339 ActiveRecord::Base.connection.insert("INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible,tags,tile) VALUES (#{newnode},#{lat},#{long},#{db_now},#{uid},1,#{tagsql},#{tile})")
341 nodelist.push(newnode)
342 renumberednodes[node.to_s]=newnode.to_s
344 points[i][2]=renumberednodes[node.to_s].to_i
347 elsif xc.has_key?(node)
349 # old node from original way - update
350 if (xs!=xc[node] or (ys/0.0000001).round!=(yc[node]/0.0000001).round or tagstr!=tagc[node])
351 ActiveRecord::Base.connection.insert("INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible,tags,tile) VALUES (#{node},#{lat},#{long},#{db_now},#{uid},1,#{tagsql},#{tile})")
352 ActiveRecord::Base.connection.update("UPDATE current_nodes SET latitude=#{lat},longitude=#{long},timestamp=#{db_now},user_id=#{uid},tags=#{tagsql},visible=1,tile=#{tile} WHERE id=#{node}")
355 # old node, created in another way and now added to this way
360 # -- 6a. delete any nodes not in modified way
362 createuniquenodes(way,db_uqn,nodelist) # nodes which appear in this way but no other
365 INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible,tile)
366 SELECT DISTINCT cn.id,cn.latitude,cn.longitude,#{db_now},#{uid},0,cn.tile
367 FROM current_nodes AS cn,#{db_uqn}
370 ActiveRecord::Base.connection.insert(sql)
373 UPDATE current_nodes AS cn, #{db_uqn}
374 SET cn.timestamp=#{db_now},cn.visible=0,cn.user_id=#{uid}
377 ActiveRecord::Base.connection.update(sql)
379 deleteuniquenoderelations(db_uqn,uid,db_now)
380 ActiveRecord::Base.connection.execute("DROP TABLE #{db_uqn}")
382 # 6b. insert new version of route into way_nodes
388 if insertsql !='' then insertsql +=',' end
389 if currentsql!='' then currentsql+=',' end
390 insertsql +="(#{way},#{p[2]},#{sequence},#{version})"
391 currentsql+="(#{way},#{p[2]},#{sequence})"
395 ActiveRecord::Base.connection.execute("DELETE FROM current_way_nodes WHERE id=#{way}");
396 ActiveRecord::Base.connection.insert( "INSERT INTO way_nodes (id,node_id,sequence_id,version) VALUES #{insertsql}");
397 ActiveRecord::Base.connection.insert( "INSERT INTO current_way_nodes (id,node_id,sequence_id ) VALUES #{currentsql}");
399 # -- 7. insert new way tags
403 attributes.each do |k,v|
404 if v=='' or v.nil? then next end
405 if v[0,6]=='(type ' then next end
406 if insertsql !='' then insertsql +=',' end
407 if currentsql!='' then currentsql+=',' end
408 insertsql +="(#{way},'"+sqlescape(k.gsub('|',':'))+"','"+sqlescape(v)+"',#{version})"
409 currentsql+="(#{way},'"+sqlescape(k.gsub('|',':'))+"','"+sqlescape(v)+"')"
412 ActiveRecord::Base.connection.execute("DELETE FROM current_way_tags WHERE id=#{way}")
413 if (insertsql !='') then ActiveRecord::Base.connection.insert("INSERT INTO way_tags (id,k,v,version) VALUES #{insertsql}" ) end
414 if (currentsql!='') then ActiveRecord::Base.connection.insert("INSERT INTO current_way_tags (id,k,v) VALUES #{currentsql}") end
416 [originalway,way,renumberednodes,xmin,xmax,ymin,ymax]
419 # ----- putpoi (user token, id, x,y,tag array,visible,baselong,basey,masterscale)
420 # returns current id, new id
421 # if new: add new row to current_nodes and nodes
422 # if old: add new row to nodes, update current_nodes
425 usertoken,id,x,y,tags,visible,baselong,basey,masterscale=args
426 uid=getuserid(usertoken)
428 db_now='@now'+uid.to_s+id.to_i.abs.to_s+Time.new.to_i.to_s # 'now' variable name, typically 51 chars
429 ActiveRecord::Base.connection.execute("SET #{db_now}=NOW()")
434 # if deleting, check node hasn't become part of a way
435 inway=ActiveRecord::Base.connection.select_one("SELECT cw.id FROM current_ways cw,current_way_nodes cwn WHERE cw.id=cwn.id AND cw.visible=1 AND cwn.node_id=#{id} LIMIT 1")
436 unless inway.nil? then return [id,id] end # should really return an error
437 deleteitemrelations(id,'node',uid,db_now)
440 x=coord2long(x.to_f,masterscale,baselong)
441 y=coord2lat(y.to_f,masterscale,basey)
442 tagsql="'"+sqlescape(array2tag(tags))+"'"
443 lat=(y * 10000000).round
444 long=(x * 10000000).round
445 tile=QuadTile.tile_for_point(y, x)
448 ActiveRecord::Base.connection.insert("INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible,tags,tile) VALUES (#{id},#{lat},#{long},#{db_now},#{uid},#{visible},#{tagsql},#{tile})");
449 ActiveRecord::Base.connection.update("UPDATE current_nodes SET latitude=#{lat},longitude=#{long},timestamp=#{db_now},user_id=#{uid},visible=#{visible},tags=#{tagsql},tile=#{tile} WHERE id=#{id}");
452 newid=ActiveRecord::Base.connection.insert("INSERT INTO current_nodes (latitude,longitude,timestamp,user_id,visible,tags,tile) VALUES (#{lat},#{long},#{db_now},#{uid},#{visible},#{tagsql},#{tile})");
453 ActiveRecord::Base.connection.update("INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible,tags,tile) VALUES (#{newid},#{lat},#{long},#{db_now},#{uid},#{visible},#{tagsql},#{tile})");
458 # ----- getpoi (id,baselong,basey,masterscale)
459 # returns id,x,y,tag array
462 id,baselong,basey,masterscale=args; id=id.to_i
463 poi=ActiveRecord::Base.connection.select_one("SELECT latitude*0.0000001 AS lat,longitude*0.0000001 AS lng,tags "+
464 "FROM current_nodes WHERE visible=1 AND id=#{id}")
465 if poi.nil? then return [nil,nil,nil,''] end
467 long2coord(poi['lng'].to_f,baselong,masterscale),
468 lat2coord(poi['lat'].to_f,basey,masterscale),
469 tag2array(poi['tags'])]
472 # ----- deleteway (user token, way, nodes to keep)
473 # returns way ID only
476 usertoken,way,preserve=args
478 RAILS_DEFAULT_LOGGER.info(" Message: deleteway, id=#{way}")
480 uid=getuserid(usertoken); if !uid then return end
483 db_uqn='unin'+uid.to_s+way.to_i.abs.to_s+Time.new.to_i.to_s # temp uniquenodes table name, typically 51 chars
484 db_now='@now'+uid.to_s+way.to_i.abs.to_s+Time.new.to_i.to_s # 'now' variable name, typically 51 chars
485 ActiveRecord::Base.connection.execute("SET #{db_now}=NOW()")
487 # - delete any otherwise unused nodes
489 createuniquenodes(way,db_uqn,[])
491 unless (preserve.empty?) then
492 ActiveRecord::Base.connection.execute("DELETE FROM #{db_uqn} WHERE node_id IN ("+preserve.join(',')+")")
496 INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible,tile)
497 SELECT DISTINCT cn.id,cn.latitude,cn.longitude,#{db_now},#{uid},0,cn.tile
498 FROM current_nodes AS cn,#{db_uqn}
501 ActiveRecord::Base.connection.insert(sql)
504 UPDATE current_nodes AS cn, #{db_uqn}
505 SET cn.timestamp=#{db_now},cn.visible=0,cn.user_id=#{uid}
508 ActiveRecord::Base.connection.update(sql)
510 deleteuniquenoderelations(db_uqn,uid,db_now)
511 ActiveRecord::Base.connection.execute("DROP TABLE #{db_uqn}")
515 ActiveRecord::Base.connection.insert("INSERT INTO ways (id,user_id,timestamp,visible) VALUES (#{way},#{uid},#{db_now},0)")
516 ActiveRecord::Base.connection.update("UPDATE current_ways SET user_id=#{uid},timestamp=#{db_now},visible=0 WHERE id=#{way}")
517 ActiveRecord::Base.connection.execute("DELETE FROM current_way_nodes WHERE id=#{way}")
518 ActiveRecord::Base.connection.execute("DELETE FROM current_way_tags WHERE id=#{way}")
519 deleteitemrelations(way,'way',uid,db_now)
525 # ====================================================================
526 # Support functions for remote calls
529 ActiveRecord::Base.connection.select_all "SELECT latitude*0.0000001 AS latitude,longitude*0.0000001 AS longitude,current_nodes.id,tags "+
530 " FROM current_way_nodes,current_nodes "+
531 " WHERE current_way_nodes.id=#{id} "+
532 " AND current_way_nodes.node_id=current_nodes.id "+
533 " AND current_nodes.visible=1 "+
534 " ORDER BY sequence_id"
537 def createuniquenodes(way,uqn_name,nodelist)
538 # Find nodes which appear in this way but no others
540 CREATE TEMPORARY TABLE #{uqn_name}
542 FROM (SELECT DISTINCT node_id FROM current_way_nodes
544 LEFT JOIN current_way_nodes b
545 ON b.node_id=a.node_id
547 WHERE b.node_id IS NULL
549 unless nodelist.empty? then
550 sql+="AND a.node_id NOT IN ("+nodelist.join(',')+")"
552 ActiveRecord::Base.connection.execute(sql)
557 # ====================================================================
559 # deleteuniquenoderelations(uqn_name,uid,db_now)
560 # deleteitemrelations(way|node,'way'|'node',uid,db_now)
562 def deleteuniquenoderelations(uqn_name,uid,db_now)
564 SELECT node_id,cr.id FROM #{uqn_name},current_relation_members crm,current_relations cr
565 WHERE crm.member_id=node_id
566 AND crm.member_type='node'
571 relnodes=ActiveRecord::Base.connection.select_all(sql)
573 removefromrelation(a['node_id'],'node',a['id'],uid,db_now)
577 def deleteitemrelations(objid,type,uid,db_now)
579 SELECT cr.id FROM current_relation_members crm,current_relations cr
580 WHERE crm.member_id=#{objid}
581 AND crm.member_type='#{type}'
586 relways=ActiveRecord::Base.connection.select_all(sql)
588 removefromrelation(objid,type,a['id'],uid,db_now)
592 def removefromrelation(objid,type,relation,uid,db_now)
593 rver=ActiveRecord::Base.connection.insert("INSERT INTO relations (id,user_id,timestamp,visible) VALUES (#{relation},#{uid},#{db_now},1)")
596 INSERT INTO relation_tags (id,k,v,version)
597 SELECT id,k,v,#{rver} FROM current_relation_tags
600 ActiveRecord::Base.connection.insert(tagsql)
603 INSERT INTO relation_members (id,member_type,member_id,member_role,version)
604 SELECT id,member_type,member_id,member_role,#{rver} FROM current_relation_members
606 AND (member_id!=#{objid} OR member_type!='#{type}')
608 ActiveRecord::Base.connection.insert(membersql)
610 ActiveRecord::Base.connection.update("UPDATE current_relations SET user_id=#{uid},timestamp=#{db_now} WHERE id=#{relation}")
611 ActiveRecord::Base.connection.execute("DELETE FROM current_relation_members WHERE id=#{relation} AND member_type='#{type}' AND member_id=#{objid}")
616 a.gsub(/[\000-\037]/,"").gsub("'","''").gsub(92.chr,92.chr+92.chr)
621 a.gsub(';;;','#%').split(';').each do |b|
625 if k.nil? then k='' end
626 if v.nil? then v='' end
627 tags[k.gsub('#%','=').gsub(':','|')]=v.gsub('#%','=')
635 if v=='' then next end
636 if v[0,6]=='(type ' then next end
637 if str!='' then str+=';' end
638 str+=k.gsub(';',';;;').gsub('=','===').gsub('|',':')+'='+v.gsub(';',';;;').gsub('=','===')
644 if (token =~ /^(.+)\+(.+)$/) then
645 user = User.authenticate(:username => $1, :password => $2)
647 user = User.authenticate(:token => token)
650 return user ? user.id : nil;
655 # ====================================================================
656 # AMF read subroutines
658 # ----- getint return two-byte integer
659 # ----- getlong return four-byte long
660 # ----- getstring return string with two-byte length
661 # ----- getdouble return eight-byte double-precision float
662 # ----- getobject return object/hash
663 # ----- getarray return numeric array
670 ((s.getc*256+s.getc)*256+s.getc)*256+s.getc
674 len=s.getc*256+s.getc
679 a=s.read(8).unpack('G') # G big-endian, E little-endian
694 while (key=getstring(s))
695 if (key=='') then break end
698 s.getc # skip the 9 'end of object' value
702 # ----- getvalue parse and get value
706 when 0; return getdouble(s) # number
707 when 1; return s.getc # boolean
708 when 2; return getstring(s) # string
709 when 3; return getobject(s) # object/hash
710 when 5; return nil # null
711 when 6; return nil # undefined
712 when 8; s.read(4) # mixedArray
713 return getobject(s) # |
714 when 10;return getarray(s) # array
715 else; return nil # error
719 # ====================================================================
720 # AMF write subroutines
722 # ----- putdata envelope data into AMF writeable form
723 # ----- encodevalue pack variables as AMF
726 d =encodestring(index+"/onResult")
727 d+=encodestring("null")
735 a=10.chr+encodelong(n.length)
743 a+=encodestring(k)+encodevalue(v)
747 2.chr+encodestring(n)
748 when 'Bignum','Fixnum','Float'
749 0.chr+encodedouble(n)
753 RAILS_DEFAULT_LOGGER.error("Unexpected Ruby type for AMF conversion: "+n.class.to_s)
757 # ----- encodestring encode string with two-byte length
758 # ----- encodedouble encode number as eight-byte double precision float
759 # ----- encodelong encode number as four-byte long
762 a,b=n.size.divmod(256)
774 # ====================================================================
775 # Co-ordinate conversion
777 def lat2coord(a,basey,masterscale)
778 -(lat2y(a)-basey)*masterscale+250
781 def long2coord(a,baselong,masterscale)
782 (a-baselong)*masterscale+350
786 180/Math::PI * Math.log(Math.tan(Math::PI/4+a*(Math::PI/180)/2))
789 def coord2lat(a,masterscale,basey)
790 y2lat((a-250)/-masterscale+basey)
793 def coord2long(a,masterscale,baselong)
794 (a-350)/masterscale+baselong
798 180/Math::PI * (2*Math.atan(Math.exp(a*Math::PI/180))-Math::PI/2)