]> git.openstreetmap.org Git - rails.git/blob - app/models/trace.rb
Provide a more sensible suggested filename when downloading traces.
[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_numericality_of :latitude, :longitude
7   validates_inclusion_of :public, :inserted, :in => [ true, false]
8   
9   belongs_to :user
10   has_many :tags, :class_name => 'Tracetag', :foreign_key => 'gpx_id', :dependent => :delete_all
11   has_many :points, :class_name => 'Tracepoint', :foreign_key => 'gpx_id', :dependent => :delete_all
12
13   def destroy
14     super
15     FileUtils.rm_f(trace_name)
16     FileUtils.rm_f(icon_picture_name)
17     FileUtils.rm_f(large_picture_name)
18   end
19
20   def tagstring
21     return tags.collect {|tt| tt.tag}.join(" ")
22   end
23
24   def tagstring=(s)
25     self.tags = s.split().collect {|tag|
26       tt = Tracetag.new
27       tt.tag = tag
28       tt
29     }
30   end
31   
32   def large_picture= (data)
33     f = File.new(large_picture_name, "wb")
34     f.syswrite(data)
35     f.close
36   end
37   
38   def icon_picture= (data)
39     f = File.new(icon_picture_name, "wb")
40     f.syswrite(data)
41     f.close
42   end
43
44   def large_picture
45     f = File.new(large_picture_name, "rb")
46     logger.info "large picture file: '#{f.path}', bytes: #{File.size(f.path)}"
47     data = f.sysread(File.size(f.path))
48     logger.info "have read data, bytes: '#{data.length}'"
49     f.close
50     data
51   end
52   
53   def icon_picture
54     f = File.new(icon_picture_name, "rb")
55     logger.info "icon picture file: '#{f.path}'"
56     data = f.sysread(File.size(f.path))
57     f.close
58     data
59   end
60   
61   # FIXME change to permanent filestore area
62   def large_picture_name
63     "/home/osm/icons/#{id}.gif"
64   end
65
66   # FIXME change to permanent filestore area
67   def icon_picture_name
68     "/home/osm/icons/#{id}_icon.gif"
69   end
70
71   def trace_name
72     "/home/osm/gpx/#{id}.gpx"
73   end
74
75   def mime_type
76     return `file -bi #{trace_name}`.chomp
77   end
78
79   def extension_name
80     filetype = `file -bz #{trace_name}`.chomp
81     gzipped = filetype =~ /gzip compressed/
82     bzipped = filetype =~ /bzip2 compressed/
83     zipped = filetype =~ /Zip archive/
84     tarred = filetype =~ /tar archive/
85
86     if tarred and gzipped then
87       extension = ".tar.gz"
88     elsif tarred and bzipped then
89       extension = ".tar.bz2"
90     elsif tarred
91       extension = ".tar"
92     elsif gzipped
93       extension = ".gpx.gz"
94     elsif bzipped
95       extension = ".gpx.bz2"
96     elsif zipped
97       extension = ".zip"
98     else
99       extension = ".gpx"
100     end
101
102     return extension
103   end
104
105   def to_xml
106     doc = OSM::API.new.get_xml_doc
107     doc.root << to_xml_node()
108     return doc
109   end
110
111   def to_xml_node
112     el1 = XML::Node.new 'gpx_file'
113     el1['id'] = self.id.to_s
114     el1['name'] = self.name.to_s
115     el1['lat'] = self.latitude.to_s
116     el1['lon'] = self.longitude.to_s
117     el1['user'] = self.user.display_name
118     el1['public'] = self.public.to_s
119     el1['pending'] = (!self.inserted).to_s
120     el1['timestamp'] = self.timestamp.xmlschema
121     return el1
122   end
123
124   def import
125     begin
126       logger.info("GPX Import importing #{name} (#{id}) from #{user.email}")
127
128       # TODO *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz'
129       filetype = `file -bz #{trace_name}`.chomp
130       gzipped = filetype =~ /gzip compressed/
131       bzipped = filetype =~ /bzip2 compressed/
132       zipped = filetype =~ /Zip archive/
133       tarred = filetype =~ /tar archive/
134
135       if tarred and gzipped then
136         filename = tempfile = "/tmp/#{rand}"
137         system("tar -zxOf #{trace_name} > #{filename}")
138       elsif tarred and bzipped then
139         filename = tempfile = "/tmp/#{rand}"
140         system("tar -jxOf #{trace_name} > #{filename}")
141       elsif tarred
142         filename = tempfile = "/tmp/#{rand}"
143         system("tar -xOf #{trace_name} > #{filename}")
144       elsif gzipped
145         filename = tempfile = "/tmp/#{rand}"
146         system("gunzip -c #{trace_name} > #{filename}")
147       elsif bzipped
148         filename = tempfile = "/tmp/#{rand}"
149         system("bunzip2 -c #{trace_name} > #{filename}")
150       elsif zipped
151         filename = tempfile = "/tmp/#{rand}"
152         system("unzip -p #{trace_name} > #{filename}")
153       else
154         filename = trace_name
155       end
156
157       gpx = OSM::GPXImporter.new(filename)
158
159       f_lat = 0
160       f_lon = 0
161       first = true
162
163       Tracepoint.delete_all(['gpx_id = ?', self.id])
164
165       gpx.points do |point|
166         if first
167           f_lat = point['latitude']
168           f_lon = point['longitude']
169         end
170
171         tp = Tracepoint.new
172         tp.lat = point['latitude'].to_f
173         tp.lng = point['longitude'].to_f
174         tp.altitude = point['altitude'].to_f
175         tp.timestamp = point['timestamp']
176         tp.user_id = user.id
177         tp.gpx_id = id
178         tp.trackid = point['segment'].to_i
179         tp.save!
180       end
181
182       if gpx.actual_points > 0
183         max_lat = Tracepoint.maximum('latitude', :conditions => ['gpx_id = ?', id])
184         min_lat = Tracepoint.minimum('latitude', :conditions => ['gpx_id = ?', id])
185         max_lon = Tracepoint.maximum('longitude', :conditions => ['gpx_id = ?', id])
186         min_lon = Tracepoint.minimum('longitude', :conditions => ['gpx_id = ?', id])
187
188         max_lat = max_lat.to_f / 1000000
189         min_lat = min_lat.to_f / 1000000
190         max_lon = max_lon.to_f / 1000000
191         min_lon = min_lon.to_f / 1000000
192
193         self.latitude = f_lat
194         self.longitude = f_lon
195         self.large_picture = gpx.get_picture(min_lat, min_lon, max_lat, max_lon, gpx.actual_points)
196         self.icon_picture = gpx.get_icon(min_lat, min_lon, max_lat, max_lon)
197         self.size = gpx.actual_points
198         self.inserted = true
199         self.save
200       end
201
202       logger.info "done trace #{id}"
203
204       return gpx
205     ensure
206       FileUtils.rm_f(tempfile) if tempfile
207     end
208   end
209 end