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 elsif reader.name == "trkseg"
42 return enum_for(:points) unless block
49 Archive::Reader.open_filename(@file).each_entry_with_data do |entry, data|
50 parse_file(XML::Reader.string(data), &block) if entry.regular?
53 io = ::File.open(@file)
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)
60 parse_file(XML::Reader.io(io), &block)
64 def picture(min_lat, min_lon, max_lat, max_lon, num_points)
70 points_per_frame = (num_points.to_f / nframes).ceil
72 proj = OSM::Mercator.new(min_lat, min_lon, max_lat, max_lon, width, height)
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])
82 frames[n].draw do |pen|
84 pen.rectangle(0, 0, width, height, true)
87 frames[n].draw do |pen|
89 pen.anti_aliasing = true
90 pen.dont_blend = false
97 points.each_with_index do |p, pt|
98 px = proj.x(p.longitude)
99 py = proj.y(p.latitude)
101 if (pt >= (points_per_frame * n)) && (pt <= (points_per_frame * (n + 1)))
109 pen.line(px, py, oldpx, oldpy) unless first
117 image = GD2::AnimatedGif.new
118 image.add(frames.first)
119 frames.each do |frame|
120 image.add(frame, :delay => delay)
124 output = StringIO.new
129 def icon(min_lat, min_lon, max_lat, max_lon)
132 proj = OSM::Mercator.new(min_lat, min_lon, max_lat, max_lon, width, height)
134 image = GD2::Image::IndexedColor.new(width, height)
136 black = image.palette.allocate(GD2::Color[0, 0, 0])
137 white = image.palette.allocate(GD2::Color[255, 255, 255])
141 pen.rectangle(0, 0, width, height, true)
146 pen.anti_aliasing = true
147 pen.dont_blend = false
155 px = proj.x(p.longitude)
156 py = proj.y(p.latitude)
158 pen.line(px, py, oldpx, oldpy) unless first
166 StringIO.new(image.gif)
170 TrkPt = Struct.new(:segment, :latitude, :longitude, :altitude, :timestamp) do
172 latitude && longitude && timestamp &&
173 latitude >= -90 && latitude <= 90 &&
174 longitude >= -180 && longitude <= 180
178 class FileTooBigError < RuntimeError
180 super("GPX File contains too many points")