Tidy up AMF controller and optimise a few rails things.
This commit is contained in:
parent
1cd26357f0
commit
66b0dd135e
1 changed files with 119 additions and 99 deletions
|
@ -145,6 +145,7 @@ class AmfController < ApplicationController
|
|||
|
||||
nodes_in_area = Node.find_by_area(ymin, xmin, ymax, xmax, :conditions => "current_nodes.visible = 0 AND current_ways.visible = 0", :include => :ways_via_history)
|
||||
way_ids = nodes_in_area.collect { |node| node.ways_via_history_ids }.flatten.uniq
|
||||
|
||||
[way_ids]
|
||||
end
|
||||
|
||||
|
@ -159,13 +160,13 @@ class AmfController < ApplicationController
|
|||
# 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
|
||||
points = []
|
||||
way = Way.find(wayid)
|
||||
way.nodes.each do |node|
|
||||
points << [node.lon, node.lat, node.id, nil, node.tags_as_hash]
|
||||
points = way.nodes.collect do |node|
|
||||
[node.lon, node.lat, node.id, nil, node.tags_as_hash]
|
||||
end
|
||||
tags = way.tags
|
||||
end
|
||||
|
||||
[wayid, points, tags]
|
||||
end
|
||||
|
||||
|
@ -184,19 +185,20 @@ class AmfController < ApplicationController
|
|||
old_way = OldWay.find(:first, :conditions => ['id = ? AND version = ?', id, version])
|
||||
points = old_way.get_nodes_revert
|
||||
end
|
||||
|
||||
old_way.tags['history'] = "Retrieved from v#{old_way.version}"
|
||||
|
||||
[0, id, points, old_way.tags, old_way.version]
|
||||
end
|
||||
|
||||
# Find history of a way. Returns an array of previous versions.
|
||||
|
||||
def getway_history(wayid) #:doc:
|
||||
history=[]
|
||||
way=Way.find(wayid)
|
||||
way.old_ways.each do |old_way|
|
||||
if old_way.user.data_public then user=old_way.user.display_name else user='anonymous' end
|
||||
history<<[old_way.version,old_way.timestamp.strftime("%d %b %Y, %H:%M"),old_way.visible ? 1 : 0,user]
|
||||
history = Way.find(wayid).old_ways.collect do |old_way|
|
||||
user = old_way.user.data_public? ? old_way.user.display_name : 'anonymous'
|
||||
[old_way.version, old_way.timestamp.strftime("%d %b %Y, %H:%M"), old_way.visible ? 1 : 0, user]
|
||||
end
|
||||
|
||||
[history]
|
||||
end
|
||||
|
||||
|
@ -208,6 +210,7 @@ class AmfController < ApplicationController
|
|||
|
||||
def getrelation(relid) #:doc:
|
||||
rel = Relation.find(relid)
|
||||
|
||||
[relid, rel.tags, rel.members]
|
||||
end
|
||||
|
||||
|
@ -219,7 +222,7 @@ class AmfController < ApplicationController
|
|||
|
||||
def putrelation(renumberednodes, renumberedways, usertoken, relid, tags, members, visible) #:doc:
|
||||
uid = getuserid(usertoken)
|
||||
if !uid then return -1,"You are not logged in, so the point could not be saved." end
|
||||
if !uid then return -1,"You are not logged in, so the relation could not be saved." end
|
||||
|
||||
relid = relid.to_i
|
||||
visible = visible.to_i
|
||||
|
@ -279,6 +282,7 @@ class AmfController < ApplicationController
|
|||
|
||||
uid = getuserid(usertoken)
|
||||
if !uid then return -1,"You are not logged in, so the way could not be saved." end
|
||||
|
||||
originalway = originalway.to_i
|
||||
|
||||
points.each do |a|
|
||||
|
@ -307,6 +311,7 @@ class AmfController < ApplicationController
|
|||
lat = n[1].to_f
|
||||
id = n[2].to_i
|
||||
savenode = false
|
||||
|
||||
if renumberednodes[id]
|
||||
id = renumberednodes[id]
|
||||
elsif id < 0
|
||||
|
@ -315,22 +320,26 @@ class AmfController < ApplicationController
|
|||
savenode = true
|
||||
else
|
||||
node = Node.find(id)
|
||||
if (!fpcomp(lat,node.lat) or !fpcomp(lon,node.lon) or \
|
||||
Tags.join(n[4])!=node.tags or node.visible==0)
|
||||
if !fpcomp(lat, node.lat) or !fpcomp(lon, node.lon) or
|
||||
Tags.join(n[4]) != node.tags or !node.visible?
|
||||
savenode = true
|
||||
end
|
||||
end
|
||||
|
||||
if savenode
|
||||
node.user_id = uid
|
||||
node.lat=lat; node.lon=lon
|
||||
node.lat = lat
|
||||
node.lon = lon
|
||||
node.tags = Tags.join(n[4])
|
||||
node.visible = true
|
||||
node.save_with_history!
|
||||
|
||||
if id != node.id
|
||||
renumberednodes[id] = node.id
|
||||
id = node.id
|
||||
end
|
||||
end
|
||||
|
||||
uniques = uniques - [id]
|
||||
nodes.push(id)
|
||||
end
|
||||
|
@ -339,6 +348,7 @@ class AmfController < ApplicationController
|
|||
|
||||
uniques.each do |n|
|
||||
deleteitemrelations(n, 'node')
|
||||
|
||||
node = Node.find(n)
|
||||
node.user_id = uid
|
||||
node.visible = false
|
||||
|
@ -370,8 +380,9 @@ class AmfController < ApplicationController
|
|||
id = id.to_i
|
||||
visible = (visible.to_i == 1)
|
||||
|
||||
if (id>0) then
|
||||
if id > 0 then
|
||||
node = Node.find(id)
|
||||
|
||||
if !visible then
|
||||
unless node.ways.empty? then return -1,"The point has since become part of a way, so you cannot save it as a POI." end
|
||||
deleteitemrelations(id, 'node')
|
||||
|
@ -381,13 +392,13 @@ class AmfController < ApplicationController
|
|||
end
|
||||
|
||||
node.user_id = uid
|
||||
node.latitude = (lat*10000000).round
|
||||
node.longitude = (lon*10000000).round
|
||||
node.lat = lat
|
||||
node.lon = lon
|
||||
node.tags = Tags.join(tags)
|
||||
node.visible = visible
|
||||
node.save_with_history!
|
||||
newid=node.id
|
||||
[0,id,newid]
|
||||
|
||||
[0, id, node.id]
|
||||
end
|
||||
|
||||
# Read POI from database
|
||||
|
@ -397,6 +408,7 @@ class AmfController < ApplicationController
|
|||
|
||||
def getpoi(id) #:doc:
|
||||
n = Node.find(id)
|
||||
|
||||
if n
|
||||
return [n.id, n.lon, n.lat, n.tags_as_hash]
|
||||
else
|
||||
|
@ -420,7 +432,8 @@ class AmfController < ApplicationController
|
|||
end
|
||||
|
||||
way.delete_with_relations_and_nodes_and_history(user)
|
||||
return [0,way_id]
|
||||
|
||||
[0, way_id]
|
||||
end
|
||||
|
||||
|
||||
|
@ -430,9 +443,11 @@ class AmfController < ApplicationController
|
|||
# Remove a node or way from all relations
|
||||
|
||||
def deleteitemrelations(objid, type) #:doc:
|
||||
relationids = RelationMember.find(:all, :conditions => ['member_type=? and member_id=?', type, objid]).collect { |ws| ws.id }.uniq
|
||||
relationids.each do |relid|
|
||||
rel=Relation.find(relid)
|
||||
relations = RelationMember.find(:all,
|
||||
:conditions => ['member_type = ? and member_id = ?', type, objid],
|
||||
:include => :relation).collect { |rm| rm.relation }.uniq
|
||||
|
||||
relations.each do |rel|
|
||||
rel.members.delete_if { |x| x[0] == type and x[1] == objid }
|
||||
rel.save_with_history!
|
||||
end
|
||||
|
@ -545,3 +560,8 @@ class AmfController < ApplicationController
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
# Local Variables:
|
||||
# indent-tabs-mode: t
|
||||
# tab-width: 4
|
||||
# End:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue