georss4rb 0.0.2 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +8 -7
- data/lib/georss4rb.rb +38 -245
- data/lib/save_georss4rb.rb +199 -0
- data/rakefile.rb +3 -2
- data/test/test_georss.rb +46 -72
- metadata +12 -2
data/README
CHANGED
@@ -4,14 +4,13 @@ GeoRSS4rb is an addon to the FeedTools library that aims at making GeoRSS inform
|
|
4
4
|
==Installing
|
5
5
|
Just type under a command line:
|
6
6
|
gem install georss4rb
|
7
|
-
And the last version will be automatically downloaded and installed, along with the latest version of FeedTools, if you don't already have it.
|
7
|
+
And the last version will be automatically downloaded and installed, along with the latest version of FeedTools, if you don't already have it, as well as GeoReuby (which is used for the actual GeoRSS parsing).
|
8
8
|
|
9
9
|
==Operations
|
10
|
-
Now you have a +location+ method on <tt>FeedTools::Feed</tt> and <tt>FeedTools::Item</tt> objects, that will return either +nil+, if no location exists, or an object of a subclass of <tt>
|
11
|
-
- <tt>
|
12
|
-
- <tt>
|
13
|
-
- <tt>
|
14
|
-
- <tt>GeoRss4rb::Box</tt>: Holds 2 corner points, +lower_corner+ and +upper_corner+
|
10
|
+
Now you have a +location+ method on <tt>FeedTools::Feed</tt> and <tt>FeedTools::Item</tt> objects, that will return either +nil+, if no location exists, or an object of a subclass of <tt>GeoRuby::SimpleFeatures::Geometry</tt> or of <tt>GeoRuby::SimpleFeatures::Envelope</tt>, if a location exists. The subclass can be one of four types:
|
11
|
+
- <tt>Point</tt>: With 2 properties, +lat+ and +lon+
|
12
|
+
- <tt>LineString</tt>: Behaves like an array of points
|
13
|
+
- <tt>Polygon</tt>: Behaves like an array of lines (although the GeoRss spec version 1 allows only the exterior ring to be defined)
|
15
14
|
|
16
15
|
Here is an example of usage:
|
17
16
|
feed = FeedTools::Feed.open('http://example.org/georss')
|
@@ -20,8 +19,10 @@ Here is an example of usage:
|
|
20
19
|
feed.items.first.location #=><GeoRss4rb::Point ...>
|
21
20
|
The location of the entire feed could be a bounding of the location of the items. Then each item can have a location item, here a point. Note that the specific GeoRSS dialect used does not make any difference in the object returned.
|
22
21
|
|
22
|
+
It is also possible to retrieve the GeoRss tags (such as +radius+ or +featuretypetag+, if present : Use the <tt>georss_tags</tt> property on either a +Feed+ or +FeedItem+ object. It will return an instance of <tt>GeoRuby::SimpleFeatures::GeoRssTags</tt>.
|
23
|
+
|
23
24
|
==Changes since last version
|
24
|
-
-
|
25
|
+
- Switch to using the GeoRSS parsing features of GeoRuby
|
25
26
|
|
26
27
|
==TODO
|
27
28
|
- Add support for other RSS parsing libraries
|
data/lib/georss4rb.rb
CHANGED
@@ -1,268 +1,61 @@
|
|
1
|
+
require 'rubygems'
|
1
2
|
require 'feed_tools'
|
3
|
+
require 'geo_ruby'
|
2
4
|
|
3
|
-
module
|
5
|
+
module Georss4rb
|
4
6
|
module Location
|
5
|
-
#Returns a
|
7
|
+
#Returns a Georss4rb::Geometry object if info about the location is found.
|
6
8
|
#Returns +nil+ if no location info is found.
|
7
9
|
def location
|
8
|
-
if
|
10
|
+
if !located?
|
11
|
+
get_location_info
|
12
|
+
end
|
13
|
+
@location
|
14
|
+
end
|
15
|
+
|
16
|
+
def georss_tags
|
17
|
+
if !located?
|
18
|
+
get_location_info
|
19
|
+
end
|
20
|
+
@georss_tags
|
21
|
+
end
|
22
|
+
|
23
|
+
#True if the feed or the item has some location info
|
24
|
+
def located?
|
25
|
+
!@location.nil?
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def get_location_info
|
31
|
+
begin
|
9
32
|
#try all the georss dialects, according to uri
|
10
33
|
#first the simple W3C geo
|
11
34
|
if w3cgeo = find_node("*[namespace-uri()='http://www.w3.org/2003/01/geo/wgs84_pos#']")
|
12
35
|
#get the lat and long
|
13
36
|
#either inside a Point element or by themselves
|
14
37
|
if w3cgeo.local_name == "Point"
|
15
|
-
|
16
|
-
lon = w3cgeo.elements["*[local-name()='long']"]
|
38
|
+
to_parse = w3cgeo.to_s
|
17
39
|
else
|
18
40
|
if w3cgeo.local_name == "lat"
|
19
|
-
|
20
|
-
|
41
|
+
lon = find_node("*[namespace-uri()='http://www.w3.org/2003/01/geo/wgs84_pos#' and local-name()='long']")
|
42
|
+
to_parse = w3cgeo.to_s + lon.to_s if lon
|
21
43
|
elsif w3cgeo.local_name == "long"
|
22
|
-
|
23
|
-
|
44
|
+
lat = find_node("*[namespace-uri()='http://www.w3.org/2003/01/geo/wgs84_pos#' and local-name()='lat']")
|
45
|
+
to_parse = w3cgeo.to_s + lat.to_s if lat
|
24
46
|
end
|
25
47
|
end
|
26
|
-
|
27
|
-
@location = GeoRss4rb::Point.new(lat,lon)
|
28
|
-
end
|
48
|
+
@location, @georss_tags = GeoRuby::SimpleFeatures::Geometry.from_georss_with_tags(to_parse) if to_parse
|
29
49
|
elsif gml = find_node("*[namespace-uri()='http://www.georss.org/georss' and local-name()='where']")
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
@location = GeoRss4rb::Point.from_gml(gml)
|
34
|
-
when "LineString"
|
35
|
-
@location = GeoRss4rb::Line.from_gml(gml)
|
36
|
-
when "Polygon"
|
37
|
-
@location = GeoRss4rb::Polygon.from_gml(gml)
|
38
|
-
when "Envelope"
|
39
|
-
@location = GeoRss4rb::Box.from_gml(gml)
|
40
|
-
end
|
41
|
-
elsif simple = find_node("*[namespace-uri()='http://www.georss.org/georss']")
|
42
|
-
#not a gml: simple
|
43
|
-
content = simple.text
|
44
|
-
options = {:feature_type_tag => simple.attributes["featuretypetag"], :relationship_tag => simple.attributes["relationshiptag"], :elevation => GeoRss4rb.try_f(simple.attributes["elev"]), :floor => GeoRss4rb.try_i(simple.attributes["floor"]), :radius => GeoRss4rb.try_f(simple.attributes["radius"])}
|
45
|
-
case simple.local_name
|
46
|
-
when 'point'
|
47
|
-
@location = GeoRss4rb::Point.from_text(content,options)
|
48
|
-
when 'line'
|
49
|
-
@location = GeoRss4rb::Line.from_text(content,options)
|
50
|
-
when 'polygon'
|
51
|
-
@location = GeoRss4rb::Polygon.from_text(content,options)
|
52
|
-
when 'box'
|
53
|
-
@location = GeoRss4rb::Box.from_text(content,options)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
return @location
|
58
|
-
end
|
59
|
-
|
60
|
-
#True if the feed or the item has some location info
|
61
|
-
def located?
|
62
|
-
location != nil #retrieve location info if not already there
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
class Geometry
|
67
|
-
attr_accessor :feature_type_tag, :relationship_tag, :elevation, :floor, :radius
|
68
|
-
def initialize(options = {})
|
69
|
-
@feature_type_tag = options[:feature_type_tag] || "location"
|
70
|
-
@relationship_tag = options[:relationship_tag] || "is-located_at"
|
71
|
-
@elevation = options[:elevation]
|
72
|
-
@floor = options[:floor]
|
73
|
-
@radius = options[:radius]
|
74
|
-
end
|
75
|
-
def to_s
|
76
|
-
inspect
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
class Point < GeoRss4rb::Geometry
|
81
|
-
attr_accessor :lat,:lon
|
82
|
-
def initialize(lat,lon,options = {})
|
83
|
-
super(options)
|
84
|
-
@lat = lat
|
85
|
-
@lon = lon
|
86
|
-
end
|
87
|
-
|
88
|
-
def ==(point)
|
89
|
-
if point.class != GeoRss4rb::Point
|
90
|
-
false
|
91
|
-
else
|
92
|
-
@lat = point.lat and @lon = point.lon
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
def self.from_array(array,options = {})
|
97
|
-
GeoRss4rb::Point.new(array[0],array[1],options)
|
98
|
-
end
|
99
|
-
|
100
|
-
def self.from_text(text,options = {})
|
101
|
-
array = GeoRss4rb::parse_text(text)
|
102
|
-
return nil if array.length != 2
|
103
|
-
self.from_array(array,options)
|
104
|
-
end
|
105
|
-
|
106
|
-
def self.from_gml(gml)
|
107
|
-
pos = gml.elements[".//*[local-name()='pos']"]
|
108
|
-
if pos.nil?
|
109
|
-
nil
|
110
|
-
else
|
111
|
-
self.from_text(pos.text)
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
class Line < GeoRss4rb::Geometry
|
117
|
-
attr_reader :points
|
118
|
-
def initialize(points,options = {})
|
119
|
-
super(options)
|
120
|
-
@points = points
|
121
|
-
end
|
122
|
-
|
123
|
-
#Makes the line behave as an array of points
|
124
|
-
def method_missing(method_name,*args,&b)
|
125
|
-
@points.send(method_name,*args,&b)
|
126
|
-
end
|
127
|
-
|
128
|
-
def ==(line)
|
129
|
-
if line.class != GeoRss4rb::Line or
|
130
|
-
line.length != self.length
|
131
|
-
false
|
132
|
-
else
|
133
|
-
index = 0
|
134
|
-
while index < length
|
135
|
-
return false if self[index] != line[index]
|
136
|
-
index+=1
|
137
|
-
end
|
138
|
-
true
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
def self.from_array(array,options = {})
|
143
|
-
points = []
|
144
|
-
0.upto(array.length / 2 - 1) do |pos|
|
145
|
-
points << GeoRss4rb::Point.from_array(array[pos * 2,2])
|
146
|
-
end
|
147
|
-
GeoRss4rb::Line.new(points,options)
|
148
|
-
end
|
149
|
-
|
150
|
-
def self.from_text(text,options = {})
|
151
|
-
array = GeoRss4rb::parse_text(text)
|
152
|
-
return nil if array.length % 2 != 0 or array.length < 4 # 2 points minimum
|
153
|
-
self.from_array(array,options)
|
154
|
-
end
|
155
|
-
|
156
|
-
def self.from_gml(gml)
|
157
|
-
pos_list = gml.elements[".//*[local-name()='posList']"]
|
158
|
-
if pos_list.nil?
|
159
|
-
nil
|
160
|
-
else
|
161
|
-
self.from_text(pos_list.text)
|
162
|
-
end
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
class Polygon < GeoRss4rb::Geometry
|
167
|
-
attr_reader :rings
|
168
|
-
def initialize(rings,options)
|
169
|
-
super(options)
|
170
|
-
@rings = rings
|
171
|
-
end
|
172
|
-
|
173
|
-
#Makes the Polygon behave as an array of rings/lines
|
174
|
-
def method_missing(method_name,*args,&b)
|
175
|
-
@rings.send(method_name,*args,&b)
|
176
|
-
end
|
177
|
-
|
178
|
-
def ==(polygon)
|
179
|
-
if polygon.class != GeoRss4rb::Polygon or
|
180
|
-
polygon.length != self.length
|
181
|
-
false
|
182
|
-
else
|
183
|
-
index = 0
|
184
|
-
while index < length
|
185
|
-
return false if self[index] != polygon[index]
|
186
|
-
index+=1
|
50
|
+
@location, @georss_tags = GeoRuby::SimpleFeatures::Geometry.from_georss_with_tags(gml.to_s)
|
51
|
+
elsif simple = find_node("*[namespace-uri()='http://www.georss.org/georss']") #not a gml : it is a simple
|
52
|
+
@location, @georss_tags = GeoRuby::SimpleFeatures::Geometry.from_georss_with_tags(simple.to_s)
|
187
53
|
end
|
188
|
-
true
|
189
54
|
end
|
190
|
-
|
191
|
-
|
192
|
-
def self.from_text(text,options = {})
|
193
|
-
array = GeoRss4rb::parse_text(text)
|
194
|
-
return nil if array.length % 2 != 0 or array.length < 8 # 4 points minimum
|
195
|
-
rings = [GeoRss4rb::Line.from_array(array)] #exterior only
|
196
|
-
GeoRss4rb::Polygon.new(rings,options)
|
197
|
-
end
|
198
|
-
def self.from_gml(gml)
|
199
|
-
pos_list = gml.elements[".//*[local-name()='posList']"]
|
200
|
-
if pos_list.nil?
|
201
|
-
nil
|
202
|
-
else
|
203
|
-
self.from_text(pos_list.text)
|
204
|
-
end
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
class Box < GeoRss4rb::Geometry
|
209
|
-
attr_reader :lower_corner,:upper_corner
|
210
|
-
def initialize(lower_corner,upper_corner,options = {})
|
211
|
-
super(options)
|
212
|
-
@lower_corner = lower_corner
|
213
|
-
@upper_corner = upper_corner
|
214
|
-
end
|
215
|
-
def ==(box)
|
216
|
-
if box.class != GeoRss4rb::Box
|
217
|
-
false
|
218
|
-
else
|
219
|
-
@lower_corner == box.lower_corner and @upper_corner == box.upper_corner
|
220
|
-
end
|
221
|
-
end
|
222
|
-
def self.from_text(text,options = {})
|
223
|
-
array = GeoRss4rb::parse_text(text)
|
224
|
-
return nil if array.length != 4
|
225
|
-
lower_corner = GeoRss4rb::Point.from_array(array[0,2])
|
226
|
-
upper_corner = GeoRss4rb::Point.from_array(array[2,2])
|
227
|
-
GeoRss4rb::Box.new(lower_corner,upper_corner,options)
|
228
|
-
end
|
229
|
-
def self.from_gml(gml)
|
230
|
-
lower_corner = gml.elements[".//*[local-name()='lowerCorner']"]
|
231
|
-
upper_corner = gml.elements[".//*[local-name()='upperCorner']"]
|
232
|
-
if lower_corner and upper_corner
|
233
|
-
self.from_text(lower_corner.text + " " + upper_corner.text) #the lazy solution
|
234
|
-
end
|
235
|
-
end
|
236
|
-
end
|
237
|
-
|
238
|
-
|
239
|
-
def self.parse_text(expr)#:nodoc:
|
240
|
-
expr.scan(/-?(?:\.\d+|\d+(?:\.\d*)?)/).map {|str| str.to_f}
|
241
|
-
end
|
242
|
-
|
243
|
-
|
244
|
-
def self.try_f(expr)#:nodoc:
|
245
|
-
if expr.nil?
|
246
|
-
nil
|
247
|
-
else
|
248
|
-
expr.to_f
|
249
|
-
end
|
250
|
-
end
|
251
|
-
|
252
|
-
|
253
|
-
def self.try_i(expr) #:nodoc:
|
254
|
-
if expr.nil?
|
255
|
-
nil
|
256
|
-
else
|
257
|
-
expr.to_f.to_i #to_f does not throw exceptions...
|
55
|
+
rescue
|
258
56
|
end
|
259
57
|
end
|
260
58
|
end
|
261
59
|
|
262
|
-
FeedTools::FeedItem.class_eval
|
263
|
-
|
264
|
-
end
|
265
|
-
|
266
|
-
FeedTools::Feed.class_eval do
|
267
|
-
include GeoRss4rb::Location
|
268
|
-
end
|
60
|
+
FeedTools::FeedItem.class_eval { include Georss4rb::Location }
|
61
|
+
FeedTools::Feed.class_eval { include Georss4rb::Location }
|
@@ -0,0 +1,199 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'feed_tools'
|
3
|
+
require 'geo_ruby'
|
4
|
+
|
5
|
+
module GeoRss4rb
|
6
|
+
module Location
|
7
|
+
#Returns a GeoRss4rb::Geometry object if info about the location is found.
|
8
|
+
#Returns +nil+ if no location info is found.
|
9
|
+
def location
|
10
|
+
if @location.nil?
|
11
|
+
#try all the georss dialects, according to uri
|
12
|
+
#first the simple W3C geo
|
13
|
+
if w3cgeo = find_node("*[namespace-uri()='http://www.w3.org/2003/01/geo/wgs84_pos#']")
|
14
|
+
#get the lat and long
|
15
|
+
#either inside a Point element or by themselves
|
16
|
+
if w3cgeo.local_name == "Point"
|
17
|
+
lat = w3cgeo.elements["*[local-name()='lat']"]
|
18
|
+
lon = w3cgeo.elements["*[local-name()='long']"]
|
19
|
+
else
|
20
|
+
if w3cgeo.local_name == "lat"
|
21
|
+
lat = w3cgeo.text.to_f
|
22
|
+
lon = find_node("*[namespace-uri()='http://www.w3.org/2003/01/geo/wgs84_pos#' and local-name()='long']").text.to_f
|
23
|
+
elsif w3cgeo.local_name == "long"
|
24
|
+
lon = w3cgeo.text.to_f
|
25
|
+
lat = find_node("*[namespace-uri()='http://www.w3.org/2003/01/geo/wgs84_pos#' and local-name()='lat']").text.to_f
|
26
|
+
end
|
27
|
+
end
|
28
|
+
if lat and lon
|
29
|
+
@location = GeoRuby::SimpleFeatures::Point.from_lon_lat(lon,lat)
|
30
|
+
end
|
31
|
+
elsif gml = find_node("*[namespace-uri()='http://www.georss.org/georss' and local-name()='where']")
|
32
|
+
#the meat!!
|
33
|
+
case gml.elements["*"].local_name
|
34
|
+
when "Point"
|
35
|
+
@location = GeoRss4rb::PointParser.from_gml(gml)
|
36
|
+
when "LineString"
|
37
|
+
@location = GeoRss4rb::LineParser.from_gml(gml)
|
38
|
+
when "Polygon"
|
39
|
+
@location = GeoRss4rb::PolygonParser.from_gml(gml)
|
40
|
+
when "Envelope"
|
41
|
+
@location = GeoRss4rb::BoxParser.from_gml(gml)
|
42
|
+
end
|
43
|
+
elsif simple = find_node("*[namespace-uri()='http://www.georss.org/georss']")
|
44
|
+
#not a gml: simple
|
45
|
+
content = simple.text
|
46
|
+
options = {:feature_type_tag => simple.attributes["featuretypetag"], :relationship_tag => simple.attributes["relationshiptag"], :elevation => GeoRss4rb.try_f(simple.attributes["elev"]), :floor => GeoRss4rb.try_i(simple.attributes["floor"]), :radius => GeoRss4rb.try_f(simple.attributes["radius"])}
|
47
|
+
case simple.local_name
|
48
|
+
when 'point'
|
49
|
+
@location = GeoRss4rb::PointParser.from_text(content,options)
|
50
|
+
when 'line'
|
51
|
+
@location = GeoRss4rb::LineParser.from_text(content,options)
|
52
|
+
when 'polygon'
|
53
|
+
@location = GeoRss4rb::PolygonParser.from_text(content,options)
|
54
|
+
when 'box'
|
55
|
+
@location = GeoRss4rb::BoxParser.from_text(content,options)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
return @location
|
60
|
+
end
|
61
|
+
|
62
|
+
#True if the feed or the item has some location info
|
63
|
+
def located?
|
64
|
+
location != nil #retrieve location info if not already there
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
module GeoRssAttributes
|
69
|
+
attr_accessor :feature_type_tag, :relationship_tag, :elevation, :floor, :radius
|
70
|
+
def add_georss_attributes(options = {})
|
71
|
+
@feature_type_tag = options[:feature_type_tag] || "location"
|
72
|
+
@relationship_tag = options[:relationship_tag] || "is-located_at"
|
73
|
+
@elevation = options[:elevation]
|
74
|
+
@floor = options[:floor]
|
75
|
+
@radius = options[:radius]
|
76
|
+
self
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
module PointParser #:nodoc:
|
81
|
+
def self.from_array(array,options = {})
|
82
|
+
GeoRuby::SimpleFeatures::Point.from_lon_lat(array[1],array[0]).add_georss_attributes(options)
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.from_text(text,options = {})
|
86
|
+
array = GeoRss4rb::parse_text(text)
|
87
|
+
return nil if array.length != 2
|
88
|
+
self.from_array(array,options)
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.from_gml(gml)
|
92
|
+
pos = gml.elements[".//*[local-name()='pos']"]
|
93
|
+
if pos.nil?
|
94
|
+
nil
|
95
|
+
else
|
96
|
+
self.from_text(pos.text)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
module LineParser #:nodoc:
|
102
|
+
def self.from_array(array,options = {})
|
103
|
+
points = []
|
104
|
+
0.upto(array.length / 2 - 1) do |pos|
|
105
|
+
points << GeoRss4b:PointParser.from_array(array[pos * 2,2])
|
106
|
+
end
|
107
|
+
GeoRuby::SimpleFeatures::LineString.from_points(points).add_georss_attributes(options)
|
108
|
+
end
|
109
|
+
|
110
|
+
def self.from_text(text,options = {})
|
111
|
+
array = GeoRss4rb::parse_text(text)
|
112
|
+
return nil if array.length % 2 != 0 or array.length < 4 # 2 points minimum
|
113
|
+
self.from_array(array,options)
|
114
|
+
end
|
115
|
+
|
116
|
+
def self.from_gml(gml)
|
117
|
+
pos_list = gml.elements[".//*[local-name()='posList']"]
|
118
|
+
if pos_list.nil?
|
119
|
+
nil
|
120
|
+
else
|
121
|
+
self.from_text(pos_list.text)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
module PolygonParser #:nodoc:
|
127
|
+
#bof...
|
128
|
+
def self.linear_ring_from_array(array)
|
129
|
+
points = []
|
130
|
+
0.upto(array.length / 2 - 1) do |pos|
|
131
|
+
points << GeoRss4b:PointParser.from_array(array[pos * 2,2])
|
132
|
+
end
|
133
|
+
GeoRuby::SimpleFeatures::LinearRing.from_points(points)
|
134
|
+
end
|
135
|
+
|
136
|
+
def self.from_text(text,options = {})
|
137
|
+
array = GeoRss4rb::parse_text(text)
|
138
|
+
return nil if array.length % 2 != 0 or array.length < 8 # 4 points minimum
|
139
|
+
rings = [self.linear_ring_from_array(array)] #exterior only
|
140
|
+
GeoRruby::SimpleFeatures::Polygon.from_linear_rings(rings).add_georss_attributes(options)
|
141
|
+
end
|
142
|
+
|
143
|
+
def self.from_gml(gml)
|
144
|
+
pos_list = gml.elements[".//*[local-name()='posList']"]
|
145
|
+
if pos_list.nil?
|
146
|
+
nil
|
147
|
+
else
|
148
|
+
self.from_text(pos_list.text)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
module BoxParser #:nodoc:
|
154
|
+
def self.from_text(text,options = {})
|
155
|
+
array = GeoRss4rb::parse_text(text)
|
156
|
+
return nil if array.length != 4
|
157
|
+
lower_corner = GeoRss4rb::PointParser.from_array(array[0,2])
|
158
|
+
upper_corner = GeoRss4rb::PointParser.from_array(array[2,2])
|
159
|
+
GeoRuby::SimpleFeatures::Envelope.from_points([lower_corner,upper_corner]).add_georss_attributes(options)
|
160
|
+
end
|
161
|
+
def self.from_gml(gml)
|
162
|
+
lower_corner = gml.elements[".//*[local-name()='lowerCorner']"]
|
163
|
+
upper_corner = gml.elements[".//*[local-name()='upperCorner']"]
|
164
|
+
if lower_corner and upper_corner
|
165
|
+
self.from_text(lower_corner.text + " " + upper_corner.text) #the lazy solution
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
|
171
|
+
def self.parse_text(expr)#:nodoc:
|
172
|
+
expr.scan(/-?(?:\.\d+|\d+(?:\.\d*)?)/).map {|str| str.to_f}
|
173
|
+
end
|
174
|
+
|
175
|
+
|
176
|
+
def self.try_f(expr)#:nodoc:
|
177
|
+
if expr.nil?
|
178
|
+
nil
|
179
|
+
else
|
180
|
+
expr.to_f
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
|
185
|
+
def self.try_i(expr) #:nodoc:
|
186
|
+
if expr.nil?
|
187
|
+
nil
|
188
|
+
else
|
189
|
+
expr.to_f.to_i #to_f does not throw exceptions...
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
FeedTools::FeedItem.include(GeoRss4rb::Location)
|
195
|
+
FeedTools::Feed.include(GeoRss4rb::Location)
|
196
|
+
GeoRuby::SimpleFeatures::Geometry.include(GeoRss4rb::GeoRssAttributes)
|
197
|
+
GeoRuby::SimpleFeatures::Envelope.include(GeoRss4rb::GeoRssAttributes)
|
198
|
+
|
199
|
+
|
data/rakefile.rb
CHANGED
@@ -24,9 +24,10 @@ spec = Gem::Specification::new do |s|
|
|
24
24
|
s.platform = Gem::Platform::RUBY
|
25
25
|
|
26
26
|
s.add_dependency('feedtools', '>= 0.2.26') #others should be ok though...
|
27
|
-
|
27
|
+
s.add_dependency('GeoRuby', '>= 1.0.0')
|
28
|
+
|
28
29
|
s.name = 'georss4rb'
|
29
|
-
s.version = "0.
|
30
|
+
s.version = "0.1.1"
|
30
31
|
s.summary = "Adds support for all GeoRSS dialects in FeedTools"
|
31
32
|
s.description = "Adds support for all GeoRSS dialects in FeedTools"
|
32
33
|
|
data/test/test_georss.rb
CHANGED
@@ -1,66 +1,40 @@
|
|
1
1
|
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
+
$:.unshift(File.dirname(__FILE__) + '/../../GeoRuby/lib')
|
2
3
|
require 'georss4rb'
|
3
4
|
require 'test/unit'
|
4
5
|
|
5
|
-
include GeoRss4rb
|
6
|
-
|
7
6
|
class TestGeorss < Test::Unit::TestCase
|
8
|
-
|
9
|
-
arr = GeoRss4rb::parse_text("456.3")
|
10
|
-
assert_equal(1,arr.length)
|
11
|
-
assert_equal(456.3,arr[0])
|
12
|
-
|
13
|
-
arr = GeoRss4rb::parse_text("456")
|
14
|
-
assert_equal(1,arr.length)
|
15
|
-
assert_equal(456,arr[0])
|
16
|
-
|
17
|
-
arr = GeoRss4rb::parse_text(".456")
|
18
|
-
assert_equal(1,arr.length)
|
19
|
-
assert_equal(0.456,arr[0])
|
20
|
-
|
21
|
-
arr = GeoRss4rb::parse_text("456;123; .444 23.44\t56")
|
22
|
-
assert_equal(5,arr.length)
|
23
|
-
assert_equal([456,123,0.444,23.44,56],arr)
|
24
|
-
|
25
|
-
arr = GeoRss4rb::parse_text("456.567 123 .444 23.44 56")
|
26
|
-
assert_equal(5,arr.length)
|
27
|
-
assert_equal([456.567,123,0.444,23.44,56],arr)
|
28
|
-
|
29
|
-
arr = GeoRss4rb::parse_text("456.567,123 23.44,56")
|
30
|
-
assert_equal(4,arr.length)
|
31
|
-
assert_equal([456.567,123,23.44,56],arr)
|
32
|
-
end
|
33
|
-
|
7
|
+
|
34
8
|
def test_w3cgeo
|
35
9
|
#atom
|
36
10
|
w3cgeo = FeedTools::Feed.open("file://#{File.dirname(__FILE__)}/atom_w3cgeo.xml")
|
37
11
|
assert(w3cgeo.respond_to?(:location))
|
38
12
|
assert(!w3cgeo.location.nil?)
|
39
13
|
assert(w3cgeo.located?)
|
40
|
-
assert(
|
41
|
-
assert_equal(
|
14
|
+
assert(GeoRuby::SimpleFeatures::Point,w3cgeo.location.class)
|
15
|
+
assert_equal(GeoRuby::SimpleFeatures::Point.from_lon_lat(-97.83,26.58),w3cgeo.location)
|
42
16
|
|
43
17
|
assert(!w3cgeo.items.nil?)
|
44
18
|
assert_equal(2,w3cgeo.items.length)
|
45
19
|
|
46
20
|
assert(!w3cgeo.items.first.location.nil?)
|
47
|
-
assert_equal(
|
21
|
+
assert_equal(GeoRuby::SimpleFeatures::Point.from_lon_lat(0.83666,28),w3cgeo.items.first.location)
|
48
22
|
|
49
23
|
assert(!w3cgeo.items[1].location.nil?)
|
50
|
-
assert_equal(
|
24
|
+
assert_equal(GeoRuby::SimpleFeatures::Point.from_lon_lat(12.552,55.701),w3cgeo.items[1].location)
|
51
25
|
|
52
26
|
#rss
|
53
27
|
w3cgeo = FeedTools::Feed.open("file://#{File.dirname(__FILE__)}/rss_w3cgeo.xml")
|
54
28
|
assert(w3cgeo.respond_to?(:location))
|
55
29
|
assert(!w3cgeo.location.nil?)
|
56
|
-
|
57
|
-
assert_equal(
|
30
|
+
assert_equal(GeoRuby::SimpleFeatures::Point,w3cgeo.location.class)
|
31
|
+
assert_equal(GeoRuby::SimpleFeatures::Point.from_lon_lat(-97.83,26.58),w3cgeo.location)
|
58
32
|
|
59
33
|
assert(!w3cgeo.items.nil?)
|
60
34
|
assert_equal(1,w3cgeo.items.length)
|
61
35
|
|
62
36
|
assert(!w3cgeo.items[0].location.nil?)
|
63
|
-
assert_equal(
|
37
|
+
assert_equal(GeoRuby::SimpleFeatures::Point.from_lon_lat(12.552,55.701),w3cgeo.items[0].location)
|
64
38
|
end
|
65
39
|
|
66
40
|
def test_simple
|
@@ -70,33 +44,33 @@ class TestGeorss < Test::Unit::TestCase
|
|
70
44
|
assert(!simple.location.nil?)
|
71
45
|
|
72
46
|
#Test point
|
73
|
-
|
74
|
-
assert_equal(
|
75
|
-
assert_equal("bidon",simple.
|
76
|
-
assert_equal("is-centered-at",simple.
|
77
|
-
assert_equal(123.3,simple.
|
78
|
-
assert_equal(101,simple.
|
79
|
-
assert_equal(456.1,simple.
|
47
|
+
assert_equal(GeoRuby::SimpleFeatures::Point,simple.location.class)
|
48
|
+
assert_equal(GeoRuby::SimpleFeatures::Point.from_lon_lat(56.4123,4.5),simple.location)
|
49
|
+
assert_equal("bidon",simple.georss_tags.featuretypetag)
|
50
|
+
assert_equal("is-centered-at",simple.georss_tags.relationshiptag)
|
51
|
+
assert_equal(123.3,simple.georss_tags.elev)
|
52
|
+
assert_equal(101,simple.georss_tags.floor)
|
53
|
+
assert_equal(456.1,simple.georss_tags.radius)
|
80
54
|
assert(!simple.items.nil?)
|
81
55
|
assert_equal(3,simple.items.length)
|
82
56
|
|
83
57
|
#Test line
|
84
58
|
first = simple.items.first
|
85
59
|
assert(!first.location.nil?)
|
86
|
-
|
87
|
-
assert_equal(
|
60
|
+
assert_equal(GeoRuby::SimpleFeatures::LineString,first.location.class)
|
61
|
+
assert_equal(GeoRuby::SimpleFeatures::LineString.from_coordinates([[4123.4,123.4],[ 123.5, 312.3],[45.1,-65.1]]),first.location)
|
88
62
|
|
89
63
|
#Test polygon
|
90
64
|
second = simple.items[1]
|
91
65
|
assert(!second.location.nil?)
|
92
|
-
|
93
|
-
assert_equal(
|
66
|
+
assert_equal(GeoRuby::SimpleFeatures::Polygon,second.location.class)
|
67
|
+
assert_equal(GeoRuby::SimpleFeatures::Polygon.from_coordinates([[[-110.45, 45.256 ],[ -109.48, 46.46 ],[ -109.86, 43.84],[ -110.45, 45.256]]]),second.location)
|
94
68
|
|
95
69
|
#Test box
|
96
70
|
third = simple.items[2]
|
97
71
|
assert(!third.location.nil?)
|
98
|
-
|
99
|
-
assert_equal(
|
72
|
+
assert_equal(GeoRuby::SimpleFeatures::Envelope,third.location.class)
|
73
|
+
assert_equal(GeoRuby::SimpleFeatures::Envelope.from_coordinates([[-110.45, 45.256 ],[ -109.48, 46.46 ]]),third.location)
|
100
74
|
|
101
75
|
#rss
|
102
76
|
simple = FeedTools::Feed.open("file://#{File.dirname(__FILE__)}/rss_simple.xml")
|
@@ -104,28 +78,28 @@ class TestGeorss < Test::Unit::TestCase
|
|
104
78
|
assert(!simple.location.nil?)
|
105
79
|
|
106
80
|
#Test box
|
107
|
-
|
108
|
-
assert_equal(
|
81
|
+
assert_equal(GeoRuby::SimpleFeatures::Envelope,simple.location.class)
|
82
|
+
assert_equal(GeoRuby::SimpleFeatures::Envelope.from_coordinates([[-18.2373046875, 45.36758436884978 ],[ 8.1298828125, 60.951776809566965]]),simple.location)
|
109
83
|
assert(!simple.items.nil?)
|
110
84
|
assert_equal(3,simple.items.length)
|
111
85
|
|
112
86
|
#Test point
|
113
87
|
first = simple.items.first
|
114
88
|
assert(!first.location.nil?)
|
115
|
-
|
116
|
-
assert_equal(
|
89
|
+
assert_equal(GeoRuby::SimpleFeatures::Point,first.location.class)
|
90
|
+
assert_equal(GeoRuby::SimpleFeatures::Point.from_lon_lat(-2.04454,52.8872),first.location)
|
117
91
|
|
118
92
|
#Test line
|
119
93
|
second = simple.items[1]
|
120
94
|
assert(!second.location.nil?)
|
121
|
-
|
122
|
-
assert_equal(
|
95
|
+
assert_equal(GeoRuby::SimpleFeatures::LineString,second.location.class)
|
96
|
+
assert_equal(GeoRuby::SimpleFeatures::LineString.from_coordinates([[-0.536977, 52.7533],[-0.3336977, 51.73]]),second.location)
|
123
97
|
|
124
98
|
#Test polygon
|
125
99
|
third = simple.items[2]
|
126
100
|
assert(!third.location.nil?)
|
127
|
-
|
128
|
-
assert_equal(
|
101
|
+
assert_equal(GeoRuby::SimpleFeatures::Polygon,third.location.class)
|
102
|
+
assert_equal(GeoRuby::SimpleFeatures::Polygon.from_coordinates([[[-3.9165, 55.9756],[-0.536977, 52.7533],[-0.3336977, 51.73],[ -0.536977, 52.7533],[-0.3336977 , 51.73]]]),third.location) #the polygon does not make any sens geometrically speaking, but it is not checked by the library
|
129
103
|
end
|
130
104
|
|
131
105
|
def test_gml
|
@@ -135,28 +109,28 @@ class TestGeorss < Test::Unit::TestCase
|
|
135
109
|
assert(!gml.location.nil?)
|
136
110
|
|
137
111
|
#Test point
|
138
|
-
|
139
|
-
assert_equal(
|
112
|
+
assert_equal(GeoRuby::SimpleFeatures::Point,gml.location.class)
|
113
|
+
assert_equal(GeoRuby::SimpleFeatures::Point.from_lon_lat(-110.45,45.256),gml.location)
|
140
114
|
assert(!gml.items.nil?)
|
141
115
|
assert_equal(3,gml.items.length)
|
142
116
|
|
143
117
|
#Test line
|
144
118
|
first = gml.items.first
|
145
119
|
assert(!first.location.nil?)
|
146
|
-
|
147
|
-
assert_equal(
|
120
|
+
assert_equal(GeoRuby::SimpleFeatures::LineString,first.location.class)
|
121
|
+
assert_equal(GeoRuby::SimpleFeatures::LineString.from_coordinates([[-110.45, 45.256],[ -109.48 , 46.46],[-109.86 , 43.84],[ -109.2, 45.8]]),first.location)
|
148
122
|
|
149
123
|
#Test polygon
|
150
124
|
second = gml.items[1]
|
151
125
|
assert(!second.location.nil?)
|
152
|
-
|
153
|
-
assert_equal(
|
126
|
+
assert_equal(GeoRuby::SimpleFeatures::Polygon,second.location.class)
|
127
|
+
assert_equal(GeoRuby::SimpleFeatures::Polygon.from_coordinates([[[-110.45, 45.256 ],[-109.48 , 46.46],[-109.86, 43.84 ],[ -110.45, 45.256 ]]]),second.location)
|
154
128
|
|
155
129
|
#Test box
|
156
130
|
third = gml.items[2]
|
157
131
|
assert(!third.location.nil?)
|
158
|
-
|
159
|
-
assert_equal(
|
132
|
+
assert_equal(GeoRuby::SimpleFeatures::Envelope,third.location.class)
|
133
|
+
assert_equal(GeoRuby::SimpleFeatures::Envelope.from_coordinates([[-71.032, 42.943 ],[-69.856, 43.039]]),third.location)
|
160
134
|
|
161
135
|
#rss
|
162
136
|
gml = FeedTools::Feed.open("file://#{File.dirname(__FILE__)}/rss_gml.xml")
|
@@ -164,27 +138,27 @@ class TestGeorss < Test::Unit::TestCase
|
|
164
138
|
assert(!gml.location.nil?)
|
165
139
|
|
166
140
|
#Test point
|
167
|
-
|
168
|
-
assert_equal(
|
141
|
+
assert_equal(GeoRuby::SimpleFeatures::Point,gml.location.class)
|
142
|
+
assert_equal(GeoRuby::SimpleFeatures::Point.from_lon_lat(-110.45,45.256),gml.location)
|
169
143
|
assert(!gml.items.nil?)
|
170
144
|
assert_equal(3,gml.items.length)
|
171
145
|
|
172
146
|
#Test line
|
173
147
|
first = gml.items.first
|
174
148
|
assert(!first.location.nil?)
|
175
|
-
assert(
|
176
|
-
assert_equal(
|
149
|
+
assert(GeoRuby::SimpleFeatures::LineString,first.location.class)
|
150
|
+
assert_equal(GeoRuby::SimpleFeatures::LineString.from_coordinates([[-110.45, 45.256],[-109.48, 46.46],[-109.86, 43.84],[-109.2 , 45.8]]),first.location)
|
177
151
|
|
178
152
|
#Test polygon
|
179
153
|
second = gml.items[1]
|
180
154
|
assert(!second.location.nil?)
|
181
|
-
|
182
|
-
assert_equal(
|
155
|
+
assert_equal(GeoRuby::SimpleFeatures::Polygon,second.location.class)
|
156
|
+
assert_equal(GeoRuby::SimpleFeatures::Polygon.from_coordinates([[[-110.45, 45.256],[-109.48 , 46.46],[-109.86, 43.84],[-110.45, 45.256 ]]]),second.location)
|
183
157
|
|
184
158
|
#Test box
|
185
159
|
third = gml.items[2]
|
186
160
|
assert(!third.location.nil?)
|
187
|
-
|
188
|
-
assert_equal(
|
161
|
+
assert_equal(GeoRuby::SimpleFeatures::Envelope,third.location.class)
|
162
|
+
assert_equal(GeoRuby::SimpleFeatures::Envelope.from_coordinates([[-71.032, 42.943 ],[-69.856, 43.039 ]]),third.location)
|
189
163
|
end
|
190
164
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: georss4rb
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date:
|
6
|
+
version: 0.1.1
|
7
|
+
date: 2007-04-01 00:00:00 +02:00
|
8
8
|
summary: Adds support for all GeoRSS dialects in FeedTools
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -30,6 +30,7 @@ authors:
|
|
30
30
|
- Guilhem Vellut
|
31
31
|
files:
|
32
32
|
- lib/georss4rb.rb
|
33
|
+
- lib/save_georss4rb.rb
|
33
34
|
- test/test_georss.rb
|
34
35
|
- README
|
35
36
|
- MIT-LICENSE
|
@@ -57,3 +58,12 @@ dependencies:
|
|
57
58
|
- !ruby/object:Gem::Version
|
58
59
|
version: 0.2.26
|
59
60
|
version:
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: GeoRuby
|
63
|
+
version_requirement:
|
64
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.0.0
|
69
|
+
version:
|