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.


8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/charta/kml.rb', line 8

def initialize(data, srid = :WGS84)
  @kml = if data.is_a? String

           Nokogiri::XML(data.to_s.squish) 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


4
5
6
# File 'lib/charta/kml.rb', line 4

def srid
  @srid
end

Class Method Details

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


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/charta/kml.rb', line 45

def document_to_ewkt(kml)
  return 'GEOMETRYCOLLECTION EMPTY' if kml.css('Document').blank?
  '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


58
59
60
# File 'lib/charta/kml.rb', line 58

def feature_to_ewkt(kml)
  object_to_ewkt(kml)
end

.line_string_to_ewkt(kml) ⇒ Object


67
68
69
70
71
# File 'lib/charta/kml.rb', line 67

def line_string_to_ewkt(kml)
  return 'LINESTRING EMPTY' if kml.css('coordinates').blank?

  'LINESTRING(' + kml.css('coordinates').collect { |coords| coords.content.split(/\r\n|\n| /) }.flatten.reject(&:empty?).collect { |c| c.split ',' }.collect { |dimension| %(#{dimension.first} #{dimension.second}) }.join(', ') + ')'
end

.multigeometry_to_ewkt(_kml) ⇒ Object


85
86
87
# File 'lib/charta/kml.rb', line 85

def multigeometry_to_ewkt(_kml)
  raise :not_implemented
end

.object_to_ewkt(fragment) ⇒ Object


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

def object_to_ewkt(fragment)
  send("#{fragment.name.snakecase}_to_ewkt", fragment)
end

.point_to_ewkt(kml) ⇒ Object


62
63
64
65
# File 'lib/charta/kml.rb', line 62

def point_to_ewkt(kml)
  return 'POINT EMPTY' if kml.css('coordinates').blank?
  'POINT(' + kml.css('coordinates').collect { |coords| coords.content.split ',' }.flatten.join(' ') + ')'
end

.polygon_to_ewkt(kml) ⇒ Object


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/charta/kml.rb', line 73

def polygon_to_ewkt(kml)
  return 'POLYGON EMPTY' if kml.css('coordinates').blank?

  'POLYGON(' + %w(outerBoundaryIs innerBoundaryIs).collect do |boundary|
    next if kml.css(boundary).empty?

    kml.css(boundary).collect do |hole|
      '(' + hole.css('coordinates').collect { |coords| coords.content.split(/\r\n|\n| /) }.flatten.reject(&:empty?).collect { |c| c.split ',' }.collect { |dimension| %(#{dimension.first} #{dimension.second}) }.join(', ') + ')'
    end.join(', ')
  end.compact.join(', ') + ')'
end

.valid?(data, srid = :WGS84) ⇒ Boolean

Test is given data is a valid KML

Returns:

  • (Boolean)

35
36
37
38
39
# File 'lib/charta/kml.rb', line 35

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

Instance Method Details

#to_ewktObject


22
23
24
# File 'lib/charta/kml.rb', line 22

def to_ewkt
  "SRID=#{@srid};" + self.class.document_to_ewkt(@kml)
end

#valid?Boolean

Returns:

  • (Boolean)

26
27
28
29
30
31
# File 'lib/charta/kml.rb', line 26

def valid?
  to_ewkt
  true
rescue
  false
end