Module: AdiwgCoordinates
- Defined in:
- lib/adiwg/mdtranslator/internal/module_coordinates.rb
Overview
History:
Stan Smith 2017-08-23 added is_polygon_clockwise method
Stan Smith 2017-05-24 added checkGeometry method to move objects to real world
Stan Smith 2016-11-10 added computedBbox computation
Stan Smith 2015-07-16 moved module_coordinates from mdJson reader to internal
Stan Smith 2015-07-14 refactored to remove global namespace constants
Stan Smith 2015-06-22 replace global ($response) with passed in object (responseObj)
Stan Smith 2014-12-15 refactored to handle namespacing readers and writers
Stan Smith 2014-05-23 added getLevels
Stan Smith 2013-11-13 added getDimension Stan Smith 2013-11-07 original script
Class Method Summary collapse
-
.addPoint(aPoint, aNorthings, aEastings) ⇒ Object
add to tuple to an array.
-
.checkGeometry(aCoords, aNorthings, aEastings) ⇒ Object
move geometry to real world if possible move geometry to a single spanned meridian.
-
.computeBbox(aGeo) ⇒ Object
compute bounding box.
-
.getDimension(aCoords) ⇒ Object
get the number of dimensions in a coordinate array.
-
.getLevels(aCoords) ⇒ Object
get the number of levels in the coordinate array.
- .is_polygon_clockwise(aCoords) ⇒ Object
-
.stringifyCoords(aCoords, responseObj) ⇒ Object
repack coordinate array into single string for ISO.
-
.unpackCoords(aCoords, aNorthings, aEastings) ⇒ Object
unpack coordinate arrays.
-
.unpackGeometry(hGeo, aNorthings, aEastings) ⇒ Object
compute bounding box.
Class Method Details
.addPoint(aPoint, aNorthings, aEastings) ⇒ Object
add to tuple to an array
221 222 223 224 |
# File 'lib/adiwg/mdtranslator/internal/module_coordinates.rb', line 221 def self.addPoint(aPoint, aNorthings, aEastings) aEastings << aPoint[0] aNorthings << aPoint[1] end |
.checkGeometry(aCoords, aNorthings, aEastings) ⇒ Object
move geometry to real world if possible move geometry to a single spanned meridian
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/adiwg/mdtranslator/internal/module_coordinates.rb', line 169 def self.checkGeometry(aCoords, aNorthings, aEastings) aGeoNorthings = [] aGeoEastings = [] unpackCoords(aCoords, aGeoNorthings, aGeoEastings) minEast = aGeoEastings.min maxEast = aGeoEastings.max # if all coordinates in -1 or +1 world, move back to real world if maxEast < -180 aGeoEastings.collect! { |x| x + 360 } end if minEast > 180 aGeoEastings.collect! { |x| x - 360 } end minEast = aGeoEastings.min maxEast = aGeoEastings.max # if geoObject spans a meridian, find out which one and make it default if @spanWorld == 0 @spanWorld = -1 if maxEast < 0 && minEast < -180 @spanWorld = 1 if minEast > 0 && maxEast > 180 end # bring -1 and +1 world objects back into default meridian world if @spanWorld == -1 && maxEast > 180 aGeoEastings.collect! { |x| x - 360 } end if @spanWorld == 1 && minEast < -180 aGeoEastings.collect! { |x| x + 360 } end aEastings.concat(aGeoEastings) aNorthings.concat(aGeoNorthings) end |
.computeBbox(aGeo) ⇒ Object
compute bounding box
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 |
# File 'lib/adiwg/mdtranslator/internal/module_coordinates.rb', line 67 def self.computeBbox(aGeo) # reset spanned world to 0 before computing bounding box @spanWorld = 0 # find all the eastings (x) and northings (y) in the GeoJSON object aNorthings = [] aEastings = [] aGeo.each do |hGeo| unpackGeometry(hGeo, aNorthings, aEastings) end # return if no coordinates found in geographic object(s) return {} if aNorthings.empty? || aEastings.empty? # find most north/south points north = aNorthings.max south = aNorthings.min # find most east/west spanning the meridian eastM = aEastings.max westM = aEastings.min # find most east/west spanning the anti-meridian aPositiveEast = [] aNegativeEast = [] aEastings.each do |east| aNegativeEast << east if east < 0.0 aPositiveEast << east if east >= 0.0 end aNegativeEast.uniq! aPositiveEast.uniq! eastAM = aNegativeEast.max westAM = aPositiveEast.min # if eastings are all positive or all negative hemisphere is decided if aPositiveEast.empty? || aNegativeEast.empty? east = eastM west = westM else # choose which meridian to span based on smaller edge-to-edge distance distanceM = eastM - westM distanceAM = (180 + eastAM) + (180 - westAM) if distanceM.abs <= distanceAM.abs # this spanned to meridian east = eastM west = westM else # this spanned tne anti-meridian east = eastAM west = westAM end end bBox = {} bBox[:westLongitude] = west bBox[:eastLongitude] = east bBox[:southLatitude] = south bBox[:northLatitude] = north return bBox end |
.getDimension(aCoords) ⇒ Object
get the number of dimensions in a coordinate array
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/adiwg/mdtranslator/internal/module_coordinates.rb', line 43 def self.getDimension(aCoords) if aCoords[0].kind_of?(Array) coordDim = getDimension(aCoords[0]) else coordDim = aCoords.length end return coordDim end |
.getLevels(aCoords) ⇒ Object
get the number of levels in the coordinate array
56 57 58 59 60 61 62 63 64 |
# File 'lib/adiwg/mdtranslator/internal/module_coordinates.rb', line 56 def self.getLevels(aCoords) i = 1 if aCoords[0].kind_of?(Array) i = i + getLevels(aCoords[0]) end return i end |
.is_polygon_clockwise(aCoords) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/adiwg/mdtranslator/internal/module_coordinates.rb', line 226 def self.is_polygon_clockwise(aCoords) area = 0.0 i = 0 n = aCoords.length - 1 until i == n area += (aCoords[i][0] * aCoords[i+1][1]) - (aCoords[i][1] * aCoords[i+1][0]) i += 1 end area += (aCoords[n][0] * aCoords[0][1]) - (aCoords[n][1] * aCoords[0][0]) return area >= 0 ? false : true end |
.stringifyCoords(aCoords, responseObj) ⇒ Object
repack coordinate array into single string for ISO
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/adiwg/mdtranslator/internal/module_coordinates.rb', line 20 def self.stringifyCoords(aCoords, responseObj) s = '' i = 0 coordCount = aCoords.length aCoords.each do |coord| if coord.kind_of?(Array) s = s + unpack(coord, responseObj) else i += 1 s = s + coord.to_s if i < coordCount s = s + ',' end s = s + ' ' end end return s end |
.unpackCoords(aCoords, aNorthings, aEastings) ⇒ Object
unpack coordinate arrays
210 211 212 213 214 215 216 217 218 |
# File 'lib/adiwg/mdtranslator/internal/module_coordinates.rb', line 210 def self.unpackCoords(aCoords, aNorthings, aEastings) if aCoords[0].kind_of?(Array) aCoords.each do |aTuple| unpackCoords(aTuple, aNorthings, aEastings) end else addPoint(aCoords, aNorthings, aEastings) end end |
.unpackGeometry(hGeo, aNorthings, aEastings) ⇒ Object
compute bounding box
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 |
# File 'lib/adiwg/mdtranslator/internal/module_coordinates.rb', line 133 def self.unpackGeometry(hGeo, aNorthings, aEastings) # geometry objects type = hGeo[:type] if %w{ Point LineString Polygon MultiPoint MultiLineString MultiPolygon }.one? {|word| word == type} checkGeometry(hGeo[:coordinates], aNorthings, aEastings) end # geometry collections if hGeo[:type] == 'GeometryCollection' hGeo[:geometryObjects].each do |aGeoObj| checkGeometry(aGeoObj[:coordinates], aNorthings, aEastings) end end # features if hGeo[:type] == 'Feature' featureType = hGeo[:geometryObject][:type] if featureType == 'GeometryCollection' unpackGeometry(hGeo[:geometryObject], aNorthings, aEastings) else checkGeometry(hGeo[:geometryObject][:coordinates], aNorthings, aEastings) end end # feature collections if hGeo[:type] == 'FeatureCollection' hGeo[:features].each do |hFeature| unpackGeometry(hFeature, aNorthings, aEastings) end end end |