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