Geohash: Difference between revisions
m (→{{header|Swift}}: code improvement) |
(Add Factor) |
||
Line 22: | Line 22: | ||
* [[wp:Geohash|Wikipedia article - Geohash]]. |
* [[wp:Geohash|Wikipedia article - Geohash]]. |
||
=={{header|Factor}}== |
|||
Factor comes with the <code>geohash</code> vocabulary. See the implementation [https://docs.factorcode.org/content/vocab-geohash.html here]. |
|||
{{works with|Factor|0.99 2020-03-02}} |
|||
<lang factor>USING: formatting generalizations geohash io kernel sequences ; |
|||
: encode-geohash ( latitude longitude precision -- str ) |
|||
[ >geohash ] [ head ] bi* ; |
|||
! Encoding |
|||
51.433718 -0.214126 2 |
|||
51.433718 -0.214126 9 |
|||
57.649110 10.407440 11 |
|||
[ |
|||
3dup encode-geohash |
|||
"geohash for [%f, %f], precision %2d = %s\n" printf |
|||
] 3 3 mnapply nl |
|||
! Decoding |
|||
"u4pruydqqvj" dup geohash> |
|||
"coordinates for %s ~= [%f, %f]\n" printf</lang> |
|||
{{out}} |
|||
<pre> |
|||
geohash for [51.433718, -0.214126], precision 2 = gc |
|||
geohash for [51.433718, -0.214126], precision 9 = gcpue5hp4 |
|||
geohash for [57.649110, 10.407440], precision 11 = u4pruydqqvj |
|||
coordinates for u4pruydqqvj ~= [57.649110, 10.407439] |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
Revision as of 13:58, 13 June 2020
Geohashes are used to represent standard latitude and longitude coordinates as single values in the form of a simple string -- using the digits (0-9) and the letters (B-Z excluding I, L, O). They can vary in length, with more characters in the string representing more precision.
- Task
Generate a Geohash with a desired precision from a coordinate represented as an array of two doubles, latitude and longitude.
- Example 1:
- print (encodeGeohash (for: [51.433718, -0.214126], withPrecision: 2))
- // Result: "gc" (all of Ireland, most of England and Wales, small part of Scotland)
- Example 2:
- print (encodeGeohash (for: [51.433718, -0.214126], withPrecision: 9))
- // Result: "gcpue5hp4" (the umpire's chair on Center Court at Wimbledon)
From the Wikipedia page, geohashes can be "useful in database systems where queries on a single index are much easier or faster than multiple-index queries."
- Reference
Factor
Factor comes with the geohash
vocabulary. See the implementation here.
<lang factor>USING: formatting generalizations geohash io kernel sequences ;
- encode-geohash ( latitude longitude precision -- str )
[ >geohash ] [ head ] bi* ;
! Encoding 51.433718 -0.214126 2 51.433718 -0.214126 9 57.649110 10.407440 11 [
3dup encode-geohash "geohash for [%f, %f], precision %2d = %s\n" printf
] 3 3 mnapply nl
! Decoding "u4pruydqqvj" dup geohash> "coordinates for %s ~= [%f, %f]\n" printf</lang>
- Output:
geohash for [51.433718, -0.214126], precision 2 = gc geohash for [51.433718, -0.214126], precision 9 = gcpue5hp4 geohash for [57.649110, 10.407440], precision 11 = u4pruydqqvj coordinates for u4pruydqqvj ~= [57.649110, 10.407439]
Go
<lang go>package main
import (
"fmt" "strings"
)
type Location struct{ lat, lng float64 }
func (loc Location) String() string { return fmt.Sprintf("[%f, %f]", loc.lat, loc.lng) }
type Range struct{ lower, upper float64 }
var gBase32 = "0123456789bcdefghjkmnpqrstuvwxyz"
func encodeGeohash(loc Location, prec int) string {
latRange := Range{-90, 90} lngRange := Range{-180, 180} var hash strings.Builder hashVal := 0 bits := 0 even := true for hash.Len() < prec { val := loc.lat rng := latRange if even { val = loc.lng rng = lngRange } mid := (rng.lower + rng.upper) / 2 if val > mid { hashVal = (hashVal << 1) + 1 rng = Range{mid, rng.upper} if even { lngRange = Range{mid, lngRange.upper} } else { latRange = Range{mid, latRange.upper} } } else { hashVal <<= 1 if even { lngRange = Range{lngRange.lower, mid} } else { latRange = Range{latRange.lower, mid} } } even = !even if bits < 4 { bits++ } else { bits = 0 hash.WriteByte(gBase32[hashVal]) hashVal = 0 } } return hash.String()
}
func main() {
locs := []Location{ {51.433718, -0.214126}, {51.433718, -0.214126}, {57.64911, 10.40744}, } precs := []int{2, 9, 11}
for i, loc := range locs { geohash := encodeGeohash(loc, precs[i]) fmt.Printf("geohash for %v, precision %-2d = %s\n", loc, precs[i], geohash) }
}</lang>
- Output:
geohash for [51.433718, -0.214126], precision 2 = gc geohash for [51.433718, -0.214126], precision 9 = gcpue5hp4 geohash for [57.649110, 10.407440], precision 11 = u4pruydqqvj
Swift
<lang Swift>extension String {
subscript(i: Int) -> String { String(self[index(startIndex, offsetBy: i)]) }
}
struct Coordinate {
var latitude: Double var longitude: Double
}
let base32 = "0123456789bcdefghjkmnpqrstuvwxyz" // no "a", "i", "l", or "o"
func encodeGeohash (for location: Coordinate, withPrecision precision: Int = 9) -> String {
var latitudeRange = -90.0...90.0 var longitudeRange = -180...180.0
var hash = "" var hashVal = 0 var bits = 0 var even = true
while (hash.count < precision) { let val = even ? location.longitude: location.latitude var range = even ? longitudeRange : latitudeRange let mid = (range.lowerBound + range.upperBound) / 2
if (val > mid) { hashVal = (hashVal << 1) + 1 range = mid...range.upperBound
if even { longitudeRange = mid...longitudeRange.upperBound } else { latitudeRange = mid...latitudeRange.upperBound } } else { hashVal = (hashVal << 1) + 0 if even { longitudeRange = longitudeRange.lowerBound...mid } else { latitudeRange = latitudeRange.lowerBound...mid } }
even = !even
if (bits < 4) { bits += 1 } else { bits = 0 hash += base32[hashVal] hashVal = 0 } } return hash
}</lang>
Wren
<lang ecmascript>import "/fmt" for Fmt
var gBase32 = "0123456789bcdefghjkmnpqrstuvwxyz"
var encodeGeohash = Fn.new { |location, prec|
var latRange = -90..90 var lngRange = -180..180 var hash = "" var hashVal = 0 var bits = 0 var even = true while (hash.count < prec) { var val = even ? location[1] : location[0] var rng = even ? lngRange : latRange var mid = (rng.from + rng.to) / 2 if (val > mid) { hashVal = hashVal*2 + 1 rng = mid..rng.to if (even) lngRange = mid..lngRange.to else latRange = mid..latRange.to } else { hashVal = hashVal * 2 if (even) lngRange = lngRange.from..mid else latRange = latRange.from..mid } even = !even if (bits < 4) { bits = bits + 1 } else { bits = 0 hash = hash + gBase32[hashVal] hashVal = 0 } } return hash
}
var data = [
[[51.433718, -0.214126], 2], [[51.433718, -0.214126], 9], [[57.64911, 10.40744 ], 11]
]
for (d in data) {
var geohash = encodeGeohash.call(d[0], d[1]) var loc = "[%(Fmt.f(9, d[0][0], 6)), %(Fmt.f(9, d[0][1], 6))]" System.print("geohash for %(loc), precision %(Fmt.d(-2, d[1])) = %(geohash)")
}</lang>
- Output:
geohash for [51.433718, -0.214126], precision 2 = gc geohash for [51.433718, -0.214126], precision 9 = gcpue5hp4 geohash for [57.649110, 10.407440], precision 11 = u4pruydqqvj