MCPcopy Create free account
hub / github.com/cockroachdb/cockroachdb-parser / SpatialObjectToGeoHash

Function SpatialObjectToGeoHash

pkg/geo/encode.go:195–232  ·  view source on GitHub ↗

SpatialObjectToGeoHash transforms a given SpatialObject to a GeoHash.

(so geopb.SpatialObject, p int)

Source from the content-addressed store, hash-verified

193
194// SpatialObjectToGeoHash transforms a given SpatialObject to a GeoHash.
195func SpatialObjectToGeoHash(so geopb.SpatialObject, p int) (string, error) {
196 if so.BoundingBox == nil {
197 return "", nil
198 }
199 bbox := so.BoundingBox
200 if so.Type == geopb.SpatialObjectType_GeographyType {
201 // Convert bounding box back to degrees.
202 bbox = &geopb.BoundingBox{
203 LoX: s1.Angle(bbox.LoX).Degrees(),
204 HiX: s1.Angle(bbox.HiX).Degrees(),
205 LoY: s1.Angle(bbox.LoY).Degrees(),
206 HiY: s1.Angle(bbox.HiY).Degrees(),
207 }
208 }
209 if bbox.LoX < -180 || bbox.HiX > 180 || bbox.LoY < -90 || bbox.HiY > 90 {
210 return "", pgerror.Newf(
211 pgcode.InvalidParameterValue,
212 "object has bounds greater than the bounds of lat/lng, got (%f %f, %f %f)",
213 bbox.LoX, bbox.LoY,
214 bbox.HiX, bbox.HiY,
215 )
216 }
217
218 // Get precision using the bounding box if required.
219 if p <= GeoHashAutoPrecision {
220 p = getPrecisionForBBox(bbox)
221 }
222
223 // Support up to 20, which is the same as PostGIS.
224 if p > GeoHashMaxPrecision {
225 p = GeoHashMaxPrecision
226 }
227
228 bbCenterLng := bbox.LoX + (bbox.HiX-bbox.LoX)/2.0
229 bbCenterLat := bbox.LoY + (bbox.HiY-bbox.LoY)/2.0
230
231 return geohash.Encode(bbCenterLat, bbCenterLng, p), nil
232}
233
234// getPrecisionForBBox is a function imitating PostGIS's ability to go from
235// a world bounding box and truncating a GeoHash to fit the given bounding box.

Callers

nothing calls this directly

Calls 3

NewfFunction · 0.92
getPrecisionForBBoxFunction · 0.85
EncodeMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…