|
| 1 | +package location |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math" |
| 6 | + "strconv" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + LATITUDE_MAX = 85.05112878 |
| 11 | + LONGITUDE_MAX = 180.0 |
| 12 | + LATITUDE_MIN = -LATITUDE_MAX |
| 13 | + LONGITUDE_MIN = -LONGITUDE_MAX |
| 14 | + EARTH_RADIUS_IN_METERS = 6372797.560856 |
| 15 | +) |
| 16 | + |
| 17 | +type Coordinates struct { |
| 18 | + Latitude float64 |
| 19 | + Longitude float64 |
| 20 | +} |
| 21 | + |
| 22 | +func NewCoordinates(latitude float64, longitude float64) Coordinates { |
| 23 | + if !isValidLatitudeLongitudePair(latitude, longitude) { |
| 24 | + panic(fmt.Sprintf("Codecrafters Internal Error - Invalid coordinates (lat=%.8f,lon=%.8f) in NewCoordinates()", |
| 25 | + latitude, longitude, |
| 26 | + )) |
| 27 | + } |
| 28 | + |
| 29 | + return Coordinates{ |
| 30 | + Latitude: latitude, |
| 31 | + Longitude: longitude, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func NewInvalidCoordinates(latitude float64, longitude float64) Coordinates { |
| 36 | + if isValidLatitudeLongitudePair(latitude, longitude) { |
| 37 | + panic(fmt.Sprintf( |
| 38 | + "Codecrafters Internal Error - Valid coordinates (lat=%.8f, lon=%.8f) in NewInvalidCoordinates()", |
| 39 | + latitude, longitude, |
| 40 | + )) |
| 41 | + } |
| 42 | + |
| 43 | + return Coordinates{ |
| 44 | + Latitude: latitude, |
| 45 | + Longitude: longitude, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// GetGeoGridCenterCoordinates decodes latitude and longitude from the geoCode |
| 50 | +// The decoded coordinates will be slightly different from the original one. |
| 51 | +// It is due to the fact that Redis drops precision at 52 bits. |
| 52 | +// The coordinates of the returned coordinate is the center of the smallest geo grid the coordinates |
| 53 | +// are a part of |
| 54 | +func (c Coordinates) GetGeoGridCenterCoordinates() Coordinates { |
| 55 | + geoCode := c.GetGeoCode() |
| 56 | + return decodeGeoCodeToCoordinates(geoCode) |
| 57 | +} |
| 58 | + |
| 59 | +// GetGeoCode returns the [WGS84](https://en.wikipedia.org/wiki/World_Geodetic_System) geocode of a coordinate pair |
| 60 | +// This is the same geocode used by Redis |
| 61 | +func (c Coordinates) GetGeoCode() uint64 { |
| 62 | + // Normalize to the range 0-2^26 |
| 63 | + latitudeOffset := (c.Latitude - LATITUDE_MIN) / (LATITUDE_MAX - LATITUDE_MIN) |
| 64 | + longitudeOffset := (c.Longitude - LONGITUDE_MIN) / (LONGITUDE_MAX - LONGITUDE_MIN) |
| 65 | + |
| 66 | + latitudeOffset *= (1 << 26) |
| 67 | + longitudeOffset *= (1 << 26) |
| 68 | + |
| 69 | + // Spread latitude bits |
| 70 | + x := uint64(latitudeOffset) |
| 71 | + x = (x | (x << 16)) & 0x0000FFFF0000FFFF |
| 72 | + x = (x | (x << 8)) & 0x00FF00FF00FF00FF |
| 73 | + x = (x | (x << 4)) & 0x0F0F0F0F0F0F0F0F |
| 74 | + x = (x | (x << 2)) & 0x3333333333333333 |
| 75 | + x = (x | (x << 1)) & 0x5555555555555555 |
| 76 | + |
| 77 | + // Spread longitude bits |
| 78 | + y := uint64(longitudeOffset) |
| 79 | + y = (y | (y << 16)) & 0x0000FFFF0000FFFF |
| 80 | + y = (y | (y << 8)) & 0x00FF00FF00FF00FF |
| 81 | + y = (y | (y << 4)) & 0x0F0F0F0F0F0F0F0F |
| 82 | + y = (y | (y << 2)) & 0x3333333333333333 |
| 83 | + y = (y | (y << 1)) & 0x5555555555555555 |
| 84 | + |
| 85 | + return x | (y << 1) |
| 86 | +} |
| 87 | + |
| 88 | +// DistanceFrom returns distance between two pair of coordinates using haversine great circle distance formula |
| 89 | +// While calculating distance, the coordinates actually used is the center of the geogrid instead of the |
| 90 | +// original coordinates. It is done to mimic Redis' way of calculating distance |
| 91 | +func (c Coordinates) DistanceFrom(coordinates Coordinates) float64 { |
| 92 | + c1 := c.GetGeoGridCenterCoordinates() |
| 93 | + c2 := coordinates.GetGeoGridCenterCoordinates() |
| 94 | + |
| 95 | + lat1radians := degreesToRadians(c1.Latitude) |
| 96 | + lat2radians := degreesToRadians(c2.Latitude) |
| 97 | + lon1radians := degreesToRadians(c1.Longitude) |
| 98 | + lon2radians := degreesToRadians(c2.Longitude) |
| 99 | + |
| 100 | + v := math.Sin((lon2radians - lon1radians) / 2) |
| 101 | + u := math.Sin((lat2radians - lat1radians) / 2) |
| 102 | + |
| 103 | + a := u*u + math.Cos(lat1radians)*math.Cos(lat2radians)*v*v |
| 104 | + return 2.0 * EARTH_RADIUS_IN_METERS * math.Asin(math.Sqrt(a)) |
| 105 | +} |
| 106 | + |
| 107 | +// LongitudeAsRedisCommandArg converts longitude of a coordinate to its |
| 108 | +// string representation with full precision |
| 109 | +func (c Coordinates) LongitudeAsRedisCommandArg() string { |
| 110 | + return strconv.FormatFloat(c.Longitude, 'f', -1, 64) |
| 111 | +} |
| 112 | + |
| 113 | +// LatitudeAsRedisCommandArg converts latitude of a coordinate to its |
| 114 | +// string representation with full precision |
| 115 | +func (c Coordinates) LatitudeAsRedisCommandArg() string { |
| 116 | + return strconv.FormatFloat(c.Latitude, 'f', -1, 64) |
| 117 | +} |
| 118 | + |
| 119 | +// decodeGeoCodeToCoordinates decodes a geocode and returns the coordinates of |
| 120 | +// the center of the geocode's decoded area |
| 121 | +func decodeGeoCodeToCoordinates(geoCode uint64) Coordinates { |
| 122 | + y := geoCode >> 1 |
| 123 | + x := geoCode |
| 124 | + |
| 125 | + // Compact bits back to 32-bit ints |
| 126 | + x = geoCode & 0x5555555555555555 |
| 127 | + x = (x | (x >> 1)) & 0x3333333333333333 |
| 128 | + x = (x | (x >> 2)) & 0x0F0F0F0F0F0F0F0F |
| 129 | + x = (x | (x >> 4)) & 0x00FF00FF00FF00FF |
| 130 | + x = (x | (x >> 8)) & 0x0000FFFF0000FFFF |
| 131 | + x = (x | (x >> 16)) & 0x00000000FFFFFFFF |
| 132 | + |
| 133 | + y = y & 0x5555555555555555 |
| 134 | + y = (y | (y >> 1)) & 0x3333333333333333 |
| 135 | + y = (y | (y >> 2)) & 0x0F0F0F0F0F0F0F0F |
| 136 | + y = (y | (y >> 4)) & 0x00FF00FF00FF00FF |
| 137 | + y = (y | (y >> 8)) & 0x0000FFFF0000FFFF |
| 138 | + y = (y | (y >> 16)) & 0x00000000FFFFFFFF |
| 139 | + |
| 140 | + latitude_scale := LATITUDE_MAX - LATITUDE_MIN |
| 141 | + longitude_scale := LONGITUDE_MAX - LONGITUDE_MIN |
| 142 | + |
| 143 | + gridLatitudeNumber := uint32(x) |
| 144 | + gridLongitudeNumber := uint32(y) |
| 145 | + |
| 146 | + gridLatitudeMin := LATITUDE_MIN + latitude_scale*(float64(gridLatitudeNumber)*1.0/(1<<26)) |
| 147 | + gridLatitudeMax := LATITUDE_MIN + latitude_scale*(float64(gridLatitudeNumber+1)*1.0/(1<<26)) |
| 148 | + gridLongitudeMin := LONGITUDE_MIN + longitude_scale*(float64(gridLongitudeNumber)*1.0/(1<<26)) |
| 149 | + gridLongitudeMax := LONGITUDE_MIN + longitude_scale*(float64(gridLongitudeNumber+1)*1.0/(1<<26)) |
| 150 | + |
| 151 | + latitude := (gridLatitudeMin + gridLatitudeMax) / 2 |
| 152 | + longitude := (gridLongitudeMin + gridLongitudeMax) / 2 |
| 153 | + |
| 154 | + if !isValidLatitudeLongitudePair(latitude, longitude) { |
| 155 | + panic( |
| 156 | + fmt.Sprintf("Codecrafters Internal Error - Decoded coordinates (lat=%.8f, lon=%.8f) is out of valid range", latitude, longitude), |
| 157 | + ) |
| 158 | + } |
| 159 | + |
| 160 | + return NewCoordinates(latitude, longitude) |
| 161 | +} |
| 162 | + |
| 163 | +func degreesToRadians(deg float64) float64 { |
| 164 | + return deg * math.Pi / 180 |
| 165 | +} |
| 166 | + |
| 167 | +func isValidLatitudeLongitudePair(latitude float64, longitude float64) bool { |
| 168 | + return latitude >= LATITUDE_MIN && latitude <= LATITUDE_MAX && |
| 169 | + longitude >= LONGITUDE_MIN && longitude <= LONGITUDE_MAX |
| 170 | +} |
0 commit comments