]> git.openstreetmap.org Git - rails.git/blob - app/models/node.rb
Add some extra indexes.
[rails.git] / app / models / node.rb
1 class Node < ActiveRecord::Base
2   require 'xml/libxml'
3
4   include GeoRecord
5
6   set_table_name 'current_nodes'
7   
8   validates_presence_of :user_id, :timestamp
9   validates_inclusion_of :visible, :in => [ true, false ]
10   validates_numericality_of :latitude, :longitude
11   validate :validate_position
12
13   belongs_to :user
14
15   has_many :old_nodes, :foreign_key => :id
16
17   has_many :way_nodes
18   has_many :ways, :through => :way_nodes
19
20   has_many :old_way_nodes
21   has_many :ways_via_history, :class_name=> "Way", :through => :old_way_nodes, :source => :way
22
23   has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
24   has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
25
26   # Sanity check the latitude and longitude and add an error if it's broken
27   def validate_position
28     errors.add_to_base("Node is not in the world") unless in_world?
29   end
30
31   #
32   # Search for nodes matching tags within bounding_box
33   #
34   # Also adheres to limitations such as within max_number_of_nodes
35   #
36   def self.search(bounding_box, tags = {})
37     min_lon, min_lat, max_lon, max_lat = *bounding_box
38     # @fixme a bit of a hack to search for only visible nodes
39     # couldn't think of another to add to tags condition
40     #conditions_hash = tags.merge({ 'visible' => 1 })
41   
42     # using named placeholders http://www.robbyonrails.com/articles/2005/10/21/using-named-placeholders-in-ruby
43     #keys = []
44     #values = {}
45
46     #conditions_hash.each do |key,value|
47     #  keys <<  "#{key} = :#{key}"
48     #  values[key.to_sym] = value
49     #end 
50     #conditions = keys.join(' AND ')
51  
52     find_by_area(min_lat, min_lon, max_lat, max_lon,
53                     :conditions => 'visible = 1',
54                     :limit => APP_CONFIG['max_number_of_nodes']+1)  
55   end
56
57   # Read in xml as text and return it's Node object representation
58   def self.from_xml(xml, create=false)
59     begin
60       p = XML::Parser.new
61       p.string = xml
62       doc = p.parse
63   
64       node = Node.new
65
66       doc.find('//osm/node').each do |pt|
67         node.lat = pt['lat'].to_f
68         node.lon = pt['lon'].to_f
69
70         return nil unless node.in_world?
71
72         unless create
73           if pt['id'] != '0'
74             node.id = pt['id'].to_i
75           end
76         end
77
78         node.visible = pt['visible'] and pt['visible'] == 'true'
79
80         if create
81           node.timestamp = Time.now
82         else
83           if pt['timestamp']
84             node.timestamp = Time.parse(pt['timestamp'])
85           end
86         end
87
88         tags = []
89
90         pt.find('tag').each do |tag|
91           tags << [tag['k'],tag['v']]
92         end
93
94         node.tags = Tags.join(tags)
95       end
96     rescue
97       node = nil
98     end
99
100     return node
101   end
102
103   # Save this node with the appropriate OldNode object to represent it's history.
104   def save_with_history!
105     Node.transaction do
106       self.timestamp = Time.now
107       self.save!
108       old_node = OldNode.from_node(self)
109       old_node.save!
110     end
111   end
112
113   # Turn this Node in to a complete OSM XML object with <osm> wrapper
114   def to_xml
115     doc = OSM::API.new.get_xml_doc
116     doc.root << to_xml_node()
117     return doc
118   end
119
120   # Turn this Node in to an XML Node without the <osm> wrapper.
121   def to_xml_node(user_display_name_cache = nil)
122     el1 = XML::Node.new 'node'
123     el1['id'] = self.id.to_s
124     el1['lat'] = self.lat.to_s
125     el1['lon'] = self.lon.to_s
126
127     user_display_name_cache = {} if user_display_name_cache.nil?
128
129     if user_display_name_cache and user_display_name_cache.key?(self.user_id)
130       # use the cache if available
131     elsif self.user.data_public?
132       user_display_name_cache[self.user_id] = self.user.display_name
133     else
134       user_display_name_cache[self.user_id] = nil
135     end
136
137     el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
138
139     Tags.split(self.tags) do |k,v|
140       el2 = XML::Node.new('tag')
141       el2['k'] = k.to_s
142       el2['v'] = v.to_s
143       el1 << el2
144     end
145
146     el1['visible'] = self.visible.to_s
147     el1['timestamp'] = self.timestamp.xmlschema
148     return el1
149   end
150
151   # Return the node's tags as a Hash of keys and their values
152   def tags_as_hash
153     hash = {}
154     Tags.split(self.tags) do |k,v|
155       hash[k] = v
156     end
157     hash
158   end
159 end