Determine if an srid refers to a geodetic CRS
Pettycash dev
alphonseessomba60 at gmail.com
Tue Apr 29 15:01:55 PDT 2025
### Answer:
To detect if an SRID refers to a **geodetic (geographic) CRS** in PostGIS,
here are the best approaches:
---
### 1) **Checking if `srtext` starts with "GEOGCS"?**
While `srtext` for geodetic CRSs (e.g., WGS84) starts with `GEOGCS` in its
WKT definition (e.g., `GEOGCS["WGS 84", ...]`), this method has caveats:
- **It’s not foolproof**: The `srtext` format can vary (e.g.,
whitespace, custom definitions).
- **Not future-proof**: Relies on text parsing, which may break if WKT
conventions change.
Example query (works in most cases, but not fully robust):
```sql
SELECT srid, srtext
FROM spatial_ref_sys
WHERE srtext LIKE 'GEOGCS%'; -- Filters geodetic CRSs
```
---
### 2) **Use the built-in PostGIS function `ST_IsGeographic()`** ✅
PostGIS provides **`ST_IsGeographic(srid)`** to directly check if an SRID
is geodetic. This is the **recommended method** because:
- It uses PostGIS’s internal logic (no text parsing).
- Handles edge cases and custom definitions.
**Examples:**
```sql
SELECT ST_IsGeographic(4326); -- Returns TRUE (WGS84, geodetic)
SELECT ST_IsGeographic(3857); -- Returns FALSE (Web Mercator, projected)
```
**To find all geodetic SRIDs:**
```sql
SELECT srid, auth_name, srtext
FROM spatial_ref_sys
WHERE ST_IsGeographic(srid) = TRUE;
```
---
### Key Takeaways:
- **Avoid parsing `srtext`**: Use `ST_IsGeographic()` for reliability.
- **Works in PostGIS 2.2+**: Ensure your PostGIS version supports this
function.
Let me know if you need further details! 😊
Le mar. 29 avr. 2025, 22:55, Pettycash dev <alphonseessomba60 at gmail.com> a
écrit :
> Pour répondre à vos questions sur la détection des SRID géodésiques dans
> PostGIS :
>
> ### 1) Vérifier si `srtext` commence par "GEO" ?
> **Non, ce n'est pas une méthode fiable.**
> Les CRS géodésiques (sphère/ellipsoïde) sont représentés en WKT par
> `GEOGCS` (ex: `GEOGCS["WGS 84", ...]`). Bien que "GEO" soit présent, il est
> préférable de vérifier **`srtext LIKE 'GEOGCS%'`** pour éviter les faux
> positifs. Cependant, cette approche reste fragile (format WKT variable,
> entrées personnalisées).
>
> ---
>
> ### 2) Existe-t-il une fonction PostGIS dédiée ?
> **Oui, utilisez `ST_IsGeographic(srid)`** (disponible depuis PostGIS
> 2.2).
> Cette fonction retourne **`true`** si le SRID correspond à un CRS
> géographique (géodésique), et **`false`** pour un CRS projeté ou inconnu.
>
> **Exemple d'utilisation :**
> ```sql
> SELECT ST_IsGeographic(4326); -- Retourne true (WGS84 géodésique)
> SELECT ST_IsGeographic(3857); -- Retourne false (Web Mercator projeté)
> ```
>
> ---
>
> ### Solution recommandée :
> ```sql
> -- Vérifier directement via ST_IsGeographic()
> SELECT srid, auth_name, srtext
> FROM spatial_ref_sys
> WHERE ST_IsGeographic(srid) = true;
> ```
>
> Cette méthode est **plus robuste** que l'analyse de `srtext` ou
> `proj4text`, car elle s'appuie sur la logique interne de PostGIS.
>
> Le mar. 29 avr. 2025, 22:45, Richard Huesken <richard.huesken at gmail.com>
> a écrit :
>
>> Hi,
>>
>> I'm wondering if there is an easy way to detect if an srid refers to a
>> geodetic CRS, by checking the spatial_ref_sys table.
>>
>> 1) Would it be safe to assume the srid is a geodetic CRS if the value in
>> the srtext column starts with 'GEO'? It almost seems to be too good to be
>> true...
>>
>> 2) Did I overlook a Postgis function that returns this information (and
>> if so, which one would that be)?
>>
>> Thanks in advance,
>>
>> Kind regards
>> Richard.
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20250429/4a0ff89e/attachment.htm>
More information about the postgis-users
mailing list