source: subversion/sites/rails_port/app/models/trace.rb @ 4144

Last change on this file since 4144 was 3636, checked in by tomhughes, 13 years ago

Don't bother setting user_id for each tracepoint - it is redundant as
the same information can be found by joining to gpx_files. It is also
never used, so the column is going to be removed.

File size: 6.0 KB
Line 
1class 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 mime_type
76    filetype = `/usr/bin/file -bz #{trace_name}`.chomp
77    gzipped = filetype =~ /gzip compressed/
78    bzipped = filetype =~ /bzip2 compressed/
79    zipped = filetype =~ /Zip archive/
80
81    if gzipped then
82      mimetype = "application/x-gzip"
83    elsif bzipped then
84      mimetype = "application/x-bzip2"
85    elsif zipped
86      mimetype = "application/x-zip"
87    else
88      mimetype = "text/xml"
89    end
90
91    return mimetype
92  end
93
94  def extension_name
95    filetype = `/usr/bin/file -bz #{trace_name}`.chomp
96    gzipped = filetype =~ /gzip compressed/
97    bzipped = filetype =~ /bzip2 compressed/
98    zipped = filetype =~ /Zip archive/
99    tarred = filetype =~ /tar archive/
100
101    if tarred and gzipped then
102      extension = ".tar.gz"
103    elsif tarred and bzipped then
104      extension = ".tar.bz2"
105    elsif tarred
106      extension = ".tar"
107    elsif gzipped
108      extension = ".gpx.gz"
109    elsif bzipped
110      extension = ".gpx.bz2"
111    elsif zipped
112      extension = ".zip"
113    else
114      extension = ".gpx"
115    end
116
117    return extension
118  end
119
120  def to_xml
121    doc = OSM::API.new.get_xml_doc
122    doc.root << to_xml_node()
123    return doc
124  end
125
126  def to_xml_node
127    el1 = XML::Node.new 'gpx_file'
128    el1['id'] = self.id.to_s
129    el1['name'] = self.name.to_s
130    el1['lat'] = self.latitude.to_s
131    el1['lon'] = self.longitude.to_s
132    el1['user'] = self.user.display_name
133    el1['public'] = self.public.to_s
134    el1['pending'] = (!self.inserted).to_s
135    el1['timestamp'] = self.timestamp.xmlschema
136    return el1
137  end
138
139  def import
140    begin
141      logger.info("GPX Import importing #{name} (#{id}) from #{user.email}")
142
143      # TODO *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz'
144      filetype = `/usr/bin/file -bz #{trace_name}`.chomp
145      gzipped = filetype =~ /gzip compressed/
146      bzipped = filetype =~ /bzip2 compressed/
147      zipped = filetype =~ /Zip archive/
148      tarred = filetype =~ /tar archive/
149
150      if tarred and gzipped then
151        filename = tempfile = "/tmp/#{rand}"
152        system("tar -zxOf #{trace_name} > #{filename}")
153      elsif tarred and bzipped then
154        filename = tempfile = "/tmp/#{rand}"
155        system("tar -jxOf #{trace_name} > #{filename}")
156      elsif tarred
157        filename = tempfile = "/tmp/#{rand}"
158        system("tar -xOf #{trace_name} > #{filename}")
159      elsif gzipped
160        filename = tempfile = "/tmp/#{rand}"
161        system("gunzip -c #{trace_name} > #{filename}")
162      elsif bzipped
163        filename = tempfile = "/tmp/#{rand}"
164        system("bunzip2 -c #{trace_name} > #{filename}")
165      elsif zipped
166        filename = tempfile = "/tmp/#{rand}"
167        system("unzip -p #{trace_name} > #{filename}")
168      else
169        filename = trace_name
170      end
171
172      gpx = OSM::GPXImporter.new(filename)
173
174      f_lat = 0
175      f_lon = 0
176      first = true
177
178      Tracepoint.delete_all(['gpx_id = ?', self.id])
179
180      gpx.points do |point|
181        if first
182          f_lat = point['latitude']
183          f_lon = point['longitude']
184        end
185
186        tp = Tracepoint.new
187        tp.lat = point['latitude'].to_f
188        tp.lng = point['longitude'].to_f
189        tp.altitude = point['altitude'].to_f
190        tp.timestamp = point['timestamp']
191        tp.gpx_id = id
192        tp.trackid = point['segment'].to_i
193        tp.save!
194      end
195
196      if gpx.actual_points > 0
197        max_lat = Tracepoint.maximum('latitude', :conditions => ['gpx_id = ?', id])
198        min_lat = Tracepoint.minimum('latitude', :conditions => ['gpx_id = ?', id])
199        max_lon = Tracepoint.maximum('longitude', :conditions => ['gpx_id = ?', id])
200        min_lon = Tracepoint.minimum('longitude', :conditions => ['gpx_id = ?', id])
201
202        max_lat = max_lat.to_f / 1000000
203        min_lat = min_lat.to_f / 1000000
204        max_lon = max_lon.to_f / 1000000
205        min_lon = min_lon.to_f / 1000000
206
207        self.latitude = f_lat
208        self.longitude = f_lon
209        self.large_picture = gpx.get_picture(min_lat, min_lon, max_lat, max_lon, gpx.actual_points)
210        self.icon_picture = gpx.get_icon(min_lat, min_lon, max_lat, max_lon)
211        self.size = gpx.actual_points
212        self.inserted = true
213        self.save
214      end
215
216      logger.info "done trace #{id}"
217
218      return gpx
219    ensure
220      FileUtils.rm_f(tempfile) if tempfile
221    end
222  end
223end
Note: See TracBrowser for help on using the repository browser.