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