- 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_numericality_of :id, :on => :update, :integer_only => true
validates_numericality_of :changeset_id, :version, :integer_only => true
validates_associated :changeset
validates_numericality_of :id, :on => :update, :integer_only => true
validates_numericality_of :changeset_id, :version, :integer_only => true
validates_associated :changeset
scope :visible, -> { where(:visible => true) }
scope :invisible, -> { where(:visible => false) }
scope :nodes, ->(*ids) { joins(:relation_members).where(:current_relation_members => { :member_type => "Node", :member_id => ids.flatten }) }
scope :ways, ->(*ids) { joins(:relation_members).where(:current_relation_members => { :member_type => "Way", :member_id => ids.flatten }) }
scope :relations, ->(*ids) { joins(:relation_members).where(:current_relation_members => { :member_type => "Relation", :member_id => ids.flatten }) }
scope :visible, -> { where(:visible => true) }
scope :invisible, -> { where(:visible => false) }
scope :nodes, ->(*ids) { joins(:relation_members).where(:current_relation_members => { :member_type => "Node", :member_id => ids.flatten }) }
scope :ways, ->(*ids) { joins(:relation_members).where(:current_relation_members => { :member_type => "Way", :member_id => ids.flatten }) }
scope :relations, ->(*ids) { joins(:relation_members).where(:current_relation_members => { :member_type => "Relation", :member_id => ids.flatten }) }
- def self.from_xml(xml, create=false)
- begin
- p = XML::Parser.string(xml)
- doc = p.parse
+ def self.from_xml(xml, create = false)
+ p = XML::Parser.string(xml)
+ doc = p.parse
- doc.find('//osm/relation').each do |pt|
- return Relation.from_xml_node(pt, create)
- end
- raise OSM::APIBadXMLError.new("node", xml, "XML doesn't contain an osm/relation element.")
- rescue LibXML::XML::Error, ArgumentError => ex
- raise OSM::APIBadXMLError.new("relation", xml, ex.message)
+ doc.find('//osm/relation').each do |pt|
+ return Relation.from_xml_node(pt, create)
+ fail OSM::APIBadXMLError.new("node", xml, "XML doesn't contain an osm/relation element.")
+ rescue LibXML::XML::Error, ArgumentError => ex
+ raise OSM::APIBadXMLError.new("relation", xml, ex.message)
- raise OSM::APIBadXMLError.new("relation", pt, "Version is required when updating") unless create or not pt['version'].nil?
+ fail OSM::APIBadXMLError.new("relation", pt, "Version is required when updating") unless create || !pt['version'].nil?
- raise OSM::APIBadXMLError.new("relation", pt, "tag is missing key") if tag['k'].nil?
- raise OSM::APIBadXMLError.new("relation", pt, "tag is missing value") if tag['v'].nil?
+ fail OSM::APIBadXMLError.new("relation", pt, "tag is missing key") if tag['k'].nil?
+ fail OSM::APIBadXMLError.new("relation", pt, "tag is missing value") if tag['v'].nil?
relation.add_tag_keyval(tag['k'], tag['v'])
end
# need to initialise the relation members array explicitly, as if this
relation.add_tag_keyval(tag['k'], tag['v'])
end
# need to initialise the relation members array explicitly, as if this
- # isn't done for a new relation then @members attribute will be nil,
- # and the members will be loaded from the database instead of being
+ # isn't done for a new relation then @members attribute will be nil,
+ # and the members will be loaded from the database instead of being
- raise OSM::APIBadXMLError.new("relation", pt, "The #{member['type']} is not allowed only, #{TYPES.inspect} allowed") unless TYPES.include? member['type']
+ fail OSM::APIBadXMLError.new("relation", pt, "The #{member['type']} is not allowed only, #{TYPES.inspect} allowed") unless TYPES.include? member['type']
member['role'] ||= "" # Allow the upload to not include this, in which case we default to an empty string.
logger.debug member['role']
relation.add_member(member['type'].classify, member['ref'], member['role'])
end
member['role'] ||= "" # Allow the upload to not include this, in which case we default to an empty string.
logger.debug member['role']
relation.add_member(member['type'].classify, member['ref'], member['role'])
end
end
def to_xml_node(visible_members = nil, changeset_cache = {}, user_display_name_cache = {})
el = XML::Node.new 'relation'
end
def to_xml_node(visible_members = nil, changeset_cache = {}, user_display_name_cache = {})
el = XML::Node.new 'relation'
add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
if visible_members
# if there is a list of visible members then use that to weed out deleted segments
if visible_members[member.member_type][member.member_id]
if visible_members
# if there is a list of visible members then use that to weed out deleted segments
if visible_members[member.member_type][member.member_id]
end
else
# otherwise, manually go to the db to check things
if member.member.visible?
end
else
# otherwise, manually go to the db to check things
if member.member.visible?
# duplicate tags are now forbidden, so we can't allow values
# in the hash to be overwritten.
# duplicate tags are now forbidden, so we can't allow values
# in the hash to be overwritten.
# the element with given +type+ and +id+. this only works with nodes
# and ways at the moment, as they're the only elements to respond to
# the :bbox call.
def update_changeset_element(type, id)
element = Kernel.const_get(type.capitalize).find(id)
changeset.update_bbox! element.bbox
# the element with given +type+ and +id+. this only works with nodes
# and ways at the moment, as they're the only elements to respond to
# the :bbox call.
def update_changeset_element(type, id)
element = Kernel.const_get(type.capitalize).find(id)
changeset.update_bbox! element.bbox
# provide repeatable reads for the used-by checks. this means it
# shouldn't be possible to get race conditions.
Relation.transaction do
self.lock!
check_consistency(self, new_relation, user)
# This will check to see if this relation is used by another relation
# provide repeatable reads for the used-by checks. this means it
# shouldn't be possible to get race conditions.
Relation.transaction do
self.lock!
check_consistency(self, new_relation, user)
# This will check to see if this relation is used by another relation
- rel = RelationMember.joins(:relation).where("visible = ? AND member_type = 'Relation' and member_id = ? ", true, self.id).first
- raise OSM::APIPreconditionFailedError.new("The relation #{new_relation.id} is used in relation #{rel.relation.id}.") unless rel.nil?
+ rel = RelationMember.joins(:relation).where("visible = ? AND member_type = 'Relation' and member_id = ? ", true, id).first
+ fail OSM::APIPreconditionFailedError.new("The relation #{new_relation.id} is used in relation #{rel.relation.id}.") unless rel.nil?
- unless new_relation.preconditions_ok?(self.members)
- raise OSM::APIPreconditionFailedError.new("Cannot update relation #{self.id}: data or member data is invalid.")
+ unless new_relation.preconditions_ok?(members)
+ fail OSM::APIPreconditionFailedError.new("Cannot update relation #{id}: data or member data is invalid.")
def create_with_history(user)
check_create_consistency(self, user)
unless self.preconditions_ok?
def create_with_history(user)
check_create_consistency(self, user)
unless self.preconditions_ok?
# relevant hash table, with the value true as a cache.
# Thus if you have nodes with the ids of 50 and 1 already in the
# relation, then the hash table nodes would contain:
# => {50=>true, 1=>true}
# relevant hash table, with the value true as a cache.
# Thus if you have nodes with the ids of 50 and 1 already in the
# relation, then the hash table nodes would contain:
# => {50=>true, 1=>true}
# pre-set all existing members to good
good_members.each { |m| elements[m[0].downcase.to_sym][m[1]] = true }
# pre-set all existing members to good
good_members.each { |m| elements[m[0].downcase.to_sym][m[1]] = true }
- unless element and element.visible? and element.preconditions_ok?
- raise OSM::APIPreconditionFailedError.new("Relation with id #{self.id} cannot be saved due to #{m[0]} with id #{m[1]}")
+ unless element && element.visible? && element.preconditions_ok?
+ fail OSM::APIPreconditionFailedError.new("Relation with id #{id} cannot be saved due to #{m[0]} with id #{m[1]}")
key = old_tag.k
# if we can match the tags we currently have to the list
# of old tags, then we never set the tags_changed flag. but
key = old_tag.k
# if we can match the tags we currently have to the list
# of old tags, then we never set the tags_changed flag. but
tags_changed |= (old_tag.v != tags[key])
# remove from the map, so that we can expect an empty map
tags_changed |= (old_tag.v != tags[key])
# remove from the map, so that we can expect an empty map
- tags_changed |= (not tags.empty?)
- RelationTag.delete_all(:relation_id => self.id)
- self.tags.each do |k,v|
+ tags_changed |= (!tags.empty?)
+ RelationTag.delete_all(:relation_id => id)
+ self.tags.each do |k, v|
# same pattern as before, but this time we're collecting the
# changed members in an array, as the bounding box updates for
# elements are per-element, not blanked on/off like for tags.
# same pattern as before, but this time we're collecting the
# changed members in an array, as the bounding box updates for
# elements are per-element, not blanked on/off like for tags.
# members may be in a different order and i don't feel like implementing
# a longest common subsequence algorithm to optimise this.
members = self.members
# members may be in a different order and i don't feel like implementing
# a longest common subsequence algorithm to optimise this.
members = self.members
- RelationMember.delete_all(:relation_id => self.id)
- members.each_with_index do |m,i|
+ RelationMember.delete_all(:relation_id => id)
+ members.each_with_index do |m, i|
# bounding box. this is similar to how the map call does things and is
# reasonable on the assumption that adding or removing members doesn't
# materially change the rest of the relation.
# bounding box. this is similar to how the map call does things and is
# reasonable on the assumption that adding or removing members doesn't
# materially change the rest of the relation.
- any_relations =
- changed_members.collect { |id,type| type == "relation" }.
- inject(false) { |b,s| b or s }
+ any_relations =
+ changed_members.collect { |_id, type| type == "relation" }
+ .inject(false) { |b, s| b || s }
# add all non-relation bounding boxes to the changeset
# FIXME: check for tag changes along with element deletions and
# make sure that the deleted element's bounding box is hit.
self.members
# add all non-relation bounding boxes to the changeset
# FIXME: check for tag changes along with element deletions and
# make sure that the deleted element's bounding box is hit.
self.members