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 :inserted, :in => [ true, false ]
10 validates_inclusion_of :visibility, :in => ["private", "public", "trackable", "identifiable"]
13 has_many :tags, :class_name => 'Tracetag', :foreign_key => 'gpx_id', :dependent => :delete_all
14 has_many :points, :class_name => 'Tracepoint', :foreign_key => 'gpx_id', :dependent => :delete_all
18 FileUtils.rm_f(trace_name)
19 FileUtils.rm_f(icon_picture_name)
20 FileUtils.rm_f(large_picture_name)
24 return tags.collect {|tt| tt.tag}.join(", ")
29 self.tags = s.split(/\s*,\s*/).select {|tag| tag !~ /^\s*$/}.collect {|tag|
35 #do as before for backwards compatibility:
36 self.tags = s.split().collect {|tag|
45 visibility == "public" || visibility == "identifiable"
49 visibility == "trackable" || visibility == "identifiable"
53 visibility == "identifiable"
56 def large_picture= (data)
57 f = File.new(large_picture_name, "wb")
62 def icon_picture= (data)
63 f = File.new(icon_picture_name, "wb")
69 f = File.new(large_picture_name, "rb")
70 logger.info "large picture file: '#{f.path}', bytes: #{File.size(f.path)}"
71 data = f.sysread(File.size(f.path))
72 logger.info "have read data, bytes: '#{data.length}'"
78 f = File.new(icon_picture_name, "rb")
79 logger.info "icon picture file: '#{f.path}'"
80 data = f.sysread(File.size(f.path))
85 def large_picture_name
86 "#{GPX_IMAGE_DIR}/#{id}.gif"
90 "#{GPX_IMAGE_DIR}/#{id}_icon.gif"
94 "#{GPX_TRACE_DIR}/#{id}.gpx"
98 filetype = `/usr/bin/file -bz #{trace_name}`.chomp
99 gzipped = filetype =~ /gzip compressed/
100 bzipped = filetype =~ /bzip2 compressed/
101 zipped = filetype =~ /Zip archive/
104 mimetype = "application/x-gzip"
106 mimetype = "application/x-bzip2"
108 mimetype = "application/x-zip"
110 mimetype = "text/xml"
117 filetype = `/usr/bin/file -bz #{trace_name}`.chomp
118 gzipped = filetype =~ /gzip compressed/
119 bzipped = filetype =~ /bzip2 compressed/
120 zipped = filetype =~ /Zip archive/
121 tarred = filetype =~ /tar archive/
123 if tarred and gzipped then
124 extension = ".tar.gz"
125 elsif tarred and bzipped then
126 extension = ".tar.bz2"
130 extension = ".gpx.gz"
132 extension = ".gpx.bz2"
143 doc = OSM::API.new.get_xml_doc
144 doc.root << to_xml_node()
149 el1 = XML::Node.new 'gpx_file'
150 el1['id'] = self.id.to_s
151 el1['name'] = self.name.to_s
152 el1['lat'] = self.latitude.to_s if self.inserted
153 el1['lon'] = self.longitude.to_s if self.inserted
154 el1['user'] = self.user.display_name
155 el1['visibility'] = self.visibility
156 el1['pending'] = (!self.inserted).to_s
157 el1['timestamp'] = self.timestamp.xmlschema
159 el2 = XML::Node.new 'description'
160 el2 << self.description
163 self.tags.each do |tag|
164 el2 = XML::Node.new('tag')
173 # TODO *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz'
174 filetype = `/usr/bin/file -bz #{trace_name}`.chomp
175 gzipped = filetype =~ /gzip compressed/
176 bzipped = filetype =~ /bzip2 compressed/
177 zipped = filetype =~ /Zip archive/
178 tarred = filetype =~ /tar archive/
180 if gzipped or bzipped or zipped or tarred then
181 tmpfile = Tempfile.new("trace.#{id}");
183 if tarred and gzipped then
184 system("tar -zxOf #{trace_name} > #{tmpfile.path}")
185 elsif tarred and bzipped then
186 system("tar -jxOf #{trace_name} > #{tmpfile.path}")
188 system("tar -xOf #{trace_name} > #{tmpfile.path}")
190 system("gunzip -c #{trace_name} > #{tmpfile.path}")
192 system("bunzip2 -c #{trace_name} > #{tmpfile.path}")
194 system("unzip -p #{trace_name} -x '__MACOSX/*' > #{tmpfile.path}")
201 file = File.open(trace_name)
208 logger.info("GPX Import importing #{name} (#{id}) from #{user.email}")
210 gpx = GPX::File.new(self.xml_file)
216 # If there are any existing points for this trace then delete
217 # them - we check for existing points first to avoid locking
218 # the table in the common case where there aren't any.
219 if Tracepoint.find(:first, :conditions => ['gpx_id = ?', self.id])
220 Tracepoint.delete_all(['gpx_id = ?', self.id])
223 gpx.points do |point|
225 f_lat = point.latitude
226 f_lon = point.longitude
231 tp.lat = point.latitude
232 tp.lon = point.longitude
233 tp.altitude = point.altitude
234 tp.timestamp = point.timestamp
236 tp.trackid = point.segment
240 if gpx.actual_points > 0
241 max_lat = Tracepoint.maximum('latitude', :conditions => ['gpx_id = ?', id])
242 min_lat = Tracepoint.minimum('latitude', :conditions => ['gpx_id = ?', id])
243 max_lon = Tracepoint.maximum('longitude', :conditions => ['gpx_id = ?', id])
244 min_lon = Tracepoint.minimum('longitude', :conditions => ['gpx_id = ?', id])
246 max_lat = max_lat.to_f / 10000000
247 min_lat = min_lat.to_f / 10000000
248 max_lon = max_lon.to_f / 10000000
249 min_lon = min_lon.to_f / 10000000
251 self.latitude = f_lat
252 self.longitude = f_lon
253 self.large_picture = gpx.picture(min_lat, min_lon, max_lat, max_lon, gpx.actual_points)
254 self.icon_picture = gpx.icon(min_lat, min_lon, max_lat, max_lon)
255 self.size = gpx.actual_points
260 logger.info "done trace #{id}"