Skip to content
demitriPublic

About

A Python implementation of the quadrilateralized spherical cube scheme for segmenting and indexing on a sphere.

Resources

Stars

7 stars

Watchers

2 watching

Forks

Repository files navigation

QLSC: Quadrilateralized Spherical Cube for Python

Documentation Status

Introduction

The quadrilateralized spherical cube (QLSC) is a geospatial indexing scheme for segmenting a sphere into pixels with the aim of optimized spatial indexing and queries. QLSC is an implementation of this scheme in a Python package. Parts of it are based on code from Sergey Koposov’s Q3C, a PostgreSQL extension that implements QLSC indexing. In addition to sphere segmentation, this package provides the catalog indexing functionality of Q3C without the need to install a PostgreSQL database.

Note that while this package is designed for astronomical use (it focusses on right ascension and declination), it can be just as easily be used for longitude and latitude coordinates, as long as you’re ok with a perfectly spherical Earth (though QLSC was designed to be used with the real Earth). The GeoQLSC and GeoQLSCIndex classes are exactly that: the same scheme and the same index, speaking longitude and latitude, with longitudes reported in the geographic range [-180,180) instead of the astronomical [0,360). Latitude is declination — same range, same sign — and a longitude may be given in any range, so the mapping is only the reporting convention plus the vocabulary. See the GeoQLSC documentation for the mapping written out in full.

from qlsc import GeoQLSC, GeoQLSCIndex

scheme = GeoQLSC(depth=20)
ipix = scheme.lonlat2ipix(lon=-74.0060, lat=40.7128)  # lon,lat -> ipix number
scheme.ipix2lonlat(ipix)                              # ipix -> lon,lat, lon in [-180,180)

index = GeoQLSCIndex(qlsc=scheme)
index.add_point(lon=-74.0060, lat=40.7128, key="New York")
index.add_point(lon=2.3522, lat=48.8566, key="Paris")
index.radial_query(lon=-74., lat=40.7, radius=1.)

Author: Demitri Muna Copyright © 2020-2026 Demitri Muna, except for the embedded Q3C code which is copyright Sergey Koposov.

Note for v1.2: the embedded Q3C code is updated to v2.0.6, which makes point_in_ellipse and ellipse_query correct for sub-arcsecond ellipses. Python 3.7 or later is now required, and four small breaking changes — ValueError instead of TypeError for a missing argument to radial_query or ang2ipix_at_depth, a validated depth in ipix_area, an empty QLSCIndex being falsy, and keys having to be strings — are described in the CHANGELOG.

Note for v1.1: QLSC.ipix2ang now returns the center of the pixel rather than the "lower left" corner, matching the Q3C PostgreSQL extension v2.0.1 and later. This is a breaking change that returns different values without raising an error; code depending on the corner should use ipix2xy/xy2ang or ipix2polygon. (ipix2ang_center is now a synonym of ipix2ang.) See the CHANGELOG for all changes.

Why Use QLSC?

  • You have a large number of points or regions on a sphere that you want to subdivide into approximately equal area divisions.
  • You are an astronomer with a catalog that you want to perform cone searches on, but don't want to install a full database to do so.
  • You want a spherical projection scheme without all that polar singularity residue.
  • You want a scheme that divides a sphere into regions that are easy to conceptualize, draw, and work with, where you can easily control the resolution down to ~0.08 square milliarcseconds per pixel (about 0.28 mas, or 280 µas, on a side).

Installation

Install from PyPi

pip install qlsc

Install from source

git clone https://github.com/demitri/qlsc.git
cd qlsc/source
python setup.py install

API Documentation

API documentation can be found here: https://qlsc.readthedocs.io/en/latest/

60 Second Segmentation Introduction

If you’re not familiar with the QLSC scheme it would be better to start below. There are two classes defined in this package, QLSC and QLSCIndex. The QLSC scheme projects each of the six faces of a cube onto a sphere. Segmentation is performed on the cube faces and performed in levels, where each level divides each bin into four. For example, depth=2 divides each bin into four, then of those into four again, which is 2^(2*depth) (i.e. 16) bins per cube face, or 96 total bins. ("Pixels" and "bin" are used somewhat interchangeably.)

The class can convert between (ra,dec) positions and the pixel number (ipix).

from qlsc import QLSC

q = QLSC(depth=2)
q.ang2ipix(45,45)     # ra,dec -> ipix number
q.ipix2ang_center(42) # ipix number -> ra,dec at center of pixel
q.ipix2polygon(42)    # returns the points on the sphere describing
                      # the pixel (joined by great circles)

60 Second Cone Search Introduction

Performing cone searches is as simple as creating a QLSCIndex index object and populating it with coordinates. The highest sphere segmentation resolution is selected by default, but a lower resolution can be chosen. By default, the index is stored in memory, but a file may be chosen so that the index can be reused as a script is rerun or by multiple scripts. Points are added in α,δ coordinates in degrees. Once points are added to the index you can perform a cone search.

The example below uses an included function to generate any number of points evenly distributed on a sphere (useful for testing).

from qlsc import QLSCIndex
from qlsc.generate import sunflower_points_on_sphere

index = QLSCIndex() # created in memory, defaults to highest resolution

# add ra,dec points from a NumPy array (shape (n,2))
points = sunflower_points_on_sphere(n=1e5)
index.add_points(points=points)

# or individually
index.add_point(12., 34.)

# keys can also be attached to points
index.add_point(56.59625, 78.00866, key="a catalog ID")

# cone search at center α=56, δ=-40
matches = index.radial_query(ra=56., dec=-40., radius=0.5) # float deg or astropy.units.Quantity

If astropy is installed, an astropy.coordinates.SkyCoord (scalar or array, in any frame — it is transformed to ICRS) may be given anywhere an ra,dec pair goes, and return_skycoord=True asks for one back:

from astropy.coordinates import SkyCoord
import astropy.units as u

index.add_point(SkyCoord(l=121.17*u.deg, b=-21.57*u.deg, frame="galactic"), key="M31")
matches = index.radial_query(SkyCoord(ra=10.68*u.deg, dec=41.27*u.deg), radius=1*u.deg,
                             return_skycoord=True)

Further examples are provided in the directory "examples" that show how the scheme and indexing can be used.

The Quadrilateralized Spherical Cube

The quadrilateralized spherical cube was first devised in 1975 in a technical report by F.K. Chan and E.M. O'Neill (available here). It begins with a cube inscribed within a sphere:

The six cube faces are then projected onto the sphere via transforms defined in the paper. This is the lowest resolution. Higher resolutions are achieved in steps: each step divides the bin, or pixel, into four. The first step will have four bins per face, the next will have 16, etc. The code refers to the level of subdivision as "depth" (number of times the bin has been subdivided), where the diagram above is depth=0. The Q3C PostgreSQL extension uses a depth of 30, which is 1,152,921,504,606,846,976 bins per cube face (six times that over the full sphere) corresponding to a mean pixel area of ~0.08 square milliarcseconds — about 0.28 mas (280 µas) on a side. The QLSC package supports any depth from 0 to 30.

Cube Face Number RA Range Face Center
0 top face α = 0°, δ = 90°
1 -45° ≤ δ ≤ 45° α = 0°, δ = 0°
2 45° ≤ δ ≤ 135° α = 90°, δ = 0°
3 135° ≤ δ ≤ 225° α = 180°, δ = 0°
4 225° ≤ δ ≤ 315° α = 270°, δ = 0°
5 bottom face α = 0°, δ = -90°

The diagram below shows the pixel numbering scheme over the entire sphere for depth=2. Note the path of the numbering scheme: this is called z-order which has the benefit that ipix numbers that are close to one another are also close spatially.

The diagram below shows face 1 divided at depth=2 and the projection of each pixel onto the sphere. Other faces have been hidden for clarity.

The scheme is advantageous in that there are no discontinuities at the poles, and the indexing scheme is optimized for fast database queries. The pixels are not, however, equal area. The largest pixel of a cube face, at its centre, is 3√3 ≈ 5.196 times the smallest, at a cube vertex; the ratio grows with depth and has essentially reached that limit by depth=12 (it is already a factor of 2.5 at depth=2). Against the mean pixel area, 4π / nbins, individual pixels run from about 1.91× down to 0.37×. Use QLSC.ipix_area() where the area of a particular pixel matters; the areas of all the pixels of a scheme sum to 4π.

For most users, working in ipix values and ra,dec coordinates will accomplish most anything needed. For those who might be performing more complex calculations, I recommend working in the native coordinates of the face plane. Each face (at any division level) has its 2D coordinate system origin at the cube face center. Both the x and y axes range from -1 to +1. Methods are provided to translate between xy coordinates, ipix value, and ra/dec coordinates. The source code for the ipix2polygon() method provides an illustrative example.

References & Links

Appendix

For quick reference, the following table lists the parameters of the QLSC scheme for every available depth.

The areas are mean areas — 4π / no. bins on sphere — since the pixels are not equal area; see the note above for the spread. The middle column is square milliarcseconds; an earlier version of this table labelled it square microarcseconds, which is off by a factor of 10⁶. The bin counts at depths 24 and above were also rounded to 15 significant figures; they are exact powers of two and are now written out in full.

Depth No. Bins per Cube Face No. Bins on Sphere Mean Bin Area (sr) Mean Bin Area (square mas) Mean Bin Area (square deg)
0 1 6 2.094395E+00 8.910640E+16 6.875494E+03
1 4 24 5.235988E-01 2.227660E+16 1.718873E+03
2 16 96 1.308997E-01 5.569150E+15 4.297183E+02
3 64 384 3.272492E-02 1.392287E+15 1.074296E+02
4 256 1,536 8.181231E-03 3.480719E+14 2.685740E+01
5 1,024 6,144 2.045308E-03 8.701797E+13 6.714349E+00
6 4,096 24,576 5.113269E-04 2.175449E+13 1.678587E+00
7 16,384 98,304 1.278317E-04 5.438623E+12 4.196468E-01
8 65,536 393,216 3.195793E-05 1.359656E+12 1.049117E-01
9 262,144 1,572,864 7.989483E-06 3.399139E+11 2.622793E-02
10 1,048,576 6,291,456 1.997371E-06 8.497848E+10 6.556982E-03
11 4,194,304 25,165,824 4.993427E-07 2.124462E+10 1.639245E-03
12 16,777,216 100,663,296 1.248357E-07 5.311155E+09 4.098114E-04
13 67,108,864 402,653,184 3.120892E-08 1.327789E+09 1.024528E-04
14 268,435,456 1,610,612,736 7.802230E-09 3.319472E+08 2.561321E-05
15 1,073,741,824 6,442,450,944 1.950557E-09 8.298680E+07 6.403302E-06
16 4,294,967,296 25,769,803,776 4.876394E-10 2.074670E+07 1.600826E-06
17 17,179,869,184 103,079,215,104 1.219098E-10 5.186675E+06 4.002064E-07
18 68,719,476,736 412,316,860,416 3.047746E-11 1.296669E+06 1.000516E-07
19 274,877,906,944 1,649,267,441,664 7.619365E-12 3.241672E+05 2.501290E-08
20 1,099,511,627,776 6,597,069,766,656 1.904841E-12 8.104180E+04 6.253225E-09
21 4,398,046,511,104 26,388,279,066,624 4.762103E-13 2.026045E+04 1.563306E-09
22 17,592,186,044,416 105,553,116,266,496 1.190526E-13 5.065112E+03 3.908266E-10
23 70,368,744,177,664 422,212,465,065,984 2.976314E-14 1.266278E+03 9.770664E-11
24 281,474,976,710,656 1,688,849,860,263,936 7.440786E-15 3.165695E+02 2.442666E-11
25 1,125,899,906,842,624 6,755,399,441,055,744 1.860197E-15 7.914238E+01 6.106665E-12
26 4,503,599,627,370,496 27,021,597,764,222,976 4.650491E-16 1.978559E+01 1.526666E-12
27 18,014,398,509,481,984 108,086,391,056,891,904 1.162623E-16 4.946399E+00 3.816666E-13
28 72,057,594,037,927,936 432,345,564,227,567,616 2.906557E-17 1.236600E+00 9.541664E-14
29 288,230,376,151,711,744 1,729,382,256,910,270,464 7.266393E-18 3.091499E-01 2.385416E-14
30 1,152,921,504,606,846,976 6,917,529,027,641,081,856 1.816598E-18 7.728748E-02 5.963540E-15

About

A Python implementation of the quadrilateralized spherical cube scheme for segmenting and indexing on a sphere.

Resources

Stars

7 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages