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