1 class AmfController < ApplicationController
5 before_filter :check_write_availability
7 # AMF controller for Potlatch
8 # ---------------------------
9 # All interaction between Potlatch (as a .SWF application) and the
10 # OSM database takes place using this controller. Messages are
11 # encoded in the Actionscript Message Format (AMF).
13 # Public domain. Set your tab width to 4 to read this document. :)
14 # editions Systeme D / Richard Fairhurst 2004-2008
16 # to trap errors (getway_old,putway,putpoi,deleteway only):
17 # return(-1,"message") <-- just puts up a dialogue
18 # return(-2,"message") <-- also asks the user to e-mail me
20 # RAILS_DEFAULT_LOGGER.error("Args: #{args[0]}, #{args[1]}, #{args[2]}, #{args[3]}")
22 # ====================================================================
25 # ---- talk process AMF request
28 req=StringIO.new(request.raw_post+0.chr) # Get POST data as request
29 # (cf http://www.ruby-forum.com/topic/122163)
30 req.read(2) # Skip version indicator and client ID
31 results={} # Results of each body
32 renumberednodes={} # Shared across repeated putways
37 headers=getint(req) # Read number of headers
39 headers.times do # Read each header
40 name=getstring(req) # |
41 req.getc # | skip boolean
42 value=getvalue(req) # |
43 header["name"]=value # |
46 bodies=getint(req) # Read number of bodies
47 bodies.times do # Read each body
48 message=getstring(req) # | get message name
49 index=getstring(req) # | get index in response sequence
50 bytes=getlong(req) # | get total size in bytes
51 args=getvalue(req) # | get response (probably an array)
54 when 'getpresets'; results[index]=putdata(index,getpresets)
55 when 'whichways'; results[index]=putdata(index,whichways(args))
56 when 'whichways_deleted'; results[index]=putdata(index,whichways_deleted(args))
57 when 'getway'; results[index]=putdata(index,getway(args))
58 when 'getway_old'; results[index]=putdata(index,getway_old(args))
59 when 'getway_history'; results[index]=putdata(index,getway_history(args))
60 when 'putway'; r=putway(args,renumberednodes)
62 results[index]=putdata(index,r)
63 when 'deleteway'; results[index]=putdata(index,deleteway(args))
64 when 'putpoi'; results[index]=putdata(index,putpoi(args))
65 when 'getpoi'; results[index]=putdata(index,getpoi(args))
72 RAILS_DEFAULT_LOGGER.info(" Response: start")
73 a,b=results.length.divmod(256)
74 render :content_type => "application/x-amf", :text => proc { |response, output|
75 output.write 0.chr+0.chr+0.chr+0.chr+a.chr+b.chr
80 RAILS_DEFAULT_LOGGER.info(" Response: end")
86 # ====================================================================
90 # return presets,presetmenus and presetnames arrays
93 RAILS_DEFAULT_LOGGER.info(" Message: getpresets")
97 presetmenus={}; presetmenus['point']=[]; presetmenus['way']=[]; presetmenus['POI']=[]
98 presetnames={}; presetnames['point']={}; presetnames['way']={}; presetnames['POI']={}
101 # StringIO.open(txt) do |file|
102 File.open("#{RAILS_ROOT}/config/potlatch/presets.txt") do |file|
103 file.each_line {|line|
105 if (t=~/(\w+)\/(\w+)/) then
108 presetmenus[presettype].push(presetcategory)
109 presetnames[presettype][presetcategory]=["(no preset)"]
110 elsif (t=~/^(.+):\s?(.+)$/) then
112 presetnames[presettype][presetcategory].push(pre)
114 kv.split(',').each {|a|
115 if (a=~/^(.+)=(.*)$/) then presets[pre][$1]=$2 end
121 # Read colours/styling
122 colours={}; casing={}; areas={}
123 File.open("#{RAILS_ROOT}/config/potlatch/colours.txt") do |file|
124 file.each_line {|line|
126 if (t=~/(\w+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/) then
128 if ($2!='-') then colours[tag]=$2.hex end
129 if ($3!='-') then casing[tag]=$3.hex end
130 if ($4!='-') then areas[tag]=$4.hex end
136 autotags={}; autotags['point']={}; autotags['way']={}; autotags['POI']={};
137 File.open("#{RAILS_ROOT}/config/potlatch/autocomplete.txt") do |file|
138 file.each_line {|line|
140 if (t=~/^(\w+)\/(\w+)\s+(.+)$/) then
141 tag=$1; type=$2; values=$3
142 if values=='-' then autotags[type][tag]=[]
143 else autotags[type][tag]=values.split(',').sort.reverse end
148 [presets,presetmenus,presetnames,colours,casing,areas,autotags]
151 # ----- whichways(left,bottom,right,top)
152 # return array of ways in current bounding box
153 # at present, instead of using correct (=more complex) SQL to find
154 # corner-crossing ways, it simply enlarges the bounding box by +/- 0.01
157 xmin = args[0].to_f-0.01
158 ymin = args[1].to_f-0.01
159 xmax = args[2].to_f+0.01
160 ymax = args[3].to_f+0.01
163 masterscale = args[6]
165 RAILS_DEFAULT_LOGGER.info(" Message: whichways, bbox=#{xmin},#{ymin},#{xmax},#{ymax}")
167 waylist = ActiveRecord::Base.connection.select_all("SELECT DISTINCT current_way_nodes.id AS wayid"+
168 " FROM current_way_nodes,current_nodes,current_ways "+
169 " WHERE current_nodes.id=current_way_nodes.node_id "+
170 " AND current_nodes.visible=1 "+
171 " AND current_ways.id=current_way_nodes.id "+
172 " AND current_ways.visible=1 "+
173 " AND "+OSM.sql_for_area(ymin, xmin, ymax, xmax, "current_nodes."))
175 ways = waylist.collect {|a| a['wayid'].to_i } # get an array of way IDs
177 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 "+
178 " FROM current_nodes "+
179 " LEFT OUTER JOIN current_way_nodes cwn ON cwn.node_id=current_nodes.id "+
180 " WHERE "+OSM.sql_for_area(ymin, xmin, ymax, xmax, "current_nodes.")+
181 " AND cwn.id IS NULL "+
182 " AND current_nodes.visible=1")
184 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
189 # ----- whichways_deleted(left,bottom,right,top)
190 # return array of deleted ways in current bounding box
192 def whichways_deleted(args)
193 xmin = args[0].to_f-0.01
194 ymin = args[1].to_f-0.01
195 xmax = args[2].to_f+0.01
196 ymax = args[3].to_f+0.01
199 masterscale = args[6]
202 SELECT DISTINCT current_ways.id
203 FROM current_nodes,way_nodes,current_ways
204 WHERE #{OSM.sql_for_area(ymin, xmin, ymax, xmax, "current_nodes.")}
205 AND way_nodes.node_id=current_nodes.id
206 AND way_nodes.id=current_ways.id
207 AND current_nodes.visible=0
208 AND current_ways.visible=0
210 waylist = ActiveRecord::Base.connection.select_all(sql)
211 ways = waylist.collect {|a| a['id'].to_i }
215 # ----- getway (objectname, way, baselong, basey, masterscale)
216 # returns objectname, array of co-ordinates, attributes,
217 # xmin,xmax,ymin,ymax
220 objname,wayid,baselong,basey,masterscale=args
224 xmax = ymax = -999999
226 RAILS_DEFAULT_LOGGER.info(" Message: getway, id=#{wayid}")
228 readwayquery(wayid,true).each {|row|
229 points<<[long2coord(row['longitude'].to_f,baselong,masterscale),lat2coord(row['latitude'].to_f,basey,masterscale),row['id'].to_i,nil,tag2array(row['tags'])]
230 xmin = [xmin,row['longitude'].to_f].min
231 xmax = [xmax,row['longitude'].to_f].max
232 ymin = [ymin,row['latitude'].to_f].min
233 ymax = [ymax,row['latitude'].to_f].max
237 attrlist=ActiveRecord::Base.connection.select_all "SELECT k,v FROM current_way_tags WHERE id=#{wayid}"
238 attrlist.each {|a| attributes[a['k'].gsub(':','|')]=a['v'] }
240 [objname,points,attributes,xmin,xmax,ymin,ymax]
243 # ----- getway_old (objectname, way, version, baselong, basey, masterscale)
244 # returns old version of way
247 RAILS_DEFAULT_LOGGER.info(" Message: getway_old (server is #{SERVER_URL})")
248 # if SERVER_URL=="www.openstreetmap.org" then return -1,"Revert is not currently enabled on the OpenStreetMap server." end
250 objname,wayid,version,baselong,basey,masterscale=args
252 version = version.to_i
254 xmax = ymax = -999999
258 version=getlastversion(wayid,version)
262 readwayquery_old(wayid,version,historic).each { |row|
263 points<<[long2coord(row['longitude'].to_f,baselong,masterscale),lat2coord(row['latitude'].to_f,basey,masterscale),row['id'].to_i,row['visible'].to_i,tag2array(row['tags'].to_s)]
264 xmin=[xmin,row['longitude'].to_f].min
265 xmax=[xmax,row['longitude'].to_f].max
266 ymin=[ymin,row['latitude' ].to_f].min
267 ymax=[ymax,row['latitude' ].to_f].max
270 # get tags from this version
272 attrlist=ActiveRecord::Base.connection.select_all "SELECT k,v FROM way_tags WHERE id=#{wayid} AND version=#{version}"
273 attrlist.each {|a| attributes[a['k'].gsub(':','|')]=a['v'] }
274 attributes['history']="Retrieved from v"+version.to_s
276 [0,objname,points,attributes,xmin,xmax,ymin,ymax,version]
279 # ----- getway_history (way)
280 # returns array of previous versions (version,timestamp,visible,user)
281 # should also show 'created_by'
283 def getway_history(wayid)
286 SELECT version,timestamp,visible,display_name,data_public
288 WHERE ways.id=#{wayid}
289 AND ways.user_id=users.id
291 ORDER BY version DESC
293 histlist=ActiveRecord::Base.connection.select_all(sql)
294 histlist.each { |row|
295 if row['data_public'].to_i==1 then user=row['display_name'] else user='anonymous' end
296 history<<[row['version'],row['timestamp'],row['visible'],user]
301 # ----- putway (user token, way, array of co-ordinates, array of attributes,
302 # baselong, basey, masterscale)
303 # returns current way ID, new way ID, hash of renumbered nodes,
304 # xmin,xmax,ymin,ymax
306 def putway(args,renumberednodes)
307 RAILS_DEFAULT_LOGGER.info(" putway started")
308 usertoken,originalway,points,attributes,oldversion,baselong,basey,masterscale=args
309 uid=getuserid(usertoken)
310 if !uid then return -1,"You are not logged in, so the way could not be saved." end
312 RAILS_DEFAULT_LOGGER.info(" putway authenticated happily")
313 db_uqn='unin'+(rand*100).to_i.to_s+uid.to_s+originalway.to_i.abs.to_s+Time.new.to_i.to_s # temp uniquenodes table name, typically 51 chars
314 db_now='@now'+(rand*100).to_i.to_s+uid.to_s+originalway.to_i.abs.to_s+Time.new.to_i.to_s # 'now' variable name, typically 51 chars
315 ActiveRecord::Base.connection.execute("SET #{db_now}=NOW()")
316 originalway=originalway.to_i
317 oldversion=oldversion.to_i
319 RAILS_DEFAULT_LOGGER.info(" Message: putway, id=#{originalway}")
321 # -- Temporary check for null IDs
324 if a[2]==0 or a[2].nil? then return -2,"Server error - node with id 0 found in way #{originalway}." end
327 # -- 3. read original way into memory
329 xc={}; yc={}; tagc={}; vc={}
332 if oldversion==0 then r=readwayquery(way,false)
333 else r=readwayquery_old(way,oldversion,true) end
337 xc[id]=row['longitude'].to_f
338 yc[id]=row['latitude' ].to_f
340 vc[id]=row['visible'].to_i
343 ActiveRecord::Base.connection.update("UPDATE current_ways SET timestamp=#{db_now},user_id=#{uid},visible=1 WHERE id=#{way}")
345 way=ActiveRecord::Base.connection.insert("INSERT INTO current_ways (user_id,timestamp,visible) VALUES (#{uid},#{db_now},1)")
348 # -- 4. get version by inserting new row into ways
350 version=ActiveRecord::Base.connection.insert("INSERT INTO ways (id,user_id,timestamp,visible) VALUES (#{way},#{uid},#{db_now},1)")
352 # -- 5. compare nodes and update xmin,xmax,ymin,ymax
359 points.each_index do |i|
360 xs=coord2long(points[i][0],masterscale,baselong)
361 ys=coord2lat(points[i][1],masterscale,basey)
362 xmin=[xs,xmin].min; xmax=[xs,xmax].max
363 ymin=[ys,ymin].min; ymax=[ys,ymax].max
364 node=points[i][2].to_i
365 tagstr=array2tag(points[i][4])
366 tagsql="'"+sqlescape(tagstr)+"'"
367 lat=(ys * 10000000).round
368 long=(xs * 10000000).round
369 tile=QuadTile.tile_for_point(ys, xs)
374 if renumberednodes[node.to_s].nil?
375 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})")
376 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})")
378 nodelist.push(newnode)
379 renumberednodes[node.to_s]=newnode.to_s
381 points[i][2]=renumberednodes[node.to_s].to_i
384 elsif xc.has_key?(node)
386 # old node from original way - update
387 if ((xs/0.0000001).round!=(xc[node]/0.0000001).round or (ys/0.0000001).round!=(yc[node]/0.0000001).round or tagstr!=tagc[node] or vc[node]==0)
388 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})")
389 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}")
392 # old node, created in another way and now added to this way
397 # -- 6a. delete any nodes not in modified way
399 createuniquenodes(way,db_uqn,nodelist) # nodes which appear in this way but no other
402 INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible,tile)
403 SELECT DISTINCT cn.id,cn.latitude,cn.longitude,#{db_now},#{uid},0,cn.tile
404 FROM current_nodes AS cn,#{db_uqn}
407 ActiveRecord::Base.connection.insert(sql)
410 UPDATE current_nodes AS cn, #{db_uqn}
411 SET cn.timestamp=#{db_now},cn.visible=0,cn.user_id=#{uid}
414 ActiveRecord::Base.connection.update(sql)
416 deleteuniquenoderelations(db_uqn,uid,db_now)
417 ActiveRecord::Base.connection.execute("DROP TEMPORARY TABLE #{db_uqn}")
419 # 6b. insert new version of route into way_nodes
425 if insertsql !='' then insertsql +=',' end
426 if currentsql!='' then currentsql+=',' end
427 insertsql +="(#{way},#{p[2]},#{sequence},#{version})"
428 currentsql+="(#{way},#{p[2]},#{sequence})"
432 ActiveRecord::Base.connection.execute("DELETE FROM current_way_nodes WHERE id=#{way}");
433 ActiveRecord::Base.connection.insert( "INSERT INTO way_nodes (id,node_id,sequence_id,version) VALUES #{insertsql}");
434 ActiveRecord::Base.connection.insert( "INSERT INTO current_way_nodes (id,node_id,sequence_id ) VALUES #{currentsql}");
436 # -- 7. insert new way tags
440 attributes.each do |k,v|
441 if v=='' or v.nil? then next end
442 if v[0,6]=='(type ' then next end
443 if insertsql !='' then insertsql +=',' end
444 if currentsql!='' then currentsql+=',' end
445 insertsql +="(#{way},'"+sqlescape(k.gsub('|',':'))+"','"+sqlescape(v)+"',#{version})"
446 currentsql+="(#{way},'"+sqlescape(k.gsub('|',':'))+"','"+sqlescape(v)+"')"
449 ActiveRecord::Base.connection.execute("DELETE FROM current_way_tags WHERE id=#{way}")
450 if (insertsql !='') then ActiveRecord::Base.connection.insert("INSERT INTO way_tags (id,k,v,version) VALUES #{insertsql}" ) end
451 if (currentsql!='') then ActiveRecord::Base.connection.insert("INSERT INTO current_way_tags (id,k,v) VALUES #{currentsql}") end
453 [0,originalway,way,renumberednodes,xmin,xmax,ymin,ymax]
456 # ----- putpoi (user token, id, x,y,tag array,visible,baselong,basey,masterscale)
457 # returns current id, new id
458 # if new: add new row to current_nodes and nodes
459 # if old: add new row to nodes, update current_nodes
462 usertoken,id,x,y,tags,visible,baselong,basey,masterscale=args
463 uid=getuserid(usertoken)
464 if !uid then return -1,"You are not logged in, so the point could not be saved." end
466 db_now='@now'+(rand*100).to_i.to_s+uid.to_s+id.to_i.abs.to_s+Time.new.to_i.to_s # 'now' variable name, typically 51 chars
467 ActiveRecord::Base.connection.execute("SET #{db_now}=NOW()")
472 # if deleting, check node hasn't become part of a way
473 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")
474 unless inway.nil? then return -1,"The point has since become part of a way, so you cannot save it as a POI." end
475 deleteitemrelations(id,'node',uid,db_now)
478 x=coord2long(x.to_f,masterscale,baselong)
479 y=coord2lat(y.to_f,masterscale,basey)
480 tagsql="'"+sqlescape(array2tag(tags))+"'"
481 lat=(y * 10000000).round
482 long=(x * 10000000).round
483 tile=QuadTile.tile_for_point(y, x)
486 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})");
487 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}");
490 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})");
491 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})");
496 # ----- getpoi (id,baselong,basey,masterscale)
497 # returns id,x,y,tag array
500 id,baselong,basey,masterscale=args; id=id.to_i
501 poi=ActiveRecord::Base.connection.select_one("SELECT latitude*0.0000001 AS lat,longitude*0.0000001 AS lng,tags "+
502 "FROM current_nodes WHERE visible=1 AND id=#{id}")
503 if poi.nil? then return [nil,nil,nil,''] end
505 long2coord(poi['lng'].to_f,baselong,masterscale),
506 lat2coord(poi['lat'].to_f,basey,masterscale),
507 tag2array(poi['tags'])]
510 # ----- deleteway (user token, way, nodes to keep)
511 # returns way ID only
516 RAILS_DEFAULT_LOGGER.info(" Message: deleteway, id=#{way}")
517 uid=getuserid(usertoken)
518 if !uid then return -1,"You are not logged in, so the way could not be deleted." end
521 db_uqn='unin'+(rand*100).to_i.to_s+uid.to_s+way.to_i.abs.to_s+Time.new.to_i.to_s # temp uniquenodes table name, typically 51 chars
522 db_now='@now'+(rand*100).to_i.to_s+uid.to_s+way.to_i.abs.to_s+Time.new.to_i.to_s # 'now' variable name, typically 51 chars
523 ActiveRecord::Base.connection.execute("SET #{db_now}=NOW()")
525 # - delete any otherwise unused nodes
527 createuniquenodes(way,db_uqn,[])
529 # unless (preserve.empty?) then
530 # ActiveRecord::Base.connection.execute("DELETE FROM #{db_uqn} WHERE node_id IN ("+preserve.join(',')+")")
534 INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible,tile)
535 SELECT DISTINCT cn.id,cn.latitude,cn.longitude,#{db_now},#{uid},0,cn.tile
536 FROM current_nodes AS cn,#{db_uqn}
539 ActiveRecord::Base.connection.insert(sql)
542 UPDATE current_nodes AS cn, #{db_uqn}
543 SET cn.timestamp=#{db_now},cn.visible=0,cn.user_id=#{uid}
546 ActiveRecord::Base.connection.update(sql)
548 deleteuniquenoderelations(db_uqn,uid,db_now)
549 ActiveRecord::Base.connection.execute("DROP TEMPORARY TABLE #{db_uqn}")
553 ActiveRecord::Base.connection.insert("INSERT INTO ways (id,user_id,timestamp,visible) VALUES (#{way},#{uid},#{db_now},0)")
554 ActiveRecord::Base.connection.update("UPDATE current_ways SET user_id=#{uid},timestamp=#{db_now},visible=0 WHERE id=#{way}")
555 ActiveRecord::Base.connection.execute("DELETE FROM current_way_nodes WHERE id=#{way}")
556 ActiveRecord::Base.connection.execute("DELETE FROM current_way_tags WHERE id=#{way}")
557 deleteitemrelations(way,'way',uid,db_now)
563 # ====================================================================
564 # Support functions for remote calls
566 def readwayquery(id,insistonvisible)
568 SELECT latitude*0.0000001 AS latitude,longitude*0.0000001 AS longitude,current_nodes.id,tags,visible
569 FROM current_way_nodes,current_nodes
570 WHERE current_way_nodes.id=#{id}
571 AND current_way_nodes.node_id=current_nodes.id
573 if insistonvisible then sql+=" AND current_nodes.visible=1 " end
574 sql+=" ORDER BY sequence_id"
575 ActiveRecord::Base.connection.select_all(sql)
578 def getlastversion(id,version)
579 row=ActiveRecord::Base.connection.select_one("SELECT version FROM ways WHERE id=#{id} AND visible=1 ORDER BY version DESC LIMIT 1")
583 def readwayquery_old(id,version,historic)
584 # Node handling on undelete (historic=false):
585 # - always use the node specified, even if it's moved
587 # Node handling on revert (historic=true):
588 # - if it's a visible node, use a new node id (i.e. not mucking up the old one)
589 # which means the SWF needs to allocate new ids
590 # - if it's an invisible node, we can reuse the old node id
592 # get node list from specified version of way,
593 # and the _current_ lat/long/tags of each node
595 row=ActiveRecord::Base.connection.select_one("SELECT timestamp FROM ways WHERE version=#{version} AND id=#{id}")
596 waytime=row['timestamp']
599 SELECT cn.id,visible,latitude*0.0000001 AS latitude,longitude*0.0000001 AS longitude,tags
600 FROM way_nodes wn,current_nodes cn
601 WHERE wn.version=#{version}
606 rows=ActiveRecord::Base.connection.select_all(sql)
608 # if historic (full revert), get the old version of each node
609 # - if it's in another way now, generate a new id
610 # - if it's not in another way, use the old ID
612 rows.each_index do |i|
614 SELECT latitude*0.0000001 AS latitude,longitude*0.0000001 AS longitude,tags,cwn.id AS currentway
616 LEFT JOIN current_way_nodes cwn
618 WHERE n.id=#{rows[i]['id']}
619 AND n.timestamp<="#{waytime}"
621 ORDER BY n.timestamp DESC
624 row=ActiveRecord::Base.connection.select_one(sql)
626 nx=row['longitude'].to_f
627 ny=row['latitude'].to_f
628 if (row['currentway'] && (nx!=rows[i]['longitude'].to_f or ny!=rows[i]['latitude'].to_f or row['tags']!=rows[i]['tags'])) then rows[i]['id']=-1 end
629 rows[i]['longitude']=nx
630 rows[i]['latitude' ]=ny
631 rows[i]['tags' ]=row['tags']
638 def createuniquenodes(way,uqn_name,nodelist)
639 # Find nodes which appear in this way but no others
641 CREATE TEMPORARY TABLE #{uqn_name}
643 FROM (SELECT DISTINCT node_id FROM current_way_nodes
645 LEFT JOIN current_way_nodes b
646 ON b.node_id=a.node_id
648 WHERE b.node_id IS NULL
650 unless nodelist.empty? then
651 sql+="AND a.node_id NOT IN ("+nodelist.join(',')+")"
653 ActiveRecord::Base.connection.execute(sql)
658 # ====================================================================
660 # deleteuniquenoderelations(uqn_name,uid,db_now)
661 # deleteitemrelations(way|node,'way'|'node',uid,db_now)
663 def deleteuniquenoderelations(uqn_name,uid,db_now)
665 SELECT node_id,cr.id FROM #{uqn_name},current_relation_members crm,current_relations cr
666 WHERE crm.member_id=node_id
667 AND crm.member_type='node'
672 relnodes=ActiveRecord::Base.connection.select_all(sql)
674 removefromrelation(a['node_id'],'node',a['id'],uid,db_now)
678 def deleteitemrelations(objid,type,uid,db_now)
680 SELECT cr.id FROM current_relation_members crm,current_relations cr
681 WHERE crm.member_id=#{objid}
682 AND crm.member_type='#{type}'
687 relways=ActiveRecord::Base.connection.select_all(sql)
689 removefromrelation(objid,type,a['id'],uid,db_now)
693 def removefromrelation(objid,type,relation,uid,db_now)
694 rver=ActiveRecord::Base.connection.insert("INSERT INTO relations (id,user_id,timestamp,visible) VALUES (#{relation},#{uid},#{db_now},1)")
697 INSERT INTO relation_tags (id,k,v,version)
698 SELECT id,k,v,#{rver} FROM current_relation_tags
701 ActiveRecord::Base.connection.insert(tagsql)
704 INSERT INTO relation_members (id,member_type,member_id,member_role,version)
705 SELECT id,member_type,member_id,member_role,#{rver} FROM current_relation_members
707 AND (member_id!=#{objid} OR member_type!='#{type}')
709 ActiveRecord::Base.connection.insert(membersql)
711 ActiveRecord::Base.connection.update("UPDATE current_relations SET user_id=#{uid},timestamp=#{db_now} WHERE id=#{relation}")
712 ActiveRecord::Base.connection.execute("DELETE FROM current_relation_members WHERE id=#{relation} AND member_type='#{type}' AND member_id=#{objid}")
717 a.gsub(/[\000-\037]/,"").gsub("'","''").gsub(92.chr) {92.chr+92.chr}
722 Tags.split(a) do |k, v|
723 tags[k.gsub(':','|')]=v
731 if v=='' then next end
732 if v[0,6]=='(type ' then next end
733 tags << [k.gsub('|',':'), v]
735 return Tags.join(tags)
739 if (token =~ /^(.+)\+(.+)$/) then
740 user = User.authenticate(:username => $1, :password => $2)
742 user = User.authenticate(:token => token)
745 return user ? user.id : nil;
750 # ====================================================================
751 # AMF read subroutines
753 # ----- getint return two-byte integer
754 # ----- getlong return four-byte long
755 # ----- getstring return string with two-byte length
756 # ----- getdouble return eight-byte double-precision float
757 # ----- getobject return object/hash
758 # ----- getarray return numeric array
765 ((s.getc*256+s.getc)*256+s.getc)*256+s.getc
769 len=s.getc*256+s.getc
774 a=s.read(8).unpack('G') # G big-endian, E little-endian
789 while (key=getstring(s))
790 if (key=='') then break end
793 s.getc # skip the 9 'end of object' value
797 # ----- getvalue parse and get value
801 when 0; return getdouble(s) # number
802 when 1; return s.getc # boolean
803 when 2; return getstring(s) # string
804 when 3; return getobject(s) # object/hash
805 when 5; return nil # null
806 when 6; return nil # undefined
807 when 8; s.read(4) # mixedArray
808 return getobject(s) # |
809 when 10;return getarray(s) # array
810 else; return nil # error
814 # ====================================================================
815 # AMF write subroutines
817 # ----- putdata envelope data into AMF writeable form
818 # ----- encodevalue pack variables as AMF
821 d =encodestring(index+"/onResult")
822 d+=encodestring("null")
830 a=10.chr+encodelong(n.length)
838 a+=encodestring(k)+encodevalue(v)
842 2.chr+encodestring(n)
843 when 'Bignum','Fixnum','Float'
844 0.chr+encodedouble(n)
848 RAILS_DEFAULT_LOGGER.error("Unexpected Ruby type for AMF conversion: "+n.class.to_s)
852 # ----- encodestring encode string with two-byte length
853 # ----- encodedouble encode number as eight-byte double precision float
854 # ----- encodelong encode number as four-byte long
857 a,b=n.size.divmod(256)
869 # ====================================================================
870 # Co-ordinate conversion
872 def lat2coord(a,basey,masterscale)
873 -(lat2y(a)-basey)*masterscale+250
876 def long2coord(a,baselong,masterscale)
877 (a-baselong)*masterscale+350
881 180/Math::PI * Math.log(Math.tan(Math::PI/4+a*(Math::PI/180)/2))
884 def coord2lat(a,masterscale,basey)
885 y2lat((a-250)/-masterscale+basey)
888 def coord2long(a,masterscale,baselong)
889 (a-350)/masterscale+baselong
893 180/Math::PI * (2*Math.atan(Math.exp(a*Math::PI/180))-Math::PI/2)