Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some updates #8

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
doc
test/*.kml
.DS_Store
Binary file added lib/.DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions lib/kml/line_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -37,4 +37,4 @@ def render(xm=Builder::XmlMarkup.new(:indent => 2))
}
end
end
end
end
4 changes: 2 additions & 2 deletions lib/kml/linear_ring.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -46,4 +46,4 @@ def render(xm=Builder::XmlMarkup.new(:indent => 2))
}
end
end
end
end
2 changes: 1 addition & 1 deletion lib/kml/point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 20 additions & 5 deletions lib/kml/polygon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
# <coordinates>...</coordinates> <!-- lon,lat[,alt] -->
# </LinearRing>
# </innerBoundaryIs>
# <innerBoundaryIs>
# ...
# </innerBoundaryIs>
# </Polygon>

module KML #:nodoc:
Expand All @@ -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
end
1 change: 1 addition & 0 deletions lib/kml_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions ruby_kml.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 21 additions & 0 deletions test/cdata_and_snippet.kml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
<name>Document with CDATA example</name>
<Snippet>Document level snippet2</Snippet>
<Placemark>
<name>CDATA example</name>
<description>
<![CDATA[<h1>CDATA Tags are useful!</h1>
<p><font color="red">Text is <i>more readable</i> and
<b>easier to write</b> when you can avoid using entity
references.</font></p>
]]>
</description>
<Snippet>Example of a snippet2</Snippet>
<Point>
<coordinates>-122.0822035425683,37.4228,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
25 changes: 25 additions & 0 deletions test/ground_overlays.kml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Folder>
<name>Ground Overlays</name>
<description>
<![CDATA[Examples of ground overlays]]>
</description>
<GroundOverlay>
<name>Large-scale overlay on terrain</name>
<description>
<![CDATA[Overlay shows Mount Etna erupting on July 13th, 2001.]]>
</description>
<Icon>
<href>http://code.google.com/apis/kml/documentation/etna.jpg</href>
</Icon>
<LatLonBox>
<north>37.91904192681665</north>
<south>37.46543388598137</south>
<east>15.35832653742206</east>
<west>14.60128369746704</west>
<rotation/>
</LatLonBox>
</GroundOverlay>
</Folder>
</kml>
41 changes: 40 additions & 1 deletion test/kml_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
33 changes: 33 additions & 0 deletions test/paths.kml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
<name>Paths</name>
<description>
<![CDATA[Examples of paths. Note that the tessellate tag is by default
set to 0. If you want to create tessellated lines, they must be authored
(or edited) directly in KML.]]>
</description>
<Style id="yellowLineGreenPoly">
<LineStyle>
<color>7f00ffff</color>
<width>4</width>
</LineStyle>
<PolyStyle>
<color>7f00ff00</color>
</PolyStyle>
</Style>
<Placemark>
<name>Absolute Extruded</name>
<description>
<![CDATA[Transparent green wall with yellow outlines]]>
</description>
<styleUrl>#yellowLineGreenPoly</styleUrl>
<LineString>
<extrude>1</extrude>
<tessellate>1</tessellate>
<altitudeMode>absolute</altitudeMode>
<coordinates>-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</coordinates>
</LineString>
</Placemark>
</Document>
</kml>
20 changes: 20 additions & 0 deletions test/polygon.kml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Placemark>
<name>The Pentagon</name>
<Polygon>
<extrude>1</extrude>
<altitudeMode>relativeToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<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</coordinates>
</LinearRing>
</outerBoundaryIs>
<innerBoundaryIs>
<LinearRing>
<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</coordinates>
</LinearRing>
</innerBoundaryIs>
</Polygon>
</Placemark>
</kml>
25 changes: 25 additions & 0 deletions test/polygon_inner.kml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Placemark>
<name>The Pentagon</name>
<Polygon>
<extrude>1</extrude>
<altitudeMode>relativeToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<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</coordinates>
</LinearRing>
</outerBoundaryIs>
<innerBoundaryIs>
<LinearRing>
<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</coordinates>
</LinearRing>
</innerBoundaryIs>
<innerBoundaryIs>
<LinearRing>
<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</coordinates>
</LinearRing>
</innerBoundaryIs>
</Polygon>
</Placemark>
</kml>
24 changes: 24 additions & 0 deletions test/polygon_style.kml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Style id="transBluePoly">
<LineStyle>
<width>1.5</width>
</LineStyle>
<PolyStyle>
<color>7dff0000</color>
</PolyStyle>
</Style>
<Placemark>
<name>Building 41</name>
<styleUrl>#transBluePoly</styleUrl>
<Polygon>
<extrude>1</extrude>
<altitudeMode>relativeToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<coordinates>-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</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</kml>
12 changes: 12 additions & 0 deletions test/simple_placemark.kml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Placemark>
<name>Simple placemark</name>
<description>
<![CDATA[Attached to the ground. Intelligently places itself at the height of the underlying terrain.]]>
</description>
<Point>
<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
</Point>
</Placemark>
</kml>
40 changes: 40 additions & 0 deletions test/style_map.kml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
<name>Highlighted Icon</name>
<description>
<![CDATA[Place your mouse over the icon to see it display the new icon]]>
</description>
<Style id="highlightPlacemark">
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/paddle/red-stars.png</href>
</Icon>
</IconStyle>
</Style>
<Style id="normalPlacemark">
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/paddle/wht-blank.png</href>
</Icon>
</IconStyle>
</Style>
<StyleMap id="exampleStyleMap">
<Pair>
<key>normal</key>
<styleUrl>#normalPlacemark</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#highlightPlacemark</styleUrl>
</Pair>
</StyleMap>
<Placemark>
<name>Roll over this icon</name>
<styleUrl>#exampleStyleMap</styleUrl>
<Point>
<coordinates>-122.0856545755255,37.42243077405461,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>