gippix 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/gippix.rb +136 -0
  3. metadata +64 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a887a30b6befacd1406c67cb14d0426ddeab808c
4
+ data.tar.gz: c4a608b82422145d4939b9d149f1910486c870e8
5
+ SHA512:
6
+ metadata.gz: cc02ec00f4d40096a5a999f66ce8af4d1baa7519408fd0dd57ba26df9f9541d32f81c51cefc9670d021e2a032c2c5a8bbd835b7ddc74ae52a0c00c5f5ff82b0d
7
+ data.tar.gz: 51e88137ff528a82d28a5b725c14c3b8232964ec4811108b57c53f9f5841160b3b8738e5dfc3daefb656421f4401193095378dff9be8c6730e3e3d9e0a771da5
@@ -0,0 +1,136 @@
1
+ require 'ostruct'
2
+ require 'nokogiri'
3
+ require 'date'
4
+
5
+ # GPX
6
+ # http://www.topografix.com/GPX/1/1/
7
+ #
8
+ # Endeavoring to be a full GPX parser/ writer.
9
+
10
+ class Gippix
11
+ class UnknownFileType < StandardError; end
12
+ class ParseError < StandardError; end
13
+
14
+ class Point < Struct.new :lat, :lon, :elevation, :time
15
+ def to_s
16
+ "POINT(%s %s %s)" % [ lon, lat, elevation ]
17
+ end
18
+ end
19
+
20
+
21
+ def self.parser(file_name)
22
+ parser_class_for_file(file_name).new(file_name)
23
+ rescue Errno::ENOENT => e
24
+ raise ParseError.new(e.to_s)
25
+ end
26
+
27
+ def self.parser_class_for_file(file_name)
28
+ {
29
+ 'gpx' => Gpx
30
+ }[ File.extname(file_name)[1..-1].downcase ] || raise(UnknownFileType.new(file_name))
31
+ end
32
+
33
+ ##########################################################################################
34
+ class Base
35
+ attr_accessor :doc, :file_name, :name, :description, :points
36
+
37
+ def initialize(file_name)
38
+ @file_name = file_name
39
+ @doc = Nokogiri::XML( File.read file_name )
40
+ @points = []
41
+ end
42
+
43
+ def with xpath, &blk
44
+ yield @doc.xpath(xpath)
45
+ end
46
+ end
47
+
48
+ ##########################################################################################
49
+ class GpxGenerator
50
+ attr_reader :builder
51
+
52
+ def initialize
53
+ @builder = Builder::XmlMarkup.new(:indent => 2)
54
+ end
55
+
56
+ def build(points)
57
+ gpx_url = 'http://www.topografix.com/GPX/1/1'
58
+
59
+ gpx_params = {
60
+ :version => "1.1",
61
+ :creator => 'Zugunroute Mobile http://zugunroute.com',
62
+ :'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
63
+ :'xmlns' => gpx_url,
64
+ :'xsi:schemaLocation' => "#{gpx_url} #{gpx_url}/gpx.xsd"
65
+ }
66
+
67
+ builder.gpx( gpx_params ) { |gpx|
68
+ gpx.metadata { |meta|
69
+ meta.name "joe"
70
+ meta.desc "Mamma"
71
+ meta.author { |a|
72
+ a.name "Zugunroute"
73
+ a.email :id => 'support', :domain => 'zugunroute.com'
74
+ }
75
+ meta.copyright(:author => "xforty technologies") { |copy| copy.year "2012" }
76
+ meta.link(:href => "http://zugunroute.com") { |link|
77
+ link.text "Zugunroute"
78
+ link.type "text/html"
79
+ }
80
+
81
+ meta.time DateTime.now.strftime("%Y-%m-%d-T%H:%M:%SZ")
82
+ }
83
+
84
+ gpx.trk { |trk|
85
+ trk.trkseg { |seg|
86
+ points.each { |ll|
87
+ seg.trkpt(:lat => ll.lat, :lon => ll.lon) { |pt|
88
+ pt.ele ll.elevation
89
+ pt.time DateTime.parse(ll.timestamp)
90
+ }
91
+ }
92
+ }
93
+ }
94
+ }
95
+ end
96
+
97
+ def write_to_tempfile
98
+ file = Tempfile.new(['activity','.gpx'])
99
+
100
+ file.puts %Q{<?xml version="1.0" encoding="UTF-8"?>}
101
+ file.puts builder.target!
102
+ file.flush
103
+ file
104
+ end
105
+ end
106
+ ##########################################################################################
107
+ class Gpx < Base
108
+
109
+ def parse
110
+ return self if @doc.nil?
111
+
112
+ @description = @name = @doc.xpath('//xmlns:gpx/xmlns:trk[1]/xmlns:name').inner_text
113
+
114
+ @points = parse_points
115
+ @doc = nil
116
+
117
+ self
118
+ rescue Nokogiri::XML::XPath::SyntaxError
119
+ raise ParseError.new("Could not find appropriate gpx root element")
120
+ end
121
+
122
+ def parse_points
123
+ doc.xpath('//xmlns:gpx/xmlns:trk//xmlns:trkpt').map { |pt|
124
+ Point.new(
125
+ pt['lat'].to_f, pt['lon'].to_f,
126
+ pt.xpath('xmlns:ele').inner_text.to_f,
127
+ parse_date(pt.xpath('xmlns:time'))
128
+ )
129
+ }
130
+ end
131
+
132
+ def parse_date(doc)
133
+ DateTime.parse doc.inner_text rescue nil
134
+ end
135
+ end
136
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gippix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Libby
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.1.4
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.1'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.1.4
33
+ description: Reads GPX files.
34
+ email: alibby@andylibby.org
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - lib/gippix.rb
40
+ homepage: http://rubygems.org/xforty/gippix
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.4.6
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: GPX parser/ reader
64
+ test_files: []