9c8d4cc6a3d5607245a1d39523b86d091c387c81
[rails.git] / app / controllers / amf_controller.rb
1 class AmfController < ApplicationController
2   require 'stringio'
3
4   session :off
5   before_filter :check_write_availability
6
7   # to log:
8   # RAILS_DEFAULT_LOGGER.error("Args: #{args[0]}, #{args[1]}, #{args[2]}, #{args[3]}")
9
10   # ====================================================================
11   # Main AMF handler
12
13   # ---- talk   process AMF request
14
15   def talk
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
20
21     # -------------
22     # Parse request
23
24     headers=getint(req)                                 # Read number of headers
25
26     headers.times do                                # Read each header
27       name=getstring(req)                               #  |
28       req.getc                                  #  | skip boolean
29       value=getvalue(req)                               #  |
30       header["name"]=value                              #  |
31     end
32
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)
39
40       case message
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))
51       end
52     end
53
54     # ------------------
55     # Write out response
56
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
61                 results.each do |k,v|
62                   output.write(v)
63                 end
64         }
65     RAILS_DEFAULT_LOGGER.info("  Response: end")
66
67   end
68
69   private
70
71   # ====================================================================
72   # Remote calls
73
74   # ----- getpresets
75   #           return presets,presetmenus and presetnames arrays
76
77   def getpresets
78     presets={}
79     presetmenus={}; presetmenus['point']=[]; presetmenus['way']=[]; presetmenus['POI']=[]
80     presetnames={}; presetnames['point']={}; presetnames['way']={}; presetnames['POI']={}
81     presettype=''
82     presetcategory=''
83
84     RAILS_DEFAULT_LOGGER.info("  Message: getpresets")
85
86     #           File.open("config/potlatch/presets.txt") do |file|
87
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....)
93
94     txt=<<-EOF
95 way/road
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)
103
104 way/footway
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
109
110 way/cycleway
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)
115
116 way/waterway
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)
123
124 way/railway
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
131
132 way/natural
133 lake: natural=water,landuse=
134 forest: landuse=forest,natural=
135
136 point/road
137 mini roundabout: highway=mini_roundabout
138 traffic lights: highway=traffic_signals
139
140 point/footway
141 bridge: highway=bridge
142 gate: highway=gate
143 stile: highway=stile
144 cattle grid: highway=cattle_grid
145
146 point/cycleway
147 gate: highway=gate
148
149 point/waterway
150 lock gate: waterway=lock_gate
151 weir: waterway=weir
152 aqueduct: waterway=aqueduct
153 winding hole: waterway=turning_point
154 mooring: waterway=mooring
155
156 point/railway
157 station: railway=station
158 viaduct: railway=viaduct
159 level crossing: railway=crossing
160
161 point/natural
162 peak: natural=peak
163
164 POI/road
165 car park: amenity=parking
166 petrol station: amenity=fuel
167
168 POI/cycleway
169 bike park: amenity=bicycle_parking
170
171 POI/place
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)
177
178 POI/tourism
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=
186
187 POI/natural
188 peak: point=peak
189 EOF
190
191     StringIO.open(txt) do |file|
192       file.each_line {|line|
193         t=line.chomp
194         if (t=~/(\w+)\/(\w+)/) then
195           presettype=$1
196           presetcategory=$2
197           presetmenus[presettype].push(presetcategory)
198           presetnames[presettype][presetcategory]=["(no preset)"]
199         elsif (t=~/^(.+):\s?(.+)$/) then
200           pre=$1; kv=$2
201           presetnames[presettype][presetcategory].push(pre)
202           presets[pre]={}
203           kv.split(',').each {|a|
204             if (a=~/^(.+)=(.*)$/) then presets[pre][$1]=$2 end
205           }
206         end
207       }
208     end
209     return [presets,presetmenus,presetnames]
210   end
211
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
216
217   def whichways(args)
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
222     baselong    = args[4]
223     basey       = args[5]
224     masterscale = args[6]
225
226     RAILS_DEFAULT_LOGGER.info("  Message: whichways, bbox=#{xmin},#{ymin},#{xmax},#{ymax}")
227
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."))
235
236     ways = waylist.collect {|a| a['wayid'].to_i } # get an array of way IDs
237
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")
244
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
246
247     return [ways,points]
248   end
249
250   # ----- whichways_deleted(left,bottom,right,top)
251   #               return array of deleted ways in current bounding box
252   
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
258     baselong    = args[4]
259     basey       = args[5]
260     masterscale = args[6]
261
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."))
268
269     ways = waylist.collect {|a| a['wayid'].to_i } # get an array of way IDs
270         [ways]
271   end
272   
273   # ----- getway (objectname, way, baselong, basey, masterscale)
274   #               returns objectname, array of co-ordinates, attributes,
275   #                               xmin,xmax,ymin,ymax
276
277   def getway(args)
278     objname,wayid,baselong,basey,masterscale=args
279     wayid = wayid.to_i
280     points = []
281     xmin = ymin =  999999
282     xmax = ymax = -999999
283
284     RAILS_DEFAULT_LOGGER.info("  Message: getway, id=#{wayid}")
285
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
292     }
293
294     attributes={}
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'] }
297
298     [objname,points,attributes,xmin,xmax,ymin,ymax]
299   end
300   
301   # -----       getway_old (objectname, way, version, baselong, basey, masterscale)
302   #                     returns old version of way
303
304   # Node handling on undelete (historic=false):
305   # - always use the node specified, even if it's moved
306   
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
311
312   def getway_old(args)
313     objname,wayid,version,baselong,basey,masterscale=args
314     wayid = wayid.to_i
315     xmin = ymin =  999999
316     xmax = ymax = -999999
317         dellist=[]
318
319         # get version (if -1) and timestamp
320         if version<0
321           historic=false
322           row=ActiveRecord::Base.connection.select_one("SELECT version FROM ways WHERE way=#{wayid} ORDER BY version DESC LIMIT 1")
323           version=row['version']
324         else
325           historic=true
326         end
327         row=ActiveRecord::Base.connection.select_one("SELECT timestamp FROM ways WHERE version=#{version} AND way=#{wayid}")
328         timestamp=row['timestamp']
329
330         # get node list from this version
331         sql=<<-EOF
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} 
335            AND wn.id=#{wayid} 
336            AND wn.node_id=cn.id 
337          ORDER BY sequence_id
338         EOF
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
345         }
346
347         # if historic (full revert), get the old version of each node
348         if historic then
349           for i in (0..points.length-1)
350                 sql=<<-EOF
351                 SELECT latitude*0.0000001 AS latitude,longitude*0.0000001 AS longitude,tags 
352                   FROM nodes 
353                  WHERE id=#{points[i][2]} 
354                    AND timestamp<=#{waytime} 
355                  ORDER BY timestamp DESC 
356                  LIMIT 1
357                 EOF
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'])
363                 end
364           end
365         end
366
367         # get tags from this version
368     attributes={}
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'] }
371
372     [objname,points,attributes,xmin,xmax,ymin,ymax]
373   end
374
375   # -----       getway_history (way)
376   #                     returns array of previous versions (version,timestamp,visible,user)
377   #                     should also show 'created_by'
378
379   def getway_history(wayid)
380     RAILS_DEFAULT_LOGGER.info("  Received history request for #{wayid}")
381         history=[]
382         sql=<<-EOF
383         SELECT version,timestamp,visible,display_name,data_public
384           FROM ways,users
385          WHERE ways.id=#{wayid}
386            AND ways.user_id=users.id
387          ORDER BY version DESC
388         EOF
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]
393         }
394         [history]
395   end
396
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
401
402   def putway(args)
403     RAILS_DEFAULT_LOGGER.info("  putway started")
404     usertoken,originalway,points,attributes,baselong,basey,masterscale=args
405     uid=getuserid(usertoken)
406     return if !uid
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
412
413     RAILS_DEFAULT_LOGGER.info("  Message: putway, id=#{originalway}")
414
415     # -- 3.     read original way into memory
416
417     xc={}; yc={}; tagc={}
418     if originalway>0
419       way=originalway
420       readwayquery(way).each { |row|
421         id=row['id'].to_i
422         xc[id]=row['longitude'].to_f
423         yc[id]=row['latitude' ].to_f
424         tagc[id]=row['tags']
425       }
426       ActiveRecord::Base.connection.update("UPDATE current_ways SET timestamp=#{db_now},user_id=#{uid},visible=1 WHERE id=#{way}")
427     else
428       way=ActiveRecord::Base.connection.insert("INSERT INTO current_ways (user_id,timestamp,visible) VALUES (#{uid},#{db_now},1)")
429     end
430
431     # -- 4.     get version by inserting new row into ways
432
433     version=ActiveRecord::Base.connection.insert("INSERT INTO ways (id,user_id,timestamp,visible) VALUES (#{way},#{uid},#{db_now},1)")
434
435     # -- 5. compare nodes and update xmin,xmax,ymin,ymax
436
437     xmin=ymin= 999999
438     xmax=ymax=-999999
439     insertsql=''
440     renumberednodes={}
441         nodelist=[]
442
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)
454
455       # compare node
456       if node<0
457         # new node - create
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})")
461           points[i][2]=newnode
462           nodelist.push(newnode)
463           renumberednodes[node.to_s]=newnode.to_s
464                 else
465           points[i][2]=renumberednodes[node.to_s].to_i
466                 end
467
468       elsif xc.has_key?(node)
469                 nodelist.push(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}")
474         end
475       else
476         # old node, created in another way and now added to this way
477       end
478     end
479
480
481         # -- 6a. delete any nodes not in modified way
482
483     createuniquenodes(way,db_uqn,nodelist)      # nodes which appear in this way but no other
484
485     sql=<<-EOF
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}
489          WHERE cn.id=node_id
490     EOF
491     ActiveRecord::Base.connection.insert(sql)
492
493     sql=<<-EOF
494       UPDATE current_nodes AS cn, #{db_uqn}
495          SET cn.timestamp=#{db_now},cn.visible=0,cn.user_id=#{uid} 
496        WHERE cn.id=node_id
497     EOF
498     ActiveRecord::Base.connection.update(sql)
499
500         deleteuniquenoderelations(db_uqn,uid,db_now)
501     ActiveRecord::Base.connection.execute("DROP TABLE #{db_uqn}")
502
503         #       6b. insert new version of route into way_nodes
504
505     insertsql =''
506     currentsql=''
507     sequence  =1
508     points.each do |p|
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})"
513       sequence  +=1
514     end
515
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}");
519
520     # -- 7. insert new way tags
521
522     insertsql =''
523     currentsql=''
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)+"')"
531     end
532
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
536
537     [originalway,way,renumberednodes,xmin,xmax,ymin,ymax]
538   end
539
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
544
545   def putpoi(args)
546     usertoken,id,x,y,tags,visible,baselong,basey,masterscale=args
547     uid=getuserid(usertoken)
548     return if !uid
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()")
551
552     id=id.to_i
553     visible=visible.to_i
554         if visible==0 then
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)
559         end
560
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)
567         
568     if (id>0) then
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}");
571         newid=id
572     else
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})");
575     end
576     [id,newid]
577   end
578
579   # -----       getpoi (id,baselong,basey,masterscale)
580   #                     returns id,x,y,tag array
581   
582   def getpoi(args)
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
587         [id,
588          long2coord(poi['lng'].to_f,baselong,masterscale),
589          lat2coord(poi['lat'].to_f,basey,masterscale),
590          tag2array(poi['tags'])]
591   end
592
593   # -----       deleteway (user token, way, nodes to keep)
594   #                     returns way ID only
595
596   def deleteway(args)
597     usertoken,way,preserve=args
598
599     RAILS_DEFAULT_LOGGER.info("  Message: deleteway, id=#{way}")
600
601     uid=getuserid(usertoken); if !uid then return end
602     way=way.to_i
603
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()")
607
608     # - delete any otherwise unused nodes
609   
610     createuniquenodes(way,db_uqn,[])
611
612         unless (preserve.empty?) then
613                 ActiveRecord::Base.connection.execute("DELETE FROM #{db_uqn} WHERE node_id IN ("+preserve.join(',')+")")
614         end
615
616     sql=<<-EOF
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}
620          WHERE cn.id=node_id
621     EOF
622     ActiveRecord::Base.connection.insert(sql)
623
624     sql=<<-EOF
625       UPDATE current_nodes AS cn, #{db_uqn}
626          SET cn.timestamp=#{db_now},cn.visible=0,cn.user_id=#{uid} 
627        WHERE cn.id=node_id
628     EOF
629     ActiveRecord::Base.connection.update(sql)
630
631         deleteuniquenoderelations(db_uqn,uid,db_now)
632     ActiveRecord::Base.connection.execute("DROP TABLE #{db_uqn}")
633
634     # - delete way
635         
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)
641     way
642 end
643
644
645
646 # ====================================================================
647 # Support functions for remote calls
648
649 def readwayquery(id)
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"
656 end
657
658 def createuniquenodes(way,uqn_name,nodelist)
659         # Find nodes which appear in this way but no others
660         sql=<<-EOF
661         CREATE TEMPORARY TABLE #{uqn_name}
662                                         SELECT a.node_id
663                                           FROM (SELECT DISTINCT node_id FROM current_way_nodes
664                                                         WHERE id=#{way}) a
665                                  LEFT JOIN current_way_nodes b
666                                                 ON b.node_id=a.node_id
667                                            AND b.id!=#{way}
668                                          WHERE b.node_id IS NULL
669         EOF
670         unless nodelist.empty? then
671           sql+="AND a.node_id NOT IN ("+nodelist.join(',')+")"
672         end
673         ActiveRecord::Base.connection.execute(sql)
674 end
675
676
677
678 # ====================================================================
679 # Relations handling
680 # deleteuniquenoderelations(uqn_name,uid,db_now)
681 # deleteitemrelations(way|node,'way'|'node',uid,db_now)
682
683 def deleteuniquenoderelations(uqn_name,uid,db_now)
684         sql=<<-EOF
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' 
688            AND crm.id=cr.id 
689            AND cr.visible=1
690         EOF
691
692         relnodes=ActiveRecord::Base.connection.select_all(sql)
693         relnodes.each do |a|
694                 removefromrelation(a['node_id'],'node',a['id'],uid,db_now)
695         end
696 end
697
698 def deleteitemrelations(objid,type,uid,db_now)
699         sql=<<-EOF
700         SELECT cr.id FROM current_relation_members crm,current_relations cr 
701          WHERE crm.member_id=#{objid} 
702            AND crm.member_type='#{type}' 
703            AND crm.id=cr.id 
704            AND cr.visible=1
705         EOF
706         
707         relways=ActiveRecord::Base.connection.select_all(sql)
708         relways.each do |a|
709                 removefromrelation(objid,type,a['id'],uid,db_now)
710         end
711 end
712
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)")
715
716         tagsql=<<-EOF
717         INSERT INTO relation_tags (id,k,v,version) 
718         SELECT id,k,v,#{rver} FROM current_relation_tags 
719          WHERE id=#{relation} 
720         EOF
721         ActiveRecord::Base.connection.insert(tagsql)
722
723         membersql=<<-EOF
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 
726          WHERE id=#{relation} 
727            AND (member_id!=#{objid} OR member_type!='#{type}')
728         EOF
729         ActiveRecord::Base.connection.insert(membersql)
730         
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}")
733 end
734
735
736 def sqlescape(a)
737   a.gsub(/[\000-\037]/,"").gsub("'","''").gsub(92.chr,92.chr+92.chr)
738 end
739
740 def tag2array(a)
741   tags={}
742   a.gsub(';;;','#%').split(';').each do |b|
743     b.gsub!('#%',';;;')
744     b.gsub!('===','#%')
745     k,v=b.split('=')
746     if k.nil? then k='' end
747     if v.nil? then v='' end
748     tags[k.gsub('#%','=').gsub(':','|')]=v.gsub('#%','=')
749   end
750   tags
751 end
752
753 def array2tag(a)
754   str=''
755   a.each do |k,v|
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('=','===')
760   end
761   str
762 end
763
764 def getuserid(token)
765   if (token =~ /^(.+)\+(.+)$/) then
766     user = User.authenticate(:username => $1, :password => $2)
767   else
768     user = User.authenticate(:token => token)
769   end
770
771   return user ? user.id : nil;
772 end
773
774
775
776 # ====================================================================
777 # AMF read subroutines
778
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
785
786 def getint(s)
787   s.getc*256+s.getc
788 end
789
790 def getlong(s)
791   ((s.getc*256+s.getc)*256+s.getc)*256+s.getc
792 end
793
794 def getstring(s)
795   len=s.getc*256+s.getc
796   s.read(len)
797 end
798
799 def getdouble(s)
800   a=s.read(8).unpack('G')                       # G big-endian, E little-endian
801   a[0]
802 end
803
804 def getarray(s)
805   len=getlong(s)
806   arr=[]
807   for i in (0..len-1)
808     arr[i]=getvalue(s)
809   end
810   arr
811 end
812
813 def getobject(s)
814   arr={}
815   while (key=getstring(s))
816     if (key=='') then break end
817     arr[key]=getvalue(s)
818   end
819   s.getc                # skip the 9 'end of object' value
820   arr
821 end
822
823 # ----- getvalue        parse and get value
824
825 def getvalue(s)
826   case s.getc
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
837   end
838 end
839
840 # ====================================================================
841 # AMF write subroutines
842
843 # ----- putdata         envelope data into AMF writeable form
844 # ----- encodevalue     pack variables as AMF
845
846 def putdata(index,n)
847   d =encodestring(index+"/onResult")
848   d+=encodestring("null")
849   d+=[-1].pack("N")
850   d+=encodevalue(n)
851 end
852
853 def encodevalue(n)
854   case n.class.to_s
855   when 'Array'
856     a=10.chr+encodelong(n.length)
857     n.each do |b|
858       a+=encodevalue(b)
859     end
860     a
861   when 'Hash'
862     a=3.chr
863     n.each do |k,v|
864       a+=encodestring(k)+encodevalue(v)
865     end
866     a+0.chr+0.chr+9.chr
867   when 'String'
868     2.chr+encodestring(n)
869   when 'Bignum','Fixnum','Float'
870     0.chr+encodedouble(n)
871   when 'NilClass'
872     5.chr
873   else
874     RAILS_DEFAULT_LOGGER.error("Unexpected Ruby type for AMF conversion: "+n.class.to_s)
875   end
876 end
877
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
881
882 def encodestring(n)
883   a,b=n.size.divmod(256)
884   a.chr+b.chr+n
885 end
886
887 def encodedouble(n)
888   [n].pack('G')
889 end
890
891 def encodelong(n)
892   [n].pack('N')
893 end
894
895 # ====================================================================
896 # Co-ordinate conversion
897
898 def lat2coord(a,basey,masterscale)
899   -(lat2y(a)-basey)*masterscale+250
900 end
901
902 def long2coord(a,baselong,masterscale)
903   (a-baselong)*masterscale+350
904 end
905
906 def lat2y(a)
907   180/Math::PI * Math.log(Math.tan(Math::PI/4+a*(Math::PI/180)/2))
908 end
909
910 def coord2lat(a,masterscale,basey)
911   y2lat((a-250)/-masterscale+basey)
912 end
913
914 def coord2long(a,masterscale,baselong)
915   (a-350)/masterscale+baselong
916 end
917
918 def y2lat(a)
919   180/Math::PI * (2*Math.atan(Math.exp(a*Math::PI/180))-Math::PI/2)
920 end
921
922 end