1 # frozen_string_literal: true
 
   9     attr_reader :possible_points, :actual_points, :tracksegs
 
  11     def initialize(file, options = {})
 
  13       @maximum_points = options[:maximum_points] || Float::INFINITY
 
  16     def parse_file(reader)
 
  21         when XML::Reader::TYPE_ELEMENT
 
  22           if reader.name == "trkpt"
 
  23             point = TrkPt.new(@tracksegs, reader["lat"].to_f, reader["lon"].to_f)
 
  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
 
  31         when XML::Reader::TYPE_END_ELEMENT
 
  32           if reader.name == "trkpt" && point&.valid?
 
  36             @lats << point.latitude
 
  37             @lons << point.longitude
 
  38           elsif reader.name == "trkseg"
 
  46       return enum_for(:points) unless block
 
  55         Archive::Reader.open_filename(@file).each_entry_with_data do |entry, data|
 
  56           parse_file(XML::Reader.string(data), &block) if entry.regular?
 
  59         io = ::File.open(@file)
 
  61         case Marcel::MimeType.for(io)
 
  62         when "application/gzip" then io = Zlib::GzipReader.open(@file)
 
  63         when "application/x-bzip2" then io = Bzip2::FFI::Reader.open(@file)
 
  66         parse_file(XML::Reader.io(io, :options => XML::Parser::Options::NOERROR), &block)
 
  70     def picture(min_lat, min_lon, max_lat, max_lon, num_points)
 
  76       points_per_frame = (num_points.to_f / nframes).ceil
 
  78       proj = OSM::Mercator.new(min_lat, min_lon, max_lat, max_lon, width, height)
 
  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])
 
  88         frames[n].draw do |pen|
 
  90           pen.rectangle(0, 0, width, height, true)
 
  93         frames[n].draw do |pen|
 
  95           pen.anti_aliasing = true
 
  96           pen.dont_blend = false
 
 103           @actual_points.times do |pt|
 
 104             px = proj.x @lons[pt]
 
 105             py = proj.y @lats[pt]
 
 107             if (pt >= (points_per_frame * n)) && (pt <= (points_per_frame * (n + 1)))
 
 115             pen.line(px, py, oldpx, oldpy) unless first
 
 123       image = GD2::AnimatedGif.new
 
 124       image.add(frames.first)
 
 125       frames.each do |frame|
 
 126         image.add(frame, :delay => delay)
 
 130       output = StringIO.new
 
 135     def icon(min_lat, min_lon, max_lat, max_lon)
 
 138       proj = OSM::Mercator.new(min_lat, min_lon, max_lat, max_lon, width, height)
 
 140       image = GD2::Image::IndexedColor.new(width, height)
 
 142       black = image.palette.allocate(GD2::Color[0, 0, 0])
 
 143       white = image.palette.allocate(GD2::Color[255, 255, 255])
 
 147         pen.rectangle(0, 0, width, height, true)
 
 152         pen.anti_aliasing = true
 
 153         pen.dont_blend = false
 
 160         @actual_points.times do |pt|
 
 161           px = proj.x @lons[pt]
 
 162           py = proj.y @lats[pt]
 
 164           pen.line(px, py, oldpx, oldpy) unless first
 
 172       StringIO.new(image.gif)
 
 176   TrkPt = Struct.new(:segment, :latitude, :longitude, :altitude, :timestamp) do
 
 178       latitude && longitude && timestamp &&
 
 179         latitude >= -90 && latitude <= 90 &&
 
 180         longitude >= -180 && longitude <= 180
 
 184   class FileTooBigError < RuntimeError
 
 186       super("GPX File contains too many points")