gpx insert now works
authorSteve Coast <steve@asklater.com>
Fri, 1 Dec 2006 17:56:13 +0000 (17:56 +0000)
committerSteve Coast <steve@asklater.com>
Fri, 1 Dec 2006 17:56:13 +0000 (17:56 +0000)
app/models/notifier.rb
app/views/notifier/gpx_failure.rhtml
app/views/notifier/gpx_success.rhtml
lib/daemons/gpx_import.rb
lib/osm.rb

index 80c1b1f8236b365200102f901c2ecfe2968fe5b0..c165dca96725085e88dd67a0317593a88c521030 100644 (file)
@@ -21,18 +21,20 @@ class Notifier < ActionMailer::Base
     @body['pass'] = pass
   end
 
-  def gpx_success(trace)
-    @recipients = user.email
+  def gpx_success(trace, possible_points)
+    @recipients = trace.user.email
     @from = 'abuse@openstreetmap.org'
     @subject = '[OpenStreetMap] GPX Import success'
     @body['trace_name'] = trace.name
     @body['trace_points'] = trace.size
+    @body['possible_points'] = possible_points
   end
 
-  def gpx_failure(trace)
-    @recipients = user.email
+  def gpx_failure(trace, error)
+    @recipients = trace.user.email
     @from = 'abuse@openstreetmap.org'
     @subject = '[OpenStreetMap] GPX Import failure'
     @body['trace_name'] = trace.name
+    @body['error'] = error
   end
 end
index 6a2f81e29cffbf47509ebbb8d37ed300c16bf180..61b08772955e4fc1e30fe7091ce6439582ba18ec 100644 (file)
@@ -4,4 +4,6 @@ It looks like your GPX file
 
   <%= @trace_name %>
 
-failed to import :-(
+failed to import. Here's the error:
+
+  <%= @error %>
index 5e77516158312304ba86877d6e43136162ff6f59..ae05bb83b2b9c41986b858262b725a58421a2ab3 100644 (file)
@@ -4,4 +4,5 @@ It looks like your GPX file
 
   <%= @trace_name %>
 
-loaded successfully with <%= @trace_points %> points.
+loaded successfully with <%= @trace_points %> out of a possible
+<%= @possible_points %> points.
index 015a2791648451ae0555ad8c1a2de53a9a2ae517..1fc60858a3cebb423fa0aca1f2dc5ebd7e7576f4 100755 (executable)
@@ -10,6 +10,8 @@ Signal.trap("TERM") do
   $running = false
 end
 
+logger = ActiveRecord::Base.logger
+
 while($running) do
   
   ActiveRecord::Base.logger.info("GPX Import daemon wake @ #{Time.now}.")
@@ -20,16 +22,33 @@ while($running) do
     traces.each do |trace|
       begin
 
-        ActiveRecord::Base.logger.info("GPX Import importing #{trace.name} from #{trace.user.email}")
-
-        #  gpx = OSM::GPXImporter.new('/tmp/2.gpx')
-        #  gpx.points do |point|
-        #    puts point['latitude']
-        #  end
-        
-        Notifier::deliver_gpx_success(trace)
-      rescue
-        Notifier::deliver_gpx_failure(trace)
+        logger.info("GPX Import importing #{trace.name} from #{trace.user.email}")
+
+        gzipped = `file -b /tmp/#{trace.id}.gpx`.chomp =~/^gzip/
+
+        if gzipped
+          logger.info("gzipped")
+        else
+          logger.info("not gzipped")
+        end
+        gpx = OSM::GPXImporter.new("/tmp/#{trace.id}.gpx")
+
+        gpx.points do |point|
+          tp = Tracepoint.new
+          tp.latitude = point['latitude']
+          tp.latitude = point['longitude']
+          tp.altitude = point['altitude']
+          tp.user_id = trace.user.id
+          tp.gpx_id = trace.id
+          tp.trackid = point['segment']
+        end 
+        trace.size = gpx.actual_points
+        trace.inserted = true
+        trace.save
+        Notifier::deliver_gpx_success(trace, gpx.possible_points)
+      rescue Exception => ex
+        trace.destroy
+        Notifier::deliver_gpx_failure(trace, ex.to_s)
       end
     end
   end
index 41c5874599ef957a8422834459ff21849b2e3d2a..af9608132e8f293aa971dbbb8317cbfa93aef99a 100644 (file)
@@ -14,11 +14,13 @@ module OSM
 
   class GPXImporter
     attr_reader :possible_points
+    attr_reader :actual_points
     attr_reader :tracksegs
 
     def initialize(filename)
       @filename = filename
       @possible_points = 0
+      @actual_points = 0
       @tracksegs = 0
     end
 
@@ -61,6 +63,7 @@ module OSM
         if gotlatlon && gotdate
           ele = '0' unless gotele
           if lat < 90 && lat > -90 && lon > -180 && lon < 180
+            @actual_points += 1
             yield Hash['latitude' => lat,'longitude' => lon,'timestamp' => date,'altitude' => ele,'segment' => @tracksegs]
           end
         end