Class: FrOData::Properties::Geography::Base

Inherits:
FrOData::Property show all
Defined in:
lib/frodata/properties/geography/base.rb

Direct Known Subclasses

LineString, Point, Polygon

Constant Summary collapse

DEFAULT_SRID =

The default SRID (same as used by GPS)

4326

Instance Attribute Summary collapse

Attributes inherited from FrOData::Property

#name, #options, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FrOData::Property

#==, #allows_nil?, #concurrency_mode, #strict?, #type, #xml_value

Constructor Details

#initialize(name, value, options = {}) ⇒ Base

Initializes a geography property.

Special options available for geographic types:

srid: the SRID (spatial reference ID) of the

coordinate system being used.
Defaults to 4326 (same as GPS).

Parameters:

  • name (to_s)
  • value (Hash|Array|to_s|nil)
  • options (Hash) (defaults to: {})


22
23
24
25
26
# File 'lib/frodata/properties/geography/base.rb', line 22

def initialize(name, value, options = {})
  super(name, value, options)
  self.value = value
  self.srid  = srid || options[:srid] || DEFAULT_SRID
end

Instance Attribute Details

#sridObject

The SRID (Spatial Reference ID) of this property.



6
7
8
# File 'lib/frodata/properties/geography/base.rb', line 6

def srid
  @srid
end

Class Method Details

.from_xml(property_xml, options = {}) ⇒ FrOData::Properties::Geography

Creates a new property instance from an XML element

Parameters:

  • property_xml (Nokogiri::XML::Element)
  • options (Hash) (defaults to: {})

Returns:



107
108
109
110
111
112
113
114
115
116
# File 'lib/frodata/properties/geography/base.rb', line 107

def self.from_xml(property_xml, options = {})
  if property_xml.attributes['null'].andand.value == 'true'
    content = nil
  else
    content = parse_xml(property_xml)
    options.merge!(srid: srid_from_xml(property_xml))
  end

  new(property_xml.name, content, options)
end

Instance Method Details

#crsHash

The full CRS representation as used by GeoJSON

Returns:

  • (Hash)


82
83
84
85
86
87
# File 'lib/frodata/properties/geography/base.rb', line 82

def crs
  {
    type: 'name',
    properties: { name: crs_name }
  }
end

#crs_nameString

The name of the CRS (Coordinate Reference System) used. Used in GeoJSON representation

Returns:



72
73
74
75
76
77
78
# File 'lib/frodata/properties/geography/base.rb', line 72

def crs_name
  if srid == DEFAULT_SRID
    "EPSG:#{srid}"
  else
    raise NotImplementedError, "Unsupported SRID #{srid}"
  end
end

#json_valueHash

Value to be used in JSON.

Returns:

  • (Hash)


50
51
52
53
54
55
56
# File 'lib/frodata/properties/geography/base.rb', line 50

def json_value
  {
    type: type_name,
    coordinates: value,
    crs: crs
  }
end

#srs_nameString

The name of the SRS (Spatial Reference System) used. Basically, the SRID in URI/URL form.

Returns:



61
62
63
64
65
66
67
# File 'lib/frodata/properties/geography/base.rb', line 61

def srs_name
  if srid == DEFAULT_SRID
    "http://www.opengis.net/def/crs/EPSG/0/#{srid}"
  else
    raise NotImplementedError, "Unsupported SRID #{srid}"
  end
end

#to_xml(xml_builder) ⇒ Object

Returns the XML representation of the property to the supplied XML builder.

Parameters:

  • xml_builder (Nokogiri::XML::Builder)


92
93
94
95
96
97
98
99
100
101
# File 'lib/frodata/properties/geography/base.rb', line 92

def to_xml(xml_builder)
  attributes = { 'metadata:type' => type }
  type_attrs = { 'gml:srsName' => srs_name }

  xml_builder['data'].send(name.to_sym, attributes) do
    xml_builder['gml'].send(type_name, type_attrs) do
      value_to_xml(xml_value, xml_builder)
    end
  end
end

#url_valueString

Value to be used in URLs.

Returns:



44
45
46
# File 'lib/frodata/properties/geography/base.rb', line 44

def url_value
  "geography'SRID=#{srid};#{type_name}(#{coords_to_s})'"
end

#value=(value) ⇒ Object

Sets the value of the property.

Parameters:

  • value (Hash|Array|to_s|nil)


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/frodata/properties/geography/base.rb', line 30

def value=(value)
  if value.nil? && allows_nil?
    @value = nil
  elsif value.is_a?(Hash)
    @value = value['coordinates']
  elsif value.is_a?(Array)
    @value = value
  else
    @value = parse_wkt(value.to_s)
  end
end