1 class Trace < ActiveRecord::Base
2 set_table_name 'gpx_files'
4 validates_presence_of :user_id, :name, :public, :timestamp
5 validates_presence_of :description, :on => :create
6 # validates_numericality_of :latitude, :longitude
7 validates_inclusion_of :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 self.tags = s.split().collect {|tag|
26 def large_picture= (data)
27 f = File.new(large_picture_name, "wb")
32 def icon_picture= (data)
33 f = File.new(icon_picture_name, "wb")
39 f = File.new(large_picture_name, "rb")
40 logger.info "large picture file: '#{f.path}', bytes: #{File.size(f.path)}"
41 data = f.sysread(File.size(f.path))
42 logger.info "have read data, bytes: '#{data.length}'"
48 f = File.new(icon_picture_name, "rb")
49 logger.info "icon picture file: '#{f.path}'"
50 data = f.sysread(File.size(f.path))
55 # FIXME change to permanent filestore area
56 def large_picture_name
57 "/home/osm/icons/#{id}.gif"
60 # FIXME change to permanent filestore area
62 "/home/osm/icons/#{id}_icon.gif"
66 "/home/osm/gpx/#{id}.gpx"
70 el1 = XML::Node.new 'gpx_file'
71 el1['id'] = self.id.to_s
72 el1['name'] = self.name.to_s
73 el1['lat'] = self.latitude.to_s
74 el1['lon'] = self.longitude.to_s
75 el1['user'] = self.user.display_name
76 el1['public'] = self.public.to_s
77 el1['pending'] = (!self.inserted).to_s
78 el1['timestamp'] = self.timestamp.xmlschema
84 logger.info("GPX Import importing #{name} (#{id}) from #{user.email}")
86 # TODO *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz'
87 filetype = `file -b #{trace_name}`.chomp
88 gzipped = filetype =~ /^gzip/
89 zipped = filetype =~ /^Zip/
92 filename = tempfile = "/tmp/#{rand}"
93 system("gunzip -c #{trace_name} > #{filename}")
95 filename = tempfile = "/tmp/#{rand}"
96 system("unzip -p #{trace_name} > #{filename}")
101 gpx = OSM::GPXImporter.new(filename)
107 Tracepoint.delete_all(['gpx_id = ?', self.id])
109 gpx.points do |point|
111 f_lat = point['latitude']
112 f_lon = point['longitude']
116 tp.lat = point['latitude'].to_f
117 tp.lng = point['longitude'].to_f
118 tp.altitude = point['altitude'].to_f
119 tp.timestamp = point['timestamp']
122 tp.trackid = point['segment'].to_i
126 if gpx.actual_points > 0
127 max_lat = Tracepoint.maximum('latitude', :conditions => ['gpx_id = ?', id])
128 min_lat = Tracepoint.minimum('latitude', :conditions => ['gpx_id = ?', id])
129 max_lon = Tracepoint.maximum('longitude', :conditions => ['gpx_id = ?', id])
130 min_lon = Tracepoint.minimum('longitude', :conditions => ['gpx_id = ?', id])
132 max_lat = max_lat.to_f / 1000000
133 min_lat = min_lat.to_f / 1000000
134 max_lon = max_lon.to_f / 1000000
135 min_lon = min_lon.to_f / 1000000
137 self.latitude = f_lat
138 self.longitude = f_lon
139 self.large_picture = gpx.get_picture(min_lat, min_lon, max_lat, max_lon, gpx.actual_points)
140 self.icon_picture = gpx.get_icon(min_lat, min_lon, max_lat, max_lon)
141 self.size = gpx.actual_points
146 logger.info "done trace #{id}"
150 FileUtils.rm_f(tempfile) if tempfile