]> git.openstreetmap.org Git - rails.git/blob - lib/osm.rb
various gpx bits
[rails.git] / lib / osm.rb
1 module OSM
2
3   # This piece of magic reads a GPX with SAX and spits out
4   # lat/lng and stuff
5   #
6   # This would print every latitude value:
7   #
8   # gpx = OSM:GPXImporter.new('somefile.gpx')
9   # gpx.points {|p| puts p['latitude']}
10   
11   require 'time'
12   require 'rexml/parsers/sax2parser'
13   require 'rexml/text'
14
15   class GPXImporter
16     attr_reader :possible_points
17     attr_reader :tracksegs
18
19     def initialize(filename)
20       @filename = filename
21       @possible_points = 0
22       @tracksegs = 0
23     end
24
25     def points
26       file = File.new(@filename)
27       parser = REXML::Parsers::SAX2Parser.new( file )
28
29       lat = -1
30       lon = -1
31       ele = -1
32       date = Time.now();
33       gotlatlon = false
34       gotele = false
35       gotdate = false
36
37       parser.listen( :start_element,  %w{ trkpt }) do |uri,localname,qname,attributes| 
38         lat = attributes['lat'].to_f
39         lon = attributes['lon'].to_f
40         gotlatlon = true
41         @possible_points += 1
42       end
43
44       parser.listen( :characters, %w{ ele } ) do |text|
45         ele = text
46         gotele = true
47       end
48
49       parser.listen( :characters, %w{ time } ) do |text|
50         if text && text != ''
51           date = Time.parse(text)
52           gotdate = true
53         end
54       end
55
56       parser.listen( :end_element, %w{ trkseg } ) do |uri, localname, qname|
57         @tracksegs += 1
58       end
59
60       parser.listen( :end_element, %w{ trkpt } ) do |uri,localname,qname|
61         if gotlatlon && gotdate
62           ele = '0' unless gotele
63           if lat < 90 && lat > -90 && lon > -180 && lon < 180
64             yield Hash['latitude' => lat,'longitude' => lon,'timestamp' => date,'altitude' => ele,'segment' => @tracksegs]
65           end
66         end
67         gotlatlon = false
68         gotele = false
69         gotdate = false
70       end
71       parser.parse
72     end
73   end
74 end