various gpx bits

This commit is contained in:
Steve Coast 2006-12-01 15:59:13 +00:00
parent ece8999dcd
commit fac305e87b
11 changed files with 154 additions and 17 deletions

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,2 @@
class TracepointController < ApplicationController
end

View file

@ -0,0 +1,2 @@
module TracepointHelper
end

View file

@ -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

9
app/models/tracepoint.rb Normal file
View file

@ -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

View file

@ -0,0 +1,7 @@
Hi,
It looks like your GPX file
<%= @trace_name %>
failed to import :-(

View file

@ -0,0 +1,7 @@
Hi,
It looks like your GPX file
<%= @trace_name %>
loaded successfully with <%= @trace_points %> points.

View file

@ -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

View file

@ -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
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

74
lib/osm.rb Normal file
View file

@ -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