]> git.openstreetmap.org Git - rails.git/blobdiff - app/controllers/amf_controller.rb
Don't preload nodes as it seem to break ways with duplicate nodes.
[rails.git] / app / controllers / amf_controller.rb
index 876dbd10e04a4cf15dc243b382b3e21f710de0b1..9592cf94100e61b35f98074e0f411d74dfe929fb 100644 (file)
@@ -109,18 +109,14 @@ 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]
+    [way_ids,points]
   end
 
   # ----- whichways_deleted
@@ -171,13 +167,12 @@ class AmfController < ApplicationController
 
     RAILS_DEFAULT_LOGGER.info("  Message: getway, id=#{wayid}")
 
-    way = Way.find_eager(wayid)
+    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
@@ -475,21 +470,21 @@ class AmfController < ApplicationController
   end
 
   # ----- getpoi
-  #              read POI from database
+  # read POI from database
   #              (only called on revert: POIs are usually read by whichways)
   #              in:   [0] node id, [1] baselong, [2] basey, [3] masterscale
   #              does: reads POI
   #              out:  [0] id (unchanged), [1] projected long, [2] projected lat,
   #                            [3] hash of tags
   def getpoi(args) #:doc:
-    id,baselong,basey,masterscale=args; id=id.to_i
-    poi=ActiveRecord::Base.connection.select_one("SELECT latitude*0.0000001 AS lat,longitude*0.0000001 AS lng,tags "+
-    "FROM current_nodes WHERE visible=1 AND id=#{id}")
-    if poi.nil? then return [nil,nil,nil,''] end
-    [id,
-      long2coord(poi['lng'].to_f,baselong,masterscale),
-      lat2coord(poi['lat'].to_f,basey,masterscale),
-      tag2array(poi['tags'])]
+    id,baselong,basey,masterscale = args
+    
+    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]
+    else
+      return [nil,nil,nil,'']
+    end
   end
 
   # ----- deleteway
@@ -559,9 +554,10 @@ class AmfController < ApplicationController
     ActiveRecord::Base.connection.select_all(sql)
   end
 
+  # Get the latest version id of a way
   def getlastversion(id,version) #:doc:
-    row=ActiveRecord::Base.connection.select_one("SELECT version FROM ways WHERE id=#{id} AND visible=1 ORDER BY version DESC LIMIT 1")
-    row['version']
+    old_way = OldWay.find(:first, :conditions => ['id = ?' , id], :order => 'version DESC')
+    old_way.version
   end
 
   def readwayquery_old(id,version,historic) #:doc:
@@ -732,11 +728,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:
@@ -744,11 +740,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)