]> git.openstreetmap.org Git - rails.git/blob - lib/daemons/gpx_import.rb
70764dd38c8a3f9b23979f6e1ba77aec0a30573a
[rails.git] / lib / daemons / gpx_import.rb
1 #!/usr/bin/env ruby
2
3 #You might want to change this
4 ENV["RAILS_ENV"] ||= "development"
5
6 require File.dirname(__FILE__) + "/../../config/environment"
7
8 $running = true;
9 Signal.trap("TERM") do 
10   $running = false
11 end
12
13 logger = ActiveRecord::Base.logger
14
15 while($running) do
16   
17   ActiveRecord::Base.logger.info("GPX Import daemon wake @ #{Time.now}.")
18
19   traces = Trace.find(:all, :conditions => ['inserted = ?', false])
20
21   if traces and traces.length > 0
22     traces.each do |trace|
23       begin
24
25         logger.info("GPX Import importing #{trace.name} from #{trace.user.email}")
26
27         gzipped = `file -b /tmp/#{trace.id}.gpx`.chomp =~/^gzip/
28
29         if gzipped
30           logger.info("gzipped")
31         else
32           logger.info("not gzipped")
33         end
34         gpx = OSM::GPXImporter.new("/tmp/#{trace.id}.gpx")
35
36         gpx.points do |point|
37           tp = Tracepoint.new
38           tp.latitude = point['latitude']
39           tp.longitude = point['longitude']
40           tp.altitude = point['altitude']
41           tp.user_id = trace.user.id
42           tp.gpx_id = trace.id
43           tp.trackid = point['segment']
44           tp.save!
45         end
46
47         if gpx.actual_points > 0
48           max_lat = Tracepoint.maximum('latitude', :conditions => ['gpx_id = ?', trace.id])
49           min_lat = Tracepoint.minimum('latitude', :conditions => ['gpx_id = ?', trace.id])
50           max_lon = Tracepoint.maximum('longitude', :conditions => ['gpx_id = ?', trace.id])
51           min_lon = Tracepoint.minimum('longitude', :conditions => ['gpx_id = ?', trace.id])
52           #logger.info("bbox: #{min_lat} #{max_lat} #{min_lon} #{max_lon}")
53           trace.large_picture = gpx.get_picture(min_lat, min_lon, max_lat, max_lon, gpx.actual_points)
54           trace.icon_picture = gpx.get_icon(min_lat, min_lon, max_lat, max_lon)
55           trace.size = gpx.actual_points
56           trace.inserted = true
57           trace.save
58           Notifier::deliver_gpx_success(trace, gpx.possible_points)
59         else
60           #trace.destroy
61           Notifier::deliver_gpx_failure(trace, '0 points parsed ok. Do they all have lat,lng,alt,timestamp?')
62         end
63
64       rescue Exception => ex
65         #trace.destroy
66         Notifier::deliver_gpx_failure(trace, ex.to_s + ex.backtrace.join("\n") )
67       end
68     end
69   end
70   sleep 15.minutes
71 end