Skip to main content

Chapter 3: Maps of the night sky

Chapter 3: Maps of the night sky

TipAt a glance
  • In this series: Part I, Places. Chapter 2 showed how positions on the Earth are drawn on a flat map. The same methods work for positions in the sky, which are also given by two angles
  • You will build: A star chart of the sky over Jakarta at New Year’s midnight, the same for any place and time, a map of the Milky Way, and a map of the stars nearest to the Sun
  • You need: Chapter 0, for the environment, and section 5 of Chapter 1, for lines that work in every shell
  • Time to run: The commands took about a minute when tested, including a download of 14 MB
  • Level: Beginner; some lines use school trigonometry, which is explained

The position of every star in the sky is given by two angles. They are used like longitude and latitude, so GDAL can process the stars as a map layer. This chapter takes a table of 119,614 stars, which contains no geometry, and makes a map of the night sky over Jakarta from it.

Once the stars are points, the same table can answer other questions. In which direction is Sirius? From which latitude can you see the most stars? Where is the Milky Way? Which stars are nearest to the Sun?

NoteLearning objectives

By the end of this chapter you will be able to:

  • turn a table without geometry into a point layer, with a coordinate system that is not for the Earth;
  • read a compressed CSV from the web, and give its numbers the right type;
  • use a map projection for data that are not on the Earth;
  • draw symbols with size and colour from attributes, using buffers;
  • write a query that is easy to change, with its inputs in one place;
  • test a calculation with values from an official source, and explain the difference that remains.

Before running this chapter, do section 1 of Chapter 0 and section 5 of Chapter 1. In a terminal, make a new empty folder and copy into it the files of sample-data/ch03. In this notebook, the next cell does that.

import sys
import time
from pathlib import Path

# The repository root, whether the notebook runs from en/, id/ or the root itself
ROOT = Path.cwd() if (Path.cwd() / "tutorial.yml").exists() else Path.cwd().parent
sys.path.insert(0, str(ROOT / "src"))
import oneline

T0 = time.perf_counter()
WORK = oneline.start("03")
print("working folder:", WORK.name, "| files copied there:", sorted(f.name for f in (ROOT / "sample-data/ch03").iterdir()))
working folder: ch03 | files copied there: ['bright.csv', 'sky.txt', 'usno.csv']

Warm-up

A few short commands first, in the form of Chapter 0. The file bright.csv has the 1,615 brightest stars, a small part of the catalogue of section 1 (ledger L17). Each row has a name, a constellation, a position in the sky as lon and lat, and a brightness. The file has no geometry, only columns with text and numbers:

gdal vector info bright.csv --summary

gdal vector make-point bright.csv bright.gpkg --x lon --y lat --overwrite
INFO: Open of `bright.csv'
      using driver `CSV' successful.
1: bright (None)

make-point takes two columns and makes a point of each row. The next lines make a grid for the whole sky, and count the stars within 10 degrees of each cell:

gdal vector grid count bright.gpkg crowd.tif --zfield mag --extent -180,-90,180,90 --size 720,360 --radius 10 --overwrite

gdal raster edit crowd.tif --nodata none

gdal raster scale crowd.tif crowd.png --ot Byte --overwrite

The sky as a wide rectangle in grey, made of overlapping discs. A brighter band in the form of a wave crosses it.

The second line keeps the cells without any star black. Otherwise they would be transparent.

Look before moving on. A bright band in the form of a wave crosses the picture. It shows where many stars are close together in the sky. This is the Milky Way, calculated with a few short commands from a text file.

The rest of this chapter makes such points from the whole catalogue, and uses them for more difficult questions.

1. A catalogue from the web

The HYG catalogue combines the stars of three older catalogues: Hipparcos, the Yale Bright Star Catalog and the Gliese catalogue of nearby stars. It is a CSV file compressed with gzip, under the licence CC BY-SA 4.0 (ledger L17).

The next line downloads about 14 MB. It took less than a minute when tested.

ogr2ogr -overwrite sky.gpkg "CSV:/vsigzip//vsicurl/https://codeberg.org/astronexus/hyg/media/branch/main/data/hyg/CURRENT/hyg_v44.csv.gz" -oo AUTODETECT_TYPE=YES -nln stars

Three things are new here:

  • /vsigzip/ unpacks a .gz file while it is read, as /vsizip/ did for a zip;
  • CSV: at the beginning names the format, because GDAL does not recognise it from the ending .csv.gz;
  • -oo passes an option to the format that is being read. A CSV file contains only text. With AUTODETECT_TYPE=YES, GDAL checks the values and stores numbers as numbers.

The last point is important for the next steps. As text, “10” comes before “9”, and a comparison such as mag<6 would compare letters. Now list the brightest stars, where id>0 leaves out the Sun:

ogr2ogr -f CSV /vsistdout/ sky.gpkg -sql "select proper, con, ra, dec, mag, ci, round(dist*3.262,1) light_years from stars where id>0 order by mag limit 8"
proper,con,ra,dec,mag,ci,light_years
Sirius,CMa,6.752481,-16.716116,-1.44,0.009,8.6
Canopus,Car,6.399195,-52.69566,-0.62,0.164,309.2
Arcturus,Boo,14.26103,19.18241,-0.05,1.239,36.7
Rigil Kentaurus,Cen,14.660765,-60.833976,-0.01,0.71,4.3
Vega,Lyr,18.61564,38.783692,0.03,-0.001,25
Capella,Aur,5.27815,45.997991,0.08,0.795,42.8
Rigel,Ori,5.242298,-8.20164,0.18,-0.03,863
Procyon,CMi,7.655033,5.224993,0.4,0.432,11.5

Look before moving on. These are the columns the chapter uses (ledger L17):

Column Meaning
proper, con the star’s name, if it has one, and its constellation
dec declination: degrees north or south of the celestial equator, like latitude
ra right ascension: the angle around the sky, like longitude, but counted in hours from 0 to 24
mag magnitude, how bright the star looks. Smaller is brighter: Sirius is the first row, with a negative number
ci colour index, the blue magnitude minus the visual one. Since smaller is brighter, a small ci means a blue star and a large one an orange or red star
dist distance in parsecs; one parsec is 3.262 light years

2. From a table to points

To use right ascension as a longitude, convert hours into degrees: 24 hours are 360 degrees, so one hour is 15. Then convert the result to the usual range from -180 to 180.

ogr2ogr -update -overwrite sky.gpkg sky.gpkg -sql "select proper, con, mag, ci, makepoint(mod(ra*15+180,360)-180, dec) geom from stars where id>0 and mag<6" -nln bright -nlt POINT -a_srs "+proj=longlat +R=1000 +no_defs"

ogr2ogr -update -overwrite sky.gpkg "/vsicurl/https://raw.githubusercontent.com/ofrohn/d3-celestial/master/data/constellations.lines.json" -nln figures -nlt PROMOTE_TO_MULTI -s_srs "+proj=longlat +R=1000 +no_defs" -t_srs "+proj=longlat +R=1000 +no_defs" -wrapdateline -datelineoffset 40

ogr2ogr -f CSV /vsistdout/ sky.gpkg -sql "select (select count(*) from bright) stars, (select count(*) from figures) constellation_figures"
stars,constellation_figures
5019,89

The first line keeps the stars brighter than magnitude 6, a limit chosen for this chapter. The second adds the lines that join stars into constellations, from the d3-celestial project, whose coordinates already follow the same rule (ledger L18).

Lines that cross 180 degrees. A constellation that crosses 180 degrees would be drawn with a line across the whole map. -wrapdateline cuts such lines at 180 degrees. It only checks shapes near that line, by default within 10 degrees of it, and -datelineoffset 40 increases this distance (ledger L25). With the default, six of eight constellation lines that cross it were left uncut. The option works during a reprojection, which is why the line names the same coordinate system as source and target.

A coordinate system for the sky. +proj=longlat +R=1000 +no_defs describes longitude and latitude on a sphere with a radius of 1,000. The sky has no radius. The number was chosen only because it makes the next steps easy to read. Do not label such data EPSG:4326: a program would then draw Sirius in the Indian Ocean, south of Sumatra, at 101 E 17 S.

Now draw the whole sky like a map of the world:

gdal pipeline read sky.gpkg ! sql "select 2 v, st_buffer(geom,0.12) geom from figures union all select * from (select case when ci is null then 8 when ci<0 then 3 when ci<0.3 then 4 when ci<0.8 then 5 when ci<1.3 then 6 else 7 end, st_buffer(geom, 0.14*(1.2+pow(1.55,5-mag))) from bright order by mag desc)" ! rasterize --attribute-name v --init 1 --ot Byte --size "5400,2700" --extent "-180,-90,180,90" ! edit --nodata none ! color-map --color-map sky.txt ! materialize ! resize --size "1800,900" -r average ! write allsky.png --overwrite

The whole sky as a rectangle on dark blue: coloured dots for stars, joined by thin lines into constellations. The largest dot, Sirius, is right of the centre, with Orion above it to the left.

The pipeline is the one of Chapter 1, with three additions:

  • A symbol is a buffer. st_buffer makes a disc around each star. Its radius is calculated from the magnitude, so brighter stars are larger. With order by mag desc, faint stars are drawn first and bright ones on top.
  • Smooth edges. The picture is drawn three times larger than needed, and resize ... -r average reduces its size at the end. Each final cell is then the mean of nine, which removes the jagged edges.
  • materialize finishes the coloured picture before the next step starts. Without it, GDAL first reduces the layer of class numbers and then colours the result. The mean of a white star (4) and the dark sky (1) is then not a colour between the two, but class 2 or 3, which has the colour of something else (ledger L28).

With --init 1, all cells have the dark blue of the sky at first. East is on the right here, as on a globe of the stars that you look at from outside. In section 4, east is on the left. The colours in sky.txt follow the colour index in five steps, from blue to orange. They were chosen for this drawing, and they are not the colours that your eyes would see.

3. Which point of the sky is directly above you?

The sky seems to turn during the night, so a chart needs a place and a moment. Astronomers combine both in one number, the local sidereal time. It is the right ascension of the stars that are on your meridian at this moment. The meridian is the line in the sky from north to south through the point directly above you. The sidereal time and your latitude together give the position of this point.

The US Naval Observatory (USNO) publishes a formula for it (ledger L19). SQLite can calculate it, because julianday() counts days, and takes times in UTC (ledger L20). New Year’s midnight in Jakarta, which is seven hours ahead of UTC, is 17:00 UTC on 31 December:

ogr2ogr -f CSV /vsistdout/ sky.gpkg -sql "select round(h,4) sidereal_hours, round(15*h,2) sidereal_degrees from (select mod(6.697375 + 0.065709824279*(julianday(date(t))-2451545) + 24*1.0027379*(julianday(t)-julianday(date(t))) + lon/15, 24) h from (select '2026-12-31 17:00' t, 106.8 lon))"
sidereal_hours,sidereal_degrees
6.7956,101.93

Look before moving on. USNO’s own calculator gives 6 h 47 min 44.3 s for the same place and time (ledger L19), which is 6.7956 hours. The result of the line is the same in all four decimals.

So the point directly above Jakarta has a right ascension of 101.93 degrees. Its declination is the latitude of Jakarta, -6.2.

4. The sky over Jakarta

The stars can be drawn on a sphere, as places are drawn on a globe. A chart of the sky for one place is then a map of this sphere, with the point directly above the place in the centre. This point is called the zenith. A stereographic projection (+proj=stere) centred on the zenith makes such a map, and the horizon becomes a circle around it:

ogr2ogr -update -overwrite sky.gpkg sky.gpkg bright -t_srs "+proj=stere +lat_0=-6.2 +lon_0=101.93 +R=1000 +axis=wnu" -nln up_bright

ogr2ogr -update -overwrite sky.gpkg sky.gpkg figures -t_srs "+proj=stere +lat_0=-6.2 +lon_0=101.93 +R=1000 +axis=wnu" -nln up_figures
Part Meaning
+lat_0=-6.2 +lon_0=101.93 the centre of the chart: the latitude, and the sidereal time in degrees from section 3
+R=1000 the same sphere as before. In this projection, a point 90 degrees from the centre lies at twice the radius, so the horizon is a circle of 2,000
+axis=wnu x counts towards the west (ledger L20)

The last part is important. A map shows the ground from above, with east on the right. We see the sky from below. If you lie on your back with your head to the north, east is on your left. Without +axis=wnu, every constellation would be mirrored.

Now draw the sky as a disc, the constellation lines cut at the horizon, and the stars inside it:

gdal pipeline read sky.gpkg ! sql "select 1 v, st_buffer(makepoint(0,0),2000,90) geom union all select 2, st_buffer(st_intersection(geom, st_buffer(makepoint(0,0),2000,90)),2) from up_figures union all select * from (select case when ci is null then 8 when ci<0 then 3 when ci<0.3 then 4 when ci<0.8 then 5 when ci<1.3 then 6 else 7 end, st_buffer(geom, 1.8*(1.2+pow(1.55,5-mag))) from up_bright where st_x(geom)*st_x(geom)+st_y(geom)*st_y(geom)<2000*2000 order by mag desc)" ! rasterize --attribute-name v --init 0 --ot Byte --size "4200,4200" --extent "-2060,-2060,2060,2060" ! edit --nodata none ! color-map --color-map sky.txt ! materialize ! resize --size "1400,1400" -r average ! write jakarta.png --overwrite

Round star chart on dark blue with constellation lines. The large white star Sirius is just below the centre, with Orion above it to the right and its orange star Betelgeuse. Canopus is further down, and the small cross of Crux is near the lower left edge.

Look before moving on. The centre is the point directly above, the edge is the horizon, north is at the top and east is on the left. Sirius, the largest dot, is close to the centre. Orion is above it to the right. Canopus is the large white dot further down, and the Southern Cross is low in the south-east, at the lower left.

The chart shows stars only. The planets and the Moon change their positions among the stars and are not in the catalogue. On that night Mars and Jupiter are also above the horizon (ledger L21).

5. In which direction is Sirius?

To find a star in the sky, you need two numbers: its altitude above the horizon, and its azimuth, the compass direction counted from north through east. Both are calculated with trigonometry on the sphere, from the declination d, the latitude p, and the hour angle h, the angle by which the star has passed the meridian:

ogr2ogr -f CSV /vsistdout/ sky.gpkg -sql "select proper star, mag, round(degrees(asin(sin(p)*sin(d)+cos(p)*cos(d)*cos(h))),1) altitude, round(mod(degrees(atan2(-cos(d)*sin(h), sin(d)*cos(p)-cos(d)*cos(h)*sin(p)))+360,360),1) azimuth from (select proper, mag, radians(st_y(geom)) d, radians(101.93-st_x(geom)) h, radians(-6.2) p from bright where mag<1 and proper<>'') where altitude>0 order by altitude desc"
star,mag,altitude,azimuth
Sirius,-1.44,79.5,183.4
Procyon,0.4,72.8,48.7
Betelgeuse,0.45,71.1,315.9
Rigel,0.18,66.8,263.6
Aldebaran,0.87,50.3,305.3
Canopus,-0.62,43.2,184.9
Capella,0.08,34,341.1
Achernar,0.45,12,212.7
Acrux,0.77,7.9,152.9

The inner query gives the names d, h and p, so the outer query looks almost like the formula in a textbook. SQLite also allows a name given in the select part, here altitude, in the where part.

Is it right? USNO lists altitudes and azimuths of the navigation stars for any place and minute. The file usno.csv has its values for this place and minute, for the 28 stars above the horizon (ledger L21). Load it, and measure the angle in the sky between the positions from the chapter and those of USNO:

ogr2ogr -update -overwrite sky.gpkg usno.csv -oo AUTODETECT_TYPE=YES -nln usno

ogr2ogr -f CSV /vsistdout/ sky.gpkg -sql "select count(*) stars, round(avg(gap),2) mean_gap, round(max(gap),2) largest_gap from (select degrees(acos(min(1, sin(radians(u.altitude))*sin(s.alt)+cos(radians(u.altitude))*cos(s.alt)*cos(radians(u.azimuth)-s.az)))) gap from usno u join (select upper(proper) name, asin(sin(p)*sin(d)+cos(p)*cos(d)*cos(h)) alt, atan2(-cos(d)*sin(h), sin(d)*cos(p)-cos(d)*cos(h)*sin(p)) az from (select proper, radians(st_y(geom)) d, radians(101.93-st_x(geom)) h, radians(-6.2) p from bright)) s on s.name=upper(u.star))"
stars,mean_gap,largest_gap
28,0.3,0.38

Look before moving on. All 28 names were found, and the largest gap is 0.38 degrees. This is accurate enough to find a star. But the gap is much larger than the rounding of the numbers, so it must have another cause.

The catalogue gives positions for the year 2000, and USNO gives them for the date. In between, the direction of the Earth’s equator has slowly changed, which is called precession (ledger L22), and the grid of right ascension and declination has turned with it. If the positions of the catalogue are first converted to the date, the largest gap is 0.008 degrees (ledger L22). So the formulas are right, and the gap comes from the different dates of the coordinates. This chapter does not correct it, because it is too small to be important on a chart of the sky.

6. Any place, any night

Sections 3 and 4 needed three steps, and a number was copied by hand from one to the next. The next line does everything: sidereal time, projection and drawing. The place and the time are at the beginning of the query, so they are easy to change. This example is for Reykjavik, at 23:00 UTC on 21 December 2026:

gdal pipeline read sky.gpkg ! sql "with place as (select 64.1 lat, -21.9 lon, '2026-12-21 23:00' t), now as (select radians(lat) p, 15*(6.697375 + 0.065709824279*(julianday(date(t))-2451545) + 24*1.0027379*(julianday(t)-julianday(date(t))))+lon lst from place), s as (select mag, ci, p, radians(st_y(geom)) d, radians(lst-st_x(geom)) h from bright, now), z as (select mag, ci, cos(d)*sin(h) x, cos(p)*sin(d)-sin(p)*cos(d)*cos(h) y, sin(p)*sin(d)+cos(p)*cos(d)*cos(h) up from s) select 1 v, st_buffer(makepoint(0,0),2000,90) geom union all select * from (select case when ci is null then 8 when ci<0 then 3 when ci<0.3 then 4 when ci<0.8 then 5 when ci<1.3 then 6 else 7 end, st_buffer(makepoint(2000*x/(1+up), 2000*y/(1+up)), 1.8*(1.2+pow(1.55,5-mag))) from z where up>0 order by mag desc)" ! rasterize --attribute-name v --init 0 --ot Byte --size "4200,4200" --extent "-2060,-2060,2060,2060" ! edit --nodata none ! color-map --color-map sky.txt ! materialize ! resize --size "1400,1400" -r average ! write reykjavik.png --overwrite

Round star chart without lines. Sirius is the largest dot, at the edge in the lower left. Orion stands above and to the right of it, low in the sky. The brightest star near the centre is the pale yellow Capella.

The query has several steps, and with gives each step a name:

  1. place contains the three inputs: latitude, longitude and time in UTC.
  2. now converts them into a latitude in radians and the sidereal time.
  3. s adds the hour angle of every star.
  4. z calculates the position of each star on a sphere around you: x towards the west, y towards the north and up towards the zenith.
  5. The final select projects the sphere onto a flat map. Dividing by 1+up is the stereographic projection, the same one that PROJ used in section 4, and up>0 keeps what is above the horizon.

Look before moving on. Sirius is high in the sky over Jakarta. In Reykjavik it is on the horizon, and Orion is low in the sky.

Change the three inputs to a place and a time of your own, for example the next night you spend outdoors. Remember that the time is UTC: for WIB subtract seven hours, for WITA eight and for WIT nine.

7. Where on Earth can you see the most stars?

Every star that can rise at a place does so once a day. From latitude p in the north, a star rises if its declination is greater than p-90, and it never sets if its declination is greater than 90-p. A small recursive query makes a list of latitudes, and two counts give the numbers for each of them:

ogr2ogr -f CSV /vsistdout/ sky.gpkg -sql "with recursive place(p) as (select 0 union all select p+15 from place where p<90) select p latitude, (select count(*) from bright where st_y(geom)>p-90) can_be_seen, round(100.0*(select count(*) from bright where st_y(geom)>p-90)/(select count(*) from bright)) percent, (select count(*) from bright where st_y(geom)>90-p) never_set from place"
latitude,can_be_seen,percent,never_set
0,5019,100,0
15,4935,98,78
30,4613,92,314
45,4114,82,743
60,3537,70,1236
75,2964,59,1838
90,2385,48,2385

Look before moving on. On the equator every star rises, and none stays above the horizon all day. At the pole, about half of the stars are never above the horizon, and the others never set. The count ignores trees, mountains and haze near the horizon.

Jakarta is at 6.2 degrees south, so only the stars in a small circle around the north celestial pole are never visible there. Which are the brightest of them?

ogr2ogr -f CSV /vsistdout/ sky.gpkg -sql "select proper star, con, mag, round(st_y(geom),1) declination from bright where st_y(geom)>90-6.2 order by mag limit 3"
star,con,mag,declination
Polaris,UMi,1.97,89.3
Tarandus,Cep,4.24,86.3
Yildun,UMi,4.35,86.6

The brightest star that is never visible from Jakarta is Polaris, the North Star.

8. Where is the Milky Way?

The Milky Way is a band of stars across the sky: the disc of our own galaxy, seen from inside (ledger L24). In the chart of section 2 the band is a curve, because that chart follows the Earth’s equator. To draw it as a straight band, use a grid whose equator is the plane of the galaxy.

PROJ can rotate the sphere before the projection, with +proj=ob_tran (ledger L23). It needs the position of the old pole in the new grid. The position of the galaxy’s north pole, right ascension 192.85948 and declination 27.12825, comes from the documentation of ESA’s Gaia mission (ledger L23). First a test with a known result: that point must get the latitude 90.

gdaltransform -output_xy -s_srs "+proj=longlat +R=1000 +no_defs" -t_srs "+proj=ob_tran +o_proj=longlat +o_lon_p=122.93192 +o_lat_p=27.12825 +lon_0=12.85948 +R=1000" -coord 192.85948 27.12825
32.93192 90

The second number is 90, as expected. Before drawing, calculate a table. For each colour of star, what percentage lies within 10 degrees of the plane of the galaxy? If the stars were spread evenly, it would be 17.4 percent, the sine of 10 degrees.

ogr2ogr -f CSV /vsistdout/ sky.gpkg -sql "select colour, count(*) stars, round(100.0*sum(abs(b)<10)/count(*),1) percent_near_plane from (select case when ci<0 then '1 blue' when ci<0.6 then '2 white' when ci<1.2 then '3 yellow' else '4 orange' end colour, degrees(asin(sin(radians(dec))*sin(radians(27.12825))+cos(radians(dec))*cos(radians(27.12825))*cos(radians(ra*15-192.85948)))) b from stars where id>0 and ci is not null) group by colour"
colour,stars,percent_near_plane
1 blue,6894,46.6
2 white,50362,25.1
3 yellow,39323,16.2
4 orange,21144,19.4

Look before moving on. The blue stars are concentrated near the plane: nearly half of them lie in a strip that covers about a sixth of the sky. The yellow and orange stars of this catalogue are spread almost evenly. So the next map shows only the blue ones, in the equal-area Mollweide projection:

ogr2ogr -update -overwrite sky.gpkg sky.gpkg -sql "select mag, con, makepoint(mod(ra*15+180,360)-180, dec) geom from stars where id>0 and ci<0" -s_srs "+proj=longlat +R=1000 +no_defs" -t_srs "+proj=ob_tran +o_proj=moll +o_lon_p=122.93192 +o_lat_p=27.12825 +lon_0=12.85948 +R=1000 +axis=wnu" -nln blue -nlt POINT

gdal pipeline read sky.gpkg ! sql "select 1 v, scalecoords(st_buffer(makepoint(0,0),1,90),2828.4,1414.2) geom union all select 2, st_buffer(makeline(makepoint(-2828,0),makepoint(2828,0)),4) union all select 3, st_buffer(geom, 6+2*pow(1.5,5-mag)) from blue" ! rasterize --attribute-name v --init 0 --ot Byte --size "4320,2160" --extent "-2880,-1440,2880,1440" ! edit --nodata none ! color-map --color-map sky.txt ! materialize ! resize --size "1440,720" -r average ! write milkyway.png --overwrite

An oval map of the sky with thousands of light blue dots. They are dense along the horizontal line through the middle, most of all on the right half, and there are fewer towards the top and bottom. A small dense group lies below the line near the right edge.

Look before moving on. The straight line is the plane of the galaxy, and the blue stars are densest near it. The dense group below the line near the right edge is Orion (ledger L26).

The map also depends on the catalogue. HYG contains the stars of three catalogues, and not every star that exists. An empty area in this picture can be an area with few stars, or one that the old catalogues covered less well. The table and the picture describe this catalogue only.

9. The stars nearest to the Sun

Until now, the maps used only the direction of each star. The catalogue also contains the distance of most of them, and gives x, y and z in parsecs with the Sun at the centre. So the axes of a map can also be in light years. First list the nearest stars:

ogr2ogr -f CSV /vsistdout/ sky.gpkg -sql "select coalesce(nullif(proper,''),gl) star, con, round(dist*3.262,2) light_years, mag from stars where id>0 order by dist limit 8"

ogr2ogr -f CSV /vsistdout/ sky.gpkg -sql "select count(*) within_20_light_years, sum(mag<6) brighter_than_magnitude_6 from stars where id>0 and dist*3.262<20"
star,con,light_years,mag
Proxima Centauri,Cen,4.23,11.01
Toliman,Cen,4.32,1.35
Rigil Kentaurus,Cen,4.32,-0.01
Barnard's Star,Oph,5.95,9.54
Wolf 359,Leo,7.8,13.45
Lalande 21185,UMa,8.31,7.49
Gl 65A,Cet,8.57,12.57
Gl 65B,Cet,8.57,12.7
within_20_light_years,brighter_than_magnitude_6
108,19

Look before moving on. Most of the nearest stars are too faint for the chart of section 4: only 19 of 108 are brighter than magnitude 6. Now draw a map of them, as seen from above the north pole, with a ring every five light years:

gdal pipeline read sky.gpkg ! sql "with recursive ring(r) as (select 5 union all select r+5 from ring where r<20) select 1 v, st_buffer(makepoint(0,0),20,90) geom union all select 2, st_buffer(exteriorring(st_buffer(makepoint(0,0),r,90)),0.04) from ring union all select * from (select case when ci is null then 8 when ci<0 then 3 when ci<0.3 then 4 when ci<0.8 then 5 when ci<1.3 then 6 else 7 end, st_buffer(makepoint(x*3.262,y*3.262), 0.12+0.05*max(0,15-absmag)) from stars where dist*3.262<20 order by absmag desc)" ! rasterize --attribute-name v --init 0 --ot Byte --size "3600,3600" --extent "-20.5,-20.5,20.5,20.5" ! edit --nodata none ! color-map --color-map sky.txt ! materialize ! resize --size "1200,1200" -r average ! write neighbours.png --overwrite

A round map with four rings. The Sun is the pale yellow dot in the centre, with the stars of Alpha Centauri close by to the lower left. Most dots are small and orange. The white dots of Sirius, above the centre, and Altair, at the bottom right, are larger.

The Sun is the dot in the middle, since this time id>0 is missing. The size comes from absmag, how bright a star would look from a fixed distance of 10 parsecs, so it shows the star itself and not how near it is. Grey dots have no colour index in the catalogue.

Look before moving on. The map has two dimensions, and space has three. A star far above or below the plane of the map appears closer to the centre than it is. Sirius is 8.6 light years away, and on the map it is a little closer to the Sun than that.

Try it yourself

  • The sky over your town. Change place in section 6. For a southern latitude write a minus sign, as in -6.2.
  • One constellation. Add and con='Sco' to the query of section 2 and draw Scorpius alone.
  • More stars around the Sun. Change 20 to 50 light years in section 9, and the extent with it.
  • A mirrored chart. Leave out +axis=wnu in section 4, and compare with the sky outside.

Checkpoint

The cell below stops with an error if the results are not what this chapter expects. The last test compares the two ways of making the chart: PROJ in section 4, and the formulas of section 6 with the same centre.

import csv
import io

def rows(sql, dataset="sky.gpkg"):
    """The rows of a query, as dictionaries."""
    return list(csv.DictReader(io.StringIO(oneline.run(f'ogr2ogr -f CSV /vsistdout/ {dataset} -sql "{sql}"'))))

n = int(rows("select count(*) n from stars")[0]["n"])
assert n > 119_000, "the catalogue should have more than 119,000 rows"

sky = rows("select proper, st_x(geom) x, st_y(geom) y from bright where proper<>''")
up = {r["proper"]: (float(r["x"]), float(r["y"])) for r in rows("select proper, st_x(geom) x, st_y(geom) y from up_bright where proper<>''")}

from math import cos, hypot, radians, sin
p, worst = radians(-6.2), 0
for r in sky:
    d, h = radians(float(r["y"])), radians(101.93 - float(r["x"]))
    z = sin(p) * sin(d) + cos(p) * cos(d) * cos(h)
    if z > 0:
        x, y = 2000 * cos(d) * sin(h) / (1 + z), 2000 * (cos(p) * sin(d) - sin(p) * cos(d) * cos(h)) / (1 + z)
        worst = max(worst, hypot(x - up[r["proper"]][0], y - up[r["proper"]][1]))
assert worst < 0.01, "PROJ and the formulas should place the stars at the same spots"

pictures = ("allsky.png", "jakarta.png", "reykjavik.png", "milkyway.png", "neighbours.png")
assert all((WORK / f).stat().st_size > 10_000 for f in pictures)
print(f"checkpoint passed: {n:,} rows; PROJ and the formulas differ by at most {worst:.6f} on a chart 4,000 wide")
print(f"time since the first cell: {time.perf_counter() - T0:.0f} s")
checkpoint passed: 119,614 rows; PROJ and the formulas differ by at most 0.000000 on a chart 4,000 wide
time since the first cell: 75 s

Troubleshooting

Symptom Cause Fix
The picture has no stars, or stars are in the wrong order The numbers were read as text Add -oo AUTODETECT_TYPE=YES to the first line
The constellations are mirrored x counts to the east, as on a map of the ground Add +axis=wnu (section 4)
The chart is turned by some hours The time was local and not UTC Subtract the difference between your time and UTC, for example 7 hours for WIB
The given file is not recognized for the .csv.gz address GDAL did not recognise the format Keep CSV: in front of the address
A reprojection reports a point outside the projection The point exactly opposite the centre has no position in a stereographic map Keep only the stars you need with a where on the declination

What comes next

In this chapter, the axes of a map were two angles in the sky, and in section 9 they were distances in light years. In Chapter 4: Earthquakes by depth the y axis is the depth, and earthquakes from the web show the plate under Java from the side.

Star data: HYG v4.4 by astronexus, CC BY-SA 4.0. The star maps on this page are made from it and are under the same licence. Constellation lines: d3-celestial, copyright 2015 Olaf Frohn, BSD 3-Clause.