]> git.openstreetmap.org Git - rails.git/blob - lib/daemons/gpx_import.rb
apply patch from TomH for #477 and dont require http auth on GET to the API.
[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
27         # TODO *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz'
28         filetype = `file -b /home/osm/gpx/#{trace.id}.gpx`.chomp
29         gzipped = filetype =~ /^gzip/
30         zipped = filetype =~ /^Zip/
31
32         if gzipped
33           logger.info("gzipped")
34           filename = "/tmp/#{rand}"
35           system("gunzip -c /home/osm/gpx/#{trace.id}.gpx > #{filename}")
36         elsif zipped
37           logger.info("zipped")
38           filename = "/tmp/#{rand}"
39           system("unzip -p /home/osm/gpx/#{trace.id}.gpx > #{filename}")
40         else
41           logger.info("not gzipped")
42           filename = "/home/osm/gpx/#{trace.id}.gpx"
43         end
44
45         gpx = OSM::GPXImporter.new(filename)
46
47         f_lat = 0
48         f_lon = 0
49         first = true
50
51         gpx.points do |point|
52           if first
53             f_lat = point['latitude']
54             f_lon = point['longitude']
55           end
56
57           tp = Tracepoint.new
58           tp.lat = point['latitude'].to_f
59           tp.lng = point['longitude'].to_f
60           tp.altitude = point['altitude'].to_f
61           tp.user_id = trace.user.id
62           tp.gpx_id = trace.id
63           tp.trackid = point['segment'].to_i
64           tp.save!
65         end
66
67         if gpx.actual_points > 0
68           max_lat = Tracepoint.maximum('latitude', :conditions => ['gpx_id = ?', trace.id])
69           min_lat = Tracepoint.minimum('latitude', :conditions => ['gpx_id = ?', trace.id])
70           max_lon = Tracepoint.maximum('longitude', :conditions => ['gpx_id = ?', trace.id])
71           min_lon = Tracepoint.minimum('longitude', :conditions => ['gpx_id = ?', trace.id])
72
73           max_lat = max_lat.to_f / 1000000
74           min_lat = min_lat.to_f / 1000000
75           max_lon = max_lon.to_f / 1000000
76           min_lon = min_lon.to_f / 1000000
77
78           trace.latitude = f_lat
79           trace.longitude = f_lon
80           trace.large_picture = gpx.get_picture(min_lat, min_lon, max_lat, max_lon, gpx.actual_points)
81           trace.icon_picture = gpx.get_icon(min_lat, min_lon, max_lat, max_lon)
82           trace.size = gpx.actual_points
83           trace.inserted = true
84           trace.save
85
86           if gzipped || zipped
87             FileUtils.rm_f(filename)
88           end
89
90           logger.info "done trace #{trace.id}"
91           Notifier::deliver_gpx_success(trace, gpx.possible_points)
92         else
93           FileUtils.rm_f("/home/osm/gpx/#{trace.id}.gpx", filename)
94           trace.destroy
95           Notifier::deliver_gpx_failure(trace, '0 points parsed ok. Do they all have lat,lng,alt,timestamp?')
96         end
97
98       rescue Exception => ex
99         logger.info ex
100         ex.backtrace.each {|l| logger.info l }
101           FileUtils.rm_f("/home/osm/gpx/#{trace.id}.gpx", filename)
102           trace.destroy
103           Notifier::deliver_gpx_failure(trace, ex.to_s + ex.backtrace.join("\n") )
104       end
105     end
106   end
107
108   Trace.find(:all, :conditions => ['inserted = ?', false]).each do |trace|
109      FileUtils.rm_f("/home/osm/gpx/#{trace.id}.gpx")
110      trace.destroy
111   end
112   exit
113   sleep 15.minutes
114 end