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.


14
15
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
# File 'lib/charta/gml.rb', line 14

def initialize(data, srid = :WGS84)
  srid ||= :WGS84
  @gml = 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
  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.blank?
    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


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

def srid
  @srid
end

Class Method Details

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


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

def document_to_ewkt(gml, srid)
  # whole document
  if gml.css("#{OGR_PREFIX}|FeatureCollection").blank? || gml.css("#{GML_PREFIX}|featureMember").blank?
    # 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


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

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

  wkt = 'LINESTRING(' + gml.css("#{GML_PREFIX}|coordinates").collect { |coords| coords.content.split(/\r\n|\n| /) }.flatten.reject(&:empty?).collect { |c| c.split ',' }.collect { |dimension| %(#{dimension.first} #{dimension.second}) }.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

.object_to_ewkt(fragment, srid) ⇒ Object


71
72
73
# File 'lib/charta/gml.rb', line 71

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

.point_to_ewkt(gml, srid) ⇒ Object


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

def point_to_ewkt(gml, srid)
  return 'POINT EMPTY' if gml.css("#{GML_PREFIX}|coordinates").blank?
  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
# File 'lib/charta/gml.rb', line 101

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

  wkt = 'POLYGON(' + %w(outerBoundaryIs innerBoundaryIs).collect do |boundary|
    next if gml.css("#{GML_PREFIX}|#{boundary}").empty?
    gml.css("#{GML_PREFIX}|#{boundary}").collect do |hole|
      '(' + hole.css("#{GML_PREFIX}|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(', ') + ')'

  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.select_value("SELECT ST_AsText(ST_Transform(ST_GeomFromText('#{data}', #{from_srid}),#{to_srid}))")
end

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

Test is given data is a valid GML

Returns:

  • (Boolean)

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

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

Instance Method Details

#to_ewktObject


52
53
54
# File 'lib/charta/gml.rb', line 52

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

#valid?Boolean

Returns:

  • (Boolean)

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

def valid?
  to_ewkt
  true
rescue
  false
end