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_numericality_of :latitude, :longitude
7 validates_inclusion_of :public, :inserted, :in => [ true, false]
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
15 FileUtils.rm_f(trace_name, icon_picture_name, large_picture_name)
19 return tags.collect {|tt| tt.tag}.join(" ")
23 self.tags = s.split().collect {|tag|
30 def large_picture= (data)
31 f = File.new(large_picture_name, "wb")
36 def icon_picture= (data)
37 f = File.new(icon_picture_name, "wb")
43 f = File.new(large_picture_name, "rb")
44 logger.info "large picture file: '#{f.path}', bytes: #{File.size(f.path)}"
45 data = f.sysread(File.size(f.path))
46 logger.info "have read data, bytes: '#{data.length}'"
52 f = File.new(icon_picture_name, "rb")
53 logger.info "icon picture file: '#{f.path}'"
54 data = f.sysread(File.size(f.path))
59 # FIXME change to permanent filestore area
60 def large_picture_name
61 "/home/osm/icons/#{id}.gif"
64 # FIXME change to permanent filestore area
66 "/home/osm/icons/#{id}_icon.gif"
70 "/home/osm/gpx/#{id}.gpx"
74 el1 = XML::Node.new 'gpx_file'
75 el1['id'] = self.id.to_s
76 el1['name'] = self.name.to_s
77 el1['lat'] = self.latitude.to_s
78 el1['lon'] = self.longitude.to_s
79 el1['user'] = self.user.display_name
80 el1['public'] = self.public.to_s
81 el1['pending'] = (!self.inserted).to_s
82 el1['timestamp'] = self.timestamp.xmlschema
88 logger.info("GPX Import importing #{name} (#{id}) from #{user.email}")
90 # TODO *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz'
91 filetype = `file -b #{trace_name}`.chomp
92 gzipped = filetype =~ /^gzip/
93 zipped = filetype =~ /^Zip/
96 filename = tempfile = "/tmp/#{rand}"
97 system("gunzip -c #{trace_name} > #{filename}")
99 filename = tempfile = "/tmp/#{rand}"
100 system("unzip -p #{trace_name} > #{filename}")
102 filename = trace_name
105 gpx = OSM::GPXImporter.new(filename)
111 Tracepoint.delete_all(['gpx_id = ?', self.id])
113 gpx.points do |point|
115 f_lat = point['latitude']
116 f_lon = point['longitude']
120 tp.lat = point['latitude'].to_f
121 tp.lng = point['longitude'].to_f
122 tp.altitude = point['altitude'].to_f
123 tp.timestamp = point['timestamp']
126 tp.trackid = point['segment'].to_i
130 if gpx.actual_points > 0
131 max_lat = Tracepoint.maximum('latitude', :conditions => ['gpx_id = ?', id])
132 min_lat = Tracepoint.minimum('latitude', :conditions => ['gpx_id = ?', id])
133 max_lon = Tracepoint.maximum('longitude', :conditions => ['gpx_id = ?', id])
134 min_lon = Tracepoint.minimum('longitude', :conditions => ['gpx_id = ?', id])
136 max_lat = max_lat.to_f / 1000000
137 min_lat = min_lat.to_f / 1000000
138 max_lon = max_lon.to_f / 1000000
139 min_lon = min_lon.to_f / 1000000
141 self.latitude = f_lat
142 self.longitude = f_lon
143 self.large_picture = gpx.get_picture(min_lat, min_lon, max_lat, max_lon, gpx.actual_points)
144 self.icon_picture = gpx.get_icon(min_lat, min_lon, max_lat, max_lon)
145 self.size = gpx.actual_points
150 logger.info "done trace #{id}"
154 FileUtils.rm_f(tempfile) if tempfile