-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
geo: order geospatial objects by Hilbert Curve index
This follows the [PostGIS way](https://info.crunchydata.com/blog/waiting-for-postgis-3-hilbert-geometry-sorting), but does not follow the same encoding mechanism, so we cannot compare results directly. For Geography we re-use the S2 infrastructure and for Geometry we use the google/hilbert library. Release note (sql change): When ordering by geospatial columns, it will now order by the Hilbert Space Curve index so that points which are geographically similar are clustered together.
- Loading branch information
Showing
8 changed files
with
405 additions
and
59 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
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
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,44 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package geo | ||
|
||
// hilbertInverse converts (x,y) to d on a Hilbert Curve. | ||
// Adapted from `xy2d` from https://en.wikipedia.org/wiki/Hilbert_curve#Applications_and_mapping_algorithms. | ||
func hilbertInverse(n, x, y uint64) uint64 { | ||
var d uint64 | ||
for s := uint64(n / 2); s > 0; s /= 2 { | ||
var rx uint64 | ||
if (x & s) > 0 { | ||
rx = 1 | ||
} | ||
var ry uint64 | ||
if (y & s) > 0 { | ||
ry = 1 | ||
} | ||
d += s * s * ((3 * rx) ^ ry) | ||
x, y = hilbertRotate(n, x, y, rx, ry) | ||
} | ||
return d | ||
} | ||
|
||
// hilberRoate rotates/flips a quadrant appropriately. | ||
// Adapted from `rot` in https://en.wikipedia.org/wiki/Hilbert_curve#Applications_and_mapping_algorithms. | ||
func hilbertRotate(n, x, y, rx, ry uint64) (uint64, uint64) { | ||
if ry == 0 { | ||
if rx == 1 { | ||
x = n - 1 - x | ||
y = n - 1 - y | ||
} | ||
|
||
x, y = y, x | ||
} | ||
return x, y | ||
} |
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
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
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
Oops, something went wrong.