X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/52e5fa6ad78f375cec5a207a123a4aa3939c8ddf..34e3e51456774127d43408b7ab65c24f41373f62:/app/models/node.rb diff --git a/app/models/node.rb b/app/models/node.rb index 0f176067d..755dc82ce 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -3,23 +3,25 @@ class Node < ActiveRecord::Base include GeoRecord include ConsistencyValidations + include NotRedactable + include ObjectMetadata - set_table_name 'current_nodes' + self.table_name = "current_nodes" belongs_to :changeset - has_many :old_nodes, :order => :version + has_many :old_nodes, -> { order(:version) } has_many :way_nodes 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 @@ -30,38 +32,14 @@ class Node < ActiveRecord::Base validate :validate_position validates_associated :changeset - scope :visible, where(:visible => true) - scope :invisible, where(:visible => false) + scope :visible, -> { where(:visible => true) } + scope :invisible, -> { where(:visible => false) } # Sanity check the latitude and longitude and add an error if it's broken def validate_position errors.add(:base, "Node is not in the world") unless in_world? end - # - # Search for nodes matching tags within bounding_box - # - # Also adheres to limitations such as within max_number_of_nodes - # - def self.search(bounding_box, tags = {}) - # @fixme a bit of a hack to search for only visible nodes - # couldn't think of another to add to tags condition - #conditions_hash = tags.merge({ 'visible' => 1 }) - - # using named placeholders http://www.robbyonrails.com/articles/2005/10/21/using-named-placeholders-in-ruby - #keys = [] - #values = {} - - #conditions_hash.each do |key,value| - # keys << "#{key} = :#{key}" - # values[key.to_sym] = value - #end - #conditions = keys.join(' AND ') - - find_by_area(bounding_box, :conditions => {:visible => true}, - :limit => MAX_NUMBER_OF_NODES+1) - end - # Read in xml as text and return it's Node object representation def self.from_xml(xml, create=false) begin @@ -79,11 +57,11 @@ class Node < ActiveRecord::Base 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 = pt['lat'].to_f - node.lon = pt['lon'].to_f + node.lat = OSM.parse_float(pt['lat'], OSM::APIBadXMLError, "node", pt, "lat not a number") + node.lon = OSM.parse_float(pt['lon'], OSM::APIBadXMLError, "node", pt, "lon not a number") raise OSM::APIBadXMLError.new("node", pt, "Changeset id is missing") if pt['changeset'].nil? node.changeset_id = pt['changeset'].to_i @@ -96,7 +74,7 @@ class Node < ActiveRecord::Base 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 @@ -106,6 +84,10 @@ class Node < ActiveRecord::Base # and set manually before the actual delete node.visible = true + # Start with no tags + node.tags = Hash.new + + # Add in any tags from the XML pt.find('tag').each do |tag| raise OSM::APIBadXMLError.new("node", pt, "tag is missing key") if tag['k'].nil? raise OSM::APIBadXMLError.new("node", pt, "tag is missing value") if tag['v'].nil? @@ -128,7 +110,7 @@ class Node < ActiveRecord::Base 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 @@ -136,17 +118,17 @@ class Node < ActiveRecord::Base 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 @@ -155,27 +137,27 @@ class Node < ActiveRecord::Base 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 @@ -194,44 +176,19 @@ class Node < ActiveRecord::Base end def to_xml_node(changeset_cache = {}, user_display_name_cache = {}) - el1 = XML::Node.new 'node' - el1['id'] = self.id.to_s - el1['lat'] = self.lat.to_s - el1['lon'] = self.lon.to_s - el1['version'] = self.version.to_s - el1['changeset'] = self.changeset_id.to_s - - if changeset_cache.key?(self.changeset_id) - # use the cache if available - else - changeset_cache[self.changeset_id] = self.changeset.user_id - end + el = XML::Node.new 'node' + el['id'] = self.id.to_s - user_id = changeset_cache[self.changeset_id] + add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache) - if user_display_name_cache.key?(user_id) - # use the cache if available - elsif self.changeset.user.data_public? - user_display_name_cache[user_id] = self.changeset.user.display_name - else - user_display_name_cache[user_id] = nil + if self.visible? + el['lat'] = self.lat.to_s + el['lon'] = self.lon.to_s end - if not user_display_name_cache[user_id].nil? - el1['user'] = user_display_name_cache[user_id] - el1['uid'] = user_id.to_s - end - - self.tags.each do |k,v| - el2 = XML::Node.new('tag') - el2['k'] = k.to_s - el2['v'] = v.to_s - el1 << el2 - end + add_tags_to_xml_node(el, self.node_tags) - el1['visible'] = self.visible.to_s - el1['timestamp'] = self.timestamp.xmlschema - return el1 + return el end def tags_as_hash @@ -239,18 +196,12 @@ class Node < ActiveRecord::Base 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 @@ -275,7 +226,7 @@ class Node < ActiveRecord::Base 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! @@ -291,10 +242,10 @@ class Node < ActiveRecord::Base 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) @@ -308,5 +259,5 @@ class Node < ActiveRecord::Base changeset.save! end end - + end