pr2gpx 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/pr2gpx.rb +30 -48
- data/lib/pr2gpx/gpx.rb +53 -0
- data/lib/pr2gpx/options.rb +28 -0
- metadata +2 -1
data/lib/pr2gpx.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
|
-
require 'nokogiri'
|
2
|
-
require 'nokogiri/xml'
|
3
|
-
require 'pr2gpx/parser'
|
4
1
|
require 'pr2gpx/options'
|
2
|
+
require 'pr2gpx/parser'
|
3
|
+
require 'pr2gpx/gpx'
|
5
4
|
|
5
|
+
# Creates an Enumerable with one entry for every file in search_path,
|
6
|
+
# containing the files content.
|
6
7
|
def enumerate_files search_path
|
7
8
|
Enumerator.new do |e|
|
8
9
|
Dir
|
9
10
|
.glob(search_path)
|
10
11
|
.each do |filename|
|
12
|
+
$stderr.puts "Reading #{filename}" if $verbose
|
11
13
|
File.open filename do |file|
|
12
14
|
e.yield file.read()
|
13
15
|
end
|
@@ -15,6 +17,13 @@ def enumerate_files search_path
|
|
15
17
|
end
|
16
18
|
end
|
17
19
|
|
20
|
+
# Runs each element of content_enum through ReportParser::parse,
|
21
|
+
# receiving an Enumerable of PositionReport. If the report passes
|
22
|
+
# through the filter, it is added to the hash of the station, if
|
23
|
+
# no other report with the same date exists.
|
24
|
+
#
|
25
|
+
# Those hashes are stored in another hash, indexed by callsign,
|
26
|
+
# which gets returned.
|
18
27
|
def load_data content_enum, filter
|
19
28
|
reportParser = ReportParser.new
|
20
29
|
stations = Hash.new
|
@@ -24,16 +33,25 @@ def load_data content_enum, filter
|
|
24
33
|
if reports
|
25
34
|
reports.each do |report|
|
26
35
|
if filter.include? report
|
36
|
+
$stderr.print 'o' if $verbose
|
27
37
|
stations[report.callsign] = Hash.new unless stations.has_key? report.callsign
|
28
38
|
stations[report.callsign][report.date] = report unless stations[report.callsign].has_key? report.date
|
39
|
+
else
|
40
|
+
$stderr.print '.' if $verbose
|
29
41
|
end
|
30
42
|
end
|
31
43
|
end
|
44
|
+
$stderr.puts if $verbose
|
32
45
|
end
|
33
46
|
|
34
47
|
stations
|
35
48
|
end
|
36
49
|
|
50
|
+
# Converts the reports the entries of the stations-hash, which are
|
51
|
+
# hashes indexed by date, into arrays sorted by date.
|
52
|
+
#
|
53
|
+
# The parameter last can be used to limit the reports to the N
|
54
|
+
# most recent ones.
|
37
55
|
def filter_data! stations, last
|
38
56
|
stations.each do |callsign, reports|
|
39
57
|
stations[callsign] = reports.values
|
@@ -46,52 +64,16 @@ def filter_data! stations, last
|
|
46
64
|
end
|
47
65
|
end
|
48
66
|
|
49
|
-
def build_gpx stations, create_trk, create_wpt
|
50
|
-
builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
|
51
|
-
xml.gpx(xmlns: 'http://www.topografix.com/GPX/1/1') do
|
52
|
-
if create_trk
|
53
|
-
stations.each do |callsign, reports|
|
54
|
-
xml.trk do
|
55
|
-
xml.name callsign
|
56
|
-
xml.trkseg do
|
57
|
-
reports.each do |report|
|
58
|
-
xml.send('trkpt', lat: report.position.latitude, lon: report.position.longitude) do
|
59
|
-
xml.name report.comment
|
60
|
-
xml.time report.date.strftime('%FT%TZ')
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
if create_wpt
|
68
|
-
stations.each do |callsign, reports|
|
69
|
-
last = reports.last
|
70
|
-
reports.each do |report|
|
71
|
-
xml.send('wpt', lat: report.position.latitude, lon: report.position.longitude) do
|
72
|
-
xml.name report.callsign
|
73
|
-
xml.desc report.comment
|
74
|
-
xml.time report.date.strftime('%FT%TZ')
|
75
|
-
xml.type 'WPT'
|
76
|
-
xml.sym report == last ? 'triangle' : 'circle'
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
builder.to_xml
|
84
|
-
end
|
85
|
-
|
86
|
-
def write_gpx filename, gpx
|
87
|
-
File.open filename, 'w:UTF-8' do |file|
|
88
|
-
file.write gpx
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
67
|
options = parse_options ARGV
|
93
68
|
exit if not options
|
94
69
|
|
70
|
+
errors = validate_options(options)
|
71
|
+
if errors
|
72
|
+
$stderr.puts errors
|
73
|
+
exit
|
74
|
+
end
|
75
|
+
|
76
|
+
# normalize path separators
|
95
77
|
options[:input].gsub!('\\', '/')
|
96
78
|
options[:output].gsub!('\\', '/') if options[:output]
|
97
79
|
|
@@ -103,7 +85,7 @@ filter = ReportFilter.new options[:callsign]
|
|
103
85
|
stations = load_data(enumerate_files(search_path), filter)
|
104
86
|
filter_data! stations, options[:last]
|
105
87
|
|
106
|
-
if options[:split]
|
88
|
+
if options[:split] # create one document for each station
|
107
89
|
stations.each do |callsign, reports|
|
108
90
|
gpx = build_gpx({ callsign => reports }, options[:create_trk], options[:create_wpt])
|
109
91
|
|
@@ -113,7 +95,7 @@ if options[:split]
|
|
113
95
|
puts gpx
|
114
96
|
end
|
115
97
|
end
|
116
|
-
else
|
98
|
+
else # create one document for all data
|
117
99
|
gpx = build_gpx(stations, options[:create_trk], options[:create_wpt])
|
118
100
|
|
119
101
|
if options[:output] then
|
data/lib/pr2gpx/gpx.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require 'nokogiri/xml'
|
3
|
+
|
4
|
+
# Creates the GPX document from the stations hash.
|
5
|
+
#
|
6
|
+
# If create_trk is true, one trk per station gets created.
|
7
|
+
#
|
8
|
+
# If create_wpt is true, one waypoint for every report of every
|
9
|
+
# station gets created.
|
10
|
+
def build_gpx stations, create_trk, create_wpt
|
11
|
+
builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
|
12
|
+
xml.gpx(xmlns: 'http://www.topografix.com/GPX/1/1') do
|
13
|
+
if create_trk
|
14
|
+
stations.each do |callsign, reports|
|
15
|
+
xml.trk do
|
16
|
+
xml.name callsign
|
17
|
+
xml.trkseg do
|
18
|
+
reports.each do |report|
|
19
|
+
xml.send('trkpt', lat: report.position.latitude, lon: report.position.longitude) do
|
20
|
+
xml.name report.comment
|
21
|
+
xml.time report.date.strftime('%FT%TZ')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
if create_wpt
|
29
|
+
stations.each do |callsign, reports|
|
30
|
+
last = reports.last
|
31
|
+
reports.each do |report|
|
32
|
+
xml.send('wpt', lat: report.position.latitude, lon: report.position.longitude) do
|
33
|
+
xml.name report.callsign
|
34
|
+
xml.desc report.comment
|
35
|
+
xml.time report.date.strftime('%FT%TZ')
|
36
|
+
xml.type 'WPT'
|
37
|
+
xml.sym report == last ? 'triangle' : 'circle'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
builder.to_xml
|
45
|
+
end
|
46
|
+
|
47
|
+
# Writes the gpx document with UTF-8 encoding to filename.
|
48
|
+
def write_gpx filename, gpx
|
49
|
+
$stderr.puts "Writing #{filename}" if $verbose
|
50
|
+
File.open filename, 'w:UTF-8' do |file|
|
51
|
+
file.write gpx
|
52
|
+
end
|
53
|
+
end
|
data/lib/pr2gpx/options.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'optparse'
|
2
2
|
|
3
|
+
# If options are valid, returns a hash of options, otherwise nil.
|
3
4
|
def parse_options argv
|
4
5
|
options = {}
|
5
6
|
mandatory = [:input]
|
@@ -95,4 +96,31 @@ def parse_options argv
|
|
95
96
|
end
|
96
97
|
|
97
98
|
options
|
99
|
+
end
|
100
|
+
|
101
|
+
# If errors are encountered, returns an array of error messages, otherwise nil.
|
102
|
+
def validate_options options
|
103
|
+
errors = []
|
104
|
+
|
105
|
+
if not File.exists?(options[:input])
|
106
|
+
errors << 'Path specified in --input does not exist.'
|
107
|
+
elsif not File.directory?(options[:input])
|
108
|
+
errors << 'Path specified in --input is not a directory.'
|
109
|
+
end
|
110
|
+
|
111
|
+
if options[:split]
|
112
|
+
if not File.exists?(options[:output])
|
113
|
+
errors << 'Path specified in --output does not exist.'
|
114
|
+
elsif not File.directory?(options[:output])
|
115
|
+
errors << 'Path specified in --output is not a directory.'
|
116
|
+
end
|
117
|
+
else
|
118
|
+
if not File.exists?(File.dirname(options[:output]))
|
119
|
+
errors << 'Path specified in --output does not exist.'
|
120
|
+
elsif File.directory?(options[:output])
|
121
|
+
errors << 'Path specified in --output is a directory.'
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
errors if errors.length > 0
|
98
126
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pr2gpx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -37,6 +37,7 @@ executables:
|
|
37
37
|
extensions: []
|
38
38
|
extra_rdoc_files: []
|
39
39
|
files:
|
40
|
+
- lib/pr2gpx/gpx.rb
|
40
41
|
- lib/pr2gpx/options.rb
|
41
42
|
- lib/pr2gpx/parser.rb
|
42
43
|
- lib/pr2gpx.rb
|