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 => :delete_all
10 has_many :points, :class_name => 'Tracepoint', :foreign_key => 'gpx_id', :dependent => :delete_all
14 FileUtils.rm_f(trace_name, icon_picture_name, large_picture_name)
18 self.tags = s.split().collect {|tag|
25 def large_picture= (data)
26 f = File.new(large_picture_name, "wb")
31 def icon_picture= (data)
32 f = File.new(icon_picture_name, "wb")
38 f = File.new(large_picture_name, "rb")
39 logger.info "large picture file: '#{f.path}', bytes: #{File.size(f.path)}"
40 data = f.sysread(File.size(f.path))
41 logger.info "have read data, bytes: '#{data.length}'"
47 f = File.new(icon_picture_name, "rb")
48 logger.info "icon picture file: '#{f.path}'"
49 data = f.sysread(File.size(f.path))
54 # FIXME change to permanent filestore area
55 def large_picture_name
56 "/home/osm/icons/#{id}.gif"
59 # FIXME change to permanent filestore area
61 "/home/osm/icons/#{id}_icon.gif"
65 "/home/osm/gpx/#{id}.gpx"
69 el1 = XML::Node.new 'gpx_file'
70 el1['id'] = self.id.to_s
71 el1['name'] = self.name.to_s
72 el1['lat'] = self.latitude.to_s
73 el1['lon'] = self.longitude.to_s
74 el1['user'] = self.user.display_name
75 el1['public'] = self.public.to_s
76 el1['pending'] = (!self.inserted).to_s
77 el1['timestamp'] = self.timestamp.xmlschema
83 logger.info("GPX Import importing #{name} (#{id}) from #{user.email}")
85 # TODO *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz'
86 filetype = `file -b #{trace_name}`.chomp
87 gzipped = filetype =~ /^gzip/
88 zipped = filetype =~ /^Zip/
91 filename = tempfile = "/tmp/#{rand}"
92 system("gunzip -c #{trace_name} > #{filename}")
94 filename = tempfile = "/tmp/#{rand}"
95 system("unzip -p #{trace_name} > #{filename}")
100 gpx = OSM::GPXImporter.new(filename)
106 Tracepoint.delete_all(['gpx_id = ?', self.id])
108 gpx.points do |point|
110 f_lat = point['latitude']
111 f_lon = point['longitude']
115 tp.lat = point['latitude'].to_f
116 tp.lng = point['longitude'].to_f
117 tp.altitude = point['altitude'].to_f
118 tp.timestamp = point['timestamp']
121 tp.trackid = point['segment'].to_i
125 if gpx.actual_points > 0
126 max_lat = Tracepoint.maximum('latitude', :conditions => ['gpx_id = ?', id])
127 min_lat = Tracepoint.minimum('latitude', :conditions => ['gpx_id = ?', id])
128 max_lon = Tracepoint.maximum('longitude', :conditions => ['gpx_id = ?', id])
129 min_lon = Tracepoint.minimum('longitude', :conditions => ['gpx_id = ?', id])
131 max_lat = max_lat.to_f / 1000000
132 min_lat = min_lat.to_f / 1000000
133 max_lon = max_lon.to_f / 1000000
134 min_lon = min_lon.to_f / 1000000
136 self.latitude = f_lat
137 self.longitude = f_lon
138 self.large_picture = gpx.get_picture(min_lat, min_lon, max_lat, max_lon, gpx.actual_points)
139 self.icon_picture = gpx.get_icon(min_lat, min_lon, max_lat, max_lon)
140 self.size = gpx.actual_points
145 logger.info "done trace #{id}"
149 FileUtils.rm_f(tempfile) if tempfile