- # ----- putpoi
- # save POI to the database
- # in: [0] user token (string),
- # [1] original node id (may be negative),
- # [2] projected longitude, [3] projected latitude,
- # [4] hash of tags, [5] visible (0 to delete, 1 otherwise),
- # [6] baselong, [7] basey, [8] masterscale
- # does: saves POI node to the database
- # refuses save if the node has since become part of a way
- # out: [0] 0 (success), [1] original node id (unchanged),
- # [2] new node id
- def putpoi(args) #:doc:
- usertoken,id,x,y,tags,visible,baselong,basey,masterscale=args
- uid=getuserid(usertoken)
- if !uid then return -1,"You are not logged in, so the point could not be saved." end
-
- db_now='@now'+(rand*100).to_i.to_s+uid.to_s+id.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()")
-
- id=id.to_i
- visible=visible.to_i
- if visible==0 then
- # if deleting, check node hasn't become part of a way
- 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")
- unless inway.nil? then return -1,"The point has since become part of a way, so you cannot save it as a POI." end
- deleteitemrelations(id,'node',uid,db_now)
- end
-
- x=coord2long(x.to_f,masterscale,baselong)
- y=coord2lat(y.to_f,masterscale,basey)
- tagsql="'"+sqlescape(array2tag(tags))+"'"
- lat=(y * 10000000).round
- long=(x * 10000000).round
- tile=QuadTile.tile_for_point(y, x)
-
- if (id>0) then
- 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})");
- 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}");
- newid=id
- else
- 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})");
- 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})");
- end
- [0,id,newid]
+ # Save a way to the database, including all nodes. Any nodes in the previous
+ # version and no longer used are deleted.
+ #
+ # Returns:
+ # 0. '0' (code for success),
+ # 1. original way id (unchanged),
+ # 2. new way id,
+ # 3. hash of renumbered nodes (old id=>new id)
+
+ def putway(renumberednodes, usertoken, originalway, points, attributes) #:doc:
+
+ # -- Initialise and carry out checks
+
+ 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|
+ if a[2] == 0 or a[2].nil? then return -2,"Server error - node with id 0 found in way #{originalway}." end
+ if a[1] == 90 then return -2,"Server error - node with lat -90 found in way #{originalway}." end
+ end
+
+ if points.length < 2 then return -2,"Server error - way is only #{points.length} points long." end
+
+ # -- Get unique nodes
+
+ if originalway < 0
+ way = Way.new
+ uniques = []
+ else
+ way = Way.find(originalway)
+ uniques = way.unshared_node_ids
+ end
+
+ # -- Compare nodes and save changes to any that have changed
+
+ nodes = []
+
+ points.each do |n|
+ lon = n[0].to_f
+ lat = n[1].to_f
+ id = n[2].to_i
+ savenode = false
+
+ if renumberednodes[id]
+ id = renumberednodes[id]
+ elsif id < 0
+ # Create new node
+ node = Node.new
+ savenode = true
+ else
+ node = Node.find(id)
+ nodetags=node.tags_as_hash
+ nodetags.delete('created_by')
+ if !fpcomp(lat, node.lat) or !fpcomp(lon, node.lon) or
+ n[4] != nodetags or !node.visible?
+ savenode = true
+ end
+ end
+
+ if savenode
+ node.user_id = uid
+ 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
+
+ # -- Delete any unique nodes
+
+ uniques.each do |n|
+ deleteitemrelations(n, 'node')
+
+ node = Node.find(n)
+ node.user_id = uid
+ node.visible = false
+ node.save_with_history!
+ end
+
+ # -- Save revised way
+
+ way.tags = attributes
+ way.nds = nodes
+ way.user_id = uid
+ way.visible = true
+ way.save_with_history!
+
+ [0, originalway, way.id, renumberednodes]