]> git.openstreetmap.org Git - rails.git/blob - app/models/trace.rb
Only treat a user as logged in if the user object has an id value - this
[rails.git] / app / models / trace.rb
1 class Trace < ActiveRecord::Base
2   set_table_name 'gpx_files'
3
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]
8   
9   belongs_to :user
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
12
13   def destroy
14     super
15     FileUtils.rm_f(trace_name)
16     FileUtils.rm_f(icon_picture_name)
17     FileUtils.rm_f(large_picture_name)
18   end
19
20   def tagstring
21     return tags.collect {|tt| tt.tag}.join(" ")
22   end
23
24   def tagstring=(s)
25     self.tags = s.split().collect {|tag|
26       tt = Tracetag.new
27       tt.tag = tag
28       tt
29     }
30   end
31   
32   def large_picture= (data)
33     f = File.new(large_picture_name, "wb")
34     f.syswrite(data)
35     f.close
36   end
37   
38   def icon_picture= (data)
39     f = File.new(icon_picture_name, "wb")
40     f.syswrite(data)
41     f.close
42   end
43
44   def large_picture
45     f = File.new(large_picture_name, "rb")
46     logger.info "large picture file: '#{f.path}', bytes: #{File.size(f.path)}"
47     data = f.sysread(File.size(f.path))
48     logger.info "have read data, bytes: '#{data.length}'"
49     f.close
50     data
51   end
52   
53   def icon_picture
54     f = File.new(icon_picture_name, "rb")
55     logger.info "icon picture file: '#{f.path}'"
56     data = f.sysread(File.size(f.path))
57     f.close
58     data
59   end
60   
61   # FIXME change to permanent filestore area
62   def large_picture_name
63     "/home/osm/icons/#{id}.gif"
64   end
65
66   # FIXME change to permanent filestore area
67   def icon_picture_name
68     "/home/osm/icons/#{id}_icon.gif"
69   end
70
71   def trace_name
72     "/home/osm/gpx/#{id}.gpx"
73   end
74
75   def to_xml_node
76     el1 = XML::Node.new 'gpx_file'
77     el1['id'] = self.id.to_s
78     el1['name'] = self.name.to_s
79     el1['lat'] = self.latitude.to_s
80     el1['lon'] = self.longitude.to_s
81     el1['user'] = self.user.display_name
82     el1['public'] = self.public.to_s
83     el1['pending'] = (!self.inserted).to_s
84     el1['timestamp'] = self.timestamp.xmlschema
85     return el1
86   end
87
88   def import
89     begin
90       logger.info("GPX Import importing #{name} (#{id}) from #{user.email}")
91
92       # TODO *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz'
93       filetype = `file -bz #{trace_name}`.chomp
94       gzipped = filetype =~ /gzip compressed/
95       bzipped = filetype =~ /bzip2 compressed/
96       zipped = filetype =~ /Zip archive/
97       tarred = filetype =~ /tar archive/
98
99       if tarred and gzipped then
100         filename = tempfile = "/tmp/#{rand}"
101         system("tar -zxOf #{trace_name} > #{filename}")
102       elsif tarred and bzipped then
103         filename = tempfile = "/tmp/#{rand}"
104         system("tar -jxOf #{trace_name} > #{filename}")
105       elsif tarred
106         filename = tempfile = "/tmp/#{rand}"
107         system("tar -xOf #{trace_name} > #{filename}")
108       elsif gzipped
109         filename = tempfile = "/tmp/#{rand}"
110         system("gunzip -c #{trace_name} > #{filename}")
111       elsif bzipped
112         filename = tempfile = "/tmp/#{rand}"
113         system("bunzip2 -c #{trace_name} > #{filename}")
114       elsif zipped
115         filename = tempfile = "/tmp/#{rand}"
116         system("unzip -p #{trace_name} > #{filename}")
117       else
118         filename = trace_name
119       end
120
121       gpx = OSM::GPXImporter.new(filename)
122
123       f_lat = 0
124       f_lon = 0
125       first = true
126
127       Tracepoint.delete_all(['gpx_id = ?', self.id])
128
129       gpx.points do |point|
130         if first
131           f_lat = point['latitude']
132           f_lon = point['longitude']
133         end
134
135         tp = Tracepoint.new
136         tp.lat = point['latitude'].to_f
137         tp.lng = point['longitude'].to_f
138         tp.altitude = point['altitude'].to_f
139         tp.timestamp = point['timestamp']
140         tp.user_id = user.id
141         tp.gpx_id = id
142         tp.trackid = point['segment'].to_i
143         tp.save!
144       end
145
146       if gpx.actual_points > 0
147         max_lat = Tracepoint.maximum('latitude', :conditions => ['gpx_id = ?', id])
148         min_lat = Tracepoint.minimum('latitude', :conditions => ['gpx_id = ?', id])
149         max_lon = Tracepoint.maximum('longitude', :conditions => ['gpx_id = ?', id])
150         min_lon = Tracepoint.minimum('longitude', :conditions => ['gpx_id = ?', id])
151
152         max_lat = max_lat.to_f / 1000000
153         min_lat = min_lat.to_f / 1000000
154         max_lon = max_lon.to_f / 1000000
155         min_lon = min_lon.to_f / 1000000
156
157         self.latitude = f_lat
158         self.longitude = f_lon
159         self.large_picture = gpx.get_picture(min_lat, min_lon, max_lat, max_lon, gpx.actual_points)
160         self.icon_picture = gpx.get_icon(min_lat, min_lon, max_lat, max_lon)
161         self.size = gpx.actual_points
162         self.inserted = true
163         self.save
164       end
165
166       logger.info "done trace #{id}"
167
168       return gpx
169     ensure
170       FileUtils.rm_f(tempfile) if tempfile
171     end
172   end
173 end