From: Steve Coast Date: Fri, 1 Dec 2006 15:59:13 +0000 (+0000) Subject: various gpx bits X-Git-Tag: live~8594 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/fac305e87b9a2df59064ddad666304ceb3d7f7a7?ds=sidebyside various gpx bits --- diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index e829a1732..d6b0b037a 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -40,12 +40,7 @@ class ApiController < ApplicationController # 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 @@ -59,15 +54,15 @@ class ApiController < ApplicationController 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 diff --git a/app/controllers/trace_controller.rb b/app/controllers/trace_controller.rb index 337f27e0e..c8b330601 100644 --- a/app/controllers/trace_controller.rb +++ b/app/controllers/trace_controller.rb @@ -15,8 +15,6 @@ class TraceController < ApplicationController 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 diff --git a/app/controllers/tracepoint_controller.rb b/app/controllers/tracepoint_controller.rb new file mode 100644 index 000000000..fed84c601 --- /dev/null +++ b/app/controllers/tracepoint_controller.rb @@ -0,0 +1,2 @@ +class TracepointController < ApplicationController +end diff --git a/app/helpers/tracepoint_helper.rb b/app/helpers/tracepoint_helper.rb new file mode 100644 index 000000000..8a5a031a9 --- /dev/null +++ b/app/helpers/tracepoint_helper.rb @@ -0,0 +1,2 @@ +module TracepointHelper +end diff --git a/app/models/notifier.rb b/app/models/notifier.rb index 4b80e1de8..80c1b1f82 100644 --- a/app/models/notifier.rb +++ b/app/models/notifier.rb @@ -21,4 +21,18 @@ class Notifier < ActionMailer::Base @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 diff --git a/app/models/tracepoint.rb b/app/models/tracepoint.rb new file mode 100644 index 000000000..9de8dc548 --- /dev/null +++ b/app/models/tracepoint.rb @@ -0,0 +1,9 @@ +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 diff --git a/app/views/notifier/gpx_failure.rhtml b/app/views/notifier/gpx_failure.rhtml new file mode 100644 index 000000000..6a2f81e29 --- /dev/null +++ b/app/views/notifier/gpx_failure.rhtml @@ -0,0 +1,7 @@ +Hi, + +It looks like your GPX file + + <%= @trace_name %> + +failed to import :-( diff --git a/app/views/notifier/gpx_success.rhtml b/app/views/notifier/gpx_success.rhtml new file mode 100644 index 000000000..5e7751615 --- /dev/null +++ b/app/views/notifier/gpx_success.rhtml @@ -0,0 +1,7 @@ +Hi, + +It looks like your GPX file + + <%= @trace_name %> + +loaded successfully with <%= @trace_points %> points. diff --git a/db/migrate/015_create_tracepoints.rb b/db/migrate/015_create_tracepoints.rb new file mode 100644 index 000000000..3d75f03e2 --- /dev/null +++ b/db/migrate/015_create_tracepoints.rb @@ -0,0 +1,11 @@ +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 diff --git a/lib/daemons/gpx_import.rb b/lib/daemons/gpx_import.rb index c3cd9b06b..015a27916 100755 --- a/lib/daemons/gpx_import.rb +++ b/lib/daemons/gpx_import.rb @@ -1,7 +1,7 @@ #!/usr/bin/env ruby #You might want to change this -ENV["RAILS_ENV"] ||= "production" +ENV["RAILS_ENV"] ||= "development" require File.dirname(__FILE__) + "/../../config/environment" @@ -12,8 +12,26 @@ end 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 diff --git a/lib/osm.rb b/lib/osm.rb new file mode 100644 index 000000000..41c587459 --- /dev/null +++ b/lib/osm.rb @@ -0,0 +1,74 @@ +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