7 attr_reader :possible_points, :actual_points, :tracksegs
9 def initialize(file, options = {})
11 @maximum_points = options[:maximum_points] || Float::INFINITY
14 def parse_file(reader)
19 when XML::Reader::TYPE_ELEMENT
20 if reader.name == "trkpt"
21 point = TrkPt.new(@tracksegs, reader["lat"].to_f, reader["lon"].to_f)
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
29 when XML::Reader::TYPE_END_ELEMENT
30 if reader.name == "trkpt" && point && point.valid?
34 @lats << point.latitude
35 @lons << point.longitude
36 elsif reader.name == "trkseg"
44 return enum_for(:points) unless block
53 Archive::Reader.open_filename(@file).each_entry_with_data do |entry, data|
54 parse_file(XML::Reader.string(data), &block) if entry.regular?
57 io = ::File.open(@file)
59 case Marcel::MimeType.for(io)
60 when "application/gzip" then io = Zlib::GzipReader.open(@file)
61 when "application/x-bzip" then io = Bzip2::FFI::Reader.open(@file)
64 parse_file(XML::Reader.io(io, :options => XML::Parser::Options::NOERROR), &block)
68 def picture(min_lat, min_lon, max_lat, max_lon, num_points)
74 points_per_frame = (num_points.to_f / nframes).ceil
76 proj = OSM::Mercator.new(min_lat, min_lon, max_lat, max_lon, width, height)
80 (0...nframes).each do |n|
81 frames[n] = GD2::Image::IndexedColor.new(width, height)
82 black = frames[n].palette.allocate(GD2::Color[0, 0, 0])
83 white = frames[n].palette.allocate(GD2::Color[255, 255, 255])
84 grey = frames[n].palette.allocate(GD2::Color[187, 187, 187])
86 frames[n].draw do |pen|
88 pen.rectangle(0, 0, width, height, true)
91 frames[n].draw do |pen|
93 pen.anti_aliasing = true
94 pen.dont_blend = false
101 points.each_with_index do |p, pt|
102 px = proj.x(p.longitude)
103 py = proj.y(p.latitude)
105 if (pt >= (points_per_frame * n)) && (pt <= (points_per_frame * (n + 1)))
113 pen.line(px, py, oldpx, oldpy) unless first
121 image = GD2::AnimatedGif.new
122 image.add(frames.first)
123 frames.each do |frame|
124 image.add(frame, :delay => delay)
128 output = StringIO.new
133 def icon(min_lat, min_lon, max_lat, max_lon)
136 proj = OSM::Mercator.new(min_lat, min_lon, max_lat, max_lon, width, height)
138 image = GD2::Image::IndexedColor.new(width, height)
140 black = image.palette.allocate(GD2::Color[0, 0, 0])
141 white = image.palette.allocate(GD2::Color[255, 255, 255])
145 pen.rectangle(0, 0, width, height, true)
150 pen.anti_aliasing = true
151 pen.dont_blend = false
159 px = proj.x(p.longitude)
160 py = proj.y(p.latitude)
162 pen.line(px, py, oldpx, oldpy) unless first
170 StringIO.new(image.gif)
174 TrkPt = Struct.new(:segment, :latitude, :longitude, :altitude, :timestamp) do
176 latitude && longitude && timestamp &&
177 latitude >= -90 && latitude <= 90 &&
178 longitude >= -180 && longitude <= 180
182 class FileTooBigError < RuntimeError
184 super("GPX File contains too many points")