class Charta::GML

Represents a Geometry with SRID

Constants

GML_PREFIX
NS
OGR_PREFIX
TAGS

Attributes

srid[R]

Public Class Methods

document_to_ewkt(gml, srid) click to toggle source
# 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
Also aliased as: geometry_collection_to_ewkt
geometry_collection_to_ewkt(gml, srid)
Alias for: document_to_ewkt
line_string_to_ewkt(gml, srid) click to toggle source
# 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
new(data, srid = :WGS84) click to toggle source
# 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
object_to_ewkt(fragment, srid) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

Test is given data is a valid GML

# File lib/charta/gml.rb, line 65
def valid?(data, srid = :WGS84)
  new(data, srid).valid?
end

Private Class Methods

transform_coordinates(coordinates) click to toggle source
# File lib/charta/gml.rb, line 145
def transform_coordinates(coordinates)
  coordinates.css("#{GML_PREFIX}|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/gml.rb, line 54
def to_ewkt
  "SRID=#{@srid};" + self.class.document_to_ewkt(@gml, @srid)
end
valid?() click to toggle source
# File lib/charta/gml.rb, line 58
def valid?
  to_ewkt
  true
end