1 class Trace < ActiveRecord::Base
2 set_table_name 'gpx_files'
4 validates_presence_of :user_id, :name, :public, :description, :timestamp
5 # validates_numericality_of :latitude, :longitude
6 validates_inclusion_of :inserted, :in => [ true, false]
9 has_many :tags, :class_name => 'Tracetag', :foreign_key => 'gpx_id', :dependent => :destroy
10 has_many :points, :class_name => 'Tracepoint', :foreign_key => 'gpx_id', :dependent => :destroy
13 self.tags = s.split().collect {|tag|
20 def large_picture= (data)
21 f = File.new(large_picture_name, "wb")
26 def icon_picture= (data)
27 f = File.new(icon_picture_name, "wb")
33 f = File.new(large_picture_name, "rb")
34 logger.info "large picture file: '#{f.path}', bytes: #{File.size(f.path)}"
35 data = f.sysread(File.size(f.path))
36 logger.info "have read data, bytes: '#{data.length}'"
42 f = File.new(icon_picture_name, "rb")
43 logger.info "icon picture file: '#{f.path}'"
44 data = f.sysread(File.size(f.path))
49 # FIXME change to permanent filestore area
50 def large_picture_name
51 "/home/osm/icons/#{id}.gif"
54 # FIXME change to permanent filestore area
56 "/home/osm/icons/#{id}_icon.gif"
60 "/home/osm/gpx/#{id}.gpx"
64 el1 = XML::Node.new 'gpx_file'
65 el1['id'] = self.id.to_s
66 el1['name'] = self.name.to_s
67 el1['lat'] = self.latitude.to_s
68 el1['lon'] = self.longitude.to_s
69 el1['user'] = self.user.display_name
70 el1['public'] = self.public.to_s
71 el1['pending'] = (!self.inserted).to_s
72 el1['timestamp'] = self.timestamp.xmlschema
78 logger.info("GPX Import importing #{name} (#{id}) from #{user.email}")
80 # TODO *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz'
81 filetype = `file -b #{trace_name}`.chomp
82 gzipped = filetype =~ /^gzip/
83 zipped = filetype =~ /^Zip/
86 filename = tempfile = "/tmp/#{rand}"
87 system("gunzip -c #{trace_name} > #{filename}")
89 filename = tempfile = "/tmp/#{rand}"
90 system("unzip -p #{trace_name} > #{filename}")
95 gpx = OSM::GPXImporter.new(filename)
101 Tracepoint.delete_all(['gpx_id = ?', self.id])
103 gpx.points do |point|
105 f_lat = point['latitude']
106 f_lon = point['longitude']
110 tp.lat = point['latitude'].to_f
111 tp.lng = point['longitude'].to_f
112 tp.altitude = point['altitude'].to_f
115 tp.trackid = point['segment'].to_i
119 if gpx.actual_points > 0
120 max_lat = Tracepoint.maximum('latitude', :conditions => ['gpx_id = ?', id])
121 min_lat = Tracepoint.minimum('latitude', :conditions => ['gpx_id = ?', id])
122 max_lon = Tracepoint.maximum('longitude', :conditions => ['gpx_id = ?', id])
123 min_lon = Tracepoint.minimum('longitude', :conditions => ['gpx_id = ?', id])
125 max_lat = max_lat.to_f / 1000000
126 min_lat = min_lat.to_f / 1000000
127 max_lon = max_lon.to_f / 1000000
128 min_lon = min_lon.to_f / 1000000
130 self.latitude = f_lat
131 self.longitude = f_lon
132 self.large_picture = gpx.get_picture(min_lat, min_lon, max_lat, max_lon, gpx.actual_points)
133 self.icon_picture = gpx.get_icon(min_lat, min_lon, max_lat, max_lon)
134 self.size = gpx.actual_points
138 Notifier::deliver_gpx_success(self, gpx.possible_points)
140 FileUtils.rm_f("/home/osm/gpx/#{id}.gpx")
142 Notifier::deliver_gpx_failure(self, '0 points parsed ok. Do they all have lat,lng,alt,timestamp?')
145 logger.info "done trace #{id}"
146 rescue Exception => ex
148 ex.backtrace.each {|l| logger.info l }
149 FileUtils.rm_f("/home/osm/gpx/#{id}.gpx")
151 Notifier::deliver_gpx_failure(self, ex.to_s + ex.backtrace.join("\n") )
153 FileUtils.rm_f(tempfile) if tempfile