Class: Charta::KML
- Inherits:
-
Object
- Object
- Charta::KML
- 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
-
#srid ⇒ Object
readonly
Returns the value of attribute srid.
Class Method Summary collapse
- .document_to_ewkt(kml) ⇒ Object (also: geometry_collection_to_ewkt)
- .feature_to_ewkt(kml) ⇒ Object
- .line_string_to_ewkt(kml) ⇒ Object
- .multigeometry_to_ewkt(_kml) ⇒ Object
- .object_to_ewkt(fragment) ⇒ Object
- .point_to_ewkt(kml) ⇒ Object
- .polygon_to_ewkt(kml) ⇒ Object
-
.valid?(data, srid = :WGS84) ⇒ Boolean
Test is given data is a valid KML.
Instance Method Summary collapse
-
#initialize(data, srid = :WGS84) ⇒ KML
constructor
A new instance of KML.
- #to_ewkt ⇒ Object
- #valid? ⇒ Boolean
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. = Nokogiri::XML::ParseOptions::NOBLANKS end else # Nokogiri::XML::Document expected data end @srid = Charta.find_srid(srid) end |
Instance Attribute Details
#srid ⇒ Object (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
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_ewkt ⇒ Object
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
30 31 32 33 34 35 |
# File 'lib/charta/kml.rb', line 30 def valid? to_ewkt true # rescue # false end |