]> git.openstreetmap.org Git - rails.git/blob - app/models/trace.rb
0307b5f653eac11a6563c7b21b5fa7ceae23a36a
[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, :public, :description, :timestamp
5 #  validates_numericality_of :latitude, :longitude
6   validates_inclusion_of :inserted, :in => [ true, false]
7   
8   belongs_to :user
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
11
12   def tagstring=(s)
13     self.tags = s.split().collect {|tag|
14       tt = Tracetag.new
15       tt.tag = tag
16       tt
17     }
18   end
19   
20   def large_picture= (data)
21     f = File.new(large_picture_name, "wb")
22     f.syswrite(data)
23     f.close
24   end
25   
26   def icon_picture= (data)
27     f = File.new(icon_picture_name, "wb")
28     f.syswrite(data)
29     f.close
30   end
31
32   def large_picture
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}'"
37     f.close
38     data
39   end
40   
41   def icon_picture
42     f = File.new(icon_picture_name, "rb")
43     logger.info "icon picture file: '#{f.path}'"
44     data = f.sysread(File.size(f.path))
45     f.close
46     data
47   end
48   
49   # FIXME change to permanent filestore area
50   def large_picture_name
51     "/home/osm/icons/#{id}.gif"
52   end
53
54   # FIXME change to permanent filestore area
55   def icon_picture_name
56     "/home/osm/icons/#{id}_icon.gif"
57   end
58
59   def trace_name
60     "/home/osm/gpx/#{id}.gpx"
61   end
62
63   def to_xml_node
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
73     return el1
74   end
75
76   def import
77     begin
78       logger.info("GPX Import importing #{name} (#{id}) from #{user.email}")
79
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/
84
85       if gzipped
86         filename = tempfile = "/tmp/#{rand}"
87         system("gunzip -c #{trace_name} > #{filename}")
88       elsif zipped
89         filename = tempfile = "/tmp/#{rand}"
90         system("unzip -p #{trace_name} > #{filename}")
91       else
92         filename = trace_name
93       end
94
95       gpx = OSM::GPXImporter.new(filename)
96
97       f_lat = 0
98       f_lon = 0
99       first = true
100
101       Tracepoint.delete_all(['gpx_id = ?', self.id])
102
103       gpx.points do |point|
104         if first
105           f_lat = point['latitude']
106           f_lon = point['longitude']
107         end
108
109         tp = Tracepoint.new
110         tp.lat = point['latitude'].to_f
111         tp.lng = point['longitude'].to_f
112         tp.altitude = point['altitude'].to_f
113         tp.user_id = user.id
114         tp.gpx_id = id
115         tp.trackid = point['segment'].to_i
116         tp.save!
117       end
118
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])
124
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
129
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
135         self.inserted = true
136         self.save
137
138         Notifier::deliver_gpx_success(self, gpx.possible_points)
139       else
140         FileUtils.rm_f("/home/osm/gpx/#{id}.gpx")
141         self.destroy
142         Notifier::deliver_gpx_failure(self, '0 points parsed ok. Do they all have lat,lng,alt,timestamp?')
143       end
144
145       logger.info "done trace #{id}"
146     rescue Exception => ex
147       logger.info ex
148       ex.backtrace.each {|l| logger.info l }
149       FileUtils.rm_f("/home/osm/gpx/#{id}.gpx")
150       self.destroy
151       Notifier::deliver_gpx_failure(self, ex.to_s + ex.backtrace.join("\n") )
152     ensure
153       FileUtils.rm_f(tempfile) if tempfile
154     end
155   end
156 end