Reference notes for transforming Accra / Ghana National Grid geometry to WGS 84 in PostGIS: ST_Transform, ST_SetSRID for columns reporting SRID 0, updating a column in place, and altering a column whose type modifier locks it to 2136. Relevant wherever data arrives on the national grid while the API and the application work in 4326 throughout. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
52 lines
2.0 KiB
Plaintext
52 lines
2.0 KiB
Plaintext
Yes, that's a standard PostGIS operation. You use the `ST_Transform` function, which reprojects geometry from one spatial reference system (SRID) to another.
|
|
|
|
The basic command looks like this:
|
|
|
|
```sql
|
|
SELECT ST_Transform(geom, 4326) AS geom_wgs84
|
|
FROM your_table;
|
|
```
|
|
|
|
Here `geom` is your geometry column, which PostGIS assumes is already in EPSG:2136. If your column doesn't have its SRID set (i.e. it reports SRID 0), you first need to tell PostGIS what it actually is before transforming:
|
|
|
|
```sql
|
|
SELECT ST_Transform(ST_SetSRID(geom, 2136), 4326) AS geom_wgs84
|
|
FROM your_table;
|
|
```
|
|
|
|
`ST_SetSRID` just labels the geometry as 2136 without changing coordinates; `ST_Transform` does the actual reprojection to 4326.
|
|
|
|
If you want to permanently update the column in place rather than just query it:
|
|
|
|
```sql
|
|
UPDATE your_table
|
|
SET geom = ST_Transform(geom, 4326);
|
|
```
|
|
|
|
Note that if the column has a type modifier locking it to SRID 2136 (e.g. `geometry(Point, 2136)`), you'll need to alter the column type first, otherwise the update will be rejected:
|
|
|
|
```sql
|
|
ALTER TABLE your_table
|
|
ALTER COLUMN geom TYPE geometry(Point, 4326)
|
|
USING ST_Transform(geom, 4326);
|
|
```
|
|
|
|
A couple of things worth knowing:
|
|
|
|
Both 2136 and 4326 must exist in the `spatial_ref_sys` table for the transform to work. 4326 (WGS 84) is always there in a standard install. EPSG:2136 is Accra / Ghana National Grid and is included in the standard EPSG dataset that PostGIS ships with, so it should be present. You can confirm with:
|
|
|
|
```sql
|
|
SELECT srid FROM spatial_ref_sys WHERE srid IN (2136, 4326);
|
|
```
|
|
|
|
If 2136 is somehow missing, you can insert its definition manually from spatialreference.org or epsg.io.
|
|
|
|
You can quickly test a single transformation without touching your table:
|
|
|
|
```sql
|
|
SELECT ST_AsText(ST_Transform(ST_SetSRID(ST_MakePoint(400000, 300000), 2136), 4326));
|
|
```
|
|
|
|
That returns the WGS 84 longitude/latitude for a sample projected coordinate, which is a good sanity check that the datum shift is being applied correctly.
|
|
|
|
Want me to tailor the exact command to your table and column names? |