Class: Charta::KML

Inherits:
Object
  • Object
show all
Defined in:
lib/charta/kml.rb

Overview

Represents a Geometry with SRID

Constant Summary collapse

TAGS =
%w[Point LineString Polygon MultiGeometry].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, srid = :WGS84) ⇒ KML

Returns a new instance of KML.



10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#sridObject (readonly)

Returns the value of attribute srid.



6
7
8
# File 'lib/charta/kml.rb', line 6

def srid
  @srid
end

Class Method Details

.document_to_ewkt(kml) ⇒ Object Also known as: geometry_collection_to_ewkt



49
50
51
52
53
54
55
56
57
58
59
60
61
# 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

.feature_to_ewkt(kml) ⇒ Object



65
66
67
# File 'lib/charta/kml.rb', line 65

def feature_to_ewkt(kml)
  object_to_ewkt(kml)
end

.line_string_to_ewkt(kml) ⇒ Object



75
76
77
78
79
# 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) ⇒ Object



93
94
95
# File 'lib/charta/kml.rb', line 93

def multigeometry_to_ewkt(_kml)
  raise :not_implemented
end

.object_to_ewkt(fragment) ⇒ Object



45
46
47
# 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) ⇒ Object



69
70
71
72
73
# 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) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# 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) ⇒ Boolean

Test is given data is a valid KML

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/charta/kml.rb', line 39

def valid?(data, srid = :WGS84)
  new(data, srid).valid?
  # rescue
  #   false
end

Instance Method Details

#to_ewktObject



24
25
26
27
28
# 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?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
# File 'lib/charta/kml.rb', line 30

def valid?
  to_ewkt
  true
  # rescue
  #   false
end