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 'whichways_deleted'; results[index]=putdata(index,whichways_deleted(args))
44 when 'getway'; results[index]=putdata(index,getway(args))
45 when 'getway_old'; results[index]=putdata(index,getway_old(args))
46 when 'getway_history'; results[index]=putdata(index,getway_history(args))
47 when 'putway'; results[index]=putdata(index,putway(args))
48 when 'deleteway'; results[index]=putdata(index,deleteway(args))
49 when 'putpoi'; results[index]=putdata(index,putpoi(args))
50 when 'getpoi'; results[index]=putdata(index,getpoi(args))
57 RAILS_DEFAULT_LOGGER.info(" Response: start")
58 a,b=results.length.divmod(256)
59 render :content_type => "application/x-amf", :text => proc { |response, output|
60 output.write 0.chr+0.chr+0.chr+0.chr+a.chr+b.chr
65 RAILS_DEFAULT_LOGGER.info(" Response: end")
71 # ====================================================================
75 # return presets,presetmenus and presetnames arrays
79 presetmenus={}; presetmenus['point']=[]; presetmenus['way']=[]; presetmenus['POI']=[]
80 presetnames={}; presetnames['point']={}; presetnames['way']={}; presetnames['POI']={}
84 RAILS_DEFAULT_LOGGER.info(" Message: getpresets")
86 # File.open("config/potlatch/presets.txt") do |file|
88 # Temporary patch to get around filepath problem
89 # To remove this patch and make the code nice again:
90 # 1. uncomment above line
91 # 2. fix the path in the above line
92 # 3. delete this here document, and the following line (StringIO....)
96 motorway: highway=motorway,ref=(type road number)
97 trunk road: highway=trunk,ref=(type road number),name=(type road name)
98 primary road: highway=primary,ref=(type road number),name=(type road name)
99 secondary road: highway=secondary,ref=(type road number),name=(type road name)
100 tertiary road: highway=tertiary,ref=,name=(type road name)
101 residential road: highway=residential,ref=,name=(type road name)
102 unclassified road: highway=unclassified,ref=,name=(type road name)
105 footpath: highway=footway,foot=yes
106 bridleway: highway=bridleway,foot=yes
107 byway: highway=unsurfaced,foot=yes
108 permissive path: highway=footway,foot=permissive
111 cycle lane: highway=cycleway,cycleway=lane,ncn_ref=
112 cycle track: highway=cycleway,cycleway=track,ncn_ref=
113 cycle lane (NCN): highway=cycleway,cycleway=lane,name=(type name here),ncn_ref=(type route number)
114 cycle track (NCN): highway=cycleway,cycleway=track,name=(type name here),ncn_ref=(type route number)
117 canal: waterway=canal,name=(type name here)
118 navigable river: waterway=river,boat=yes,name=(type name here)
119 navigable drain: waterway=drain,boat=yes,name=(type name here)
120 derelict canal: waterway=derelict_canal,name=(type name here)
121 unnavigable river: waterway=river,boat=no,name=(type name here)
122 unnavigable drain: waterway=drain,boat=no,name=(type name here)
125 railway: railway=rail
126 tramway: railway=tram
127 light railway: railway=light_rail
128 preserved railway: railway=preserved
129 disused railway tracks: railway=disused
130 course of old railway: railway=abandoned
133 lake: natural=water,landuse=
134 forest: landuse=forest,natural=
137 mini roundabout: highway=mini_roundabout
138 traffic lights: highway=traffic_signals
141 bridge: highway=bridge
144 cattle grid: highway=cattle_grid
150 lock gate: waterway=lock_gate
152 aqueduct: waterway=aqueduct
153 winding hole: waterway=turning_point
154 mooring: waterway=mooring
157 station: railway=station
158 viaduct: railway=viaduct
159 level crossing: railway=crossing
165 car park: amenity=parking
166 petrol station: amenity=fuel
169 bike park: amenity=bicycle_parking
172 city: place=city,name=(type name here),is_in=(type region or county)
173 town: place=town,name=(type name here),is_in=(type region or county)
174 suburb: place=suburb,name=(type name here),is_in=(type region or county)
175 village: place=village,name=(type name here),is_in=(type region or county)
176 hamlet: place=hamlet,name=(type name here),is_in=(type region or county)
179 attraction: tourism=attraction,amenity=,religion=,denomination=
180 church: tourism=,amenity=place_of_worship,name=(type name here),religion=christian,denomination=(type denomination here)
181 hotel: tourism=hotel,amenity=,religion=,denomination=
182 other religious: tourism=,amenity=place_of_worship,name=(type name here),religion=(type religion),denomination=
183 post box: amenity=post_box,tourism=,name=,religion=,denomination=
184 post office: amenity=post_office,tourism=,name=,religion=,denomination=
185 pub: tourism=,amenity=pub,name=(type name here),religion=,denomination=
191 StringIO.open(txt) do |file|
192 file.each_line {|line|
194 if (t=~/(\w+)\/(\w+)/) then
197 presetmenus[presettype].push(presetcategory)
198 presetnames[presettype][presetcategory]=["(no preset)"]
199 elsif (t=~/^(.+):\s?(.+)$/) then
201 presetnames[presettype][presetcategory].push(pre)
203 kv.split(',').each {|a|
204 if (a=~/^(.+)=(.*)$/) then presets[pre][$1]=$2 end
209 return [presets,presetmenus,presetnames]
212 # ----- whichways(left,bottom,right,top)
213 # return array of ways in current bounding box
214 # at present, instead of using correct (=more complex) SQL to find
215 # corner-crossing ways, it simply enlarges the bounding box by +/- 0.01
218 xmin = args[0].to_f-0.01
219 ymin = args[1].to_f-0.01
220 xmax = args[2].to_f+0.01
221 ymax = args[3].to_f+0.01
224 masterscale = args[6]
226 RAILS_DEFAULT_LOGGER.info(" Message: whichways, bbox=#{xmin},#{ymin},#{xmax},#{ymax}")
228 waylist = ActiveRecord::Base.connection.select_all("SELECT DISTINCT current_way_nodes.id AS wayid"+
229 " FROM current_way_nodes,current_nodes,current_ways "+
230 " WHERE current_nodes.id=current_way_nodes.node_id "+
231 " AND current_nodes.visible=1 "+
232 " AND current_ways.id=current_way_nodes.id "+
233 " AND current_ways.visible=1 "+
234 " AND "+OSM.sql_for_area(ymin, xmin, ymax, xmax, "current_nodes."))
236 ways = waylist.collect {|a| a['wayid'].to_i } # get an array of way IDs
238 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 "+
239 " FROM current_nodes "+
240 " LEFT OUTER JOIN current_way_nodes cwn ON cwn.node_id=current_nodes.id "+
241 " WHERE "+OSM.sql_for_area(ymin, xmin, ymax, xmax, "current_nodes.")+
242 " AND cwn.id IS NULL "+
243 " AND current_nodes.visible=1")
245 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
250 # ----- whichways_deleted(left,bottom,right,top)
251 # return array of deleted ways in current bounding box
253 def whichways_deleted(args)
254 xmin = args[0].to_f-0.01
255 ymin = args[1].to_f-0.01
256 xmax = args[2].to_f+0.01
257 ymax = args[3].to_f+0.01
260 masterscale = args[6]
262 waylist = ActiveRecord::Base.connection.select_all("SELECT DISTINCT current_way_nodes.id AS wayid"+
263 " FROM current_way_nodes,current_nodes,current_ways "+
264 " WHERE current_nodes.id=current_way_nodes.node_id "+
265 " AND current_ways.id=current_way_nodes.id "+
266 " AND current_ways.visible=0 "+
267 " AND "+OSM.sql_for_area(ymin, xmin, ymax, xmax, "current_nodes."))
269 ways = waylist.collect {|a| a['wayid'].to_i } # get an array of way IDs
273 # ----- getway (objectname, way, baselong, basey, masterscale)
274 # returns objectname, array of co-ordinates, attributes,
275 # xmin,xmax,ymin,ymax
278 objname,wayid,baselong,basey,masterscale=args
282 xmax = ymax = -999999
284 RAILS_DEFAULT_LOGGER.info(" Message: getway, id=#{wayid}")
286 readwayquery(wayid).each {|row|
287 points<<[long2coord(row['longitude'].to_f,baselong,masterscale),lat2coord(row['latitude'].to_f,basey,masterscale),row['id'].to_i,nil,tag2array(row['tags'])]
288 xmin = [xmin,row['longitude'].to_f].min
289 xmax = [xmax,row['longitude'].to_f].max
290 ymin = [ymin,row['latitude'].to_f].min
291 ymax = [ymax,row['latitude'].to_f].max
295 attrlist=ActiveRecord::Base.connection.select_all "SELECT k,v FROM current_way_tags WHERE id=#{wayid}"
296 attrlist.each {|a| attributes[a['k'].gsub(':','|')]=a['v'] }
298 [objname,points,attributes,xmin,xmax,ymin,ymax]
301 # ----- getway_old (objectname, way, version, baselong, basey, masterscale)
302 # returns old version of way
304 # Node handling on undelete (historic=false):
305 # - always use the node specified, even if it's moved
307 # Node handling on revert (historic=true):
308 # - if it's a visible node, use a new node id (i.e. not mucking up the old one)
309 # which means the SWF needs to allocate new ids
310 # - if it's an invisible node, we can reuse the old node id
313 objname,wayid,version,baselong,basey,masterscale=args
316 xmax = ymax = -999999
319 # get version (if -1) and timestamp
322 row=ActiveRecord::Base.connection.select_one("SELECT version FROM ways WHERE way=#{wayid} ORDER BY version DESC LIMIT 1")
323 version=row['version']
327 row=ActiveRecord::Base.connection.select_one("SELECT timestamp FROM ways WHERE version=#{version} AND way=#{wayid}")
328 timestamp=row['timestamp']
330 # get node list from this version
332 SELECT cn.id,visible,latitude*0.0000001 AS latitude,longitude*0.0000001 AS longitude,tags
333 FROM way_nodes wn,current_nodes cn
334 WHERE wn.version=#{version}
339 ActiveRecord::Base.connection.select_all(sql).each {|row|
340 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'])]
341 xmin=[xmin,row['longitude'].to_f].min
342 xmax=[xmax,row['longitude'].to_f].max
343 ymin=[ymin,row['latitude'].to_f].min
344 ymax=[ymax,row['latitude'].to_f].max
347 # if historic (full revert), get the old version of each node
349 for i in (0..points.length-1)
351 SELECT latitude*0.0000001 AS latitude,longitude*0.0000001 AS longitude,tags
353 WHERE id=#{points[i][2]}
354 AND timestamp<=#{waytime}
355 ORDER BY timestamp DESC
358 row=ActiveRecord::Base.connection.select_one(sql)
359 unless row.empty? then
360 points[i][0]=long2coord(row['longitude'].to_f,baselong,masterscale)
361 points[i][1]=lat2coord(row['latitude'].to_f,baselong,masterscale)
362 points[i][4]=tag2array(row['tags'])
367 # get tags from this version
369 attrlist=ActiveRecord::Base.connection.select_all "SELECT k,v FROM current_way_tags WHERE id=#{wayid} AND version=#{version}"
370 attrlist.each {|a| attributes[a['k'].gsub(':','|')]=a['v'] }
372 [objname,points,attributes,xmin,xmax,ymin,ymax]
375 # ----- getway_history (way)
376 # returns array of previous versions (version,timestamp,visible,user)
377 # should also show 'created_by'
379 def getway_history(wayid)
380 RAILS_DEFAULT_LOGGER.info(" Received history request for #{wayid}")
383 SELECT version,timestamp,visible,display_name,data_public
385 WHERE ways.id=#{wayid}
386 AND ways.user_id=users.id
387 ORDER BY version DESC
389 histlist=ActiveRecord::Base.connection.select_all(sql)
390 histlist.each { |row|
391 if row['data_public'] then user=row['display_name'] else user='anonymous' end
392 history<<[row['version'],row['timestamp'],row['visible'],user]
397 # ----- putway (user token, way, array of co-ordinates, array of attributes,
398 # baselong, basey, masterscale)
399 # returns current way ID, new way ID, hash of renumbered nodes,
400 # xmin,xmax,ymin,ymax
403 RAILS_DEFAULT_LOGGER.info(" putway started")
404 usertoken,originalway,points,attributes,baselong,basey,masterscale=args
405 uid=getuserid(usertoken)
407 RAILS_DEFAULT_LOGGER.info(" putway authenticated happily")
408 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
409 db_now='@now'+uid.to_s+originalway.to_i.abs.to_s+Time.new.to_i.to_s # 'now' variable name, typically 51 chars
410 ActiveRecord::Base.connection.execute("SET #{db_now}=NOW()")
411 originalway=originalway.to_i
413 RAILS_DEFAULT_LOGGER.info(" Message: putway, id=#{originalway}")
415 # -- 3. read original way into memory
417 xc={}; yc={}; tagc={}
420 readwayquery(way).each { |row|
422 xc[id]=row['longitude'].to_f
423 yc[id]=row['latitude' ].to_f
426 ActiveRecord::Base.connection.update("UPDATE current_ways SET timestamp=#{db_now},user_id=#{uid},visible=1 WHERE id=#{way}")
428 way=ActiveRecord::Base.connection.insert("INSERT INTO current_ways (user_id,timestamp,visible) VALUES (#{uid},#{db_now},1)")
431 # -- 4. get version by inserting new row into ways
433 version=ActiveRecord::Base.connection.insert("INSERT INTO ways (id,user_id,timestamp,visible) VALUES (#{way},#{uid},#{db_now},1)")
435 # -- 5. compare nodes and update xmin,xmax,ymin,ymax
443 points.each_index do |i|
444 xs=coord2long(points[i][0],masterscale,baselong)
445 ys=coord2lat(points[i][1],masterscale,basey)
446 xmin=[xs,xmin].min; xmax=[xs,xmax].max
447 ymin=[ys,ymin].min; ymax=[ys,ymax].max
448 node=points[i][2].to_i
449 tagstr=array2tag(points[i][4])
450 tagsql="'"+sqlescape(tagstr)+"'"
451 lat=(ys * 10000000).round
452 long=(xs * 10000000).round
453 tile=QuadTile.tile_for_point(ys, xs)
458 if renumberednodes[node.to_s].nil?
459 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})")
460 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})")
462 nodelist.push(newnode)
463 renumberednodes[node.to_s]=newnode.to_s
465 points[i][2]=renumberednodes[node.to_s].to_i
468 elsif xc.has_key?(node)
470 # old node from original way - update
471 if (xs!=xc[node] or (ys/0.0000001).round!=(yc[node]/0.0000001).round or tagstr!=tagc[node])
472 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})")
473 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}")
476 # old node, created in another way and now added to this way
481 # -- 6a. delete any nodes not in modified way
483 createuniquenodes(way,db_uqn,nodelist) # nodes which appear in this way but no other
486 INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible,tile)
487 SELECT DISTINCT cn.id,cn.latitude,cn.longitude,#{db_now},#{uid},0,cn.tile
488 FROM current_nodes AS cn,#{db_uqn}
491 ActiveRecord::Base.connection.insert(sql)
494 UPDATE current_nodes AS cn, #{db_uqn}
495 SET cn.timestamp=#{db_now},cn.visible=0,cn.user_id=#{uid}
498 ActiveRecord::Base.connection.update(sql)
500 deleteuniquenoderelations(db_uqn,uid,db_now)
501 ActiveRecord::Base.connection.execute("DROP TABLE #{db_uqn}")
503 # 6b. insert new version of route into way_nodes
509 if insertsql !='' then insertsql +=',' end
510 if currentsql!='' then currentsql+=',' end
511 insertsql +="(#{way},#{p[2]},#{sequence},#{version})"
512 currentsql+="(#{way},#{p[2]},#{sequence})"
516 ActiveRecord::Base.connection.execute("DELETE FROM current_way_nodes WHERE id=#{way}");
517 ActiveRecord::Base.connection.insert( "INSERT INTO way_nodes (id,node_id,sequence_id,version) VALUES #{insertsql}");
518 ActiveRecord::Base.connection.insert( "INSERT INTO current_way_nodes (id,node_id,sequence_id ) VALUES #{currentsql}");
520 # -- 7. insert new way tags
524 attributes.each do |k,v|
525 if v=='' or v.nil? then next end
526 if v[0,6]=='(type ' then next end
527 if insertsql !='' then insertsql +=',' end
528 if currentsql!='' then currentsql+=',' end
529 insertsql +="(#{way},'"+sqlescape(k.gsub('|',':'))+"','"+sqlescape(v)+"',#{version})"
530 currentsql+="(#{way},'"+sqlescape(k.gsub('|',':'))+"','"+sqlescape(v)+"')"
533 ActiveRecord::Base.connection.execute("DELETE FROM current_way_tags WHERE id=#{way}")
534 if (insertsql !='') then ActiveRecord::Base.connection.insert("INSERT INTO way_tags (id,k,v,version) VALUES #{insertsql}" ) end
535 if (currentsql!='') then ActiveRecord::Base.connection.insert("INSERT INTO current_way_tags (id,k,v) VALUES #{currentsql}") end
537 [originalway,way,renumberednodes,xmin,xmax,ymin,ymax]
540 # ----- putpoi (user token, id, x,y,tag array,visible,baselong,basey,masterscale)
541 # returns current id, new id
542 # if new: add new row to current_nodes and nodes
543 # if old: add new row to nodes, update current_nodes
546 usertoken,id,x,y,tags,visible,baselong,basey,masterscale=args
547 uid=getuserid(usertoken)
549 db_now='@now'+uid.to_s+id.to_i.abs.to_s+Time.new.to_i.to_s # 'now' variable name, typically 51 chars
550 ActiveRecord::Base.connection.execute("SET #{db_now}=NOW()")
555 # if deleting, check node hasn't become part of a way
556 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")
557 unless inway.nil? then return [id,id] end # should really return an error
558 deleteitemrelations(id,'node',uid,db_now)
561 x=coord2long(x.to_f,masterscale,baselong)
562 y=coord2lat(y.to_f,masterscale,basey)
563 tagsql="'"+sqlescape(array2tag(tags))+"'"
564 lat=(y * 10000000).round
565 long=(x * 10000000).round
566 tile=QuadTile.tile_for_point(y, x)
569 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})");
570 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}");
573 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})");
574 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})");
579 # ----- getpoi (id,baselong,basey,masterscale)
580 # returns id,x,y,tag array
583 id,baselong,basey,masterscale=args; id=id.to_i
584 poi=ActiveRecord::Base.connection.select_one("SELECT latitude*0.0000001 AS lat,longitude*0.0000001 AS lng,tags "+
585 "FROM current_nodes WHERE visible=1 AND id=#{id}")
586 if poi.nil? then return [nil,nil,nil,''] end
588 long2coord(poi['lng'].to_f,baselong,masterscale),
589 lat2coord(poi['lat'].to_f,basey,masterscale),
590 tag2array(poi['tags'])]
593 # ----- deleteway (user token, way, nodes to keep)
594 # returns way ID only
597 usertoken,way,preserve=args
599 RAILS_DEFAULT_LOGGER.info(" Message: deleteway, id=#{way}")
601 uid=getuserid(usertoken); if !uid then return end
604 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
605 db_now='@now'+uid.to_s+way.to_i.abs.to_s+Time.new.to_i.to_s # 'now' variable name, typically 51 chars
606 ActiveRecord::Base.connection.execute("SET #{db_now}=NOW()")
608 # - delete any otherwise unused nodes
610 createuniquenodes(way,db_uqn,[])
612 unless (preserve.empty?) then
613 ActiveRecord::Base.connection.execute("DELETE FROM #{db_uqn} WHERE node_id IN ("+preserve.join(',')+")")
617 INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible,tile)
618 SELECT DISTINCT cn.id,cn.latitude,cn.longitude,#{db_now},#{uid},0,cn.tile
619 FROM current_nodes AS cn,#{db_uqn}
622 ActiveRecord::Base.connection.insert(sql)
625 UPDATE current_nodes AS cn, #{db_uqn}
626 SET cn.timestamp=#{db_now},cn.visible=0,cn.user_id=#{uid}
629 ActiveRecord::Base.connection.update(sql)
631 deleteuniquenoderelations(db_uqn,uid,db_now)
632 ActiveRecord::Base.connection.execute("DROP TABLE #{db_uqn}")
636 ActiveRecord::Base.connection.insert("INSERT INTO ways (id,user_id,timestamp,visible) VALUES (#{way},#{uid},#{db_now},0)")
637 ActiveRecord::Base.connection.update("UPDATE current_ways SET user_id=#{uid},timestamp=#{db_now},visible=0 WHERE id=#{way}")
638 ActiveRecord::Base.connection.execute("DELETE FROM current_way_nodes WHERE id=#{way}")
639 ActiveRecord::Base.connection.execute("DELETE FROM current_way_tags WHERE id=#{way}")
640 deleteitemrelations(way,'way',uid,db_now)
646 # ====================================================================
647 # Support functions for remote calls
650 ActiveRecord::Base.connection.select_all "SELECT latitude*0.0000001 AS latitude,longitude*0.0000001 AS longitude,current_nodes.id,tags "+
651 " FROM current_way_nodes,current_nodes "+
652 " WHERE current_way_nodes.id=#{id} "+
653 " AND current_way_nodes.node_id=current_nodes.id "+
654 " AND current_nodes.visible=1 "+
655 " ORDER BY sequence_id"
658 def createuniquenodes(way,uqn_name,nodelist)
659 # Find nodes which appear in this way but no others
661 CREATE TEMPORARY TABLE #{uqn_name}
663 FROM (SELECT DISTINCT node_id FROM current_way_nodes
665 LEFT JOIN current_way_nodes b
666 ON b.node_id=a.node_id
668 WHERE b.node_id IS NULL
670 unless nodelist.empty? then
671 sql+="AND a.node_id NOT IN ("+nodelist.join(',')+")"
673 ActiveRecord::Base.connection.execute(sql)
678 # ====================================================================
680 # deleteuniquenoderelations(uqn_name,uid,db_now)
681 # deleteitemrelations(way|node,'way'|'node',uid,db_now)
683 def deleteuniquenoderelations(uqn_name,uid,db_now)
685 SELECT node_id,cr.id FROM #{uqn_name},current_relation_members crm,current_relations cr
686 WHERE crm.member_id=node_id
687 AND crm.member_type='node'
692 relnodes=ActiveRecord::Base.connection.select_all(sql)
694 removefromrelation(a['node_id'],'node',a['id'],uid,db_now)
698 def deleteitemrelations(objid,type,uid,db_now)
700 SELECT cr.id FROM current_relation_members crm,current_relations cr
701 WHERE crm.member_id=#{objid}
702 AND crm.member_type='#{type}'
707 relways=ActiveRecord::Base.connection.select_all(sql)
709 removefromrelation(objid,type,a['id'],uid,db_now)
713 def removefromrelation(objid,type,relation,uid,db_now)
714 rver=ActiveRecord::Base.connection.insert("INSERT INTO relations (id,user_id,timestamp,visible) VALUES (#{relation},#{uid},#{db_now},1)")
717 INSERT INTO relation_tags (id,k,v,version)
718 SELECT id,k,v,#{rver} FROM current_relation_tags
721 ActiveRecord::Base.connection.insert(tagsql)
724 INSERT INTO relation_members (id,member_type,member_id,member_role,version)
725 SELECT id,member_type,member_id,member_role,#{rver} FROM current_relation_members
727 AND (member_id!=#{objid} OR member_type!='#{type}')
729 ActiveRecord::Base.connection.insert(membersql)
731 ActiveRecord::Base.connection.update("UPDATE current_relations SET user_id=#{uid},timestamp=#{db_now} WHERE id=#{relation}")
732 ActiveRecord::Base.connection.execute("DELETE FROM current_relation_members WHERE id=#{relation} AND member_type='#{type}' AND member_id=#{objid}")
737 a.gsub(/[\000-\037]/,"").gsub("'","''").gsub(92.chr,92.chr+92.chr)
742 a.gsub(';;;','#%').split(';').each do |b|
746 if k.nil? then k='' end
747 if v.nil? then v='' end
748 tags[k.gsub('#%','=').gsub(':','|')]=v.gsub('#%','=')
756 if v=='' then next end
757 if v[0,6]=='(type ' then next end
758 if str!='' then str+=';' end
759 str+=k.gsub(';',';;;').gsub('=','===').gsub('|',':')+'='+v.gsub(';',';;;').gsub('=','===')
765 if (token =~ /^(.+)\+(.+)$/) then
766 user = User.authenticate(:username => $1, :password => $2)
768 user = User.authenticate(:token => token)
771 return user ? user.id : nil;
776 # ====================================================================
777 # AMF read subroutines
779 # ----- getint return two-byte integer
780 # ----- getlong return four-byte long
781 # ----- getstring return string with two-byte length
782 # ----- getdouble return eight-byte double-precision float
783 # ----- getobject return object/hash
784 # ----- getarray return numeric array
791 ((s.getc*256+s.getc)*256+s.getc)*256+s.getc
795 len=s.getc*256+s.getc
800 a=s.read(8).unpack('G') # G big-endian, E little-endian
815 while (key=getstring(s))
816 if (key=='') then break end
819 s.getc # skip the 9 'end of object' value
823 # ----- getvalue parse and get value
827 when 0; return getdouble(s) # number
828 when 1; return s.getc # boolean
829 when 2; return getstring(s) # string
830 when 3; return getobject(s) # object/hash
831 when 5; return nil # null
832 when 6; return nil # undefined
833 when 8; s.read(4) # mixedArray
834 return getobject(s) # |
835 when 10;return getarray(s) # array
836 else; return nil # error
840 # ====================================================================
841 # AMF write subroutines
843 # ----- putdata envelope data into AMF writeable form
844 # ----- encodevalue pack variables as AMF
847 d =encodestring(index+"/onResult")
848 d+=encodestring("null")
856 a=10.chr+encodelong(n.length)
864 a+=encodestring(k)+encodevalue(v)
868 2.chr+encodestring(n)
869 when 'Bignum','Fixnum','Float'
870 0.chr+encodedouble(n)
874 RAILS_DEFAULT_LOGGER.error("Unexpected Ruby type for AMF conversion: "+n.class.to_s)
878 # ----- encodestring encode string with two-byte length
879 # ----- encodedouble encode number as eight-byte double precision float
880 # ----- encodelong encode number as four-byte long
883 a,b=n.size.divmod(256)
895 # ====================================================================
896 # Co-ordinate conversion
898 def lat2coord(a,basey,masterscale)
899 -(lat2y(a)-basey)*masterscale+250
902 def long2coord(a,baselong,masterscale)
903 (a-baselong)*masterscale+350
907 180/Math::PI * Math.log(Math.tan(Math::PI/4+a*(Math::PI/180)/2))
910 def coord2lat(a,masterscale,basey)
911 y2lat((a-250)/-masterscale+basey)
914 def coord2long(a,masterscale,baselong)
915 (a-350)/masterscale+baselong
919 180/Math::PI * (2*Math.atan(Math.exp(a*Math::PI/180))-Math::PI/2)