initial commit
This commit is contained in:
63
encoding/polylines/decoding.go
Normal file
63
encoding/polylines/decoding.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package polylines
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"github.com/paulmach/orb"
|
||||
)
|
||||
|
||||
func Decode(encoded *string, precisionOptional ...int) orb.LineString {
|
||||
// default to 6 digits of precision
|
||||
precision := 6
|
||||
if len(precisionOptional) > 0 {
|
||||
precision = precisionOptional[0]
|
||||
}
|
||||
factor := math.Pow10(precision)
|
||||
|
||||
// Coordinates have variable length when encoded, so just keep
|
||||
// track of whether we've hit the end of the string. In each
|
||||
// loop iteration, a single coordinate is decoded.
|
||||
lat, lng := 0, 0
|
||||
var coordinates orb.LineString
|
||||
index := 0
|
||||
for index < len(*encoded) {
|
||||
// Consume varint bits for lat until we run out
|
||||
var byte int = 0x20
|
||||
shift, result := 0, 0
|
||||
for byte >= 0x20 {
|
||||
byte = int((*encoded)[index]) - 63
|
||||
result |= (byte & 0x1f) << shift
|
||||
shift += 5
|
||||
index++
|
||||
}
|
||||
|
||||
// check if we need to go negative or not
|
||||
if (result & 1) > 0 {
|
||||
lat += ^(result >> 1)
|
||||
} else {
|
||||
lat += result >> 1
|
||||
}
|
||||
|
||||
// Consume varint bits for lng until we run out
|
||||
byte = 0x20
|
||||
shift, result = 0, 0
|
||||
for byte >= 0x20 {
|
||||
byte = int((*encoded)[index]) - 63
|
||||
result |= (byte & 0x1f) << shift
|
||||
shift += 5
|
||||
index++
|
||||
}
|
||||
|
||||
// check if we need to go negative or not
|
||||
if (result & 1) > 0 {
|
||||
lng += ^(result >> 1)
|
||||
} else {
|
||||
lng += result >> 1
|
||||
}
|
||||
|
||||
// scale the int back to floating point and storeLineString it
|
||||
coordinates = append(coordinates, orb.Point{float64(lat) / factor, float64(lng) / factor})
|
||||
}
|
||||
|
||||
return coordinates
|
||||
}
|
||||
17
encoding/polylines/encoding.go
Normal file
17
encoding/polylines/encoding.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package polylines
|
||||
|
||||
import (
|
||||
"github.com/paulmach/orb"
|
||||
"github.com/twpayne/go-polyline"
|
||||
)
|
||||
|
||||
func Encode(line orb.LineString) string {
|
||||
|
||||
preparedLine := [][]float64{}
|
||||
|
||||
for _, point := range line {
|
||||
preparedLine = append(preparedLine, []float64{point.Lon(), point.Lat()})
|
||||
}
|
||||
|
||||
return string(polyline.EncodeCoords(preparedLine))
|
||||
}
|
||||
Reference in New Issue
Block a user