]> git.openstreetmap.org Git - rails.git/blobdiff - app/controllers/amf_controller.rb
migration 013 removes tags from nodes and old nodes tables/ 014 does not do anything atm
[rails.git] / app / controllers / amf_controller.rb
index c526141222f77f8fe879a5da5cfe3b6d2de7769c..60b7ab4438641dbe015b65c7b41ecd9302261af3 100644 (file)
@@ -14,8 +14,6 @@ class AmfController < ApplicationController
   # editions Systeme D / Richard Fairhurst 2004-2008
   #
   # All in/out parameters are floats unless explicitly stated.
-  # Note that in getway/getway_old, SWF object name and way id are
-  #ĂŠidentical and one could probably be eliminated.
   # 
   # to trap errors (getway_old,putway,putpoi,deleteway only):
   #   return(-1,"message")             <-- just puts up a dialogue
@@ -62,8 +60,8 @@ class AmfController < ApplicationController
       when 'getway_old';               results[index]=putdata(index,getway_old(args))
       when 'getway_history';   results[index]=putdata(index,getway_history(args))
       when 'putway';                   r=putway(args,renumberednodes)
-        renumberednodes=r[3]
-        results[index]=putdata(index,r)
+                                                               renumberednodes=r[3]
+                                                               results[index]=putdata(index,r)
       when 'deleteway';                        results[index]=putdata(index,deleteway(args))
       when 'putpoi';                   results[index]=putdata(index,putpoi(args))
       when 'getpoi';                   results[index]=putdata(index,getpoi(args))
@@ -157,20 +155,17 @@ class AmfController < ApplicationController
     [presets,presetmenus,presetnames,colours,casing,areas,autotags]
   end
 
+
   # ----- whichways
-  #              return array of ways in current bounding box
-
-  #              in:   [0] xmin, [1] ymin, [2] xmax, [3] ymax (bbox in degrees)
-  #                            [4] baselong (longitude of SWF map origin),
-  #                            [5] basey (projected latitude of SWF map origin),
-  #                            [6] masterscale (SWF map scale)
-  #              does: finds all ways and POI nodes in bounding box
-  #                            at present, instead of using correct (=more complex) SQL to find
-  #                            corner-crossing ways, it simply enlarges the bounding box
-  #              out:  [0] array of way ids,
-  #                            [1] array of POIs
-  #                            (where each POI is an array containing:
-  #                             [0] id, [1] projected long, [2] projected lat, [3] hash of tags)
+  
+  # Find all the way ids and nodes (including tags and projected lat/lng) which aren't part of those ways in an are
+  # 
+  # The argument is an array containing the following, in order:
+  # 0. minimum longitude
+  # 1. minimum latitude
+  # 2. maximum longitude
+  # 3. maximum latitude
+  # 4. baselong, 5. basey, 6. masterscale as above
 
   def whichways(args)
     xmin = args[0].to_f-0.01
@@ -183,35 +178,27 @@ class AmfController < ApplicationController
 
     RAILS_DEFAULT_LOGGER.info("  Message: whichways, bbox=#{xmin},#{ymin},#{xmax},#{ymax}")
 
-    waylist = ActiveRecord::Base.connection.select_all("SELECT DISTINCT current_way_nodes.id AS wayid"+
-       "  FROM current_way_nodes,current_nodes,current_ways "+
-       " WHERE current_nodes.id=current_way_nodes.node_id "+
-       "   AND current_nodes.visible=1 "+
-       "   AND current_ways.id=current_way_nodes.id "+
-       "   AND current_ways.visible=1 "+
-       "   AND "+OSM.sql_for_area(ymin, xmin, ymax, xmax, "current_nodes."))
-
-    ways = waylist.collect {|a| a['wayid'].to_i } # get an array of way IDs
+    # 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
 
-    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 "+
-       "  FROM current_nodes "+
-       "  LEFT OUTER JOIN current_way_nodes cwn ON cwn.node_id=current_nodes.id "+
-       " WHERE "+OSM.sql_for_area(ymin, xmin, ymax, xmax, "current_nodes.")+
-       "   AND cwn.id IS NULL "+
-       "   AND current_nodes.visible=1")
-
-    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
+    # 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] }
 
     [ways,points]
   end
 
   # ----- whichways_deleted
   #              return array of deleted ways in current bounding box
-
   #              in:   as whichways
   #              does: finds all deleted ways with a deleted node in bounding box
   #              out:  [0] array of way ids
-
+  
   def whichways_deleted(args)
     xmin = args[0].to_f-0.01
     ymin = args[1].to_f-0.01
@@ -235,47 +222,55 @@ class AmfController < ApplicationController
     [ways]
   end
 
+
   # ----- getway
-  #              in:   [0] SWF object name, 
-  #                            [1] way id, [2] baselong, [3] basey, [4] masterscale
-  #              does: gets way and all nodes
-  #              out:  [0] SWF object name (unchanged),
-  #                            [1] array of points
-  #                                    (where each point is an array containing
-  #                                     [0] projected long, [1] projected lat, [2] node id,
-  #                                     [3] null, [4] hash of node tags),
-  #                            [2] xmin, [3] xmax, [4] ymin, [5] ymax (unprojected bbox)
+
+  # Get a way with all of it's nodes and tags
+  # The input is an array with the following components, in order:
+  # 0. wayid - the ID of the way to get
+  # 1. baselong - origin of SWF map (longitude)
+  # 2. basey - origin of SWF map (latitude)
+  # 3. masterscale - SWF map scale
+  #
+  # The output is an array which contains all the nodes (with projected 
+  # latitude and longitude) and tags for a way (and all the nodes tags). 
+  # It also has the way's unprojected (WGS84) bbox.
+  #
+  # FIXME: The server really shouldn't be figuring out a ways bounding box and doing projection for potlatch
+  # FIXME: the argument splitting should be done in the 'talk' method, not here
 
   def getway(args)
-    objname,wayid,baselong,basey,masterscale=args
+    wayid,baselong,basey,masterscale = args
     wayid = wayid.to_i
-    points = []
-    xmin = ymin =  999999
-    xmax = ymax = -999999
 
     RAILS_DEFAULT_LOGGER.info("  Message: getway, id=#{wayid}")
 
-    readwayquery(wayid,true).each {|row|
-      points<<[long2coord(row['longitude'].to_f,baselong,masterscale),lat2coord(row['latitude'].to_f,basey,masterscale),row['id'].to_i,nil,tag2array(row['tags'])]
-      xmin = [xmin,row['longitude'].to_f].min
-      xmax = [xmax,row['longitude'].to_f].max
-      ymin = [ymin,row['latitude'].to_f].min
-      ymax = [ymax,row['latitude'].to_f].max
-    }
+    way = Way.find_eager(wayid)
+    long_array = []
+    lat_array = []
+    points = []
 
-    attributes={}
-    attrlist=ActiveRecord::Base.connection.select_all "SELECT k,v FROM current_way_tags WHERE id=#{wayid}"
-    attrlist.each {|a| attributes[a['k'].gsub(':','|')]=a['v'] }
+    way.way_nodes.each do |way_node|
+      node = way_node.node # get the node record
+      projected_longitude = node.lon_potlatch(baselong,masterscale) # do projection for potlatch
+      projected_latitude = node.lat_potlatch(basey,masterscale)
+      id = node.id
+      tags_hash = node.tags_as_hash
+      
+      points << [projected_longitude, projected_latitude, id, nil, tags_hash]
+      long_array << projected_longitude
+      lat_array << projected_latitude
+    end
 
-    [objname,points,attributes,xmin,xmax,ymin,ymax]
+    [wayid,points,way.tags,long_array.min,long_array.max,lat_array.min,lat_array.max]
   end
 
   # ----- getway_old
   #              returns old version of way
 
-  #              in:   [0] SWF object name, [1] way id,
-  #                            [2] way version to get (or -1 for "last deleted version")
-  #                            [3] baselong, [4] basey, [5] masterscale
+  #              in:   [0] way id,
+  #                            [1] way version to get (or -1 for "last deleted version")
+  #                            [2] baselong, [3] basey, [4] masterscale
   #              does: gets old version of way and all constituent nodes
   #                            for undelete, always uses the most recent version of each node
   #                              (even if it's moved)
@@ -290,7 +285,7 @@ class AmfController < ApplicationController
     RAILS_DEFAULT_LOGGER.info("  Message: getway_old (server is #{SERVER_URL})")
     #  if SERVER_URL=="www.openstreetmap.org" then return -1,"Revert is not currently enabled on the OpenStreetMap server." end
 
-    objname,wayid,version,baselong,basey,masterscale=args
+    wayid,version,baselong,basey,masterscale=args
     wayid = wayid.to_i
     version = version.to_i
     xmin = ymin =  999999
@@ -316,7 +311,7 @@ class AmfController < ApplicationController
     attrlist.each {|a| attributes[a['k'].gsub(':','|')]=a['v'] }
     attributes['history']="Retrieved from v"+version.to_s
 
-    [0,objname,points,attributes,xmin,xmax,ymin,ymax,version]
+    [0,wayid,points,attributes,xmin,xmax,ymin,ymax,version]
   end
 
   # ----- getway_history
@@ -792,7 +787,6 @@ class AmfController < ApplicationController
     ActiveRecord::Base.connection.execute("DELETE FROM current_relation_members WHERE id=#{relation} AND member_type='#{type}' AND member_id=#{objid}")
   end
 
-
   def sqlescape(a)
     a.gsub(/[\000-\037]/,"").gsub("'","''").gsub(92.chr) {92.chr+92.chr}
   end