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