]> git.openstreetmap.org Git - rails.git/blob - app/models/trace.rb
Make field names consistent with the account editing page.
[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, :timestamp
5   validates_presence_of :description, :on => :create
6 #  validates_numericality_of :latitude, :longitude
7   validates_inclusion_of :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, icon_picture_name, large_picture_name)
16   end
17
18   def tagstring=(s)
19     self.tags = s.split().collect {|tag|
20       tt = Tracetag.new
21       tt.tag = tag
22       tt
23     }
24   end
25   
26   def large_picture= (data)
27     f = File.new(large_picture_name, "wb")
28     f.syswrite(data)
29     f.close
30   end
31   
32   def icon_picture= (data)
33     f = File.new(icon_picture_name, "wb")
34     f.syswrite(data)
35     f.close
36   end
37
38   def large_picture
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}'"
43     f.close
44     data
45   end
46   
47   def icon_picture
48     f = File.new(icon_picture_name, "rb")
49     logger.info "icon picture file: '#{f.path}'"
50     data = f.sysread(File.size(f.path))
51     f.close
52     data
53   end
54   
55   # FIXME change to permanent filestore area
56   def large_picture_name
57     "/home/osm/icons/#{id}.gif"
58   end
59
60   # FIXME change to permanent filestore area
61   def icon_picture_name
62     "/home/osm/icons/#{id}_icon.gif"
63   end
64
65   def trace_name
66     "/home/osm/gpx/#{id}.gpx"
67   end
68
69   def to_xml_node
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
79     return el1
80   end
81
82   def import
83     begin
84       logger.info("GPX Import importing #{name} (#{id}) from #{user.email}")
85
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/
90
91       if gzipped
92         filename = tempfile = "/tmp/#{rand}"
93         system("gunzip -c #{trace_name} > #{filename}")
94       elsif zipped
95         filename = tempfile = "/tmp/#{rand}"
96         system("unzip -p #{trace_name} > #{filename}")
97       else
98         filename = trace_name
99       end
100
101       gpx = OSM::GPXImporter.new(filename)
102
103       f_lat = 0
104       f_lon = 0
105       first = true
106
107       Tracepoint.delete_all(['gpx_id = ?', self.id])
108
109       gpx.points do |point|
110         if first
111           f_lat = point['latitude']
112           f_lon = point['longitude']
113         end
114
115         tp = Tracepoint.new
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']
120         tp.user_id = user.id
121         tp.gpx_id = id
122         tp.trackid = point['segment'].to_i
123         tp.save!
124       end
125
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])
131
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
136
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
142         self.inserted = true
143         self.save
144       end
145
146       logger.info "done trace #{id}"
147
148       return gpx
149     ensure
150       FileUtils.rm_f(tempfile) if tempfile
151     end
152   end
153 end