]> git.openstreetmap.org Git - rails.git/blob - lib/daemons/gpx_import.rb
updates from hacking day
[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} (#{trace.id}) from #{trace.user.email}")
26 \r
27         # TODO *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz'
28         gzipped = `file -b /tmp/#{trace.id}.gpx`.chomp =~/^gzip/\r
29
30         if gzipped
31           logger.info("gzipped")
32         else
33           logger.info("not gzipped")
34         end
35
36         gpx = OSM::GPXImporter.new("/home/osm/gpx/#{trace.id}.gpx")
37
38         f_lat = 0
39         f_lon = 0
40         first = true
41
42         gpx.points do |point|
43           if first
44             f_lat = point['latitude']
45             f_lon = point['longitude']
46           end
47
48           tp = Tracepoint.new
49           tp.lat = point['latitude'].to_f
50           tp.lng = point['longitude'].to_f
51           tp.altitude = point['altitude'].to_f
52           tp.user_id = trace.user.id
53           tp.gpx_id = trace.id
54           tp.trackid = point['segment'].to_i
55           tp.save!
56         end
57
58         if gpx.actual_points > 0
59           max_lat = Tracepoint.maximum('latitude', :conditions => ['gpx_id = ?', trace.id])
60           min_lat = Tracepoint.minimum('latitude', :conditions => ['gpx_id = ?', trace.id])
61           max_lon = Tracepoint.maximum('longitude', :conditions => ['gpx_id = ?', trace.id])
62           min_lon = Tracepoint.minimum('longitude', :conditions => ['gpx_id = ?', trace.id])
63           
64           max_lat = max_lat.to_f / 1000000
65           min_lat = min_lat.to_f / 1000000
66           max_lon = max_lon.to_f / 1000000
67           min_lon = min_lon.to_f / 1000000
68  
69           trace.latitude = f_lat
70           trace.longitude = f_lon
71           trace.large_picture = gpx.get_picture(min_lat, min_lon, max_lat, max_lon, gpx.actual_points)
72           trace.icon_picture = gpx.get_icon(min_lat, min_lon, max_lat, max_lon)
73           trace.size = gpx.actual_points
74           trace.inserted = true
75           trace.save
76
77           logger.info "done trace #{trace.id} -------------------------------------------------------------------------------"
78     #      Notifier::deliver_gpx_success(trace, gpx.possible_points)
79         else
80           #trace.destroy
81 #          Notifier::deliver_gpx_failure(trace, '0 points parsed ok. Do they all have lat,lng,alt,timestamp?')
82         end
83
84       rescue Exception => ex
85         logger.info ex
86         ex.backtrace.each {|l| logger.info l }
87         #trace.destroy
88  #       Notifier::deliver_gpx_failure(trace, ex.to_s + ex.backtrace.join("\n") )
89       end
90     end
91   end
92   sleep 15.minutes
93 end