]> git.openstreetmap.org Git - rails.git/blob - app/models/trace.rb
Merge 16743:16811 from trunk.
[rails.git] / app / models / trace.rb
1 class Trace < ActiveRecord::Base
2   set_table_name 'gpx_files'
3
4   validates_presence_of :user_id, :name, :timestamp
5   validates_presence_of :description, :on => :create
6   validates_length_of :name, :maximum => 255
7   validates_length_of :description, :maximum => 255
8 #  validates_numericality_of :latitude, :longitude
9   validates_inclusion_of :public, :inserted, :in => [ true, false]
10   
11   belongs_to :user
12   has_many :tags, :class_name => 'Tracetag', :foreign_key => 'gpx_id', :dependent => :delete_all
13   has_many :points, :class_name => 'Tracepoint', :foreign_key => 'gpx_id', :dependent => :delete_all
14
15   def destroy
16     super
17     FileUtils.rm_f(trace_name)
18     FileUtils.rm_f(icon_picture_name)
19     FileUtils.rm_f(large_picture_name)
20   end
21
22   def tagstring
23     return tags.collect {|tt| tt.tag}.join(", ")
24   end
25
26   def tagstring=(s)
27     if s.include?','
28       self.tags = s.split(/\s*,\s*/).collect {|tag|
29         tt = Tracetag.new
30         tt.tag = tag
31         tt
32       }
33     else
34       #do as before for backwards compatibility:
35       self.tags = s.split().collect {|tag|
36         tt = Tracetag.new
37         tt.tag = tag
38         tt
39       }
40     end
41   end
42   
43   def large_picture= (data)
44     f = File.new(large_picture_name, "wb")
45     f.syswrite(data)
46     f.close
47   end
48   
49   def icon_picture= (data)
50     f = File.new(icon_picture_name, "wb")
51     f.syswrite(data)
52     f.close
53   end
54
55   def large_picture
56     f = File.new(large_picture_name, "rb")
57     logger.info "large picture file: '#{f.path}', bytes: #{File.size(f.path)}"
58     data = f.sysread(File.size(f.path))
59     logger.info "have read data, bytes: '#{data.length}'"
60     f.close
61     data
62   end
63   
64   def icon_picture
65     f = File.new(icon_picture_name, "rb")
66     logger.info "icon picture file: '#{f.path}'"
67     data = f.sysread(File.size(f.path))
68     f.close
69     data
70   end
71   
72   def large_picture_name
73     "#{GPX_IMAGE_DIR}/#{id}.gif"
74   end
75
76   def icon_picture_name
77     "#{GPX_IMAGE_DIR}/#{id}_icon.gif"
78   end
79
80   def trace_name
81     "#{GPX_TRACE_DIR}/#{id}.gpx"
82   end
83
84   def mime_type
85     filetype = `/usr/bin/file -bz #{trace_name}`.chomp
86     gzipped = filetype =~ /gzip compressed/
87     bzipped = filetype =~ /bzip2 compressed/
88     zipped = filetype =~ /Zip archive/
89
90     if gzipped then
91       mimetype = "application/x-gzip"
92     elsif bzipped then
93       mimetype = "application/x-bzip2"
94     elsif zipped
95       mimetype = "application/x-zip"
96     else
97       mimetype = "text/xml"
98     end
99
100     return mimetype
101   end
102
103   def extension_name
104     filetype = `/usr/bin/file -bz #{trace_name}`.chomp
105     gzipped = filetype =~ /gzip compressed/
106     bzipped = filetype =~ /bzip2 compressed/
107     zipped = filetype =~ /Zip archive/
108     tarred = filetype =~ /tar archive/
109
110     if tarred and gzipped then
111       extension = ".tar.gz"
112     elsif tarred and bzipped then
113       extension = ".tar.bz2"
114     elsif tarred
115       extension = ".tar"
116     elsif gzipped
117       extension = ".gpx.gz"
118     elsif bzipped
119       extension = ".gpx.bz2"
120     elsif zipped
121       extension = ".zip"
122     else
123       extension = ".gpx"
124     end
125
126     return extension
127   end
128
129   def to_xml
130     doc = OSM::API.new.get_xml_doc
131     doc.root << to_xml_node()
132     return doc
133   end
134
135   def to_xml_node
136     el1 = XML::Node.new 'gpx_file'
137     el1['id'] = self.id.to_s
138     el1['name'] = self.name.to_s
139     el1['lat'] = self.latitude.to_s
140     el1['lon'] = self.longitude.to_s
141     el1['user'] = self.user.display_name
142     el1['public'] = self.public.to_s
143     el1['pending'] = (!self.inserted).to_s
144     el1['timestamp'] = self.timestamp.xmlschema
145     return el1
146   end
147
148   def xml_file
149     # TODO *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz'
150     filetype = `/usr/bin/file -bz #{trace_name}`.chomp
151     gzipped = filetype =~ /gzip compressed/
152     bzipped = filetype =~ /bzip2 compressed/
153     zipped = filetype =~ /Zip archive/
154     tarred = filetype =~ /tar archive/
155
156     if gzipped or bzipped or zipped or tarred then
157       tmpfile = Tempfile.new("trace.#{id}");
158
159       if tarred and gzipped then
160         system("tar -zxOf #{trace_name} > #{tmpfile.path}")
161       elsif tarred and bzipped then
162         system("tar -jxOf #{trace_name} > #{tmpfile.path}")
163       elsif tarred
164         system("tar -xOf #{trace_name} > #{tmpfile.path}")
165       elsif gzipped
166         system("gunzip -c #{trace_name} > #{tmpfile.path}")
167       elsif bzipped
168         system("bunzip2 -c #{trace_name} > #{tmpfile.path}")
169       elsif zipped
170         system("unzip -p #{trace_name} -x '__MACOSX/*' > #{tmpfile.path}")
171       end
172
173       tmpfile.unlink
174
175       file = tmpfile.file
176     else
177       file = File.open(trace_name)
178     end
179
180     return file
181   end
182
183   def import
184     logger.info("GPX Import importing #{name} (#{id}) from #{user.email}")
185
186     gpx = GPX::File.new(self.xml_file)
187
188     f_lat = 0
189     f_lon = 0
190     first = true
191
192     # If there are any existing points for this trace then delete
193     # them - we check for existing points first to avoid locking
194     # the table in the common case where there aren't any.
195     if Tracepoint.find(:first, :conditions => ['gpx_id = ?', self.id])
196       Tracepoint.delete_all(['gpx_id = ?', self.id])
197     end
198
199     gpx.points do |point|
200       if first
201         f_lat = point.latitude
202         f_lon = point.longitude
203         first = false
204       end
205
206       tp = Tracepoint.new
207       tp.lat = point.latitude
208       tp.lon = point.longitude
209       tp.altitude = point.altitude
210       tp.timestamp = point.timestamp
211       tp.gpx_id = id
212       tp.trackid = point.segment
213       tp.save!
214     end
215
216     if gpx.actual_points > 0
217       max_lat = Tracepoint.maximum('latitude', :conditions => ['gpx_id = ?', id])
218       min_lat = Tracepoint.minimum('latitude', :conditions => ['gpx_id = ?', id])
219       max_lon = Tracepoint.maximum('longitude', :conditions => ['gpx_id = ?', id])
220       min_lon = Tracepoint.minimum('longitude', :conditions => ['gpx_id = ?', id])
221
222       max_lat = max_lat.to_f / 10000000
223       min_lat = min_lat.to_f / 10000000
224       max_lon = max_lon.to_f / 10000000
225       min_lon = min_lon.to_f / 10000000
226
227       self.latitude = f_lat
228       self.longitude = f_lon
229       self.large_picture = gpx.picture(min_lat, min_lon, max_lat, max_lon, gpx.actual_points)
230       self.icon_picture = gpx.icon(min_lat, min_lon, max_lat, max_lon)
231       self.size = gpx.actual_points
232       self.inserted = true
233       self.save!
234     end
235
236     logger.info "done trace #{id}"
237
238     return gpx
239   end
240 end