nord-sahara-wgs84: converting Algeria's Nord Sahara 1959 datum to WGS 84 by hand
Open an old Algerian map, a cadastral sheet or a well report, and the coordinates on it are very likely in Nord Sahara 1959, the classic Algerian datum built on the Clarke 1880 (RGS) ellipsoid. Your phone, your GPS receiver and almost every web map speak WGS 84. Put a Nord Sahara point on a WGS 84 map without converting it and it lands in the wrong place, by an amount large enough to matter.
The usual answer is "call PROJ and move on", and for production work that is the right answer. But I wanted to know what actually happens between the two sets of numbers. nord-sahara-wgs84 is the result: a small pure-Python library and CLI, with no runtime dependencies, that does the whole conversion step by step and checks every number against PROJ.
This is a learning project. The goal was to write the maths plainly enough to read, then prove it is right.
What it does
Datum conversion both ways. It converts between Nord Sahara 1959 (EPSG:4307) and WGS 84 (EPSG:4326), in either direction.
UTM on both datums. It also converts to and from the UTM zones 29N to 32N, on Nord Sahara 1959 (EPSG:30729 to 30732) and on WGS 84 (EPSG:32629 to 32632). You can go straight from a projected coordinate on one datum to a projected coordinate on the other.
Every published transformation. The EPSG dataset publishes no grid (NTv2 or similar) for Nord Sahara 1959, only Helmert parameter sets. The library includes all ten of them, with the values exactly as published. They are not equal:
- EPSG 10296 (the default) and 5660 are 7-parameter Position Vector transformations with a published accuracy of 1 m over Algeria.
- EPSG 1815 is a 7-parameter set for District 3, at 5 m.
- The rest are geocentric translations (3 parameters) with accuracies from 5 m for specific licence areas (Ahnet, Hassi Mouina) to 44 m for Algeria onshore and 100 m for In Amenas, Hassi Bir Reikaz and Ain Tsila.
The default, 10296, is also what PROJ ranks first for this pair. The --op auto option picks the most accurate operation whose area of use contains your point.
One sentence from the README is worth repeating: the published accuracy is how well the parameters reproduce the real relationship between the datums. It is not a property of the code.
A CLI with CSV batch mode. The ns59 command converts single points or a CSV stream, and passes extra columns through untouched.
How to use it
It needs Python 3.13 or later. Clone the repository and run it from the checkout with uv:
git clone https://github.com/AbdeldjalilChougui/nord-sahara-wgs84.git cd nord-sahara-wgs84 uv sync uv run ns59 convert --from ns59 --to wgs84 3.0588 36.7538 # lon lat # 3.058054140 36.753905322 uv run ns59 convert --from ns59-utm31 --to wgs84-utm31 -v 500000 4070000 uv run ns59 convert --from 30732 --to 4326 --op auto 712000 3530000 # best local operation printf "x,y,name\n500000,4000000,well-1\n" | uv run ns59 convert --from 30731 --to 32631 --csv uv run ns59 ops # published transformations uv run ns59 crs # supported CRSs From Python:
from nord_sahara_wgs84 import convert, ns59_to_wgs84 r = convert(500000, 4070000, "EPSG:30731", "EPSG:32631") # easting/northing in, out print(r.x, r.y, r.operation.name) lat, lon, h = ns59_to_wgs84(36.7538, 3.0588, op=1253) # pick an EPSG operation Watch the axis order. convert always takes x then y, so (lon, lat) or (easting, northing), like PROJ's always_xy=True. The lower-level ns59_to_wgs84 and wgs84_to_ns59 take (lat, lon).
CRSs can be named by EPSG code or by alias: ns59, wgs84, ns59-utm31, wgs84-utm32 and so on.
What I built from scratch
A datum conversion looks like one function call from outside. Inside, it is a pipeline of four steps, and I wrote each one.
- UTM to geographic, on the source ellipsoid, when the input is projected. Transverse Mercator uses Krüger's series to order n⁶ in Karney's (2011) formulation. That is nanometre-accurate inside the zone and sub-millimetre several degrees outside it.
- Geographic to geocentric (ECEF) on Clarke 1880 (RGS): a = 6 378 249.145 m, 1/f = 293.465. Latitude and longitude become X, Y, Z in metres from the centre of the ellipsoid.
- Helmert in ECEF. The EPSG 9606 method (position vector) uses the linearised rotation matrix, with rotations in arc-seconds and scale in ppm. The EPSG 9603 method uses translations only. The reverse direction applies the transposed matrix, as PROJ does.
- Geocentric back to geographic on WGS 84, with a Bowring start plus fixed-point iteration, and then to UTM if the target is projected.
What I learned
A datum shift happens in 3D, even for 2D points. You cannot shift latitude and longitude directly. You go through geocentric X, Y, Z, apply the Helmert there, and come back. As in EPSG and PROJ, these operations work in the geog2D domain: the input ellipsoidal height feeds the geocentric step but comes back unchanged. Heights are not transformed.
"Which transformation" is a real question. Before this project I thought of Nord Sahara to WGS 84 as one formula. It is ten published operations, with accuracies that range from 1 m to 100 m and areas of use that range from all of Algeria to a single licence block. Two tools that disagree by tens of metres may both be "right", using different operations.
Round trips are not exact, and that is expected. Results agree with PROJ to about 1e-12 degrees. But NS59 to WGS 84 and back to NS59 is only good to a few millimetres, because the height is not carried through the shift and the linearised rotation is not exactly orthogonal. PROJ behaves the same way. Finding that and understanding why taught me more than the cases that matched on the first try.
An oracle makes the work honest. pyproj and PROJ are used only in the tests, as the reference. uv run pytest runs 52 tests, including one doctest:
- every operation, forward and reverse, against PROJ at 200 random points across Algeria each (tolerance 1e-9 degrees, about 0.1 mm)
- geocentric conversions on both ellipsoids against
+proj=cart(1 µm) - UTM 29N to 32N on both datums against PROJ, inside and up to 3 degrees outside each zone (1 mm), plus inverse round trips
- projected to projected across datums against a PROJ pipeline
- the default operation against PROJ's own choice
- the CLI: single point, CSV batch with pass-through columns, and errors
Things to know
- Only the CRSs listed above are supported. The Lambert projections Nord Algérie and Sud Algérie (EPSG:30791/30792) and Voirol 1875/1879 are not included yet.
- There is no vertical datum handling. Heights are ellipsoidal and passed through.
- UTM zone exceptions (Norway/Svalbard) are irrelevant here and not handled.
- It is pure Python and slow for millions of points. Use PROJ or pyproj for bulk work. This project exists to show the maths plainly.
- The transformation parameters, ellipsoid constants, CRS definitions and areas of use come from the EPSG Geodetic Parameter Dataset (IOGP), read from the copy distributed with PROJ 9.8. The dataset is © IOGP: it may be used and redistributed, but must not be modified and presented as EPSG data.
Why I built it
Nord Sahara 1959 is the datum behind a lot of Algerian coordinates, and the conversion to WGS 84 is usually treated as a black box: you pass an EPSG code and trust what comes out. There is no grid for this datum, only Helmert parameter sets, which makes it a good case to implement in full. The pipeline is short enough to write by hand, and PROJ is there to tell you whether you got it right. I wanted a version where every step is readable code, and where the choice between the ten operations is visible instead of hidden.
Get the code
The source, the tests and the full table of operations are on GitHub: https://github.com/AbdeldjalilChougui/nord-sahara-wgs84. The code is MIT licensed. Issues and pull requests are welcome.
Originally published at abdeldjalilchougui.biz.