]> git.openstreetmap.org Git - rails.git/blobdiff - app/controllers/amf_controller.rb
restore Steve's deleteway stuff
[rails.git] / app / controllers / amf_controller.rb
index e83ed4bb1c0239f9ae4779eb0c89610084f27524..83a6cd4b58fb871b30c4762dc7630ff6db74f4cf 100644 (file)
@@ -30,6 +30,7 @@ class AmfController < ApplicationController
     req.read(2)                                                                        # Skip version indicator and client ID
     results={}                                                                 # Results of each body
     renumberednodes={}                                                 # Shared across repeated putways
+    renumberedways={}                                                  # Shared across repeated putways
 
     # -------------
     # Parse request
@@ -55,11 +56,16 @@ class AmfController < ApplicationController
       when 'whichways';                        results[index]=AMF.putdata(index,whichways(args))
       when 'whichways_deleted';        results[index]=AMF.putdata(index,whichways_deleted(args))
       when 'getway';                   results[index]=AMF.putdata(index,getway(args))
+      when 'getrelation';              results[index]=AMF.putdata(index,getrelation(args))
       when 'getway_old';               results[index]=AMF.putdata(index,getway_old(args))
       when 'getway_history';   results[index]=AMF.putdata(index,getway_history(args))
       when 'putway';                   r=putway(args,renumberednodes)
-        renumberednodes=r[3]
-        results[index]=AMF.putdata(index,r)
+                                                               renumberednodes=r[3]
+                                                               if r[1] != r[2]
+                                                                       renumberedways[r[1]] = r[2]
+                                                               end
+                                                               results[index]=AMF.putdata(index,r)
+      when 'putrelation';              results[index]=AMF.putdata(index,putrelation(args, renumberednodes, renumberedways))
       when 'deleteway';                        results[index]=AMF.putdata(index,deleteway(args))
       when 'putpoi';                   results[index]=AMF.putdata(index,putpoi(args))
       when 'getpoi';                   results[index]=AMF.putdata(index,getpoi(args))
@@ -109,18 +115,17 @@ class AmfController < ApplicationController
     RAILS_DEFAULT_LOGGER.info("  Message: whichways, bbox=#{xmin},#{ymin},#{xmax},#{ymax}")
 
     # find the way ids in an area
-    nodes_in_area = Node.find_by_area(ymin, xmin, ymax, xmax,:conditions => "visible = 1", :include => :way_nodes)
-    waynodes_in_area = nodes_in_area.collect {|node| node.way_nodes }.flatten
-    ways = waynodes_in_area.collect {|way_node| way_node.id[0]}.uniq
+    nodes_in_area = Node.find_by_area(ymin, xmin, ymax, xmax, :conditions => "current_nodes.visible = 1", :include => :ways)
+    way_ids = nodes_in_area.collect { |node| node.way_ids }.flatten.uniq
 
     # find the node ids in an area that aren't part of ways
-    node_ids_in_area = nodes_in_area.collect {|node| node.id}.uniq
-    node_ids_used_in_ways = waynodes_in_area.collect {|way_node| way_node.node_id}.uniq
-    node_ids_not_used_in_area = node_ids_in_area - node_ids_used_in_ways
-    nodes_not_used_in_area = Node.find(node_ids_not_used_in_area)
-    points = nodes_not_used_in_area.collect {|n| [n.id, n.lon_potlatch(baselong,masterscale), n.lat_potlatch(basey,masterscale), n.tags_as_hash] }
+    nodes_not_used_in_area = nodes_in_area.select { |node| node.ways.empty? }
+    points = nodes_not_used_in_area.collect { |n| [n.id, n.lon_potlatch(baselong,masterscale), n.lat_potlatch(basey,masterscale), n.tags_as_hash] }
 
-    [ways,points]
+    # find the relations used by those nodes and ways
+    relation_ids = Relation.find_for_nodes_and_ways(nodes_in_area.collect {|n| n.id}, way_ids).collect {|n| n.id}.uniq
+
+    [way_ids,points,relation_ids]
   end
 
   # ----- whichways_deleted
@@ -171,13 +176,16 @@ class AmfController < ApplicationController
 
     RAILS_DEFAULT_LOGGER.info("  Message: getway, id=#{wayid}")
 
-    way = Way.find_eager(wayid)
+    # Ideally we would do ":include => :nodes" here but if we do that
+    # then rails only seems to return the first copy of a node when a
+    # way includes a node more than once
+    way = Way.find(wayid)
+
     long_array = []
     lat_array = []
     points = []
 
-    way.way_nodes.each do |way_node|
-      node = way_node.node # get the node record
+    way.nodes.each do |node|
       projected_longitude = node.lon_potlatch(baselong,masterscale) # do projection for potlatch
       projected_latitude = node.lat_potlatch(basey,masterscale)
       id = node.id
@@ -265,6 +273,80 @@ class AmfController < ApplicationController
     [history]
   end
 
+  # ----- getrelation
+  # Get a relation with all of it's tags, and member IDs
+  # The input is an array with the following components, in order:
+  # 0. relid - the ID of the relation to get
+  #
+  # The output is an array which contains:
+  # [0] relation id, [1] hash of tags, [2] list of members
+  def getrelation(args) #:doc:
+    relid = args[0]
+    relid = relid.to_i
+
+    RAILS_DEFAULT_LOGGER.info("  Message: getrel, id=#{relid}")
+
+    rel = Relation.find(relid)
+
+    [relid,rel.tags,rel.members]#nodes,ways]
+  end
+
+  # ----- getrelation
+  #              save relation to the database
+  #              in:   [0] user token (string),
+  #                            [1] original relation id (may be negative),
+  #                            [2] hash of tags, [3] list of members,
+  #                            [4] visible
+  #              out:  [0] 0 (success), [1] original relation id (unchanged),
+  #                            [2] new relation id
+  def putrelation(args, renumberednodes, renumberedways) #:doc:
+    usertoken,relid,tags,members,visible=args
+    uid=getuserid(usertoken)
+    if !uid then return -1,"You are not logged in, so the point could not be saved." end
+
+    relid = relid.to_i
+       visible = visible.to_i
+
+       # create a new relation, or find the existing one
+    if relid <= 0
+      rel = Relation.new
+    else
+      rel = Relation.find(relid)
+    end
+
+    # check the members are all positive, and correctly type
+    typedmembers = []
+    members.each do |m|
+      mid = m[1].to_i
+      if mid < 0
+        mid = renumberednodes[mid] if m[0] == 'node'
+        mid = renumberedways[mid] if m[0] == 'way'
+        if mid < 0
+          return -2, "Negative ID unresolved"
+        end
+      end
+      typedmembers << [m[0], mid, m[2]]
+    end
+
+       # assign new contents
+       rel.members = typedmembers
+       rel.tags = tags
+       rel.visible = visible
+       rel.user_id = uid
+
+    # check it then save it
+    # BUG: the following is commented out because it always fails on my
+    #  install. I think it's a Rails bug.
+
+    #if !rel.preconditions_ok?
+    #  return -2, "Relation preconditions failed"
+    #else
+      rel.save_with_history!
+    #end
+
+    [0,relid,rel.id]
+  end
+
   # ----- putway
   #              saves a way to the database
   #              in:   [0] user token (string),
@@ -294,11 +376,13 @@ class AmfController < ApplicationController
 
     RAILS_DEFAULT_LOGGER.info("  Message: putway, id=#{originalway}")
 
-    # -- Temporary check for null IDs
+    # -- Check for null IDs or short ways
 
     points.each do |a|
       if a[2]==0 or a[2].nil? then return -2,"Server error - node with id 0 found in way #{originalway}." end
     end
+    
+    if points.length<2 then return -2,"Server error - way is only #{points.length} points long." end
 
     # -- 3.    read original way into memory
 
@@ -486,7 +570,7 @@ class AmfController < ApplicationController
     
     n = Node.find(id.to_i)
     if n
-      return [n.id, n.long_potlatch(baselong,masterscale), n.lat_potlatch(basey,masterscale), n.tags_as_hash]
+      return [n.id, n.lon_potlatch(baselong,masterscale), n.lat_potlatch(basey,masterscale), n.tags_as_hash]
     else
       return [nil,nil,nil,'']
     end
@@ -498,55 +582,20 @@ class AmfController < ApplicationController
   #              does: deletes way from db and any constituent nodes not used elsewhere
   #                            also removes ways/nodes from any relations they're in
   #              out:  [0] 0 (success), [1] way id (unchanged)
-  def deleteway(args) #:doc:
 
-    usertoken,way=args
-
-    RAILS_DEFAULT_LOGGER.info("  Message: deleteway, id=#{way}")
+  def deleteway(args) #:doc:
+    usertoken,way_id=args
+    RAILS_DEFAULT_LOGGER.info("  Message: deleteway, id=#{way_id}")
     uid=getuserid(usertoken)
     if !uid then return -1,"You are not logged in, so the way could not be deleted." end
 
-    way=way.to_i
-    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
-    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
-    ActiveRecord::Base.connection.execute("SET #{db_now}=NOW()")
-
-    # - delete any otherwise unused nodes
-
-    createuniquenodes(way,db_uqn,[])
-
-    #  unless (preserve.empty?) then
-    #          ActiveRecord::Base.connection.execute("DELETE FROM #{db_uqn} WHERE node_id IN ("+preserve.join(',')+")")
-    #  end
-
-    sql=<<-EOF
-  INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible,tile)
-  SELECT DISTINCT cn.id,cn.latitude,cn.longitude,#{db_now},#{uid},0,cn.tile
-    FROM current_nodes AS cn,#{db_uqn}
-   WHERE cn.id=node_id
-    EOF
-    ActiveRecord::Base.connection.insert(sql)
-
-    sql=<<-EOF
-      UPDATE current_nodes AS cn, #{db_uqn}
-         SET cn.timestamp=#{db_now},cn.visible=0,cn.user_id=#{uid} 
-       WHERE cn.id=node_id
-    EOF
-    ActiveRecord::Base.connection.update(sql)
-
-    deleteuniquenoderelations(db_uqn,uid,db_now)
-    ActiveRecord::Base.connection.execute("DROP TEMPORARY TABLE #{db_uqn}")
-
-    # - delete way
-
-    ActiveRecord::Base.connection.insert("INSERT INTO ways (id,user_id,timestamp,visible) VALUES (#{way},#{uid},#{db_now},0)")
-    ActiveRecord::Base.connection.update("UPDATE current_ways SET user_id=#{uid},timestamp=#{db_now},visible=0 WHERE id=#{way}")
-    ActiveRecord::Base.connection.execute("DELETE FROM current_way_nodes WHERE id=#{way}")
-    ActiveRecord::Base.connection.execute("DELETE FROM current_way_tags WHERE id=#{way}")
-    deleteitemrelations(way,'way',uid,db_now)
-    [0,way]
+    user = User.find(uid)
+    way = Way.find(way_id)
+    way.delete_with_relations_and_nodes_and_history(user)  
+    return [0,way_id]
   end
 
+
   def readwayquery(id,insistonvisible) #:doc:
     sql=<<-EOF
     SELECT latitude*0.0000001 AS latitude,longitude*0.0000001 AS longitude,current_nodes.id,tags,visible 
@@ -561,7 +610,7 @@ class AmfController < ApplicationController
 
   # Get the latest version id of a way
   def getlastversion(id,version) #:doc:
-    old_way = OldWay.find(:first, :conditions => ['id = ?' , id], :order => 'version DESC')
+    old_way = OldWay.find(:first, :conditions => ['visible=1 AND id=?' , id], :order => 'version DESC')
     old_way.version
   end
 
@@ -733,11 +782,11 @@ class AmfController < ApplicationController
   # Co-ordinate conversion
 
   def lat2coord(a,basey,masterscale) #:doc:
-    -(lat2y(a)-basey)*masterscale+250
+    -(lat2y(a)-basey)*masterscale
   end
 
   def long2coord(a,baselong,masterscale) #:doc:
-    (a-baselong)*masterscale+350
+    (a-baselong)*masterscale
   end
 
   def lat2y(a) #:doc:
@@ -745,11 +794,11 @@ class AmfController < ApplicationController
   end
 
   def coord2lat(a,masterscale,basey) #:doc:
-    y2lat((a-250)/-masterscale+basey)
+    y2lat(a/-masterscale+basey)
   end
 
   def coord2long(a,masterscale,baselong) #:doc:
-    (a-350)/masterscale+baselong
+    a/masterscale+baselong
   end
 
   def y2lat(a)