-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rewrite node encoding for clarity and ease of extension
- Loading branch information
1 parent
b217f99
commit dc26fe3
Showing
3 changed files
with
131 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/binary" | ||
"math" | ||
) | ||
|
||
// an empty byte slice used for zeroing | ||
var zeros = make([]byte, 64) | ||
|
||
// node encoding | ||
type encoding []byte | ||
|
||
type encodingMetadata struct { | ||
EntranceType uint8 | ||
AccessibilityType uint8 | ||
} | ||
|
||
// encode lat/lon as 64 bit floats packed in to 8 bytes, | ||
// each float is then truncated to 6 bytes because we don't | ||
// need the additional precision (> 8 decimal places) | ||
func (e encoding) setCoords(lat float64, lon float64) { | ||
binary.BigEndian.PutUint64(e[0:8], math.Float64bits(lat)) | ||
binary.BigEndian.PutUint64(e[6:14], math.Float64bits(lon)) | ||
copy(e[12:14], zeros) | ||
} | ||
|
||
// decode lat/lon as truncated 64 bit floats from the first 12 bytes | ||
func (e encoding) getCoords() (float64, float64) { | ||
buffer := make([]byte, 8) | ||
|
||
copy(buffer, e[:6]) | ||
lat := math.Float64frombits(binary.BigEndian.Uint64(buffer)) | ||
|
||
copy(buffer, e[6:12]) | ||
lon := math.Float64frombits(binary.BigEndian.Uint64(buffer)) | ||
|
||
return lat, lon | ||
} | ||
|
||
// leftmost two bits are for the entrance, next two bits are accessibility | ||
// remaining 4 rightmost bits are reserved for future use. | ||
func (e encoding) setMetadata(meta encodingMetadata) int { | ||
if meta.EntranceType > 0 { | ||
e[12] = ((meta.EntranceType & 0b00000011) << 6) // values [0,1,2] (stored in leftmost two bits) | ||
e[12] |= ((meta.AccessibilityType & 0b00000011) << 4) // values [0,1,2] (stored in next two bits) | ||
|
||
// 13 byte encoding | ||
return 13 | ||
} | ||
|
||
// 12 byte encoding | ||
return 12 | ||
} | ||
|
||
func (e encoding) getMetadata() encodingMetadata { | ||
meta := encodingMetadata{} | ||
|
||
if len(e) > 12 { | ||
meta.EntranceType = (e[12] & 0b11000000) >> 6 | ||
meta.AccessibilityType = (e[12] & 0b00110000) >> 4 | ||
} | ||
|
||
return meta | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/qedus/osmpbf" | ||
) | ||
|
||
const ( | ||
EntranceNone uint8 = iota | ||
EntranceNormal | ||
EntranceMain | ||
) | ||
|
||
const ( | ||
WheelchairAccessibleNo uint8 = iota | ||
WheelchairAccessibleImplicitYes | ||
WheelchairAccessibleExplicitYes | ||
) | ||
|
||
// determine if the node is for an entrance | ||
// https://wiki.openstreetmap.org/wiki/Key:entrance | ||
func entranceType(node *osmpbf.Node) uint8 { | ||
if val, ok := node.Tags["entrance"]; ok { | ||
var norm = strings.ToLower(val) | ||
switch norm { | ||
case "main": | ||
return EntranceMain | ||
case "yes", "home", "staircase": | ||
return EntranceNormal | ||
} | ||
} | ||
return EntranceNone | ||
} | ||
|
||
// determine if the node is accessible for wheelchair users | ||
// https://wiki.openstreetmap.org/wiki/Key:entrance | ||
func accessibilityType(node *osmpbf.Node) uint8 { | ||
if val, ok := node.Tags["wheelchair"]; ok { | ||
var norm = strings.ToLower(val) | ||
switch norm { | ||
case "yes": | ||
return WheelchairAccessibleExplicitYes | ||
case "no": | ||
return WheelchairAccessibleNo | ||
default: | ||
return WheelchairAccessibleImplicitYes | ||
} | ||
} | ||
return WheelchairAccessibleNo | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters