class Charta::KML
Represents a Geometry
with SRID
Constants
- TAGS
Attributes
srid[R]
Public Class Methods
document_to_ewkt(kml)
click to toggle source
# File lib/charta/kml.rb, line 49 def document_to_ewkt(kml) return 'GEOMETRYCOLLECTION EMPTY' if kml.css('Document').nil? 'GEOMETRYCOLLECTION(' + kml.css('Placemark').collect do |placemark| TAGS.collect do |tag| next if placemark.css(tag).empty? placemark.css(tag).collect do |fragment| object_to_ewkt(fragment) end.compact.join(', ') end.compact.join(', ') end.compact.join(', ') + ')' end
Also aliased as: geometry_collection_to_ewkt
feature_to_ewkt(kml)
click to toggle source
# File lib/charta/kml.rb, line 65 def feature_to_ewkt(kml) object_to_ewkt(kml) end
line_string_to_ewkt(kml)
click to toggle source
# File lib/charta/kml.rb, line 75 def line_string_to_ewkt(kml) return 'LINESTRING EMPTY' if kml.css('coordinates').nil? "LINESTRING(#{transform_coordinates(kml)})" end
multigeometry_to_ewkt(_kml)
click to toggle source
# File lib/charta/kml.rb, line 93 def multigeometry_to_ewkt(_kml) raise :not_implemented end
new(data, srid = :WGS84)
click to toggle source
# File lib/charta/kml.rb, line 10 def initialize(data, srid = :WGS84) @kml = if data.is_a? String Nokogiri::XML(data.to_s.split.join(' ')) do |config| config.options = Nokogiri::XML::ParseOptions::NOBLANKS end else # Nokogiri::XML::Document expected data end @srid = Charta.find_srid(srid) end
object_to_ewkt(fragment)
click to toggle source
# File lib/charta/kml.rb, line 45 def object_to_ewkt(fragment) send("#{Charta.underscore(fragment.name)}_to_ewkt", fragment) end
point_to_ewkt(kml)
click to toggle source
# File lib/charta/kml.rb, line 69 def point_to_ewkt(kml) return 'POINT EMPTY' if kml.css('coordinates').nil? 'POINT(' + kml.css('coordinates').collect { |coords| coords.content.split ',' }.flatten.join(' ') + ')' end
polygon_to_ewkt(kml)
click to toggle source
# File lib/charta/kml.rb, line 81 def polygon_to_ewkt(kml) return 'POLYGON EMPTY' if kml.css('coordinates').nil? 'POLYGON(' + %w[outerBoundaryIs innerBoundaryIs].collect do |boundary| next if kml.css(boundary).empty? kml.css(boundary).collect do |hole| "(#{transform_coordinates(hole)})" end.join(', ') end.compact.join(', ') + ')' end
valid?(data, srid = :WGS84)
click to toggle source
Test is given data is a valid KML
# File lib/charta/kml.rb, line 39 def valid?(data, srid = :WGS84) new(data, srid).valid? # rescue # false end
Private Class Methods
transform_coordinates(coordinates)
click to toggle source
# File lib/charta/kml.rb, line 99 def transform_coordinates(coordinates) coordinates.css('coordinates') .collect { |coords| coords.content.split(/\r\n|\n| /) } .flatten .reject(&:empty?) .collect { |c| c.split ',' } .collect { |dimension| %(#{dimension.first} #{dimension[1]}) } .join(', ') end
Public Instance Methods
to_ewkt()
click to toggle source
# File lib/charta/kml.rb, line 24 def to_ewkt srid_part = @srid.nil? ? '' : "SRID=#{@srid};" srid_part + self.class.document_to_ewkt(@kml) end
valid?()
click to toggle source
# File lib/charta/kml.rb, line 30 def valid? to_ewkt true # rescue # false end