# get missing nodes if there are any
nodes += Node.find(missing_nodes) if missing_nodes.length > 0
- doc = XML::Document.new
- doc.encoding = 'UTF-8'
- root = XML::Node.new 'osm'
- root['version'] = API_VERSION
- root['generator'] = 'OpenStreetMap server'
- doc.root = root
+ doc = get_xml_doc
# get ways
# find which ways are needed
end
nodes.each do |node|
- root << node.to_xml_node()
+ doc.root << node.to_xml_node()
end
segments.each do |segment|
- root << segment.to_xml_node()
+ doc.root << segment.to_xml_node()
end
ways.each do |way|
- root << way.to_xml_node()
+ doc.root << way.to_xml_node()
end
render :text => doc.to_s
File.open(filename, "w") { |f| f.write(@params['trace']['gpx_file'].read) }
@params['trace']['name'] = @params['trace']['gpx_file'].original_filename.gsub(/[^a-zA-Z0-9.]/, '_') # This makes sure filenames are sane
- #@params['trace']['data'] = @params['trace']['gpx_file'].read
-# @params['trace']['mime_type'] = @params['trace']['gpx_file'].content_type.chomp
@params['trace'].delete('gpx_file') # let's remove the field from the hash, because there's no such field in the DB anyway.
@trace = Trace.new(@params['trace'])
@trace.inserted = false
--- /dev/null
+class TracepointController < ApplicationController
+end
--- /dev/null
+module TracepointHelper
+end
@body['pass'] = pass
end
+ def gpx_success(trace)
+ @recipients = user.email
+ @from = 'abuse@openstreetmap.org'
+ @subject = '[OpenStreetMap] GPX Import success'
+ @body['trace_name'] = trace.name
+ @body['trace_points'] = trace.size
+ end
+
+ def gpx_failure(trace)
+ @recipients = user.email
+ @from = 'abuse@openstreetmap.org'
+ @subject = '[OpenStreetMap] GPX Import failure'
+ @body['trace_name'] = trace.name
+ end
end
--- /dev/null
+class Tracepoint < ActiveRecord::Base
+ set_table_name 'gps_points'
+
+ validates_numericality_of :latitude
+ validates_numericality_of :longitude
+
+ belongs_to :user
+ belongs_to :trace, :foreign_key => 'gpx_id'
+end
--- /dev/null
+Hi,
+
+It looks like your GPX file
+
+ <%= @trace_name %>
+
+failed to import :-(
--- /dev/null
+Hi,
+
+It looks like your GPX file
+
+ <%= @trace_name %>
+
+loaded successfully with <%= @trace_points %> points.
--- /dev/null
+class CreateTracepoints < ActiveRecord::Migration
+ def self.up
+ create_table :tracepoints do |t|
+ # t.column :name, :string
+ end
+ end
+
+ def self.down
+ drop_table :tracepoints
+ end
+end
#!/usr/bin/env ruby
#You might want to change this
-ENV["RAILS_ENV"] ||= "production"
+ENV["RAILS_ENV"] ||= "development"
require File.dirname(__FILE__) + "/../../config/environment"
while($running) do
- # Replace this with your code
- ActiveRecord::Base.logger << "This daemon is still running at #{Time.now}.\n"
-
- sleep 10
-end
\ No newline at end of file
+ ActiveRecord::Base.logger.info("GPX Import daemon wake @ #{Time.now}.")
+
+ traces = Trace.find(:all, :conditions => ['inserted = ?', false])
+
+ if traces and traces.length > 0
+ 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)
+ end
+ end
+ end
+ sleep 15.minutes
+end
--- /dev/null
+module OSM
+
+ # This piece of magic reads a GPX with SAX and spits out
+ # lat/lng and stuff
+ #
+ # This would print every latitude value:
+ #
+ # gpx = OSM:GPXImporter.new('somefile.gpx')
+ # gpx.points {|p| puts p['latitude']}
+
+ require 'time'
+ require 'rexml/parsers/sax2parser'
+ require 'rexml/text'
+
+ class GPXImporter
+ attr_reader :possible_points
+ attr_reader :tracksegs
+
+ def initialize(filename)
+ @filename = filename
+ @possible_points = 0
+ @tracksegs = 0
+ end
+
+ def points
+ file = File.new(@filename)
+ parser = REXML::Parsers::SAX2Parser.new( file )
+
+ lat = -1
+ lon = -1
+ ele = -1
+ date = Time.now();
+ gotlatlon = false
+ gotele = false
+ gotdate = false
+
+ parser.listen( :start_element, %w{ trkpt }) do |uri,localname,qname,attributes|
+ lat = attributes['lat'].to_f
+ lon = attributes['lon'].to_f
+ gotlatlon = true
+ @possible_points += 1
+ end
+
+ parser.listen( :characters, %w{ ele } ) do |text|
+ ele = text
+ gotele = true
+ end
+
+ parser.listen( :characters, %w{ time } ) do |text|
+ if text && text != ''
+ date = Time.parse(text)
+ gotdate = true
+ end
+ end
+
+ parser.listen( :end_element, %w{ trkseg } ) do |uri, localname, qname|
+ @tracksegs += 1
+ end
+
+ parser.listen( :end_element, %w{ trkpt } ) do |uri,localname,qname|
+ if gotlatlon && gotdate
+ ele = '0' unless gotele
+ if lat < 90 && lat > -90 && lon > -180 && lon < 180
+ yield Hash['latitude' => lat,'longitude' => lon,'timestamp' => date,'altitude' => ele,'segment' => @tracksegs]
+ end
+ end
+ gotlatlon = false
+ gotele = false
+ gotdate = false
+ end
+ parser.parse
+ end
+ end
+end