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