diff --git a/.gitignore b/.gitignore index 817eee7..101712f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ doc -test/*.kml +.DS_Store diff --git a/lib/.DS_Store b/lib/.DS_Store new file mode 100644 index 0000000..9841041 Binary files /dev/null and b/lib/.DS_Store differ diff --git a/lib/kml/line_string.rb b/lib/kml/line_string.rb index 942d26e..e6f867c 100644 --- a/lib/kml/line_string.rb +++ b/lib/kml/line_string.rb @@ -16,7 +16,7 @@ def coordinates def coordinates=(c) case c when String - @coordinates = c.split(/\s+/).collect { |coord| coord.split(',') } + @coordinates = c.strip.split(/\s+/).collect { |coord| coord.split(',') } when Array c.each do |coord_array| unless coord_array.is_a?(Array) @@ -37,4 +37,4 @@ def render(xm=Builder::XmlMarkup.new(:indent => 2)) } end end -end \ No newline at end of file +end diff --git a/lib/kml/linear_ring.rb b/lib/kml/linear_ring.rb index 2cf441c..885d673 100644 --- a/lib/kml/linear_ring.rb +++ b/lib/kml/linear_ring.rb @@ -25,7 +25,7 @@ def coordinates def coordinates=(c) case c when String - @coordinates = c.split(/\s+/).collect { |coord| coord.split(',') } + @coordinates = c.strip.split(/\s+/).collect { |coord| coord.split(',') } when Array c.each do |coord_array| unless coord_array.is_a?(Array) @@ -46,4 +46,4 @@ def render(xm=Builder::XmlMarkup.new(:indent => 2)) } end end -end \ No newline at end of file +end diff --git a/lib/kml/point.rb b/lib/kml/point.rb index 9fec034..112b7d1 100644 --- a/lib/kml/point.rb +++ b/lib/kml/point.rb @@ -23,7 +23,7 @@ def coordinates def coordinates=(c) case c when String - @coordinates = c.split(',') + @coordinates = c.strip.split(',') unless @coordinates.length == 2 || @coordinates.length == 3 raise "Coordinates string may only have 2 parts (indicating lat and long) or 3 parts (lat, long and altitude)" end diff --git a/lib/kml/polygon.rb b/lib/kml/polygon.rb index e5f4fc3..1283fe9 100644 --- a/lib/kml/polygon.rb +++ b/lib/kml/polygon.rb @@ -16,6 +16,9 @@ # ... # # +# +# ... +# # module KML #:nodoc: @@ -24,21 +27,33 @@ module KML #:nodoc: # which gives the appearance of a building. When a Polygon is extruded, each point is extruded individually. # Extruded Polygons use PolyStyle for their color, color mode, and fill. class Polygon < Geometry - attr_accessor :inner_boundary_is attr_accessor :outer_boundary_is - + + def inner_boundary_is + @inner_boundary_is ||= [] + end + + # allow old semantics for adding inner boundaries + def inner_boundary_is=(ib) + if ib.kind_of?(Array) + @inner_boundary_is = ib + else + self.inner_boundary_is << ib + end + end + def render(xm=Builder::XmlMarkup.new(:indent => 2)) xm.Polygon { super xm.outerBoundaryIs { outer_boundary_is.render(xm) } - unless inner_boundary_is.nil? + inner_boundary_is.each do |ib| xm.innerBoundaryIs { - inner_boundary_is.render(xm) + ib.render(xm) } end } end end -end \ No newline at end of file +end diff --git a/lib/kml_file.rb b/lib/kml_file.rb index 0e2796f..a029b45 100644 --- a/lib/kml_file.rb +++ b/lib/kml_file.rb @@ -15,6 +15,7 @@ class KMLFile def objects @objects ||= [] end + alias :features :objects # Render the KML file def render(xm=Builder::XmlMarkup.new(:indent => 2)) diff --git a/ruby_kml.gemspec b/ruby_kml.gemspec index e1ab6ba..bb65e43 100644 --- a/ruby_kml.gemspec +++ b/ruby_kml.gemspec @@ -9,4 +9,5 @@ Gem::Specification.new do |s| s.has_rdoc = false s.authors = ["aeden, schleyfox, xaviershay, andykram, IanVaughan"] s.files = Dir['**/**'] + s.add_dependency('builder') end diff --git a/test/cdata_and_snippet.kml b/test/cdata_and_snippet.kml new file mode 100644 index 0000000..5ed7c1b --- /dev/null +++ b/test/cdata_and_snippet.kml @@ -0,0 +1,21 @@ + + + + Document with CDATA example + Document level snippet2 + + CDATA example + + CDATA Tags are useful! +

Text is more readable and +easier to write when you can avoid using entity +references.

+]]> +
+ Example of a snippet2 + + -122.0822035425683,37.4228,0 + +
+
+
diff --git a/test/ground_overlays.kml b/test/ground_overlays.kml new file mode 100644 index 0000000..68ceca8 --- /dev/null +++ b/test/ground_overlays.kml @@ -0,0 +1,25 @@ + + + + Ground Overlays + + + + + Large-scale overlay on terrain + + + + + http://code.google.com/apis/kml/documentation/etna.jpg + + + 37.91904192681665 + 37.46543388598137 + 15.35832653742206 + 14.60128369746704 + + + + + diff --git a/test/kml_file_test.rb b/test/kml_file_test.rb index 297c7ad..db55465 100644 --- a/test/kml_file_test.rb +++ b/test/kml_file_test.rb @@ -3,12 +3,13 @@ class KMLFileTest < Test::Unit::TestCase include KML + # This also tests the stripping of coordinates def test_placemark kml = KMLFile.new kml.objects << Placemark.new( :name => 'Simple placemark', :description => 'Attached to the ground. Intelligently places itself at the height of the underlying terrain.', - :geometry => Point.new(:coordinates=>'-122.0822035425683,37.42228990140251,0') + :geometry => Point.new(:coordinates=>' -122.0822035425683,37.42228990140251,0 ') ) assert_equal File.read('test/simple_placemark.kml'), kml.render end @@ -127,6 +128,44 @@ def test_polygon assert_equal File.read('test/polygon.kml'), kml.render end + def test_polygon_with_multiple_inner_boundaries + kml = KMLFile.new + kml.objects << Placemark.new( + :name => 'The Pentagon', + :geometry => Polygon.new( + :extrude => true, + :altitude_mode => 'relativeToGround', + :outer_boundary_is => LinearRing.new( + :coordinates => '-77.05788457660967,38.87253259892824,100 + -77.05465973756702,38.87291016281703,100 + -77.05315536854791,38.87053267794386,100 + -77.05552622493516,38.868757801256,100 + -77.05844056290393,38.86996206506943,100 + -77.05788457660967,38.87253259892824,100' + ), + :inner_boundary_is => [ + LinearRing.new( + :coordinates => '-77.05668055019126,38.87154239798456,100 + -77.05542625960818,38.87167890344077,100 + -77.05485125901024,38.87076535397792,100 + -77.05577677433152,38.87008686581446,100 + -77.05691162017543,38.87054446963351,100 + -77.05668055019126,38.87154239798456,100' + ), + LinearRing.new( + :coordinates => '-77.05668055019126,38.87154239798456,100 + -77.05542625960818,38.87167890344077,100 + -77.05485125901024,38.87076535397792,100 + -77.05577677433152,38.87008686581446,100 + -77.05691162017543,38.87054446963351,100 + -77.05668055019126,38.87154239798456,100' + ) + ] + ) + ) + assert_equal File.read('test/polygon_inner.kml'), kml.render + end + def test_geometry_styles kml = KMLFile.new kml.objects << Style.new( diff --git a/test/paths.kml b/test/paths.kml new file mode 100644 index 0000000..0049525 --- /dev/null +++ b/test/paths.kml @@ -0,0 +1,33 @@ + + + + Paths + + + + + + Absolute Extruded + + + + #yellowLineGreenPoly + + 1 + 1 + absolute + -112.2550785337791,36.07954952145647,2357 -112.2549277039738,36.08117083492122,2357 -112.2552505069063,36.08260761307279,2357 -112.2564540158376,36.08395660588506,2357 -112.2580238976449,36.08511401044813,2357 -112.2595218489022,36.08584355239394,2357 -112.2608216347552,36.08612634548589,2357 -112.262073428656,36.08626019085147,2357 -112.2633204928495,36.08621519860091,2357 -112.2644963846444,36.08627897945274,2357 -112.2656969554589,36.08649599090644,2357 + + + + diff --git a/test/polygon.kml b/test/polygon.kml new file mode 100644 index 0000000..09895a0 --- /dev/null +++ b/test/polygon.kml @@ -0,0 +1,20 @@ + + + + The Pentagon + + 1 + relativeToGround + + + -77.05788457660967,38.87253259892824,100 -77.05465973756702,38.87291016281703,100 -77.05315536854791,38.87053267794386,100 -77.05552622493516,38.868757801256,100 -77.05844056290393,38.86996206506943,100 -77.05788457660967,38.87253259892824,100 + + + + + -77.05668055019126,38.87154239798456,100 -77.05542625960818,38.87167890344077,100 -77.05485125901024,38.87076535397792,100 -77.05577677433152,38.87008686581446,100 -77.05691162017543,38.87054446963351,100 -77.05668055019126,38.87154239798456,100 + + + + + diff --git a/test/polygon_inner.kml b/test/polygon_inner.kml new file mode 100644 index 0000000..20775df --- /dev/null +++ b/test/polygon_inner.kml @@ -0,0 +1,25 @@ + + + + The Pentagon + + 1 + relativeToGround + + + -77.05788457660967,38.87253259892824,100 -77.05465973756702,38.87291016281703,100 -77.05315536854791,38.87053267794386,100 -77.05552622493516,38.868757801256,100 -77.05844056290393,38.86996206506943,100 -77.05788457660967,38.87253259892824,100 + + + + + -77.05668055019126,38.87154239798456,100 -77.05542625960818,38.87167890344077,100 -77.05485125901024,38.87076535397792,100 -77.05577677433152,38.87008686581446,100 -77.05691162017543,38.87054446963351,100 -77.05668055019126,38.87154239798456,100 + + + + + -77.05668055019126,38.87154239798456,100 -77.05542625960818,38.87167890344077,100 -77.05485125901024,38.87076535397792,100 -77.05577677433152,38.87008686581446,100 -77.05691162017543,38.87054446963351,100 -77.05668055019126,38.87154239798456,100 + + + + + diff --git a/test/polygon_style.kml b/test/polygon_style.kml new file mode 100644 index 0000000..70a51ca --- /dev/null +++ b/test/polygon_style.kml @@ -0,0 +1,24 @@ + + + + + Building 41 + #transBluePoly + + 1 + relativeToGround + + + -122.0857412771483,37.42227033155257,17 -122.0858169768481,37.42231408832346,17 -122.085852582875,37.42230337469744,17 -122.0858799945639,37.42225686138789,17 -122.0858860101409,37.4222311076138,17 -122.0858069157288,37.42220250173855,17 -122.0858379542653,37.42214027058678,17 -122.0856732640519,37.42208690214408,17 -122.0856022926407,37.42214885429042,17 -122.0855902778436,37.422128290487,17 -122.0855841672237,37.42208171967246,17 -122.0854852065741,37.42210455874995,17 -122.0855067264352,37.42214267949824,17 -122.0854430712915,37.42212783846172,17 -122.0850990714904,37.42251282407603,17 -122.0856769818632,37.42281815323651,17 -122.0860162273783,37.42244918858722,17 -122.0857260327004,37.42229239604253,17 -122.0857412771483,37.42227033155257,17 + + + + + diff --git a/test/simple_placemark.kml b/test/simple_placemark.kml new file mode 100644 index 0000000..f78fa39 --- /dev/null +++ b/test/simple_placemark.kml @@ -0,0 +1,12 @@ + + + + Simple placemark + + + + + -122.0822035425683,37.42228990140251,0 + + + diff --git a/test/style_map.kml b/test/style_map.kml new file mode 100644 index 0000000..63ebc45 --- /dev/null +++ b/test/style_map.kml @@ -0,0 +1,40 @@ + + + + Highlighted Icon + + + + + + + + normal + #normalPlacemark + + + highlight + #highlightPlacemark + + + + Roll over this icon + #exampleStyleMap + + -122.0856545755255,37.42243077405461,0 + + + +