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

Last change on this file since 3112 was 3112, checked in by richard, 13 years ago

Latest Potlatch, plus TomH's GPX fix

File size: 4.5 KB
Line 
1class 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.timestamp = point['timestamp']
114        tp.user_id = user.id
115        tp.gpx_id = id
116        tp.trackid = point['segment'].to_i
117        tp.save!
118      end
119
120      if gpx.actual_points > 0
121        max_lat = Tracepoint.maximum('latitude', :conditions => ['gpx_id = ?', id])
122        min_lat = Tracepoint.minimum('latitude', :conditions => ['gpx_id = ?', id])
123        max_lon = Tracepoint.maximum('longitude', :conditions => ['gpx_id = ?', id])
124        min_lon = Tracepoint.minimum('longitude', :conditions => ['gpx_id = ?', id])
125
126        max_lat = max_lat.to_f / 1000000
127        min_lat = min_lat.to_f / 1000000
128        max_lon = max_lon.to_f / 1000000
129        min_lon = min_lon.to_f / 1000000
130
131        self.latitude = f_lat
132        self.longitude = f_lon
133        self.large_picture = gpx.get_picture(min_lat, min_lon, max_lat, max_lon, gpx.actual_points)
134        self.icon_picture = gpx.get_icon(min_lat, min_lon, max_lat, max_lon)
135        self.size = gpx.actual_points
136        self.inserted = true
137        self.save
138
139        Notifier::deliver_gpx_success(self, gpx.possible_points)
140      else
141        FileUtils.rm_f("/home/osm/gpx/#{id}.gpx")
142        self.destroy
143        Notifier::deliver_gpx_failure(self, '0 points parsed ok. Do they all have lat,lng,alt,timestamp?')
144      end
145
146      logger.info "done trace #{id}"
147    rescue Exception => ex
148      logger.info ex
149      ex.backtrace.each {|l| logger.info l }
150      FileUtils.rm_f("/home/osm/gpx/#{id}.gpx")
151      self.destroy
152      Notifier::deliver_gpx_failure(self, ex.to_s + ex.backtrace.join("\n") )
153    ensure
154      FileUtils.rm_f(tempfile) if tempfile
155    end
156  end
157end
Note: See TracBrowser for help on using the repository browser.