1 class Trace < ActiveRecord::Base
2 set_table_name 'gpx_files'
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]
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
17 FileUtils.rm_f(trace_name)
18 FileUtils.rm_f(icon_picture_name)
19 FileUtils.rm_f(large_picture_name)
23 return tags.collect {|tt| tt.tag}.join(", ")
28 self.tags = s.split(/\s*,\s*/).collect {|tag|
34 self.tags = s.split().collect {|tag|
42 def large_picture= (data)
43 f = File.new(large_picture_name, "wb")
48 def icon_picture= (data)
49 f = File.new(icon_picture_name, "wb")
55 f = File.new(large_picture_name, "rb")
56 logger.info "large picture file: '#{f.path}', bytes: #{File.size(f.path)}"
57 data = f.sysread(File.size(f.path))
58 logger.info "have read data, bytes: '#{data.length}'"
64 f = File.new(icon_picture_name, "rb")
65 logger.info "icon picture file: '#{f.path}'"
66 data = f.sysread(File.size(f.path))
71 def large_picture_name
72 "#{GPX_IMAGE_DIR}/#{id}.gif"
76 "#{GPX_IMAGE_DIR}/#{id}_icon.gif"
80 "#{GPX_TRACE_DIR}/#{id}.gpx"
84 filetype = `/usr/bin/file -bz #{trace_name}`.chomp
85 gzipped = filetype =~ /gzip compressed/
86 bzipped = filetype =~ /bzip2 compressed/
87 zipped = filetype =~ /Zip archive/
90 mimetype = "application/x-gzip"
92 mimetype = "application/x-bzip2"
94 mimetype = "application/x-zip"
103 filetype = `/usr/bin/file -bz #{trace_name}`.chomp
104 gzipped = filetype =~ /gzip compressed/
105 bzipped = filetype =~ /bzip2 compressed/
106 zipped = filetype =~ /Zip archive/
107 tarred = filetype =~ /tar archive/
109 if tarred and gzipped then
110 extension = ".tar.gz"
111 elsif tarred and bzipped then
112 extension = ".tar.bz2"
116 extension = ".gpx.gz"
118 extension = ".gpx.bz2"
129 doc = OSM::API.new.get_xml_doc
130 doc.root << to_xml_node()
135 el1 = XML::Node.new 'gpx_file'
136 el1['id'] = self.id.to_s
137 el1['name'] = self.name.to_s
138 el1['lat'] = self.latitude.to_s
139 el1['lon'] = self.longitude.to_s
140 el1['user'] = self.user.display_name
141 el1['public'] = self.public.to_s
142 el1['pending'] = (!self.inserted).to_s
143 el1['timestamp'] = self.timestamp.xmlschema
148 # TODO *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz'
149 filetype = `/usr/bin/file -bz #{trace_name}`.chomp
150 gzipped = filetype =~ /gzip compressed/
151 bzipped = filetype =~ /bzip2 compressed/
152 zipped = filetype =~ /Zip archive/
153 tarred = filetype =~ /tar archive/
155 if gzipped or bzipped or zipped or tarred then
156 tmpfile = Tempfile.new("trace.#{id}");
158 if tarred and gzipped then
159 system("tar -zxOf #{trace_name} > #{tmpfile.path}")
160 elsif tarred and bzipped then
161 system("tar -jxOf #{trace_name} > #{tmpfile.path}")
163 system("tar -xOf #{trace_name} > #{tmpfile.path}")
165 system("gunzip -c #{trace_name} > #{tmpfile.path}")
167 system("bunzip2 -c #{trace_name} > #{tmpfile.path}")
169 system("unzip -p #{trace_name} -x '__MACOSX/*' > #{tmpfile.path}")
176 file = File.open(trace_name)
183 logger.info("GPX Import importing #{name} (#{id}) from #{user.email}")
185 gpx = GPX::File.new(self.xml_file)
191 # If there are any existing points for this trace then delete
192 # them - we check for existing points first to avoid locking
193 # the table in the common case where there aren't any.
194 if Tracepoint.find(:first, :conditions => ['gpx_id = ?', self.id])
195 Tracepoint.delete_all(['gpx_id = ?', self.id])
198 gpx.points do |point|
200 f_lat = point.latitude
201 f_lon = point.longitude
206 tp.lat = point.latitude
207 tp.lon = point.longitude
208 tp.altitude = point.altitude
209 tp.timestamp = point.timestamp
211 tp.trackid = point.segment
215 if gpx.actual_points > 0
216 max_lat = Tracepoint.maximum('latitude', :conditions => ['gpx_id = ?', id])
217 min_lat = Tracepoint.minimum('latitude', :conditions => ['gpx_id = ?', id])
218 max_lon = Tracepoint.maximum('longitude', :conditions => ['gpx_id = ?', id])
219 min_lon = Tracepoint.minimum('longitude', :conditions => ['gpx_id = ?', id])
221 max_lat = max_lat.to_f / 10000000
222 min_lat = min_lat.to_f / 10000000
223 max_lon = max_lon.to_f / 10000000
224 min_lon = min_lon.to_f / 10000000
226 self.latitude = f_lat
227 self.longitude = f_lon
228 self.large_picture = gpx.picture(min_lat, min_lon, max_lat, max_lon, gpx.actual_points)
229 self.icon_picture = gpx.icon(min_lat, min_lon, max_lat, max_lon)
230 self.size = gpx.actual_points
235 logger.info "done trace #{id}"