Module: PNNL::BuildingId::V3
- Defined in:
- lib/pnnl/building_id/v3.rb
Constant Summary collapse
- SEPARATOR_ =
The separator for OLC codes in a UBID code.
"-"
- FORMAT_STRING_ =
Format string for UBID codes.
"%s-%.0f-%.0f-%.0f-%.0f"
- RE_PATTERN_ =
Regular expression for UBID codes.
Regexp.new([ "^", "([#{Regexp.quote(PlusCodes::CODE_ALPHABET)}]{4,8}#{Regexp.quote(PlusCodes::SEPARATOR)}[#{Regexp.quote(PlusCodes::CODE_ALPHABET)}]*)", "#{Regexp.quote(SEPARATOR_)}", "(0|[1-9][0-9]*)", "#{Regexp.quote(SEPARATOR_)}", "(0|[1-9][0-9]*)", "#{Regexp.quote(SEPARATOR_)}", "(0|[1-9][0-9]*)", "#{Regexp.quote(SEPARATOR_)}", "(0|[1-9][0-9]*)", "$", ].join)
- RE_GROUP_OPENLOCATIONCODE_ =
The first group of a UBID code is the OLC for the geometric center of mass (i.e., centroid) of the building footprint.
1
- RE_GROUP_NORTH_ =
The second group of the UBID code is the Chebyshev distance in OLC grid units from the OLC for the centroid of the building footprint to the northern extent of the OLC bounding box for the building footprint.
2
- RE_GROUP_EAST_ =
The third group of the UBID code is the Chebyshev distance in OLC grid units from the OLC for the centroid of the building footprint to the eastern extent of the OLC bounding box for the building footprint.
3
- RE_GROUP_SOUTH_ =
The fourth group of the UBID code is the Chebyshev distance in OLC grid units from the OLC for the centroid of the building footprint to the southern extent of the OLC bounding box for the building footprint.
4
- RE_GROUP_WEST_ =
The fifth group of the UBID code is the Chebyshev distance in OLC grid units from the OLC for the centroid of the building footprint to the western extent of the OLC bounding box for the building footprint.
5
Class Method Summary collapse
-
.decode(code) ⇒ PNNL::BuildingId::CodeArea
Returns the UBID code area for the given UBID code.
-
.encode(latitude_lo, longitude_lo, latitude_hi, longitude_hi, latitudeCenter, longitudeCenter, options = {}) ⇒ String
Returns the UBID code for the given coordinates.
-
.encode_code_area(code_area) ⇒ String
Returns the UBID code for the given UBID code area.
-
.valid?(code) ⇒ Boolean
Is the given UBID code valid?.
Class Method Details
.decode(code) ⇒ PNNL::BuildingId::CodeArea
Returns the UBID code area for the given UBID code.
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 |
# File 'lib/pnnl/building_id/v3.rb', line 61 def self.decode(code) if !(md = RE_PATTERN_.match(code)).nil? # Extract the OLC for the centroid of the building footprint. centroid_openlocationcode = md[RE_GROUP_OPENLOCATIONCODE_] # Decode the OLC for the centroid of the building footprint. centroid_openlocationcode_CodeArea = open_location_code.decode(centroid_openlocationcode) # Calculate the size of the OLC for the centroid of the building footprint # in decimal degree units. height = centroid_openlocationcode_CodeArea.north_latitude - centroid_openlocationcode_CodeArea.south_latitude width = centroid_openlocationcode_CodeArea.east_longitude - centroid_openlocationcode_CodeArea.west_longitude # Calculate the size of the OLC bounding box for the building footprint, # assuming that the datum are Chebyshev distances. north_latitude = centroid_openlocationcode_CodeArea.north_latitude + (Float(md[RE_GROUP_NORTH_]) * height) east_longitude = centroid_openlocationcode_CodeArea.east_longitude + (Float(md[RE_GROUP_EAST_]) * width) south_latitude = centroid_openlocationcode_CodeArea.south_latitude - (Float(md[RE_GROUP_SOUTH_]) * height) west_longitude = centroid_openlocationcode_CodeArea.west_longitude - (Float(md[RE_GROUP_WEST_]) * width) # Construct and return the UBID code area. return PNNL::BuildingId::CodeArea.new( centroid_openlocationcode_CodeArea, centroid_openlocationcode.length - PlusCodes::SEPARATOR.length, north_latitude, south_latitude, east_longitude, west_longitude ) else raise ArgumentError.new('Invalid UBID') end end |
.encode(latitude_lo, longitude_lo, latitude_hi, longitude_hi, latitudeCenter, longitudeCenter, options = {}) ⇒ String
Returns the UBID code for the given coordinates.
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 |
# File 'lib/pnnl/building_id/v3.rb', line 107 def self.encode(latitude_lo, longitude_lo, latitude_hi, longitude_hi, latitudeCenter, longitudeCenter, = {}) codeLength = [:codeLength] || PlusCodes::PAIR_CODE_LENGTH # Encode the OLCs for the northeast and southwest corners of the minimal # bounding box for the building footprint. northeast_openlocationcode = open_location_code.encode(latitude_hi, longitude_hi, codeLength) southwest_openlocationcode = open_location_code.encode(latitude_lo, longitude_lo, codeLength) # Encode the OLC for the centroid of the building footprint. centroid_openlocationcode = open_location_code.encode(latitudeCenter, longitudeCenter, codeLength) # Decode the OLCs for the northeast and southwest corners of the minimal # bounding box for the building footprint. northeast_openlocationcode_CodeArea = open_location_code.decode(northeast_openlocationcode) southwest_openlocationcode_CodeArea = open_location_code.decode(southwest_openlocationcode) # Decode the OLC for the centroid of the building footprint. centroid_openlocationcode_CodeArea = open_location_code.decode(centroid_openlocationcode) # Calculate the size of the OLC for the centroid of the building footprint # in decimal degree units. height = centroid_openlocationcode_CodeArea.north_latitude - centroid_openlocationcode_CodeArea.south_latitude width = centroid_openlocationcode_CodeArea.east_longitude - centroid_openlocationcode_CodeArea.west_longitude # Calculate the Chebyshev distances to the northern, eastern, southern and # western of the OLC bounding box for the building footprint. delta_north = (northeast_openlocationcode_CodeArea.north_latitude - centroid_openlocationcode_CodeArea.north_latitude) / height delta_east = (northeast_openlocationcode_CodeArea.east_longitude - centroid_openlocationcode_CodeArea.east_longitude) / width delta_south = (centroid_openlocationcode_CodeArea.south_latitude - southwest_openlocationcode_CodeArea.south_latitude) / height delta_west = (centroid_openlocationcode_CodeArea.west_longitude - southwest_openlocationcode_CodeArea.west_longitude) / width # Construct and return the UBID code. Kernel.sprintf(FORMAT_STRING_, centroid_openlocationcode, delta_north, delta_east, delta_south, delta_west) end |
.encode_code_area(code_area) ⇒ String
Returns the UBID code for the given UBID code area.
147 148 149 150 151 152 |
# File 'lib/pnnl/building_id/v3.rb', line 147 def self.encode_code_area(code_area) raise ArgumentError.new('Invalid PNNL::BuildingId::CodeArea') if code_area.nil? # Delegate. encode(code_area.south_latitude, code_area.west_longitude, code_area.north_latitude, code_area.east_longitude, code_area.centroid_code_area.latitude_center, code_area.centroid_code_area.longitude_center, codeLength: code_area.centroid_code_length) end |
.valid?(code) ⇒ Boolean
Is the given UBID code valid?
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/pnnl/building_id/v3.rb', line 158 def self.valid?(code) # Undefined UBID codes are invalid. return false if code.nil? # Attempt to match the regular expression. if !(md = RE_PATTERN_.match(code)).nil? open_location_code.valid?(md[RE_GROUP_OPENLOCATIONCODE_]) else # UBID codes that fail to match the regular expression are invalid. false end end |