has_many :ways, :through => :way_nodes
has_many :node_tags
-
+
has_many :old_way_nodes
has_many :ways_via_history, :class_name=> "Way", :through => :old_way_nodes, :source => :way
has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
- has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
+ has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation
validates_presence_of :id, :on => :update
validates_presence_of :timestamp,:version, :changeset_id
def self.from_xml_node(pt, create=false)
node = Node.new
-
+
raise OSM::APIBadXMLError.new("node", pt, "lat missing") if pt['lat'].nil?
raise OSM::APIBadXMLError.new("node", pt, "lon missing") if pt['lon'].nil?
node.lat = OSM.parse_float(pt['lat'], OSM::APIBadXMLError, "node", pt, "lat not a number")
unless create
raise OSM::APIBadXMLError.new("node", pt, "ID is required when updating.") if pt['id'].nil?
node.id = pt['id'].to_i
- # .to_i will return 0 if there is no number that can be parsed.
+ # .to_i will return 0 if there is no number that can be parsed.
# We want to make sure that there is no id with zero anyway
raise OSM::APIBadUserInput.new("ID of node cannot be zero when updating.") if node.id == 0
end
raise OSM::APIAlreadyDeletedError.new("node", new_node.id)
end
- # need to start the transaction here, so that the database can
+ # need to start the transaction here, so that the database can
# provide repeatable reads for the used-by checks. this means it
# shouldn't be possible to get race conditions.
Node.transaction do
check_consistency(self, new_node, user)
ways = Way.joins(:way_nodes).where(:visible => true, :current_way_nodes => { :node_id => id }).order(:id)
raise OSM::APIPreconditionFailedError.new("Node #{self.id} is still used by ways #{ways.collect { |w| w.id }.join(",")}.") unless ways.empty?
-
+
rels = Relation.joins(:relation_members).where(:visible => true, :current_relation_members => { :member_type => "Node", :member_id => id }).order(:id)
raise OSM::APIPreconditionFailedError.new("Node #{self.id} is still used by relations #{rels.collect { |r| r.id }.join(",")}.") unless rels.empty?
self.changeset_id = new_node.changeset_id
self.tags = {}
self.visible = false
-
+
# update the changeset with the deleted position
changeset.update_bbox!(bbox)
-
+
save_with_history!
end
end
Node.transaction do
self.lock!
check_consistency(self, new_node, user)
-
+
# update changeset first
self.changeset_id = new_node.changeset_id
self.changeset = new_node.changeset
-
+
# update changeset bbox with *old* position first
changeset.update_bbox!(bbox);
-
+
# FIXME logic needs to be double checked
- self.latitude = new_node.latitude
+ self.latitude = new_node.latitude
self.longitude = new_node.longitude
self.tags = new_node.tags
self.visible = true
-
+
# update changeset bbox with *new* position
changeset.update_bbox!(bbox);
-
+
save_with_history!
end
end
-
+
def create_with_history(user)
check_create_consistency(self, user)
self.version = 0
end
def tags
- unless @tags
- @tags = {}
- self.node_tags.each do |tag|
- @tags[tag.k] = tag.v
- end
- end
- @tags
+ @tags ||= Hash[self.node_tags.collect { |t| [t.k, t.v] }]
end
def tags=(t)
- @tags = t
- end
+ @tags = t
+ end
def add_tag_key_val(k,v)
@tags = Hash.new unless @tags
def fix_placeholders!(id_map, placeholder_id = nil)
# nodes don't refer to anything, so there is nothing to do here
end
-
+
private
def save_with_history!
tags.each do |k,v|
tag = NodeTag.new
tag.node_id = self.id
- tag.k = k
- tag.v = v
+ tag.k = k
+ tag.v = v
tag.save!
- end
+ end
# Create an OldNode
old_node = OldNode.from_node(self)
changeset.save!
end
end
-
+
end