1 # == Schema Information
3 # Table name: current_nodes
5 # id :bigint(8) not null, primary key
6 # latitude :integer not null
7 # longitude :integer not null
8 # changeset_id :bigint(8) not null
9 # visible :boolean not null
10 # timestamp :datetime not null
11 # tile :bigint(8) not null
12 # version :bigint(8) not null
16 # current_nodes_tile_idx (tile)
17 # current_nodes_timestamp_idx (timestamp)
21 # current_nodes_changeset_id_fkey (changeset_id => changesets.id)
24 class Node < ApplicationRecord
28 include ConsistencyValidations
31 self.table_name = "current_nodes"
35 has_many :old_nodes, -> { order(:version) }, :inverse_of => :current_node
38 has_many :ways, :through => :way_nodes
42 has_many :old_way_nodes
43 has_many :ways_via_history, :class_name => "Way", :through => :old_way_nodes, :source => :way
45 has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
46 has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation
48 validates :id, :uniqueness => true, :presence => { :on => :update },
49 :numericality => { :on => :update, :only_integer => true }
50 validates :version, :presence => true,
51 :numericality => { :only_integer => true }
52 validates :latitude, :presence => true,
53 :numericality => { :only_integer => true }
54 validates :longitude, :presence => true,
55 :numericality => { :only_integer => true }
56 validates :timestamp, :presence => true
57 validates :changeset, :associated => true
58 validates :visible, :inclusion => [true, false]
60 validate :validate_position
62 scope :visible, -> { where(:visible => true) }
63 scope :invisible, -> { where(:visible => false) }
65 # Sanity check the latitude and longitude and add an error if it's broken
67 errors.add(:base, "Node is not in the world") unless in_world?
70 # Read in xml as text and return it's Node object representation
71 def self.from_xml(xml, create: false)
72 p = XML::Parser.string(xml, :options => XML::Parser::Options::NOERROR)
74 pt = doc.find_first("//osm/node")
77 Node.from_xml_node(pt, :create => create)
79 raise OSM::APIBadXMLError.new("node", xml, "XML doesn't contain an osm/node element.")
81 rescue LibXML::XML::Error, ArgumentError => e
82 raise OSM::APIBadXMLError.new("node", xml, e.message)
85 def self.from_xml_node(pt, create: false)
88 raise OSM::APIBadXMLError.new("node", pt, "lat missing") if pt["lat"].nil?
89 raise OSM::APIBadXMLError.new("node", pt, "lon missing") if pt["lon"].nil?
91 node.lat = OSM.parse_float(pt["lat"], OSM::APIBadXMLError, "node", pt, "lat not a number")
92 node.lon = OSM.parse_float(pt["lon"], OSM::APIBadXMLError, "node", pt, "lon not a number")
93 raise OSM::APIBadXMLError.new("node", pt, "Changeset id is missing") if pt["changeset"].nil?
95 node.changeset_id = pt["changeset"].to_i
97 raise OSM::APIBadUserInput, "The node is outside this world" unless node.in_world?
99 # version must be present unless creating
100 raise OSM::APIBadXMLError.new("node", pt, "Version is required when updating") unless create || !pt["version"].nil?
102 node.version = create ? 0 : pt["version"].to_i
105 raise OSM::APIBadXMLError.new("node", pt, "ID is required when updating.") if pt["id"].nil?
107 node.id = pt["id"].to_i
108 # .to_i will return 0 if there is no number that can be parsed.
109 # We want to make sure that there is no id with zero anyway
110 raise OSM::APIBadUserInput, "ID of node cannot be zero when updating." if node.id.zero?
113 # We don't care about the time, as it is explicitly set on create/update/delete
114 # We don't care about the visibility as it is implicit based on the action
115 # and set manually before the actual delete
121 # Add in any tags from the XML
122 pt.find("tag").each do |tag|
123 raise OSM::APIBadXMLError.new("node", pt, "tag is missing key") if tag["k"].nil?
124 raise OSM::APIBadXMLError.new("node", pt, "tag is missing value") if tag["v"].nil?
126 node.add_tag_key_val(tag["k"], tag["v"])
133 # the bounding box around a node, which is used for determining the changeset's
136 BoundingBox.new(longitude, latitude, longitude, latitude)
139 # Should probably be renamed delete_from to come in line with update
140 def delete_with_history!(new_node, user)
141 raise OSM::APIAlreadyDeletedError.new("node", new_node.id) unless visible
143 # need to start the transaction here, so that the database can
144 # provide repeatable reads for the used-by checks. this means it
145 # shouldn't be possible to get race conditions.
148 check_consistency(self, new_node, user)
149 ways = Way.joins(:way_nodes).where(:visible => true, :current_way_nodes => { :node_id => id }).order(:id)
150 raise OSM::APIPreconditionFailedError, "Node #{id} is still used by ways #{ways.collect(&:id).join(',')}." unless ways.empty?
152 rels = Relation.joins(:relation_members).where(:visible => true, :current_relation_members => { :member_type => "Node", :member_id => id }).order(:id)
153 raise OSM::APIPreconditionFailedError, "Node #{id} is still used by relations #{rels.collect(&:id).join(',')}." unless rels.empty?
155 self.changeset_id = new_node.changeset_id
159 # update the changeset with the deleted position
160 changeset.update_bbox!(bbox)
166 def update_from(new_node, user)
169 check_consistency(self, new_node, user)
171 # update changeset first
172 self.changeset_id = new_node.changeset_id
173 self.changeset = new_node.changeset
175 # update changeset bbox with *old* position first
176 changeset.update_bbox!(bbox)
178 # FIXME: logic needs to be double checked
179 self.latitude = new_node.latitude
180 self.longitude = new_node.longitude
181 self.tags = new_node.tags
184 # update changeset bbox with *new* position
185 changeset.update_bbox!(bbox)
191 def create_with_history(user)
192 check_create_consistency(self, user)
196 # update the changeset to include the new location
197 changeset.update_bbox!(bbox)
207 @tags ||= node_tags.to_h { |t| [t.k, t.v] }
212 def add_tag_key_val(k, v)
215 # duplicate tags are now forbidden, so we can't allow values
216 # in the hash to be overwritten.
217 raise OSM::APIDuplicateTagsError.new("node", id, k) if @tags.include? k
223 # are the preconditions OK? this is mainly here to keep the duck
224 # typing interface the same between nodes, ways and relations.
225 def preconditions_ok?
230 # dummy method to make the interfaces of node, way and relation
232 def fix_placeholders!(_id_map, _placeholder_id = nil)
233 # nodes don't refer to anything, so there is nothing to do here
238 def save_with_history!
245 # clone the object before saving it so that the original is
246 # still marked as dirty if we retry the transaction
251 NodeTag.where(:node_id => id).delete_all
261 old_node = OldNode.from_node(self)
262 old_node.timestamp = t
263 old_node.save_with_dependencies!
265 # tell the changeset we updated one element only
266 changeset.add_changes! 1
268 # save the changeset in case of bounding box updates