]> git.openstreetmap.org Git - rails.git/blob - lib/gpx.rb
Merge remote-tracking branch 'upstream/pull/4720'
[rails.git] / lib / gpx.rb
1 module GPX
2   class File
3     require "libxml"
4
5     include LibXML
6
7     attr_reader :possible_points, :actual_points, :tracksegs
8
9     def initialize(file, options = {})
10       @file = file
11       @maximum_points = options[:maximum_points] || Float::INFINITY
12     end
13
14     def parse_file(reader)
15       point = nil
16
17       while reader.read
18         case reader.node_type
19         when XML::Reader::TYPE_ELEMENT
20           if reader.name == "trkpt"
21             point = TrkPt.new(@tracksegs, reader["lat"].to_f, reader["lon"].to_f)
22             @possible_points += 1
23             raise FileTooBigError if @possible_points > @maximum_points
24           elsif reader.name == "ele" && point
25             point.altitude = reader.read_string.to_f
26           elsif reader.name == "time" && point
27             point.timestamp = Time.parse(reader.read_string).utc
28           end
29         when XML::Reader::TYPE_END_ELEMENT
30           if reader.name == "trkpt" && point && point.valid?
31             point.altitude ||= 0
32             yield point
33             @actual_points += 1
34           elsif reader.name == "trkseg"
35             @tracksegs += 1
36           end
37         end
38       end
39     end
40
41     def points(&block)
42       return enum_for(:points) unless block
43
44       @possible_points = 0
45       @actual_points = 0
46       @tracksegs = 0
47
48       begin
49         Archive::Reader.open_filename(@file).each_entry_with_data do |entry, data|
50           parse_file(XML::Reader.string(data), &block) if entry.regular?
51         end
52       rescue Archive::Error
53         io = ::File.open(@file)
54
55         case Marcel::MimeType.for(io)
56         when "application/gzip" then io = Zlib::GzipReader.open(@file)
57         when "application/x-bzip" then io = Bzip2::FFI::Reader.open(@file)
58         end
59
60         parse_file(XML::Reader.io(io), &block)
61       end
62     end
63
64     def picture(min_lat, min_lon, max_lat, max_lon, num_points)
65       nframes = 10
66       width = 250
67       height = 250
68       delay = 50
69
70       points_per_frame = (num_points.to_f / nframes).ceil
71
72       proj = OSM::Mercator.new(min_lat, min_lon, max_lat, max_lon, width, height)
73
74       frames = []
75
76       (0...nframes).each do |n|
77         frames[n] = GD2::Image::IndexedColor.new(width, height)
78         black = frames[n].palette.allocate(GD2::Color[0, 0, 0])
79         white = frames[n].palette.allocate(GD2::Color[255, 255, 255])
80         grey = frames[n].palette.allocate(GD2::Color[187, 187, 187])
81
82         frames[n].draw do |pen|
83           pen.color = white
84           pen.rectangle(0, 0, width, height, true)
85         end
86
87         frames[n].draw do |pen|
88           pen.color = black
89           pen.anti_aliasing = true
90           pen.dont_blend = false
91
92           oldpx = 0.0
93           oldpy = 0.0
94
95           first = true
96
97           points.each_with_index do |p, pt|
98             px = proj.x(p.longitude)
99             py = proj.y(p.latitude)
100
101             if (pt >= (points_per_frame * n)) && (pt <= (points_per_frame * (n + 1)))
102               pen.thickness = 3
103               pen.color = black
104             else
105               pen.thickness = 1
106               pen.color = grey
107             end
108
109             pen.line(px, py, oldpx, oldpy) unless first
110             first = false
111             oldpy = py
112             oldpx = px
113           end
114         end
115       end
116
117       image = GD2::AnimatedGif.new
118       image.add(frames.first)
119       frames.each do |frame|
120         image.add(frame, :delay => delay)
121       end
122       image.end
123
124       output = StringIO.new
125       image.export(output)
126       output
127     end
128
129     def icon(min_lat, min_lon, max_lat, max_lon)
130       width = 50
131       height = 50
132       proj = OSM::Mercator.new(min_lat, min_lon, max_lat, max_lon, width, height)
133
134       image = GD2::Image::IndexedColor.new(width, height)
135
136       black = image.palette.allocate(GD2::Color[0, 0, 0])
137       white = image.palette.allocate(GD2::Color[255, 255, 255])
138
139       image.draw do |pen|
140         pen.color = white
141         pen.rectangle(0, 0, width, height, true)
142       end
143
144       image.draw do |pen|
145         pen.color = black
146         pen.anti_aliasing = true
147         pen.dont_blend = false
148
149         oldpx = 0.0
150         oldpy = 0.0
151
152         first = true
153
154         points do |p|
155           px = proj.x(p.longitude)
156           py = proj.y(p.latitude)
157
158           pen.line(px, py, oldpx, oldpy) unless first
159
160           first = false
161           oldpy = py
162           oldpx = px
163         end
164       end
165
166       StringIO.new(image.gif)
167     end
168   end
169
170   TrkPt = Struct.new(:segment, :latitude, :longitude, :altitude, :timestamp) do
171     def valid?
172       latitude && longitude && timestamp &&
173         latitude >= -90 && latitude <= 90 &&
174         longitude >= -180 && longitude <= 180
175     end
176   end
177
178   class FileTooBigError < RuntimeError
179     def initialise
180       super("GPX File contains too many points")
181     end
182   end
183 end