Module: Adiwg_GeographicElement

Defined in:
lib/adiwg/mdtranslator/readers/adiwgJson/modules_0.9.0/module_geographicElement.rb

Class Method Summary collapse

Class Method Details

.unpack(aGeoElements) ⇒ Object



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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/adiwg/mdtranslator/readers/adiwgJson/modules_0.9.0/module_geographicElement.rb', line 27

def self.unpack(aGeoElements)

  # only one geometry is allowed per geographic element.
  # ... in GeoJSON each geometry is allowed a bounding box;
  # ... This code splits bounding boxes to separate elements

  # instance classes needed in script
  aIntGeoEle = Array.new

  aGeoElements.each do |hGeoJsonElement|

    # instance classes needed in script
    intMetadataClass = .new
    hGeoElement = intMetadataClass.newGeoElement

    # find geographic element type
    if hGeoJsonElement.has_key?('type')
      elementType = hGeoJsonElement['type']
    else
      # invalid geographic element
      return nil
    end

    # set geographic element id
    if hGeoJsonElement.has_key?('id')
      s = hGeoJsonElement['id']
      if s != ''
        hGeoElement[:elementId] = s
      end
    end

    # set geographic element coordinate reference system - CRS
    if hGeoJsonElement.has_key?('crs')
      hGeoCrs = hGeoJsonElement['crs']
      Adiwg_GeoCoordSystem.unpack(hGeoCrs, hGeoElement)
    end

    # set geographic element properties
    if hGeoJsonElement.has_key?('properties')
      hGeoProps = hGeoJsonElement['properties']
      Adiwg_GeoProperties.unpack(hGeoProps, hGeoElement)
    end

    # process geographic element bounding box
    # the bounding box must be represented as a separate geographic element for ISO
    # need to make a deep copy of current state of geographic element for bounding box
    if hGeoJsonElement.has_key?('bbox')
      if hGeoJsonElement['bbox'].length == 4
        aBBox = hGeoJsonElement['bbox']

        boxElement = Marshal.load(Marshal.dump(hGeoElement))
        boxElement[:elementGeometry] = Adiwg_BoundingBox.unpack(aBBox)

        aIntGeoEle << boxElement
      end
    end

    # unpack geographic element
    case elementType

      # GeoJSON Features
      when 'Feature'
        if hGeoJsonElement.has_key?('geometry')
          hGeometry = hGeoJsonElement['geometry']

          # geoJSON requires geometry to be 'null' when geometry is bounding box only
          # JSON null converts in parsing to ruby nil
          unless hGeometry.nil?
            unless hGeometry.empty?
              if hGeometry.has_key?('type')
                geometryType = hGeometry['type']
                aCoordinates = hGeometry['coordinates']
                unless aCoordinates.empty?
                  case geometryType
                    when 'Point', 'MultiPoint'
                      hGeoElement[:elementGeometry] = Adiwg_Point.unpack(aCoordinates, geometryType)
                    when 'LineString', 'MultiLineString'
                      hGeoElement[:elementGeometry] = Adiwg_LineString.unpack(aCoordinates, geometryType)
                    when 'Polygon', 'MultiPolygon'
                      hGeoElement[:elementGeometry] = Adiwg_Polygon.unpack(aCoordinates, geometryType)
                    else
                      # log - the GeoJSON geometry type is not supported
                  end
                  aIntGeoEle << hGeoElement
                end
              end
            end
          end

        end

      # GeoJSON Feature Collection
      when 'FeatureCollection'
        if hGeoJsonElement.has_key?('features')
          aFeatures = hGeoJsonElement['features']
          unless aFeatures.empty?
            intGeometry = intMetadataClass.newGeometry
            intGeometry[:geoType] = 'MultiGeometry'
            intGeometry[:geometry] = Adiwg_GeographicElement.unpack(aFeatures)
            hGeoElement[:elementGeometry] = intGeometry
            aIntGeoEle << hGeoElement
          end
        end

      # GeoJSON Geometries
      when 'Point', 'MultiPoint'
        aCoordinates = hGeoJsonElement['coordinates']
        hGeoElement[:elementGeometry] = Adiwg_Point.unpack(aCoordinates, elementType)
        aIntGeoEle << hGeoElement

      when 'LineString', 'MultiLineString'
        aCoordinates = hGeoJsonElement['coordinates']
        hGeoElement[:elementGeometry] = Adiwg_LineString.unpack(aCoordinates, elementType)
        aIntGeoEle << hGeoElement

      when 'Polygon', 'MultiPolygon'
        aCoordinates = hGeoJsonElement['coordinates']
        hGeoElement[:elementGeometry] = Adiwg_Polygon.unpack(aCoordinates, elementType)
        aIntGeoEle << hGeoElement

      # GeoJSON Geometry Collection
      when 'GeometryCollection'
        if hGeoJsonElement.has_key?('geometries')
          aGeometries = hGeoJsonElement['geometries']
          unless aGeometries.empty?
            intGeometry = intMetadataClass.newGeometry
            intGeometry[:geoType] = 'MultiGeometry'
            intGeometry[:geometry] = Adiwg_GeographicElement.unpack(aGeometries)
            hGeoElement[:elementGeometry] = intGeometry
            aIntGeoEle << hGeoElement
          end
        end

    end

  end

  return aIntGeoEle

end