Class: Charta::GML

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

Overview

Represents a Geometry with SRID

Constant Summary collapse

TAGS =
%w[Point LineString Polygon MultiGeometry].freeze
OGR_PREFIX =
'ogr'.freeze
GML_PREFIX =
'gml'.freeze
NS =
{
  gml: 'http://www.opengis.net/gml',
  ogr: 'http://ogr.maptools.org/'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of GML.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/charta/gml.rb', line 16

def initialize(data, srid = :WGS84)
  srid ||= :WGS84
  @gml = 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
  up = false
  # ensure namespaces are defined
  begin
    @gml.root.add_namespace_definition('xmlns', '')
    NS.each do |k, v|
      if @gml.xpath("//@*[xmlns:#{k}]").empty?
        @gml.root.namespace_definitions << @gml.root.add_namespace_definition(k.to_s, v)
        up = true
      end
    end
  rescue
    false
  end

  @gml = Nokogiri::XML(@gml.to_xml) if up

  boundaries = @gml.css("#{GML_PREFIX}|boundedBy")
  unless boundaries.nil?
    boundaries.each do |node|
      srid = Charta.find_srid(node['srsName']) unless node['srsName'].nil?
    end
  end

  @srid = Charta.find_srid(srid)
end

Instance Attribute Details

#sridObject (readonly)

Returns the value of attribute srid.



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

def srid
  @srid
end

Class Method Details

.document_to_ewkt(gml, srid) ⇒ Object Also known as: geometry_collection_to_ewkt



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/charta/gml.rb', line 73

def document_to_ewkt(gml, srid)
  # whole document
  if gml.css("#{OGR_PREFIX}|FeatureCollection").empty? || gml.css("#{GML_PREFIX}|featureMember").empty?
    # fragment
    if gml.root.name && TAGS.include?(gml.root.name)
      object_to_ewkt(gml.root, srid)
    else
      'GEOMETRYCOLLECTION EMPTY'
    end
  else
    'GEOMETRYCOLLECTION(' + gml.css("#{GML_PREFIX}|featureMember").collect do |feature|
      TAGS.collect do |tag|
        next if feature.css("#{GML_PREFIX}|#{tag}").empty?

        feature.css("#{GML_PREFIX}|#{tag}").collect do |fragment|
          object_to_ewkt(fragment, srid)
        end.compact.join(', ')
      end.compact.join(', ')
    end.compact.join(', ') + ')'
  end
end

.line_string_to_ewkt(gml, srid) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/charta/gml.rb', line 131

def line_string_to_ewkt(gml, srid)
  return 'LINESTRING EMPTY' if gml.css("#{GML_PREFIX}|coordinates").nil?

  wkt = "LINESTRING(#{transform_coordinates(gml)})"

  unless gml['srsName'].nil? || Charta.find_srid(gml['srsName']).to_s == srid.to_s
    wkt = transform(wkt, Charta.find_srid(gml['srsName']), srid)
  end

  wkt
end

.object_to_ewkt(fragment, srid) ⇒ Object



69
70
71
# File 'lib/charta/gml.rb', line 69

def object_to_ewkt(fragment, srid)
  send("#{Charta.underscore(fragment.name)}_to_ewkt", fragment, srid)
end

.point_to_ewkt(gml, srid) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/charta/gml.rb', line 119

def point_to_ewkt(gml, srid)
  return 'POINT EMPTY' if gml.css("#{GML_PREFIX}|coordinates").nil?

  wkt = 'POINT(' + gml.css("#{GML_PREFIX}|coordinates").collect { |coords| coords.content.split ',' }.flatten.join(' ') + ')'

  unless gml['srsName'].nil? || Charta.find_srid(gml['srsName']).to_s == srid.to_s
    wkt = transform(wkt, Charta.find_srid(gml['srsName']), srid)
  end

  wkt
end

.polygon_to_ewkt(gml, srid) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/charta/gml.rb', line 101

def polygon_to_ewkt(gml, srid)
  return 'POLYGON EMPTY' if gml.css("#{GML_PREFIX}|coordinates").nil?

  wkt = 'POLYGON(' + %w[outerBoundaryIs innerBoundaryIs].collect do |boundary|
    next if gml.css("#{GML_PREFIX}|#{boundary}").empty?

    gml.css("#{GML_PREFIX}|#{boundary}").collect do |hole|
      "(#{transform_coordinates(hole)})"
    end.join(', ')
  end.compact.join(', ') + ')'

  unless gml['srsName'].nil? || Charta.find_srid(gml['srsName']).to_s == srid.to_s
    wkt = transform(wkt, Charta.find_srid(gml['srsName']), srid)
  end

  wkt
end

.transform(data, from_srid, to_srid) ⇒ Object



97
98
99
# File 'lib/charta/gml.rb', line 97

def transform(data, from_srid, to_srid)
  Charta.new_geometry(data, from_srid).transform(to_srid).to_text
end

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

Test is given data is a valid GML

Returns:

  • (Boolean)


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

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

Instance Method Details

#to_ewktObject



54
55
56
# File 'lib/charta/gml.rb', line 54

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

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  to_ewkt
  true
end