From bbb938b73aded99a6e7c2d44dbe1875c0356542d Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Wed, 10 Oct 2012 00:13:45 +0200 Subject: [PATCH 1/7] Strip coordinates before splitting This is for when the coordinates are read in from an external source and contain leading or trailing space, which would otherwise result in an empty coordinate. --- lib/.DS_Store | Bin 0 -> 6148 bytes lib/kml/line_string.rb | 4 ++-- lib/kml/linear_ring.rb | 4 ++-- lib/kml/point.rb | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) create mode 100644 lib/.DS_Store diff --git a/lib/.DS_Store b/lib/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..98410415476e237ea741369ce69640d17edcd161 GIT binary patch literal 6148 zcmeHK!AiqG5S?wRCa92$ptr?yk+w?h!9xfYJPIxJU_}!mG#H!Gr1Vgt5fA>3U*q>U zv%40Hh2BJT26o@uotb3wAiFyNApB|60B`}o#zrWJtT38h>8hAufr!n8Y#b(Oh@-b~ zY<#~RXzdo>NMK35GpUP(~qoCE2?P^%`FAK(m6<`He0akz&Sf_wFP3-M;f$V1mSb=}1 z0PPPF8=+^g)Tp)&>{JPWSjA~&$f=f~9BI%qSZYKMn$W3;I#rkwL+EszmnP0LSZdVi zAWZQg%$q-p7opyc&zCA3gr|{vR)7^)R>0Cjm(Krl{AE@a`O7IhVg*=%Kc;}FcDfxO zKgyh~-#(AdT8Zrz8yUrwM9`2gT>@}G`^dg>YQH3oah}0aBRz%YRXQSH1e74$u>wD! Fzz6<4LQMbw literal 0 HcmV?d00001 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 From 11ff9d0e432765a178fc5ee6835f7b8e1281fafa Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Wed, 10 Oct 2012 00:34:03 +0200 Subject: [PATCH 2/7] Exclude .DS_Store file and include test kmls - we need the test kmls in the repository to run tests --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 817eee7..101712f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ doc -test/*.kml +.DS_Store From 0bd179c7af14d12df75e6019b0784b603e7b3f84 Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Wed, 10 Oct 2012 00:35:43 +0200 Subject: [PATCH 3/7] Add the test kmls so we can run tests --- test/cdata_and_snippet.kml | 21 ++++++++++++++++++++ test/ground_overlays.kml | 25 ++++++++++++++++++++++++ test/paths.kml | 33 +++++++++++++++++++++++++++++++ test/polygon.kml | 20 +++++++++++++++++++ test/polygon_style.kml | 24 +++++++++++++++++++++++ test/simple_placemark.kml | 12 ++++++++++++ test/style_map.kml | 40 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 175 insertions(+) create mode 100644 test/cdata_and_snippet.kml create mode 100644 test/ground_overlays.kml create mode 100644 test/paths.kml create mode 100644 test/polygon.kml create mode 100644 test/polygon_style.kml create mode 100644 test/simple_placemark.kml create mode 100644 test/style_map.kml 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/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_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 + + + + From 31fee80fed1c661d0830225043d138c787e5de26 Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Wed, 10 Oct 2012 00:38:53 +0200 Subject: [PATCH 4/7] Test the stripping of the coordinates field --- test/kml_file_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/kml_file_test.rb b/test/kml_file_test.rb index 297c7ad..02ffd3a 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 From 7bb0effa9dc71bcd0534fade13fdd54d60e72beb Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Wed, 10 Oct 2012 00:40:07 +0200 Subject: [PATCH 5/7] Polygons can have multiple inner boundaries This update still allows the old way of setting a single inner bounday via equals: polygon.inner_boundary_is = LinearRing.new(...) Is adds support to assign as array: polygon.inner_boundary_is = [ LinearRing.new(...), LinearRing.new(...) ] Or add to the existing array: polygon.inner_boundary_is << LinearRing.new(...) polygon.inner_boundary_is << LinearRing.new(...) I have added a test for this --- lib/kml/polygon.rb | 25 ++++++++++++++++++++----- test/kml_file_test.rb | 38 ++++++++++++++++++++++++++++++++++++++ test/polygon_inner.kml | 25 +++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 test/polygon_inner.kml 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/test/kml_file_test.rb b/test/kml_file_test.rb index 02ffd3a..db55465 100644 --- a/test/kml_file_test.rb +++ b/test/kml_file_test.rb @@ -128,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/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 + + + + + From 1d9b5ec220c7b28dc108e8346b2d878f920b4b4d Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Wed, 10 Oct 2012 00:45:25 +0200 Subject: [PATCH 6/7] Alias objects as features This allows you to loop over the features in the KMLFile in the same way you loop over features in containers --- lib/kml_file.rb | 1 + 1 file changed, 1 insertion(+) 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)) From 5cdf552d6093d34f62a89514e64c18d741a5e542 Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Wed, 10 Oct 2012 00:47:32 +0200 Subject: [PATCH 7/7] Specify the dependency on builder --- ruby_kml.gemspec | 1 + 1 file changed, 1 insertion(+) 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